36
36
#include "supervisor/shared/translate.h"
37
37
38
38
#include "nrfx_uarte.h"
39
+ #include "nrf_gpio.h"
39
40
#include <string.h>
40
41
41
42
// expression to examine, and return value in case of failing
@@ -98,10 +99,16 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context)
98
99
99
100
switch ( event -> type ) {
100
101
case NRFX_UARTE_EVT_RX_DONE :
101
- ringbuf_put_n (& self -> ringbuf , event -> data .rxtx .p_data , event -> data .rxtx .bytes );
102
+ if (ringbuf_num_empty (& self -> ringbuf ) >= event -> data .rxtx .bytes ) {
103
+ ringbuf_put_n (& self -> ringbuf , event -> data .rxtx .p_data , event -> data .rxtx .bytes );
104
+ // keep receiving
105
+ (void ) nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 );
106
+ } else {
107
+ // receive buffer full, suspend
108
+ self -> rx_paused = true;
109
+ nrf_gpio_pin_write (self -> rts_pin_number , true);
110
+ }
102
111
103
- // keep receiving
104
- (void ) nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 );
105
112
break ;
106
113
107
114
case NRFX_UARTE_EVT_TX_DONE :
@@ -137,8 +144,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
137
144
mp_float_t timeout , uint16_t receiver_buffer_size , byte * receiver_buffer ,
138
145
bool sigint_enabled ) {
139
146
140
- if ((rts != NULL ) || ( cts != NULL ) || ( rs485_dir != NULL ) || (rs485_invert )) {
141
- mp_raise_ValueError (translate ("RTS/CTS/ RS485 Not yet supported on this device" ));
147
+ if ((rs485_dir != NULL ) || (rs485_invert )) {
148
+ mp_raise_ValueError (translate ("RS485 Not yet supported on this device" ));
142
149
}
143
150
144
151
// Find a free UART peripheral.
@@ -166,16 +173,18 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
166
173
mp_raise_ValueError (translate ("Odd parity is not supported" ));
167
174
}
168
175
176
+ bool hwfc = rts != NULL || cts != NULL ;
177
+
169
178
nrfx_uarte_config_t config = {
170
179
.pseltxd = (tx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : tx -> number ,
171
180
.pselrxd = (rx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rx -> number ,
172
- .pselcts = NRF_UARTE_PSEL_DISCONNECTED ,
173
- .pselrts = NRF_UARTE_PSEL_DISCONNECTED ,
181
+ .pselcts = ( cts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : cts -> number ,
182
+ .pselrts = ( rts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rts -> number ,
174
183
.p_context = self ,
175
184
.baudrate = get_nrf_baud (baudrate ),
176
185
.interrupt_priority = 7 ,
177
186
.hal_cfg = {
178
- .hwfc = NRF_UARTE_HWFC_DISABLED ,
187
+ .hwfc = hwfc ? NRF_UARTE_HWFC_ENABLED : NRF_UARTE_HWFC_DISABLED ,
179
188
.parity = (parity == BUSIO_UART_PARITY_NONE ) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED
180
189
}
181
190
};
@@ -207,9 +216,25 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
207
216
self -> tx_pin_number = NO_PIN ;
208
217
}
209
218
219
+ if ( rts != NULL ) {
220
+ self -> rts_pin_number = rts -> number ;
221
+ claim_pin (rts );
222
+ } else {
223
+ self -> rts_pin_number = NO_PIN ;
224
+ }
225
+
226
+ if ( cts != NULL ) {
227
+ self -> cts_pin_number = cts -> number ;
228
+ claim_pin (cts );
229
+ } else {
230
+ self -> cts_pin_number = NO_PIN ;
231
+ }
232
+
210
233
self -> baudrate = baudrate ;
211
234
self -> timeout_ms = timeout * 1000 ;
212
235
236
+ self -> rx_paused = false;
237
+
213
238
// Initial wait for incoming byte
214
239
_VERIFY_ERR (nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 ));
215
240
}
@@ -223,8 +248,12 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
223
248
nrfx_uarte_uninit (self -> uarte );
224
249
reset_pin_number (self -> tx_pin_number );
225
250
reset_pin_number (self -> rx_pin_number );
251
+ reset_pin_number (self -> rts_pin_number );
252
+ reset_pin_number (self -> cts_pin_number );
226
253
self -> tx_pin_number = NO_PIN ;
227
254
self -> rx_pin_number = NO_PIN ;
255
+ self -> rts_pin_number = NO_PIN ;
256
+ self -> cts_pin_number = NO_PIN ;
228
257
ringbuf_free (& self -> ringbuf );
229
258
}
230
259
}
@@ -237,6 +266,13 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
237
266
238
267
uint64_t start_ticks = supervisor_ticks_ms64 ();
239
268
269
+ // check removed to reduce code size
270
+ /*
271
+ if (len > ringbuf_capacity(&self->ringbuf)) {
272
+ mp_raise_ValueError(translate("Reading >receiver_buffer_size bytes is not supported"));
273
+ }
274
+ */
275
+
240
276
// Wait for all bytes received or timeout
241
277
while ( (ringbuf_num_filled (& self -> ringbuf ) < len ) && (supervisor_ticks_ms64 () - start_ticks < self -> timeout_ms ) ) {
242
278
RUN_BACKGROUND_TASKS ;
@@ -252,6 +288,16 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
252
288
// Copy as much received data as available, up to len bytes.
253
289
size_t rx_bytes = ringbuf_get_n (& self -> ringbuf , data , len );
254
290
291
+ // restart reader, if stopped
292
+ if (self -> rx_paused ) {
293
+ // the character that did not fit in ringbuf is in rx_char
294
+ ringbuf_put_n (& self -> ringbuf , & self -> rx_char , 1 );
295
+ // keep receiving
296
+ (void ) nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 );
297
+ nrf_gpio_pin_write (self -> rts_pin_number , false);
298
+ self -> rx_paused = false;
299
+ }
300
+
255
301
NVIC_EnableIRQ (nrfx_get_irq_number (self -> uarte -> p_reg ));
256
302
257
303
return rx_bytes ;
0 commit comments