diff --git a/Firmware/MotorControl/axis.hpp b/Firmware/MotorControl/axis.hpp index 446958d6..3271e432 100644 --- a/Firmware/MotorControl/axis.hpp +++ b/Firmware/MotorControl/axis.hpp @@ -163,6 +163,10 @@ public: // TODO: change arming logic to arm after waiting bool main_continue = update_handler(); + if (axis_num_ == 0) { + uart_poll(); // TODO: move to board-level control loop once it exists + } + // Check we meet deadlines after queueing ++loop_counter_; diff --git a/Firmware/MotorControl/odrive_main.h b/Firmware/MotorControl/odrive_main.h index 392b9c7e..d0dcface 100644 --- a/Firmware/MotorControl/odrive_main.h +++ b/Firmware/MotorControl/odrive_main.h @@ -12,6 +12,7 @@ #include #include #include +#include extern "C" { #endif diff --git a/Firmware/communication/interface_uart.cpp b/Firmware/communication/interface_uart.cpp index aa195574..08cd83f4 100644 --- a/Firmware/communication/interface_uart.cpp +++ b/Firmware/communication/interface_uart.cpp @@ -62,9 +62,7 @@ static void uart_server_thread(void * ctx) { (void) ctx; for (;;) { - osDelay(1); - - // Check for UART errors and restart recieve DMA transfer if required + // Check for UART errors and restart receive DMA transfer if required if (huart4.RxState != HAL_UART_STATE_BUSY_RX) { HAL_UART_AbortReceive(&huart4); HAL_UART_Receive_DMA(&huart4, dma_rx_buffer, sizeof(dma_rx_buffer)); @@ -92,11 +90,17 @@ static void uart_server_thread(void * ctx) { new_rcv_idx - dma_last_rcv_idx, uart4_stream_output); dma_last_rcv_idx = new_rcv_idx; } - }; + + // The thread is woken up by the control loop at 8kHz. This should be + // enough for most applications. + // At 1Mbaud/s that corresponds to at most 12.5 bytes which can arrive + // during the sleep period. + osThreadSuspend(nullptr); + } } void start_uart_server() { - // DMA is set up to recieve in a circular buffer forever. + // DMA is set up to receive in a circular buffer forever. // We dont use interrupts to fetch the data, instead we periodically read // data out of the circular buffer into a parse buffer, controlled by a state machine HAL_UART_Receive_DMA(&huart4, dma_rx_buffer, sizeof(dma_rx_buffer)); @@ -107,6 +111,10 @@ void start_uart_server() { uart_thread = osThreadCreate(osThread(uart_server_thread_def), NULL); } +void uart_poll() { + osThreadResume(uart_thread); +} + void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart) { osSemaphoreRelease(sem_uart_dma); } diff --git a/Firmware/communication/interface_uart.h b/Firmware/communication/interface_uart.h index 65033a6f..18f1a2dc 100644 --- a/Firmware/communication/interface_uart.h +++ b/Firmware/communication/interface_uart.h @@ -14,6 +14,7 @@ extern osThreadId uart_thread; extern const uint32_t stack_size_uart_thread; void start_uart_server(void); +void uart_poll(void); #ifdef __cplusplus }