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