@@ -178,10 +178,19 @@ void UART_IRQ_HANDLER(void)
178
178
179
179
#if DEVICE_SERIAL_ASYNCH
180
180
if (UART_CB .tx_active ) {
181
- nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_TXDRDY );
182
-
183
- nrf_uart_txd_set (UART_INSTANCE , UART_CB .tx_buffer [UART_CB .tx_pos ]);
184
- if (++ UART_CB .tx_pos >= UART_CB .tx_length ) {
181
+ if (++ UART_CB .tx_pos <= UART_CB .tx_length ) {
182
+ // When there is still something to send, clear the TXDRDY event
183
+ // and put next byte to transmitter.
184
+ nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_TXDRDY );
185
+ nrf_uart_txd_set (UART_INSTANCE ,
186
+ UART_CB .tx_buffer [UART_CB .tx_pos ]);
187
+ }
188
+ else {
189
+ // When the TXDRDY event is set after the last byte to be sent
190
+ // has been passed to the transmitter, the job is done and TX
191
+ // complete can be indicated.
192
+ // Don't clear the TXDRDY event, it needs to remain set for the
193
+ // 'serial_writable' function to work properly.
185
194
end_asynch_tx ();
186
195
187
196
UART_CB .events_occured |= SERIAL_EVENT_TX_COMPLETE ;
@@ -229,27 +238,29 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
229
238
(tx == NC ) ? NRF_UART_PSEL_DISCONNECTED : (uint32_t )tx ;
230
239
UART_CB .pselrxd =
231
240
(rx == NC ) ? NRF_UART_PSEL_DISCONNECTED : (uint32_t )rx ;
241
+ if (UART_CB .pseltxd != NRF_UART_PSEL_DISCONNECTED ) {
242
+ nrf_gpio_pin_set (UART_CB .pseltxd );
243
+ nrf_gpio_cfg_output (UART_CB .pseltxd );
244
+ }
245
+ if (UART_CB .pselrxd != NRF_UART_PSEL_DISCONNECTED ) {
246
+ nrf_gpio_cfg_input (UART_CB .pselrxd , NRF_GPIO_PIN_NOPULL );
247
+ }
248
+
249
+ // UART pins must only be configured when the peripheral is disabled.
250
+ nrf_uart_disable (UART_INSTANCE );
251
+
232
252
if (UART_CB .initialized ) {
233
253
// Reconfigure RX/TX pins only.
234
- nrf_uart_txrx_pins_set (UART_INSTANCE ,
235
- UART_CB . pseltxd , UART_CB . pselrxd );
254
+ nrf_uart_txrx_pins_set (UART_INSTANCE , UART_CB . pseltxd , UART_CB . pselrxd );
255
+ nrf_uart_enable ( UART_INSTANCE );
236
256
}
237
257
else {
238
- if (UART_CB .pseltxd != NRF_UART_PSEL_DISCONNECTED ) {
239
- nrf_gpio_pin_set (UART_CB .pseltxd );
240
- nrf_gpio_cfg_output (UART_CB .pseltxd );
241
- }
242
- if (UART_CB .pselrxd != NRF_UART_PSEL_DISCONNECTED ) {
243
- nrf_gpio_cfg_input (UART_CB .pselrxd , NRF_GPIO_PIN_NOPULL );
244
- }
245
-
246
258
UART_CB .baudrate = UART_DEFAULT_BAUDRATE ;
247
259
UART_CB .parity = UART_DEFAULT_PARITY ;
248
260
UART_CB .hwfc = UART_DEFAULT_HWFC ;
249
261
UART_CB .pselcts = UART_DEFAULT_CTS ;
250
262
UART_CB .pselrts = UART_DEFAULT_RTS ;
251
263
252
- nrf_uart_enable (UART_INSTANCE );
253
264
nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_RXDRDY );
254
265
nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_TXDRDY );
255
266
nrf_uart_task_trigger (UART_INSTANCE , NRF_UART_TASK_STARTRX );
@@ -273,19 +284,23 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
273
284
// Perform it with disconnected TX pin, so nothing actually comes out
274
285
// of the device.
275
286
nrf_uart_txrx_pins_disconnect (UART_INSTANCE );
287
+ nrf_uart_hwfc_pins_disconnect (UART_INSTANCE );
288
+ nrf_uart_enable (UART_INSTANCE );
276
289
nrf_uart_txd_set (UART_INSTANCE , 0 );
277
290
while (!nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_TXDRDY )) {
278
291
}
292
+ nrf_uart_disable (UART_INSTANCE );
279
293
280
294
// Now everything is prepared to set the default configuration and
281
295
// connect the peripheral to actual pins.
296
+ nrf_uart_txrx_pins_set (UART_INSTANCE , UART_CB .pseltxd , UART_CB .pselrxd );
282
297
nrf_uart_baudrate_set (UART_INSTANCE , UART_CB .baudrate );
283
298
nrf_uart_configure (UART_INSTANCE , UART_CB .parity , UART_CB .hwfc );
284
299
if (UART_CB .hwfc == NRF_UART_HWFC_ENABLED ) {
285
300
serial_set_flow_control (obj , FlowControlRTSCTS ,
286
301
UART_CB .pselrts , UART_CB .pselcts );
287
302
}
288
- nrf_uart_txrx_pins_set (UART_INSTANCE , UART_CB . pseltxd , UART_CB . pselrxd );
303
+ nrf_uart_enable (UART_INSTANCE );
289
304
290
305
UART_CB .initialized = true;
291
306
}
@@ -469,6 +484,9 @@ void serial_putc(serial_t *obj, int c)
469
484
470
485
nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_TXDRDY );
471
486
nrf_uart_txd_set (UART_INSTANCE , (uint8_t )c );
487
+ // Wait until sending is completed.
488
+ while (!nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_TXDRDY )) {
489
+ }
472
490
}
473
491
474
492
int serial_readable (serial_t * obj )
@@ -527,7 +545,9 @@ void serial_set_flow_control(serial_t *obj, FlowControl type,
527
545
if (UART_CB .pselcts != NRF_UART_PSEL_DISCONNECTED ) {
528
546
nrf_gpio_cfg_input (UART_CB .pselcts , NRF_GPIO_PIN_NOPULL );
529
547
}
548
+ nrf_uart_disable (UART_INSTANCE );
530
549
nrf_uart_hwfc_pins_set (UART_INSTANCE , UART_CB .pselrts , UART_CB .pselcts );
550
+ nrf_uart_enable (UART_INSTANCE );
531
551
}
532
552
533
553
void serial_clear (serial_t * obj ) {
0 commit comments