8000 nrf/modules/machine/uart: Add timeout keyword options and "any" method. · peterwillcn/micropython@7ea192a · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ea192a

Browse files
robert-hhdpgeorge
authored andcommitted
nrf/modules/machine/uart: Add timeout keyword options and "any" method.
Changes in this commit: - Add the timeout and timeout_char keyword options. - Make uart.read() non-blocking. - Add uart.any(). - Add ioctl MP_STREAM_POLL handling. - Change uart.write() into non-busy waiting. uart.write() still waits until all data has been sent, but calls MICROPY_EVENT_POLL_HOOK while waiting. uart.write() uses DMA for transfer. One option would be to add a small local buffer, such that transfers up to the size of the buffer could be done without waiting. - As a side effect to the change of uart.write(), uart.txdone() and ioctl flush now report/wait correctly for the end of transmission. - Change machine_hard_uart_buf_t in machine_hard_uart_obj_t to an instance of that struct, rather than a pointer to one.
1 parent 42511b5 commit 7ea192a

File tree

2 files changed

+74
-41
lines changed

2 files changed

+74
-41
lines changed

ports/nrf/modules/machine/uart.c

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ typedef struct _machine_hard_uart_buf_t {
9898
typedef struct _machine_hard_uart_obj_t {
9999
mp_obj_base_t base;
100100
const nrfx_uart_t * p_uart; // Driver instance
101-
machine_hard_uart_buf_t *buf;
101+
machine_hard_uart_buf_t buf;
102+
uint16_t timeout; // timeout waiting for first char (in ms)
103+
uint16_t timeout_char; // timeout waiting between chars (in ms)
102104
} machine_hard_uart_obj_t;
103105

104106
static const nrfx_uart_t instance0 = NRFX_UART_INSTANCE(0);
105107

106-
STATIC machine_hard_uart_buf_t machine_hard_uart_buf[1];
107-
108-
STATIC const machine_hard_uart_obj_t machine_hard_uart_obj[] = {
109-
{{&machine_uart_type}, .p_uart = &instance0, .buf = &machine_hard_uart_buf[0]},
108+
STATIC machine_hard_uart_obj_t machine_hard_uart_obj[] = {
109+
{{&machine_uart_type}, .p_uart = &instance0}
110110
};
111111

112112
void uart_init0(void) {
@@ -124,45 +124,45 @@ STATIC int uart_find(mp_obj_t id) {
124124
STATIC void uart_event_handler(nrfx_uart_event_t const *p_event, void *p_context) {
125125
machine_hard_uart_obj_t *self = p_context;
126126
if (p_event->type == NRFX_UART_EVT_RX_DONE) {
127-
int chr = self->buf->rx_buf[0];
128-
nrfx_uart_rx(self->p_uart, &self->buf->rx_buf[0], 1);
127+
nrfx_uart_rx(self->p_uart, &self->buf.rx_buf[0], 1);
128+
int chr = self->buf.rx_buf[0];
129129
#if !MICROPY_PY_BLE_NUS && MICROPY_KBD_EXCEPTION
130130
if (chr == mp_interrupt_char) {
131-
self->buf->rx_ringbuf.iget = 0;
132-
self->buf->rx_ringbuf.iput = 0;
131+
self->buf.rx_ringbuf.iget = 0;
132+
self->buf.rx_ringbuf.iput = 0;
133133
mp_sched_keyboard_interrupt();
134134
} else
135135
#endif
136136
{
137-
ringbuf_put((ringbuf_t*)&self->buf->rx_ringbuf, chr);
137+
ringbuf_put((ringbuf_t *)&self->buf.rx_ringbuf, chr);
138138
}
139139
}
140140
}
141141

142-
bool uart_rx_any(const machine_hard_uart_obj_t *self) {
143-
return self->buf->rx_ringbuf.iput != self->buf->rx_ringbuf.iget;
142+
bool uart_rx_any(machine_hard_uart_obj_t *self) {
143+
return self->buf.rx_ringbuf.iput != self->buf.rx_ringbuf.iget;
144144
}
145145

146-
int uart_rx_char(const machine_hard_uart_obj_t * self) {
147-
return ringbuf_get((ringbuf_t*)&self->buf->rx_ringbuf);
146+
int uart_rx_char(machine_hard_uart_obj_t *self) {
147+
return ringbuf_get((ringbuf_t *)&self->buf.rx_ringbuf);
148148
}
149149

150-
STATIC nrfx_err_t uart_tx_char(const machine_hard_uart_obj_t * self, int c) {
150+
STATIC nrfx_err_t uart_tx_char(machine_hard_uart_obj_t *self, int c) {
151151
while (nrfx_uart_tx_in_progress(self->p_uart)) {
152152
;
153153
}
154-
self->buf->tx_buf[0] = c;
155-
return nrfx_uart_tx(self->p_uart, &self->buf->tx_buf[0], 1);
154+
self->buf.tx_buf[0] = c;
155+
return nrfx_uart_tx(self->p_uart, &self->buf.tx_buf[0], 1);
156156
}
157157

158158

159-
void uart_tx_strn(const machine_hard_uart_obj_t *uart_obj, const char *str, uint len) {
159+
void uart_tx_strn(machine_hard_uart_obj_t *uart_obj, const char *str, uint len) {
160160
for (const char *top = str + len; str < top; str++) {
161161
uart_tx_char(uart_obj, *str);
162162
}
163163
}
164164

165-
void uart_tx_strn_cooked(const machine_hard_uart_obj_t *uart_obj, const char *str, uint len) {
165+
void uart_tx_strn_cooked(machine_hard_uart_obj_t *uart_obj, const char *str, uint len) {
166166
for (const char *top = str + len; str < top; str++) {
167167
if (*str == '\n') {
168168
uart_tx_char(uart_obj, '\r');
@@ -184,10 +184,12 @@ STATIC void machine_hard_uart_print(const mp_print_t *print, mp_obj_t self_in, m
184184
/// - `id`is bus id.
185185
/// - `baudrate` is the clock rate.
186186
STATIC mp_obj_t machine_hard_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
187-
enum { ARG_id, ARG_baudrate };
187+
enum { ARG_id, ARG_baudrate, ARG_timeout, ARG_timeout_char };
188188
static const mp_arg_t allowed_args[] = {
189189
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
190190
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 9600} },
191+
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
192+
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
191193
};
192194

193195
// parse args
@@ -196,7 +198,7 @@ STATIC mp_obj_t machine_hard_uart_make_new(const mp_obj_type_t *type, size_t n_a
196198

197199
// get static peripheral object
198200
int uart_id = uart_find(args[ARG_id].u_obj);
199-
const machine_hard_uart_obj_t * self = &machine_hard_uart_obj[uart_id];
201+
machine_hard_uart_obj_t *self = &machine_hard_uart_obj[uart_id];
200202

201203
nrfx_uart_config_t config;
202204

@@ -238,19 +240,21 @@ STATIC mp_obj_t machine_hard_uart_make_new(const mp_obj_type_t *type, size_t n_a
238240
config.pselrts = MICROPY_HW_UART1_RTS;
239241
config.pselcts = MICROPY_HW_UART1_CTS;
240242
#endif
243+
self->timeout = args[ARG_timeout].u_int;
244+
self->timeout_char = args[ARG_timeout_char].u_int;
241245

242246
// Set context to this instance of UART
243247
config.p_context = (void *)self;
244248

245249
// Initialise ring buffer
246-
self->buf->rx_ringbuf.buf = self->buf->rx_ringbuf_array;
247-
self->buf->rx_ringbuf.size = sizeof(self->buf->rx_ringbuf_array);
248-
self->buf->rx_ringbuf.iget = 0;
249-
self->buf->rx_ringbuf.iput = 0;
250+
self->buf.rx_ringbuf.buf = self->buf.rx_ringbuf_array;
251+
self->buf.rx_ringbuf.size = sizeof(self->buf.rx_ringbuf_array);
252+
self->buf.rx_ringbuf.iget = 0;
253+
self->buf.rx_ringbuf.iput = 0;
250254

251255
// Enable event callback and start asynchronous receive
252256
nrfx_uart_init(self->p_uart, &config, uart_event_handler);
253-
nrfx_uart_rx(self->p_uart, &self->buf->rx_buf[0], 1);
257+
nrfx_uart_rx(self->p_uart, &self->buf.rx_buf[0], 1);
254258

255259
#if NRFX_UART_ENABLED
256260
nrfx_uart_rx_enable(self->p_uart);
@@ -286,20 +290,29 @@ STATIC mp_obj_t machine_hard_uart_readchar(mp_obj_t self_in) {
286290
}
287291
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_hard_uart_readchar_obj, machine_hard_uart_readchar);
288292

293+
// uart.any()
294+
STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) {
295+
machine_hard_uart_obj_t *self = self_in;
296+
return MP_OBJ_NEW_SMALL_INT(ringbuf_avail((ringbuf_t *)&self->buf.rx_ringbuf));
297+
}
298+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any);
299+
289300
// uart.sendbreak()
290301
STATIC mp_obj_t machine_hard_uart_sendbreak(mp_obj_t self_in) {
291302
return mp_const_none;
292303
}
293304
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_hard_uart_sendbreak_obj, machine_hard_uart_sendbreak);
294305

295-
// Since uart.write() waits up to the last byte, uart.txdone() always returns True.
306+
// uart.txdone()
296307
STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
297-
return mp_const_true;
308+
machine_hard_uart_obj_t *self = self_in;
309+
return mp_obj_new_bool(!nrfx_uart_tx_in_progress(self->p_uart));
298310
}
299311
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
300312

301313
STATIC const mp_rom_map_elem_t machine_hard_uart_locals_dict_table[] = {
302314
// instance methods
315+
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) },
303316
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
304317
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
305318
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
@@ -321,29 +334,38 @@ STATIC const mp_rom_map_elem_t machine_hard_uart_locals_dict_table[] = {
321334
STATIC MP_DEFINE_CONST_DICT(machine_hard_uart_locals_dict, machine_hard_uart_locals_dict_table);
322335

323336
STATIC mp_uint_t machine_hard_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
324-
const machine_hard_uart_obj_t *self = self_in;
337+
machine_hard_uart_obj_t *self = self_in;
325338
byte *buf = buf_in;
339+
uint32_t t = self->timeout + mp_hal_ticks_ms();
326340

327341
// read the data
328342
for (size_t i = 0; i < size; i++) {
329343
while (!uart_rx_any(self)) {
344+
if ((int32_t)(mp_hal_ticks_ms() - t) >= 0) { // timed out
345+
if (i == 0) {
346+
*errcode = MP_EAGAIN;
347+
return MP_STREAM_ERROR;
348+
} else {
349+
return i;
350+
}
351+
}
352+
MICROPY_EVENT_POLL_HOOK;
330353
}
331354
buf[i] = uart_rx_char(self);
355+
t = self->timeout_char + mp_hal_ticks_ms();
332356
}
333357

334358
return size;
335359
}
336360

337361
STATIC mp_uint_t machine_hard_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
338362
machine_hard_uart_obj_t *self = self_in;
339-
const byte *buf = buf_in;
340-
341-
nrfx_err_t err = NRFX_SUCCESS;
342-
for (int i = 0; i < size; i++) {
343-
err = uart_tx_char(self, (int)((uint8_t *)buf)[i]);
344-
}
345363

364+
nrfx_err_t err = nrfx_uart_tx(self->p_uart, buf_in, size);
346365
if (err == NRFX_SUCCESS) {
366+
while (nrfx_uart_tx_in_progress(self->p_uart)) {
367+
MICROPY_EVENT_POLL_HOOK;
368+
}
347369
// return number of bytes written
348370
return size;
349371
} else {
@@ -355,9 +377,20 @@ STATIC mp_uint_t machine_hard_uart_write(mp_obj_t self_in, const void *buf_in, m
355377
STATIC mp_uint_t machine_hard_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
356378
machine_hard_uart_obj_t *self = self_in;
357379
(void)self;
380+
mp_uint_t ret = 0;
358381

359-
if (request == MP_STREAM_FLUSH) {
360-
// Since uart.write() waits up to the last byte, uart.flush() always succeds.
382+
if (request == MP_STREAM_POLL) {
383+
uintptr_t flags = arg;
384+
if ((flags & MP_STREAM_POLL_RD) && uart_rx_any(self) != 0) {
385+
ret |= MP_STREAM_POLL_RD;
386+
}
387+
if ((flags & MP_STREAM_POLL_WR) && !nrfx_uart_tx_in_progress(self->p_uart)) {
388+
ret |= MP_STREAM_POLL_WR;
389+
}
390+
} else if (request == MP_STREAM_FLUSH) {
391+
while (nrfx_uart_tx_in_progress(self->p_uart)) {
392+
MICROPY_EVENT_POLL_HOOK;
393+
}
361394
return 0;
362395
}
363396
return MP_STREAM_ERROR;

ports/nrf/modules/machine/uart.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ void uart_init0(void);
3838
void uart_deinit(void);
3939
void uart_irq_handler(mp_uint_t uart_id);
4040

41-
bool uart_rx_any(const machine_hard_uart_obj_t * uart_obj);
42-
int uart_rx_char(const machine_hard_uart_obj_t * uart_obj);
43-
void uart_tx_strn(const machine_hard_uart_obj_t * uart_obj, const char *str, uint len);
44-
void uart_tx_strn_cooked(const machine_hard_uart_obj_t *uart_obj, const char *str, uint len);
41+
bool uart_rx_any(machine_hard_uart_obj_t *uart_obj);
42+
int uart_rx_char(machine_hard_uart_obj_t *uart_obj);
43+
void uart_tx_strn(machine_hard_uart_obj_t *uart_obj, const char *str, uint len);
44+
void uart_tx_strn_cooked(machine_hard_uart_obj_t *uart_obj, const char *str, uint len);
4545

4646
#endif

0 commit comments

Comments
 (0)
0