@@ -103,7 +103,7 @@ static int uart_buf_get(uart_ringbuf_t *r, uint8_t *dest, uint16_t len) {
103103 if (r -> iget == r -> iput ) break ;
104104 }
105105 // move the buffer and adjust the pointers
106- memmove (r -> buf , r -> buf + res , res );
106+ memmove (r -> buf , r -> buf + res , r -> iput - res );
107107 r -> iget -= res ;
108108 r -> iput -= res ;
109109
@@ -120,15 +120,16 @@ static int uart_buf_put(uart_ringbuf_t *r, uint8_t *source, uint16_t len) {
120120 return res ;
121121}
122122
123- //---------------------------------------------------------------------------------------
124- int pattern_match (uint8_t * text , int text_length , uint8_t * pattern , int pattern_length ) {
123+ //---------------------------------------------------------------------------------------------
124+ static int match_pattern (uint8_t * text , int text_length , uint8_t * pattern , int pattern_length )
125+ {
125126 int c , d , e , position = -1 ;
126127
127128 if (pattern_length > text_length ) return -1 ;
128129
129130 for (c = 0 ; c <= (text_length - pattern_length ); c ++ ) {
130131 position = e = c ;
131-
132+ // check pattern
132133 for (d = 0 ; d < pattern_length ; d ++ ) {
133134 if (pattern [d ] == text [e ]) e ++ ;
134135 else break ;
@@ -197,7 +198,7 @@ static void uart_event_task(void *pvParameters)
197198 }
198199 else if (self -> pattern_cb ) {
199200 // ** callback on pattern received
200- res = pattern_match (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> pattern , self -> pattern_len );
201+ res = match_pattern (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> pattern , self -> pattern_len );
201202 if (res >= 0 ) {
202203 // found, pull data, including pattern from buffer
203204 uart_buf_get (uart_buf [self -> uart_num ], dtmp , res + self -> pattern_len );
@@ -288,10 +289,33 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
288289 machine_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
289290 uint32_t baudrate ;
290291 uart_get_baudrate (self -> uart_num + 1 , & baudrate );
292+ char lnend [16 ] = {'\0' };
293+ int lnend_idx = 0 ;
294+ for (int i = 0 ; i < strlen ((char * )self -> lineend ); i ++ ) {
295+ if (self -> lineend [i ] == 0 ) break ;
296+ if ((self -> lineend [i ] < 32 ) || (self -> lineend [i ] > 126 )) {
297+ if (self -> lineend [i ] == '\r' ) {
298+ sprintf (lnend + lnend_idx , "\\r" );
299+ lnend_idx += 2 ;
300+ }
301+ else if (self -> lineend [i ] == '\n' ) {
302+ sprintf (lnend + lnend_idx , "\\n" );
303+ lnend_idx += 2 ;
304+ }
305+ else {
306+ sprintf (lnend + lnend_idx , "\\x%2x" , self -> lineend [i ]);
307+ lnend_idx += 4 ;
308+ }
309+ }
310+ else {
311+ sprintf (lnend + lnend_idx , "%c" , self -> lineend [i ]);
312+ lnend_idx ++ ;
313+ }
314+ }
291315
292- mp_printf (print , "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, rts=%d, cts=%d, timeout=%u, buf_size=%u)" ,
316+ mp_printf (print , "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, tx=%d, rx=%d, rts=%d, cts=%d, timeout=%u, buf_size=%u, lineend=b'%s' )" ,
293317 self -> uart_num + 1 , baudrate , self -> bits , _parity_name [self -> parity ],
294- self -> stop , self -> tx , self -> rx , self -> rts , self -> cts , self -> timeout , self -> buffer_size );
318+ self -> stop , self -> tx , self -> rx , self -> rts , self -> cts , self -> timeout , self -> buffer_size , lnend );
295319 if (self -> data_cb ) {
296320 mp_printf (print , "\n data CB: True, on len: %d" , self -> data_cb_size );
297321 }
@@ -418,7 +442,7 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
418442 if (MP_OBJ_IS_STR (args [ARG_lineend ].u_obj )) {
419443 size_t lnendlen ;
420444 const char * lnend = mp_obj_str_get_data (args [ARG_lineend ].u_obj , & lnendlen );
421- if ((lnend ) && (lnendlen > 0 ) && (lnendlen > 0 )) sprintf ((char * )self -> lineend , "%s" , lnend );
445+ if ((lnend ) && (lnendlen > 0 ) && (lnendlen < 3 )) sprintf ((char * )self -> lineend , "%s" , lnend );
422446 }
423447}
424448
@@ -445,7 +469,8 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
445469 .parity = UART_PARITY_DISABLE ,
446470 .stop_bits = UART_STOP_BITS_1 ,
447471 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE ,
448- .rx_flow_ctrl_thresh = 0
472+ .rx_flow_ctrl_thresh = 0 ,
473+ .use_ref_tick = true
449474 };
450475
451476 if (uart_mutex == NULL ) {
@@ -493,12 +518,14 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
493518 mp_arg_val_t kargs [MP_ARRAY_SIZE (allowed_args )];
494519 mp_arg_parse_all (n_args - 1 , args + 1 , & kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , kargs );
495520
521+ // Set buffer size
496522 int bufsize = kargs [ARG_buffer_size ].u_int ;
497523 if (bufsize < 512 ) bufsize = 512 ;
498524 if (bufsize > 8192 ) bufsize = 8192 ;
499525 self -> buffer_size = bufsize ;
500526
501527 if (uart_buf [self -> uart_num ] == NULL ) {
528+ // First time, create ring buffer
502529 uart_ringbuf_alloc (self -> uart_num , bufsize );
503530 if (uart_buf [self -> uart_num ] == NULL ) {
504531 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "UART(%d) Error allocating ring buffer" , uart_num ));
@@ -569,9 +596,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_flush_obj, machine_uart_flush);
569596STATIC mp_obj_t machine_uart_readln (size_t n_args , const mp_obj_t * args ) {
570597 machine_uart_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
571598
572- vstr_t vstr ;
573- int res = -1 ;
599+ uint8_t * rdstr = NULL ;
600+ int rdlen = -1 ;
574601 int lnendlen = strlen ((char * )self -> lineend );
602+ if (lnendlen == 0 ) return mp_const_none ;
603+
575604 int timeout = self -> timeout ;
576605 if (n_args == 2 ) timeout = mp_obj_get_int (args [1 ]);
577606
@@ -582,14 +611,18 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
582611 if (uart_mutex ) xSemaphoreGive (uart_mutex );
583612 return mp_const_none ;
584613 }
585- res = pattern_match (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> lineend , lnendlen );
586- if (res >= 0 ) {
614+ rdlen = match_pattern (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> lineend , lnendlen );
615+ if (rdlen >= 0 ) {
587616 // found, pull data, including pattern from buffer
588- vstr_init_len (& vstr , res + lnendlen );
589- uart_buf_get (uart_buf [self -> uart_num ], (uint8_t * )vstr .buf , res + lnendlen );
617+ rdlen += lnendlen ;
618+ rdstr = calloc (rdlen + 1 , 1 );
619+ if (rdstr ) {
620+ uart_buf_get (uart_buf [self -> uart_num ], rdstr , rdlen );
621+ rdstr [rdlen ] = 0 ;
622+ }
590623 }
591624 if (uart_mutex ) xSemaphoreGive (uart_mutex );
592- if (res < 0 ) return mp_const_none ;
625+ if (rdlen < 0 ) return mp_const_none ;
593626 }
594627 else {
595628 // wait until line end received or timeout
@@ -610,11 +643,15 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
610643 mp_hal_reset_wdt ();
611644 continue ;
612645 }
613- res = pattern_match (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> lineend , lnendlen );
614- if (res >= 0 ) {
646+ rdlen = match_pattern (uart_buf [self -> uart_num ]-> buf , uart_buf [self -> uart_num ]-> iput , self -> lineend , lnendlen );
647+ if (rdlen >= 0 ) {
648+ rdlen += lnendlen ;
615649 // found, pull data, including pattern from buffer
616- vstr_init_len (& vstr , res + lnendlen );
617- uart_buf_get (uart_buf [self -> uart_num ], (uint8_t * )vstr .buf , res + lnendlen );
650+ rdstr = calloc (rdlen + 1 , 1 );
651+ if (rdstr ) {
652+ uart_buf_get (uart_buf [self -> uart_num ], rdstr , rdlen );
653+ rdstr [rdlen ] = 0 ;
654+ }
618655 if (uart_mutex ) xSemaphoreGive (uart_mutex );
619656 break ;
620657 }
@@ -624,10 +661,12 @@ STATIC mp_obj_t machine_uart_readln(size_t n_args, const mp_obj_t *args) {
624661 mp_hal_reset_wdt ();
625662 }
626663 MP_THREAD_GIL_ENTER ();
627- if (res < 0 ) return mp_const_none ;
664+ if (rdlen < 0 ) return mp_const_none ;
628665 }
629-
630- return mp_obj_new_str_from_vstr (& mp_type_bytes , & vstr );
666+ if (rdstr == NULL ) return mp_const_none ;
667+ mp_obj_t res_str = mp_obj_new_str ((const char * )rdstr , rdlen , false);
668+ free (rdstr );
669+ return res_str ;
631670}
632671STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_uart_readln_obj , 1 , 2 , machine_uart_readln );
633672
@@ -728,6 +767,9 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
728767};
729768STATIC MP_DEFINE_CONST_DICT (machine_uart_locals_dict , machine_uart_locals_dict_table );
730769
770+
771+ // === Stream UART functions ===
772+
731773//------------------------------------------------------------------------------------------------
732774STATIC mp_uint_t machine_uart_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
733775 machine_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
0 commit comments