mirror of
https://github.com/EFeru/hoverboard-sideboard-hack-STM.git
synced 2025-07-27 09:19:33 +00:00
UART update
- disabled the Rx errors to avoid DMA stop and additional need to manage the UART error handler
This commit is contained in:
parent
437ed266ac
commit
27f6320129
@ -56,6 +56,7 @@ void intro_demo_led(uint32_t tDelay);
|
||||
|
||||
/* input initialization function */
|
||||
void input_init(void);
|
||||
void UART_DisableRxErrors(UART_HandleTypeDef *huart);
|
||||
|
||||
/* usart read functions */
|
||||
void usart_rx_check(void);
|
||||
@ -65,7 +66,6 @@ void usart_process_debug(uint8_t *userCommand, uint32_t len);
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
void usart_process_data(SerialFeedback *Feedback_in, SerialFeedback *Feedback_out);
|
||||
#endif
|
||||
void UART_EndRxTransfer(UART_HandleTypeDef *huart);
|
||||
|
||||
/* i2c write/read functions */
|
||||
int8_t i2c_writeBytes(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t *data);
|
||||
|
62
Src/util.c
62
Src/util.c
@ -39,7 +39,6 @@ SerialSideboard Sideboard;
|
||||
#if defined(SERIAL_DEBUG) || defined(SERIAL_FEEDBACK)
|
||||
static uint8_t rx_buffer[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer
|
||||
static uint32_t rx_buffer_len = ARRAY_LEN(rx_buffer);
|
||||
static uint32_t old_pos;
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
@ -123,11 +122,9 @@ void intro_demo_led(uint32_t tDelay)
|
||||
|
||||
/* =========================== Input Initialization Function =========================== */
|
||||
void input_init(void) {
|
||||
#ifdef SERIAL_CONTROL
|
||||
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Sideboard, sizeof(Sideboard));
|
||||
#endif
|
||||
#if defined(SERIAL_DEBUG) || defined(SERIAL_FEEDBACK)
|
||||
HAL_UART_Receive_DMA (&huart2, (uint8_t *)rx_buffer, sizeof(rx_buffer));
|
||||
HAL_UART_Receive_DMA(&huart2, (uint8_t *)rx_buffer, sizeof(rx_buffer));
|
||||
UART_DisableRxErrors(&huart2);
|
||||
#endif
|
||||
|
||||
intro_demo_led(100); // Short LEDs intro demo with 100 ms delay. This also gives some time for the MPU-6050 to power-up.
|
||||
@ -147,6 +144,23 @@ void input_init(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disable Rx Errors detection interrupts on UART peripheral (since we do not want DMA to be stopped)
|
||||
* The incorrect data will be filtered based on the START_FRAME and checksum.
|
||||
* @param huart: UART handle.
|
||||
* @retval None
|
||||
*/
|
||||
#if defined(SERIAL_DEBUG) || defined(SERIAL_FEEDBACK)
|
||||
void UART_DisableRxErrors(UART_HandleTypeDef *huart)
|
||||
{
|
||||
/* Disable PE (Parity Error) interrupts */
|
||||
CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
|
||||
|
||||
/* Disable EIE (Frame error, noise error, overrun error) interrupts */
|
||||
CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* =========================== USART READ Functions =========================== */
|
||||
|
||||
/*
|
||||
@ -156,6 +170,7 @@ void input_init(void) {
|
||||
void usart_rx_check(void)
|
||||
{
|
||||
#ifdef SERIAL_DEBUG
|
||||
static uint32_t old_pos;
|
||||
uint32_t pos;
|
||||
|
||||
pos = rx_buffer_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer, Rx: DMA1_Channel6->CNDTR, Tx: DMA1_Channel7
|
||||
@ -176,8 +191,10 @@ void usart_rx_check(void)
|
||||
#endif // SERIAL_DEBUG
|
||||
|
||||
#ifdef SERIAL_FEEDBACK
|
||||
static uint32_t old_pos;
|
||||
uint32_t pos;
|
||||
uint8_t *ptr;
|
||||
uint8_t *ptr;
|
||||
|
||||
pos = rx_buffer_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer, Rx: DMA1_Channel6->CNDTR, Tx: DMA1_Channel7
|
||||
if (pos != old_pos) { // Check change in received data
|
||||
ptr = (uint8_t *)&FeedbackRaw; // Initialize the pointer with FeedbackRaw address
|
||||
@ -237,39 +254,6 @@ void usart_process_data(SerialFeedback *Feedback_in, SerialFeedback *Feedback_ou
|
||||
}
|
||||
#endif // SERIAL_FEEDBACK
|
||||
|
||||
/*
|
||||
* UART User Error Callback
|
||||
* - According to the STM documentation, when a DMA transfer error occurs during a DMA read or a write access,
|
||||
* the faulty channel is automatically disabled through a hardware clear of its EN bit
|
||||
* - For hoverboard applications, the UART communication can be unrealiable, disablind the DMA transfer
|
||||
* - therefore the DMA needs to be re-started
|
||||
*/
|
||||
void HAL_UART_ErrorCallback(UART_HandleTypeDef *uartHandle) {
|
||||
#if defined(SERIAL_DEBUG) || defined(SERIAL_FEEDBACK)
|
||||
if(uartHandle->Instance == USART2) {
|
||||
HAL_DMA_Abort(uartHandle->hdmarx);
|
||||
UART_EndRxTransfer(uartHandle);
|
||||
HAL_UART_Receive_DMA(uartHandle, (uint8_t *)rx_buffer, sizeof(rx_buffer));
|
||||
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;
|
||||
}
|
||||
|
||||
/* =========================== I2C WRITE Functions =========================== */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user