21
21
#include "app_util_platform.h"
22
22
#include "nrf_gpio.h"
23
23
24
+ #if DEVICE_SERIAL
25
+
24
26
#if DEVICE_SERIAL_ASYNCH
25
27
#define SERIAL_S (obj ) (&obj->serial)
26
28
#else
@@ -211,8 +213,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
211
213
UART_CB .initialized = true;
212
214
nrf_drv_uart_rx_enable ();
213
215
214
- stdio_uart_inited = 1 ;
215
- memcpy (& stdio_uart , obj , sizeof (serial_t ));
216
+ if (tx == STDIO_UART_TX && rx == STDIO_UART_RX ) {
217
+ stdio_uart_inited = 1 ;
218
+ memcpy (& stdio_uart , obj , sizeof (serial_t ));
219
+ }
216
220
}
217
221
else {
218
222
error ("UART init failure." );
@@ -225,6 +229,8 @@ void serial_free(serial_t *obj)
225
229
if (UART_CB .initialized ) {
226
230
nrf_drv_uart_uninit ();
227
231
UART_CB .initialized = false;
232
+
233
+ stdio_uart_inited = 0 ;
228
234
}
229
235
}
230
236
@@ -240,32 +246,40 @@ int serial_writable(serial_t *obj)
240
246
int serial_readable (serial_t * obj )
241
247
{
242
248
(void )obj ;
243
- return nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_RXDRDY );
249
+ return (!UART_CB .rx_active &&
250
+ nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_RXDRDY ));
244
251
}
245
252
246
253
void serial_putc (serial_t * obj , int c )
247
254
{
248
255
(void )obj ;
249
- UART_CB .async_mode = false;
250
- UART_CB .tx_active = true;
251
- uint8_t data = c ;
252
- nrf_drv_uart_tx (& data , 1 );
253
256
254
- while (UART_CB .tx_active ) {
257
+ // Interrupt on the TXDRDY event must be temporarily disabled, otherwise
258
+ // the driver would try to handle (and clear) this event in the interrupt
259
+ // handler.
260
+ nrf_uart_int_disable (UART_INSTANCE , NRF_UART_INT_MASK_TXDRDY );
261
+
262
+ nrf_uart_task_trigger (UART_INSTANCE , NRF_UART_TASK_STARTTX );
263
+ nrf_uart_txd_set (UART_INSTANCE , (uint8_t )c );
264
+ while (!nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_TXDRDY )) {
255
265
}
266
+ nrf_uart_task_trigger (UART_INSTANCE , NRF_UART_TASK_STOPTX );
267
+
268
+ nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_TXDRDY );
269
+ nrf_uart_int_enable (UART_INSTANCE , NRF_UART_INT_MASK_TXDRDY );
256
270
}
257
271
258
272
int serial_getc (serial_t * obj )
259
273
{
260
274
(void )obj ;
261
- UART_CB .async_mode = false;
262
- UART_CB .rx_active = true;
263
- uint8_t data ;
264
- nrf_drv_uart_rx (& data , 1 );
265
275
266
- while (UART_CB .rx_active ) {
276
+ nrf_uart_task_trigger (UART_INSTANCE , NRF_UART_TASK_STARTRX );
277
+ while (!nrf_uart_event_check (UART_INSTANCE , NRF_UART_EVENT_RXDRDY )) {
267
278
}
268
- return (int )data ;
279
+ nrf_uart_task_trigger (UART_INSTANCE , NRF_UART_TASK_STOPRX );
280
+ nrf_uart_event_clear (UART_INSTANCE , NRF_UART_EVENT_RXDRDY );
281
+
282
+ return nrf_uart_rxd_get (UART_INSTANCE );
269
283
}
270
284
271
285
void serial_irq_handler (serial_t * obj , uart_irq_handler handler , uint32_t id )
@@ -467,3 +481,5 @@ void serial_clear(serial_t *obj)
467
481
{
468
482
(void )obj ;
469
483
}
484
+
485
+ #endif // DEVICE_SERIAL
0 commit comments