Skip to content

Commit e294bee

Browse files
committed
stmhal: Use stream's readinto.
1 parent e5b1b73 commit e294bee

1 file changed

Lines changed: 2 additions & 48 deletions

File tree

stmhal/uart.c

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -575,53 +575,6 @@ STATIC mp_obj_t pyb_uart_readchar(mp_obj_t self_in) {
575575
}
576576
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_readchar_obj, pyb_uart_readchar);
577577

578-
/// \method readinto(buf, len=-1)
579-
///
580-
/// Read data on the bus:
581-
///
582-
/// - `buf` is a mutable buffer which will be filled with read characters.
583-
/// - `len` is the maximum number of characters to read; if negative, uses len(buf).
584-
///
585-
/// Return value: number of characters stored in buf.
586-
STATIC mp_obj_t pyb_uart_readinto(mp_uint_t n_args, const mp_obj_t *pos_args) {
587-
pyb_uart_obj_t *self = pos_args[0];
588-
589-
// get the buffer to read into
590-
mp_buffer_info_t bufinfo;
591-
mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_WRITE);
592-
bufinfo.len >>= self->char_width;
593-
594-
// adjust the length, if given
595-
if (n_args == 3) {
596-
mp_int_t len = mp_obj_get_int(pos_args[2]);
597-
if (len >= 0 && len < bufinfo.len) {
598-
bufinfo.len = len;
599-
}
600-
}
601-
602-
// make sure we want at least 1 char, and wait for it to become available
603-
if (bufinfo.len == 0 || !uart_rx_wait(self, self->timeout)) {
604-
return MP_OBJ_NEW_SMALL_INT(0);
605-
}
606-
607-
// read the chars
608-
byte *buf = bufinfo.buf;
609-
for (;;) {
610-
int data = uart_rx_char(self);
611-
if (self->char_width == CHAR_WIDTH_9BIT) {
612-
*(uint16_t*)buf = data;
613-
buf += 2;
614-
} else {
615-
*buf++ = data;
616-
}
617-
if (--bufinfo.len == 0 || !uart_rx_wait(self, self->timeout_char)) {
618-
// return the number of chars read
619-
return mp_obj_new_int((buf - (byte*)bufinfo.buf) >> self->char_width);
620-
}
621-
}
622-
}
623-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pyb_uart_readinto_obj, 2, pyb_uart_readinto);
624-
625578
STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
626579
// instance methods
627580

@@ -635,12 +588,13 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
635588
{ MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj },
636589
/// \method readline()
637590
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
591+
/// \method readinto(buf[, nbytes])
592+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj },
638593
/// \method write(buf)
639594
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
640595

641596
{ MP_OBJ_NEW_QSTR(MP_QSTR_writechar), (mp_obj_t)&pyb_uart_writechar_obj },
642597
{ MP_OBJ_NEW_QSTR(MP_QSTR_readchar), (mp_obj_t)&pyb_uart_readchar_obj },
643-
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&pyb_uart_readinto_obj },
644598
};
645599

646600
STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table);

0 commit comments

Comments
 (0)