UART in Interrupt mode Stuck??
- November 22, 2023
- 1 reply
- 3453 views
Hello all,
I'm using STM32 F103CBT6 MCU on a custom board where MAXRS485 is used. Rx and Tx of the Modbus chip is connected to USART1 on the MCU.
To receive and Transmit data I'm using UART in interrupt mode. The program also has 8 GPIO interrupts on multiple ports, tim3 interrupts every 10ms and tim1 is used for software debouncing which only works when an input occurs for 50ms.
Priorities are set as follows:
GPIO 0
UART 1
TIM1 2
TIM3 3
I'm facing following issue in UART functionality :
When UART receives MODBUS query every 5 seconds, after 7- 8 hours, the UART interrupt stops receiving new data. If breakpoint is set, it does not get triggered on the
HAL_UART_RxCpltCallback
and no data is received in the buffer. I have multiple boards kept on testing, this situation occurs on different boards everyday. UART starts working again after reseting the chip.
I've connected an LED on both Rx and Tx pin when the data is received on Modbus chip, an LED glows. That is how I know that the Modbus data is getting received in MAXRS485.
I'm attaching all the interrupt code below as well as UART Rx and Tx code.
Please help me find the root cause of this solution.
UART Rx 1 Image attached
UART Rx 2
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
data_in[DataPos] = rxBuff;
DataPos+=1;
uart_rx_count += 1;
TotalCharsReceived=DataPos;
HAL_UART_Receive_IT(&huart2, &rxBuff, 1);
}
UART Tx
void MBSendData(unsigned char count)
{
int temp_size = 0;
isr_flags=USART2->SR;
cr1_its=USART2->CR1;
cr3_its=USART2->CR3;
/* UART parity error interrupt occurred ----------------------------------*/
if (((isr_flags & USART_SR_PE) != RESET) && ((cr1_its & USART_CR1_PEIE) != RESET))
{
USART2->DR=HAL_UART_ERROR_PE;
data_in[count]=HAL_UART_ERROR_PE;
}
/* UART noise error interrupt occurred -----------------------------------*/
if (((isr_flags & USART_SR_NE) != RESET) && ((cr3_its & USART_CR3_EIE) != RESET))
{
USART2->DR=HAL_UART_ERROR_NE;
data_in[count]=HAL_UART_ERROR_NE;
}
/* UART frame error interrupt occurred -----------------------------------*/
if (((isr_flags & USART_SR_FE) != RESET) && ((cr3_its & USART_CR3_EIE) != RESET))
{
USART2->DR=HAL_UART_ERROR_FE;
data_in[count]=HAL_UART_ERROR_FE;
}
/* UART Over-Run interrupt occurred --------------------------------------*/
if (((isr_flags & USART_SR_ORE) != RESET) && (((cr1_its & USART_CR1_RXNEIE) != RESET) || ((cr3_its & USART_CR3_EIE) != RESET)))
{
USART2->DR=HAL_UART_ERROR_ORE;
data_in[count]=HAL_UART_ERROR_ORE;
}
for (unsigned char c=0; c<count;c++)
{
while( !( USART2->SR & (1<<7u) ) ) {}; //wait till transmit buffer is empty
temp_size++;
USART2->DR = data_in[c];
// ** 1.5 Character delay
delay_us(1200); //9600
}
//** 3.5 character delay
delay_us(4000); //9600
//** Clear Data in buffer
memset(data_in, 0, sizeof(data_in));
}