3636#include "inc/hw_nvic.h"
3737#include "hw_memmap.h"
3838#include "py/mpstate.h"
39+ #include "py/runtime.h"
3940#include MICROPY_HAL_H
4041#include "rom_map.h"
4142#include "interrupt.h"
4748#include "pybuart.h"
4849#include "utils.h"
4950#include "irq.h"
51+ #include "moduos.h"
5052
5153#ifdef USE_FREERTOS
5254#include "FreeRTOS.h"
@@ -67,11 +69,6 @@ static void hal_TickInit (void);
6769 ******************************************************************************/
6870static volatile uint32_t HAL_tickCount ;
6971
70- /******************************************************************************
71- DECLARE PUBLIC DATA
72- ******************************************************************************/
73- struct _pyb_uart_obj_t * pyb_stdio_uart ;
74-
7572/******************************************************************************
7673 DECLARE IMPORTED DATA
7774 ******************************************************************************/
@@ -141,34 +138,56 @@ void mp_hal_stdout_tx_str(const char *str) {
141138}
142139
143140void mp_hal_stdout_tx_strn (const char * str , uint32_t len ) {
144- // send stdout to UART
145- if (pyb_stdio_uart != NULL ) {
146- uart_tx_strn (pyb_stdio_uart , str , len );
141+ if (MP_STATE_PORT (os_term_dup_obj )) {
142+ if (MP_OBJ_IS_TYPE (MP_STATE_PORT (os_term_dup_obj )-> stream_o , & pyb_uart_type )) {
143+ uart_tx_strn (MP_STATE_PORT (os_term_dup_obj )-> stream_o , str , len );
144+ } else {
145+ MP_STATE_PORT (os_term_dup_obj )-> write [2 ] = mp_obj_new_bytes ((const byte * )str , len );
146+ mp_call_method_n_kw (1 , 0 , MP_STATE_PORT (os_term_dup_obj )-> write );
147+ }
147148 }
148149 // and also to telnet
149- if (telnet_is_active ()) {
150- telnet_tx_strn (str , len );
151- }
150+ telnet_tx_strn (str , len );
152151}
153152
154- void mp_hal_stdout_tx_strn_cooked (const char * str , uint32_t len ) {
155- // send stdout to UART
156- if (pyb_stdio_uart != NULL ) {
157- uart_tx_strn_cooked (pyb_stdio_uart , str , len );
153+ void mp_hal_stdout_tx_strn_cooked (const char * str , uint32_t len ) {
154+ int32_t nslen = 0 ;
155+ const char * _str = str ;
156+
157+ for (int i = 0 ; i < len ; i ++ ) {
158+ if (str [i ] == '\n' ) {
159+ mp_hal_stdout_tx_strn (_str , nslen );
160+ mp_hal_stdout_tx_strn ("\r\n" , 2 );
161+ _str += nslen + 1 ;
162+ nslen = 0 ;
163+ } else {
164+ nslen ++ ;
165+ }
158166 }
159- // and also to telnet
160- if (telnet_is_active ()) {
161- telnet_tx_strn_cooked (str , len );
167+ if (_str < str + len ) {
168+ mp_hal_stdout_tx_strn (_str , nslen );
162169 }
163170}
164171
165172int mp_hal_stdin_rx_chr (void ) {
166173 for ( ;; ) {
174+ // read telnet first
167175 if (telnet_rx_any ()) {
168176 return telnet_rx_char ();
169- }
170- else if (pyb_stdio_uart != NULL && uart_rx_any (pyb_stdio_uart )) {
171- return uart_rx_char (pyb_stdio_uart );
177+ } else if (MP_STATE_PORT (os_term_dup_obj )) { // then the stdio_dup
178+ if (MP_OBJ_IS_TYPE (MP_STATE_PORT (os_term_dup_obj )-> stream_o , & pyb_uart_type )) {
179+ if (uart_rx_any (MP_STATE_PORT (os_term_dup_obj )-> stream_o )) {
180+ return uart_rx_char (MP_STATE_PORT (os_term_dup_obj )-> stream_o );
181+ }
182+ } else {
183+ MP_STATE_PORT (os_term_dup_obj )-> read [2 ] = mp_obj_new_int (1 );
184+ mp_obj_t rbytes = mp_call_method_n_kw (1 , 0 , MP_STATE_PORT (os_term_dup_obj )-> read );
185+ if (rbytes != mp_const_none ) {
186+ mp_buffer_info_t bufinfo ;
187+ mp_get_buffer_raise (rbytes , & bufinfo , MP_BUFFER_READ );
188+ return ((int * )(bufinfo .buf ))[0 ];
189+ }
190+ }
172191 }
173192 HAL_Delay (1 );
174193 }
0 commit comments