2017-03-30 08:44 AM
Hi to all,
I have a problem about interrupt, expecially USART interrupt.I use USART1 to interface my boarf to computer (FT232), but often my routine crashes.Here is the code:uint16_t iocnt = 0;extern 'C' void USART1_IRQHandler() // Default print{ uint8_t c; c = USART1->DR; if(iocnt < MAXBUF - 10) { if(c != '\n') { iobuf[iocnt++] = c; iobuf[iocnt] = 0; } else { USART1->CR1 &= ~USART_CR1_RXNEIE; //Disable RXNE interrupt iobuf[iocnt] = 0; iocnt = 0; DecodeCom(iobuf); USART1->CR1 |= USART_CR1_RXNEIE; //Enable RXNE interrupt myusart1.PrintString((uint8_t *) '\n\tInterrupt finished\n ', 0); } } return;}Every input character is correctly catched
Function DecodeCom() is correctly executedCheck string ('interrupt finished') correctly sent to pcThen sometime MCU stops (that is no further program line is executed), sometime program continue its run.
It seems the problem could be in return from routine but I do not know where I can checkAny hint?
Thank you
Freya2017-03-30 08:58 AM
Try to reuse a Cube UART example based on interrupt to get guideline.
The code is dangerous as it is for the following reasons:
1. If the IRQ is called, can it be called for something else than byte received?
2. IRQ won't interrupt itself, so no need to disable/enable the UART within
3. IRQ must last few microseconds... printf() should be done in the main loop.
for example
A: set a flag in the IRQ when a message is ready.
B: When starting the IRQ, read the UART DR and trash it if the flag is set.
C: In the main loop, poll for the flag and push the string to UART TX in polling mode. When done, clear the flag.
Intuitively, the buffer management may need to be more elaborated to avoid missing received data... such as bigger circular buffer can latch the start/stop index for a received string.
Some USART can automatically detect a /n character to pop an interrupt, combined with DMA transfer to reduce IRQ core cload.