48
48
}\
49
49
}while(0)
50
50
51
- #define UARTE_DEBUG 0
52
-
53
- #if UARTE_DEBUG
54
- #define PRINT_INT (x ) printf("%s: %d: " #x " = %ld\n" , __FUNCTION__, __LINE__, (uint32_t) (x) )
55
- #else
56
- #define PRINT_INT (x )
57
- #endif
58
-
59
51
static uint32_t get_nrf_baud (uint32_t baudrate );
60
52
61
- static uint32_t _err = 0 ;
62
- static uint32_t _err_count = 0 ;
63
-
64
-
65
53
static void uart_callback_irq (const nrfx_uarte_event_t * event , void * context ) {
66
54
busio_uart_obj_t * self = (busio_uart_obj_t * ) context ;
67
55
@@ -74,12 +62,9 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context)
74
62
break ;
75
63
76
64
case NRFX_UART_EVT_ERROR :
77
- // Abort too fast will cause error occasionally
78
65
if ( self -> rx_count == -1 ) {
79
- self -> rx_count = 0 ; // event->data.error.rxtx.bytes;
66
+ self -> rx_count = 0 ;
80
67
}
81
- _err_count = event -> data .error .rxtx .bytes ;
82
- _err = event -> data .error .error_mask ;
83
68
break ;
84
69
85
70
default :
@@ -184,7 +169,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
184
169
uint64_t start_ticks = ticks_ms ;
185
170
186
171
while ( 1 ) {
187
- // Wait for on-going reception to complete
172
+ // Wait for on-going transfer to complete
188
173
while ( (self -> rx_count == -1 ) && (ticks_ms - start_ticks < self -> timeout_ms ) ) {
189
174
#ifdef MICROPY_VM_HOOK_LOOP
190
175
MICROPY_VM_HOOK_LOOP
@@ -233,8 +218,19 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
233
218
234
219
if ( len == 0 ) return 0 ;
235
220
236
- if ( !nrfx_uarte_tx_in_progress (& self -> uarte ) ) {
237
- nrfx_uarte_tx_abort (& self -> uarte );
221
+ uint64_t start_ticks = ticks_ms ;
222
+
223
+ // Wait for on-going transfer to complete
224
+ while ( nrfx_uarte_tx_in_progress (& self -> uarte ) && (ticks_ms - start_ticks < self -> timeout_ms ) ) {
225
+ #ifdef MICROPY_VM_HOOK_LOOP
226
+ MICROPY_VM_HOOK_LOOP
227
+ #endif
228
+ }
229
+
230
+ // Time up
231
+ if ( !(ticks_ms - start_ticks < self -> timeout_ms ) ) {
232
+ * errcode = MP_EAGAIN ;
233
+ return MP_STREAM_ERROR ;
238
234
}
239
235
240
236
// EasyDMA can only access SRAM
@@ -248,7 +244,6 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
248
244
_VERIFY_ERR (* errcode );
249
245
(* errcode ) = 0 ;
250
246
251
- uint64_t start_ticks = ticks_ms ;
252
247
while ( nrfx_uarte_tx_in_progress (& self -> uarte ) && (ticks_ms - start_ticks < self -> timeout_ms ) ) {
253
248
#ifdef MICROPY_VM_HOOK_LOOP
254
249
MICROPY_VM_HOOK_LOOP
@@ -283,7 +278,7 @@ uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
283
278
#ifndef NRF52840_XXAA
284
279
mp_raise_NotImplementedError (translate ("busio.UART not yet implemented" ));
285
280
#else
286
- return (self -> rx_count > 0 ) ? 1 : 0 ;
281
+ return (self -> rx_count > 0 ) ? self -> rx_count : 0 ;
287
282
#endif
288
283
}
289
284
0 commit comments