@@ -27,7 +27,7 @@ void serialEventRun(void)
27
27
if (serialEvent && Serial.available () ) serialEvent ();
28
28
}
29
29
30
- Uart::Uart (NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
30
+ Uart::Uart (NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX)
31
31
{
32
32
nrfUart = _nrfUart;
33
33
IRQn = _IRQn;
@@ -39,7 +39,7 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
39
39
_begun = false ;
40
40
}
41
41
42
- Uart::Uart (NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
42
+ Uart::Uart (NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
43
43
{
44
44
nrfUart = _nrfUart;
45
45
IRQn = _IRQn;
@@ -55,8 +55,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
55
55
56
56
void Uart::setPins (uint8_t pin_rx, uint8_t pin_tx)
57
57
{
58
- uc_pinRX = pin_rx;
59
- uc_pinTX = pin_tx;
58
+ uc_pinRX = g_ADigitalPinMap[ pin_rx] ;
59
+ uc_pinTX = g_ADigitalPinMap[ pin_tx] ;
60
60
}
61
61
62
62
void Uart::begin (unsigned long baudrate)
@@ -69,23 +69,15 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
69
69
// skip if already begun
70
70
if ( _begun ) return ;
71
71
72
- nrfUart->PSELTXD = uc_pinTX;
73
- nrfUart->PSELRXD = uc_pinRX;
72
+ nrfUart->PSEL . TXD = uc_pinTX;
73
+ nrfUart->PSEL . RXD = uc_pinRX;
74
74
75
75
if (uc_hwFlow == 1 ) {
76
- nrfUart->PSELCTS = uc_pinCTS;
77
- nrfUart->PSELRTS = uc_pinRTS;
78
- if (config==SERIAL_8E1) {
79
- nrfUart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
80
- } else {
81
- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
82
- }
76
+ nrfUart->PSEL .CTS = uc_pinCTS;
77
+ nrfUart->PSEL .RTS = uc_pinRTS;
78
+ nrfUart->CONFIG = config | (UARTE_CONFIG_HWFC_Enabled << UARTE_CONFIG_HWFC_Pos);
83
79
} else {
84
- if (config==SERIAL_8E1) {
85
- nrfUart->CONFIG = (UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
86
- } else {
87
- nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
88
- }
80
+ nrfUart->CONFIG = config | (UARTE_CONFIG_HWFC_Disabled << UARTE_CONFIG_HWFC_Pos);
89
81
}
90
82
91
83
uint32_t nrfBaudRate;
@@ -126,15 +118,16 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
126
118
127
119
nrfUart->BAUDRATE = nrfBaudRate;
128
120
129
- nrfUart->ENABLE = UART_ENABLE_ENABLE_Enabled ;
121
+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Enabled ;
130
122
131
- nrfUart->EVENTS_RXDRDY = 0x0UL ;
132
- nrfUart->EVENTS_TXDRDY = 0x0UL ;
123
+ nrfUart->TXD . PTR = ( uint32_t )txBuffer ;
124
+ nrfUart->EVENTS_ENDTX = 0x0UL ;
133
125
126
+ nrfUart->RXD .PTR = (uint32_t )&rxRcv;
127
+ nrfUart->RXD .MAXCNT = 1 ;
134
128
nrfUart->TASKS_STARTRX = 0x1UL ;
135
- nrfUart->TASKS_STARTTX = 0x1UL ;
136
129
137
- nrfUart->INTENSET = UART_INTENSET_RXDRDY_Msk ;
130
+ nrfUart->INTENSET = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
138
131
139
132
NVIC_ClearPendingIRQ (IRQn);
140
133
NVIC_SetPriority (IRQn, 3 );
@@ -148,18 +141,18 @@ void Uart::end()
148
141
{
149
142
NVIC_DisableIRQ (IRQn);
150
143
151
- nrfUart->INTENCLR = UART_INTENCLR_RXDRDY_Msk ;
144
+ nrfUart->INTENCLR = UARTE_INTENSET_ENDRX_Msk | UARTE_INTENSET_ENDTX_Msk ;
152
145
153
146
nrfUart->TASKS_STOPRX = 0x1UL ;
154
147
nrfUart->TASKS_STOPTX = 0x1UL ;
155
148
156
- nrfUart->ENABLE = UART_ENABLE_ENABLE_Disabled ;
149
+ nrfUart->ENABLE = UARTE_ENABLE_ENABLE_Disabled ;
157
150
158
- nrfUart->PSELTXD = 0xFFFFFFFF ;
159
- nrfUart->PSELRXD = 0xFFFFFFFF ;
151
+ nrfUart->PSEL . TXD = 0xFFFFFFFF ;
152
+ nrfUart->PSEL . RXD = 0xFFFFFFFF ;
160
153
161
- nrfUart->PSELRTS = 0xFFFFFFFF ;
162
- nrfUart->PSELCTS = 0xFFFFFFFF ;
154
+ nrfUart->PSEL . RTS = 0xFFFFFFFF ;
155
+ nrfUart->PSEL . CTS = 0xFFFFFFFF ;
163
156
164
157
rxBuffer.clear ();
165
158
@@ -170,15 +163,28 @@ void Uart::end()
170
163
171
164
void Uart::flush ()
172
165
{
173
- rxBuffer.clear ();
166
+ if ( _begun ) {
167
+ xSemaphoreTake (_mutex, portMAX_DELAY);
168
+ xSemaphoreGive (_mutex);
169
+ }
174
170
}
175
171
176
172
void Uart::IrqHandler ()
177
173
{
178
- if (nrfUart->EVENTS_RXDRDY )
174
+ if (nrfUart->EVENTS_ENDRX )
175
+ {
176
+ nrfUart->EVENTS_ENDRX = 0x0UL ;
177
+ if (nrfUart->RXD .AMOUNT )
178
+ {
179
+ rxBuffer.store_char (rxRcv);
180
+ }
181
+ nrfUart->TASKS_STARTRX = 0x1UL ;
182
+ }
183
+
184
+ if (nrfUart->EVENTS_ENDTX )
179
185
{
180
- nrfUart->EVENTS_RXDRDY = 0x0UL ;
181
- rxBuffer. store_char (nrfUart-> RXD );
186
+ nrfUart->EVENTS_ENDTX = 0x0UL ;
187
+ xSemaphoreGiveFromISR (_mutex, NULL );
182
188
}
183
189
}
184
190
@@ -197,26 +203,39 @@ int Uart::read()
197
203
return rxBuffer.read_char ();
198
204
}
199
205
200
- size_t Uart::write (const uint8_t data)
206
+ size_t Uart::write (uint8_t data)
207
+ {
208
+ return write (&data, 1 );
209
+ }
210
+
211
+ size_t Uart::write (const uint8_t *buffer, size_t size)
201
212
{
202
- xSemaphoreTake (_mutex, portMAX_DELAY) ;
213
+ if (size == 0 ) return 0 ;
203
214
204
- nrfUart-> TXD = data ;
215
+ size_t sent = 0 ;
205
216
206
- while (!nrfUart->EVENTS_TXDRDY );
217
+ do
218
+ {
219
+ size_t remaining = size - sent;
220
+ size_t txSize = min (remaining, SERIAL_BUFFER_SIZE);
221
+
222
+ xSemaphoreTake (_mutex, portMAX_DELAY);
207
223
208
- nrfUart-> EVENTS_TXDRDY = 0x0UL ;
224
+ memcpy (txBuffer, buffer + sent, txSize) ;
209
225
210
- xSemaphoreGive (_mutex);
226
+ nrfUart->TXD .MAXCNT = txSize;
227
+ nrfUart->TASKS_STARTTX = 0x1UL ;
228
+ sent += txSize;
211
229
212
- return 1 ;
230
+ } while (sent < size);
231
+
232
+ return sent;
213
233
}
214
234
215
- Uart SERIAL_PORT_HARDWARE ( NRF_UART0 , UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
235
+ Uart SERIAL_PORT_HARDWARE ( NRF_UARTE0 , UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
216
236
217
- #ifdef HAVE_HWSERIAL2
218
- // TODO UART1 is UARTE only, need update class Uart to work
219
- Uart Serial2 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
237
+ #ifdef SERIAL_PORT_HARDWARE1
238
+ Uart SERIAL_PORT_HARDWARE1 ( NRF_UARTE1, UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX );
220
239
#endif
221
240
222
241
extern " C"
@@ -225,4 +244,11 @@ extern "C"
225
244
{
226
245
SERIAL_PORT_HARDWARE.IrqHandler ();
227
246
}
247
+
248
+ #ifdef SERIAL_PORT_HARDWARE1
249
+ void UARTE1_IRQHandler ()
250
+ {
251
+ SERIAL_PORT_HARDWARE1.IrqHandler ();
252
+ }
253
+ #endif
228
254
}
0 commit comments