1 | #include "Uart.h"
|
---|
2 | #include "WVariant.h"
|
---|
3 | #include "wiring_digital.h"
|
---|
4 |
|
---|
5 | Uart::Uart(SERCOM *_s, uint8_t _pinRX, uint8_t _pinTX)
|
---|
6 | {
|
---|
7 | sercom = _s;
|
---|
8 | uc_pinRX = _pinRX;
|
---|
9 | uc_pinTX = _pinTX;
|
---|
10 | }
|
---|
11 |
|
---|
12 | void Uart::begin(unsigned long baudrate)
|
---|
13 | {
|
---|
14 | begin(baudrate, (uint8_t)SERIAL_8N1);
|
---|
15 | }
|
---|
16 |
|
---|
17 | void Uart::begin(unsigned long baudrate, uint8_t config)
|
---|
18 | {
|
---|
19 | pinPeripheral(uc_pinRX, g_APinDescription[uc_pinRX].ulPinType);
|
---|
20 | pinPeripheral(uc_pinTX, g_APinDescription[uc_pinTX].ulPinType);
|
---|
21 |
|
---|
22 | sercom->initUART(UART_INT_CLOCK, SAMPLE_RATE_x16, baudrate);
|
---|
23 | sercom->initFrame(extractCharSize(config), LSB_FIRST, extractParity(config), extractNbStopBit(config));
|
---|
24 | sercom->initPads(UART_TX_PAD_2, SERCOM_RX_PAD_3);
|
---|
25 |
|
---|
26 |
|
---|
27 | sercom->enableUART();
|
---|
28 | }
|
---|
29 |
|
---|
30 | void Uart::end()
|
---|
31 | {
|
---|
32 | sercom->resetUART();
|
---|
33 | rxBuffer.clear();
|
---|
34 | }
|
---|
35 |
|
---|
36 | void Uart::flush()
|
---|
37 | {
|
---|
38 | sercom->flushUART();
|
---|
39 | }
|
---|
40 |
|
---|
41 | void Uart::IrqHandler()
|
---|
42 | {
|
---|
43 | if(sercom->availableDataUART())
|
---|
44 | {
|
---|
45 | rxBuffer.store_char(sercom->readDataUART());
|
---|
46 | }
|
---|
47 |
|
---|
48 | if( sercom->isBufferOverflowErrorUART() ||
|
---|
49 | sercom->isFrameErrorUART() ||
|
---|
50 | sercom->isParityErrorUART())
|
---|
51 | {
|
---|
52 | sercom->clearStatusUART();
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | int Uart::available()
|
---|
57 | {
|
---|
58 | return rxBuffer.available();
|
---|
59 | }
|
---|
60 |
|
---|
61 | int Uart::peek()
|
---|
62 | {
|
---|
63 | return rxBuffer.peek();
|
---|
64 | }
|
---|
65 |
|
---|
66 | int Uart::read()
|
---|
67 | {
|
---|
68 | return rxBuffer.read_char();
|
---|
69 | }
|
---|
70 |
|
---|
71 | size_t Uart::write(const uint8_t data)
|
---|
72 | {
|
---|
73 | sercom->writeDataUART(data);
|
---|
74 | return 1;
|
---|
75 | }
|
---|
76 |
|
---|
77 | size_t Uart::write(const char * data)
|
---|
78 | {
|
---|
79 | size_t writed = 0;
|
---|
80 |
|
---|
81 | while(*data != '\0')
|
---|
82 | {
|
---|
83 | writed += write(*data);
|
---|
84 | ++data;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return writed;
|
---|
88 | }
|
---|
89 |
|
---|
90 | SercomNumberStopBit Uart::extractNbStopBit(uint8_t config)
|
---|
91 | {
|
---|
92 | switch(config & HARDSER_STOP_BIT_MASK)
|
---|
93 | {
|
---|
94 | case HARDSER_STOP_BIT_1:
|
---|
95 | default:
|
---|
96 | return SERCOM_STOP_BIT_1;
|
---|
97 |
|
---|
98 | case HARDSER_STOP_BIT_2:
|
---|
99 | return SERCOM_STOP_BITS_2;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | SercomUartCharSize Uart::extractCharSize(uint8_t config)
|
---|
104 | {
|
---|
105 | switch(config & HARDSER_DATA_MASK)
|
---|
106 | {
|
---|
107 | case HARDSER_DATA_5:
|
---|
108 | return UART_CHAR_SIZE_5_BITS;
|
---|
109 |
|
---|
110 | case HARDSER_DATA_6:
|
---|
111 | return UART_CHAR_SIZE_6_BITS;
|
---|
112 |
|
---|
113 | case HARDSER_DATA_7:
|
---|
114 | return UART_CHAR_SIZE_7_BITS;
|
---|
115 |
|
---|
116 | case HARDSER_DATA_8:
|
---|
117 | default:
|
---|
118 | return UART_CHAR_SIZE_8_BITS;
|
---|
119 |
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | SercomParityMode Uart::extractParity(uint8_t config)
|
---|
124 | {
|
---|
125 | switch(config & HARDSER_PARITY_MASK)
|
---|
126 | {
|
---|
127 | case HARDSER_PARITY_NONE:
|
---|
128 | default:
|
---|
129 | return SERCOM_NO_PARITY;
|
---|
130 |
|
---|
131 | case HARDSER_PARITY_EVEN:
|
---|
132 | return SERCOM_EVEN_PARITY;
|
---|
133 |
|
---|
134 | case HARDSER_PARITY_ODD:
|
---|
135 | return SERCOM_ODD_PARITY;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | void SERCOM0_Handler()
|
---|
140 | {
|
---|
141 | Serial5.IrqHandler();
|
---|
142 | }
|
---|
143 | void SERCOM5_Handler()
|
---|
144 | {
|
---|
145 | Serial.IrqHandler();
|
---|
146 | }
|
---|