From ebf1cdc39e146e2e3c735d1ee91c6a93f0aeb38f Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 8 Oct 2024 11:55:40 +0200 Subject: [PATCH] bricks/ev3rt/mphalport: Optionally use sensor debug port. If you have the hardware to use the debug port, this is much less painful than refreshing the rfcomm port every time. --- bricks/ev3rt/mphalport.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/bricks/ev3rt/mphalport.c b/bricks/ev3rt/mphalport.c index 7f1dfab2d..c317f941f 100644 --- a/bricks/ev3rt/mphalport.c +++ b/bricks/ev3rt/mphalport.c @@ -29,20 +29,31 @@ void pb_stack_get_info(char **sstack, char **estack) { *estack = *sstack + (uint32_t)main_task_init_block->sstksz; } -static FILE *bt = NULL; -bool bluetooth_is_connected(uint32_t *in_waiting) { - if (!bt) { - bt = ev3_serial_open_file(EV3_SERIAL_BT); - syslog(LOG_NOTICE, "open bt file"); +// On the /ev3rt/etc/rc.conf.ini choose: +// [Bluetooth] TurnOff=1 and [Sensors] DisablePort1=1 to use sensor port 1 for stdio +// [Bluetooth] TurnOff=0 and [Sensors] DisablePort1=0 to use bluetooth rfcomm0 for stdio +static FILE *fd = NULL; +extern const bool_t *ev3rt_bluetooth_disabled; +extern const bool_t *ev3rt_sensor_port_1_disabled; +static serial_port_t serial_port_type = EV3_SERIAL_UART; + +static bool serial_configured(uint32_t *in_waiting) { + if (!fd) { + serial_port_type = (*ev3rt_sensor_port_1_disabled && *ev3rt_bluetooth_disabled) ? serial_port_type : EV3_SERIAL_BT; + + fd = ev3_serial_open_file(serial_port_type); + syslog(LOG_NOTICE, "open serial file"); + + // Disables default echo and extra CR on REPL, which confuses MicroPython. + serial_ctl_por(serial_port_type, (/*IOCTL_ECHO | IOCTL_CRLF | */ IOCTL_FCSND | IOCTL_FCRCV)); } T_SERIAL_RPOR rpor; - ER ercd = serial_ref_por(EV3_SERIAL_BT, &rpor); + ER ercd = serial_ref_por(serial_port_type, &rpor); *in_waiting = 0; return ercd == E_OK; } - void mp_hal_delay_ms(mp_uint_t Delay) { uint32_t start = pbdrv_clock_get_ms(); do { @@ -53,7 +64,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) { uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { uintptr_t ret = 0; uint32_t in_waiting; - if ((poll_flags & MP_STREAM_POLL_RD) && bluetooth_is_connected(&in_waiting) && in_waiting) { + if ((poll_flags & MP_STREAM_POLL_RD) && serial_configured(&in_waiting) && in_waiting) { ret |= MP_STREAM_POLL_RD; } @@ -63,22 +74,22 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { // Receive single character int mp_hal_stdin_rx_chr(void) { uint32_t in_waiting; - if (!bluetooth_is_connected(&in_waiting)) { + if (!serial_configured(&in_waiting)) { return 0; } - // while (bluetooth_is_connected(&in_waiting) && in_waiting == 0) { + // while (serial_configured(&in_waiting) && in_waiting == 0) { // MICROPY_EVENT_POLL_HOOK // } uint8_t c; - serial_rea_dat(EV3_SERIAL_BT, (char *)&c, 1); + serial_rea_dat(serial_port_type, (char *)&c, 1); return c; } // Send string of given length void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { uint32_t in_waiting; - if (bluetooth_is_connected(&in_waiting)) { - serial_wri_dat(EV3_SERIAL_BT, str, len); + if (serial_configured(&in_waiting)) { + serial_wri_dat(serial_port_type, str, len); } }