From 46144499d07cf287421ba9168018dbf6fbc7bb28 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Tue, 4 Feb 2025 09:45:53 -0800 Subject: [PATCH 1/4] fix: :bug: Wait for serial port to be properly configured by VEXos --- src/devices/vdml_serial.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/devices/vdml_serial.c b/src/devices/vdml_serial.c index a6cec624..031efc88 100644 --- a/src/devices/vdml_serial.c +++ b/src/devices/vdml_serial.c @@ -20,6 +20,19 @@ #include "vdml/registry.h" #include "vdml/vdml.h" +v5_device_e_t registry_get_plugged_type(uint8_t port); + +#define SERIAL_TIMEOUT 500 + +/** + * VEXos does not immediately configure a port as geeric serial after calling + * vexDeviceGenericSerialEnable. Therefore, we have to check that the port is + * configured as a generic serial port, and if not, delay for up to 500ms. + */ +#define claim_serial_port_i(port) for (uint8_t i = 0; i < SERIAL_TIMEOUT / 5 || \ + registry_get_plugged_type(port) == E_DEVICE_SERIAL; ++i) { delay(5); } \ + claim_port_i(port, E_DEVICE_SERIAL) + // Control function int32_t serial_enable(uint8_t port) { @@ -43,13 +56,13 @@ int32_t serial_enable(uint8_t port) { } int32_t serial_set_baudrate(uint8_t port, int32_t baudrate) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); vexDeviceGenericSerialBaudrate(device->device_info, baudrate); return_port(port - 1, PROS_SUCCESS); } int32_t serial_flush(uint8_t port) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); vexDeviceGenericSerialFlush(device->device_info); return_port(port - 1, PROS_SUCCESS); } @@ -57,13 +70,13 @@ int32_t serial_flush(uint8_t port) { // Telemetry functions int32_t serial_get_read_avail(uint8_t port) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialReceiveAvail(device->device_info); return_port(port - 1, rtn); } int32_t serial_get_write_free(uint8_t port) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialWriteFree(device->device_info); return_port(port - 1, rtn); } @@ -71,19 +84,19 @@ int32_t serial_get_write_free(uint8_t port) { // Read functions int32_t serial_peek_byte(uint8_t port) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialPeekChar(device->device_info); return_port(port - 1, rtn); } int32_t serial_read_byte(uint8_t port) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialReadChar(device->device_info); return_port(port - 1, rtn); } int32_t serial_read(uint8_t port, uint8_t* buffer, int32_t length) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialReceive(device->device_info, buffer, length); return_port(port - 1, rtn); } @@ -91,7 +104,7 @@ int32_t serial_read(uint8_t port, uint8_t* buffer, int32_t length) { // Write functions int32_t serial_write_byte(uint8_t port, uint8_t buffer) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialWriteChar(device->device_info, buffer); if (rtn == -1) { errno = EIO; @@ -101,7 +114,7 @@ int32_t serial_write_byte(uint8_t port, uint8_t buffer) { } int32_t serial_write(uint8_t port, uint8_t* buffer, int32_t length) { - claim_port_i(port - 1, E_DEVICE_SERIAL); + claim_serial_port_i(port - 1); int32_t rtn = vexDeviceGenericSerialTransmit(device->device_info, buffer, length); if (rtn == -1) { errno = EIO; From fe92d67fdc6dfe805ff42570f3a63201b737d720 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Thu, 27 Mar 2025 10:58:42 -0700 Subject: [PATCH 2/4] fix: :adhesive_bandage: Remove unnecessary wait when device is not serial --- src/devices/vdml_serial.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/devices/vdml_serial.c b/src/devices/vdml_serial.c index 031efc88..78b11360 100644 --- a/src/devices/vdml_serial.c +++ b/src/devices/vdml_serial.c @@ -25,13 +25,14 @@ v5_device_e_t registry_get_plugged_type(uint8_t port); #define SERIAL_TIMEOUT 500 /** - * VEXos does not immediately configure a port as geeric serial after calling + * VEXos does not immediately configure a port as generic serial after calling * vexDeviceGenericSerialEnable. Therefore, we have to check that the port is * configured as a generic serial port, and if not, delay for up to 500ms. */ #define claim_serial_port_i(port) for (uint8_t i = 0; i < SERIAL_TIMEOUT / 5 || \ - registry_get_plugged_type(port) == E_DEVICE_SERIAL; ++i) { delay(5); } \ - claim_port_i(port, E_DEVICE_SERIAL) + registry_get_plugged_type(port) == E_DEVICE_SERIAL || \ + registry_get_plugged_type(port) != E_DEVICE_NONE; ++i) { delay(5); } \ + claim_port_i(port, E_DEVICE_SERIAL) // Control function From 629e570c565dc49ad89bb6a5ac94aa64a7c5eeac Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Wed, 2 Apr 2025 15:43:31 -0400 Subject: [PATCH 3/4] cleanup claim_serial_port_i, call vexBackgroundProcessing when rtos uninitialized --- src/devices/vdml_serial.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/devices/vdml_serial.c b/src/devices/vdml_serial.c index 78b11360..275651cd 100644 --- a/src/devices/vdml_serial.c +++ b/src/devices/vdml_serial.c @@ -29,10 +29,17 @@ v5_device_e_t registry_get_plugged_type(uint8_t port); * vexDeviceGenericSerialEnable. Therefore, we have to check that the port is * configured as a generic serial port, and if not, delay for up to 500ms. */ -#define claim_serial_port_i(port) for (uint8_t i = 0; i < SERIAL_TIMEOUT / 5 || \ - registry_get_plugged_type(port) == E_DEVICE_SERIAL || \ - registry_get_plugged_type(port) != E_DEVICE_NONE; ++i) { delay(5); } \ - claim_port_i(port, E_DEVICE_SERIAL) +#define claim_serial_port_i(port) \ + for (int i = 0; i < SERIAL_TIMEOUT / 5 && \ + registry_get_plugged_type(port) == E_DEVICE_NONE; ++i) { \ + if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) { \ + vexBackgroundProcessing(); \ + registry_update_types(); \ + } else { \ + delay(5); \ + } \ + } \ + claim_port_i(port, E_DEVICE_SERIAL) // Control function From 7ed3532f597d0afb329e631b04c81cbb147be099 Mon Sep 17 00:00:00 2001 From: Andrew Lu Date: Wed, 2 Apr 2025 15:47:44 -0400 Subject: [PATCH 4/4] switch loop variable back to uint8_t --- src/devices/vdml_serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devices/vdml_serial.c b/src/devices/vdml_serial.c index 275651cd..66a4d04c 100644 --- a/src/devices/vdml_serial.c +++ b/src/devices/vdml_serial.c @@ -30,7 +30,7 @@ v5_device_e_t registry_get_plugged_type(uint8_t port); * configured as a generic serial port, and if not, delay for up to 500ms. */ #define claim_serial_port_i(port) \ - for (int i = 0; i < SERIAL_TIMEOUT / 5 && \ + for (uint8_t i = 0; i < SERIAL_TIMEOUT / 5 && \ registry_get_plugged_type(port) == E_DEVICE_NONE; ++i) { \ if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) { \ vexBackgroundProcessing(); \