diff --git a/Inc/defines.h b/Inc/defines.h index d8d3577..414a3a5 100644 --- a/Inc/defines.h +++ b/Inc/defines.h @@ -176,4 +176,5 @@ void usart_process_debug(uint8_t *userCommand, uint32_t len); #endif #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx); -#endif \ No newline at end of file +#endif +void UART_EndRxTransfer(UART_HandleTypeDef *huart); \ No newline at end of file diff --git a/Src/control.c b/Src/control.c index f9fce75..ec963d4 100644 --- a/Src/control.c +++ b/Src/control.c @@ -23,6 +23,7 @@ extern UART_HandleTypeDef huart3; #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) extern uint8_t rx_buffer_L[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_L_len = ARRAY_LEN(rx_buffer_L); +static uint32_t old_pos; #endif #if defined(CONTROL_SERIAL_USART2) extern uint16_t timeoutCntSerial_L; // Timeout counter for Rx Serial command @@ -31,6 +32,7 @@ extern uint16_t timeoutCntSerial_L; // Timeout counter for Rx Ser #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) extern uint8_t rx_buffer_R[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_R_len = ARRAY_LEN(rx_buffer_R); +static uint32_t old_pos; #endif #if defined(CONTROL_SERIAL_USART3) extern uint16_t timeoutCntSerial_R; // Timeout counter for Rx Serial command @@ -151,7 +153,6 @@ void Nunchuck_Read() { void usart2_rx_check(void) { #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) - static uint32_t old_pos; uint32_t pos; pos = rx_buffer_L_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer #endif @@ -203,7 +204,6 @@ void usart2_rx_check(void) void usart3_rx_check(void) { #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) - static uint32_t old_pos; uint32_t pos; pos = rx_buffer_R_len - __HAL_DMA_GET_COUNTER(huart3.hdmarx); // Calculate current position in buffer #endif @@ -300,13 +300,36 @@ void usart_process_command(SerialCommand *command_in, SerialCommand *command_out void HAL_UART_ErrorCallback(UART_HandleTypeDef *uartHandle) { #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) if(uartHandle->Instance == USART2) { + HAL_DMA_Abort(uartHandle->hdmarx); + UART_EndRxTransfer(uartHandle); HAL_UART_Receive_DMA(uartHandle, (uint8_t *)rx_buffer_L, sizeof(rx_buffer_L)); + old_pos = 0; } #endif #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) if(uartHandle->Instance == USART3) { + HAL_DMA_Abort(uartHandle->hdmarx); + UART_EndRxTransfer(uartHandle); HAL_UART_Receive_DMA(uartHandle, (uint8_t *)rx_buffer_R, sizeof(rx_buffer_R)); + old_pos = 0; } #endif } + +/** + * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). + * @param huart: UART handle. + * @retval None + */ +void UART_EndRxTransfer(UART_HandleTypeDef *huart) +{ + /* Disable RXNE (Interrupt Enable) and PE (Parity Error) interrupts */ + CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); + /* Disable EIE (Frame error, noise error, overrun error) interrupts */ + CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); + + /* At end of Rx process, restore huart->RxState to Ready */ + huart->RxState = HAL_UART_STATE_READY; +} +