Skip to content

Commit

Permalink
bricks/ev3rt/mphalport: Optionally use sensor debug port.
Browse files Browse the repository at this point in the history
If you have the hardware to use the debug port, this is much less painful than refreshing the rfcomm port every time.
  • Loading branch information
laurensvalk committed Oct 15, 2024
1 parent c0b7ca9 commit ebf1cdc
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions bricks/ev3rt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand All @@ -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);
}
}

Expand Down

0 comments on commit ebf1cdc

Please sign in to comment.