I'm facing a problem while writing an UART application in STM32F205 using FreeRTOS.
I have two tasks one will send data out through UART and other will read data coming in through the same UART and process it.
I have assigned the Receiving task with priority (tskIDLE_PRIORITY+2) and writing task with the priority (tskIDLE_PRIORITY+1).
UART IRQ uses TXE interrupt to send data out and it receives a data for every occurance of the RXNE interrupt and places the data into a buffer, from which my receiving task will read.
I have used the NVIC Configurations as instructed in FreeRTOS manual
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
My Receive Task is blocked on a Binary semaphore which is actually released by IRQ handelr for every byte reception.
IRQ handler uses these calls to release the Semaphore
My application works as i expected.
I dont face any problem when I send minimal amount of data out. If i periodically send data lessthan 50 bytes for a long time no problem.
If i try to send 300 bytes continuously, i'm receiving some of the data what i'm sending out and HardFault ocuurs.
I suspect a corruption in NVIC, it handles TXE as RXNE and my IRQ reads in the same data that is sent out.
Call Stack indicates the hardFault occurance in " xPortPendSVHandler+29 ".
If I trace this leads to xPortPendSVHandler
ldr r0, [r1]/* The first item in pxCurrentTCB is the task top of stack. */
I can't make much out of this hint. Also I tried to debug in many ways, like using Queue instead of Semaphore as shown in FreeRTOS example. But HardFault can't be prevented.
If the fault is occurring on that line, then it looks like you have a RAM corruption somewhere that has corrupted the value loaded into R1 prior to that line of code.
Do you have stack overflow detection switched on?
Is your UART in an error state (overrun error, for example) that is not being handled?
How is your UART processing received bytes (placing them in a circular buffer, for example)?
Yes I have enabled stack overflow checking in my code. I have used the "vApplicationStackOverflowHook" to just print the name of the task having corrupted stack. But this vApplicationStackOverflowHook is never executed. Aiso i checked the size of stacks for all the tasks in FreeRTOS plugin for IAR. It is showing all the tasks having size >256.
My UART IRQ handler will receive a byte for every RXNE interrupt and copies it to a circular buffer. Then it will signal to a task using a semaphore.
For debugging purpose i tweaked this logic and implemented similar to a FreeRTOS demo program given for Cortex-M3. IRQ reads a byte for every RXNE interrupt and palces it in a queue. The corresponding task is blocked on the queue.
in this way i'm making sure data is read immediately after the occurrence of interrupt. But then also hardfault occurs.
Actually i should not receive the same data what i'm sending out. But i'm receiving some unwanted RXNE interrupts and reading the same data i'm sending out.
if i check UART overrun error flag after hardfault, it is set.