AnsweredAssumed Answered

STM32F4 FreeRTOS task and SPI interrupt problem

Question asked by deng.nan on Apr 16, 2013
Latest reply on Apr 16, 2013 by deng.nan
Hi,I am working on a project with STM32F4 and FreeRTOS. I have a SPI ISR for receiving data from SPI in the speed of about 5Mbps, I also have a task which handles those data. The Queue is used to be between the interrupt and task.xQueue=xQueueCreate(10,sizeof(uint8_t)*BufferSize);The problem is that the task stops working after some time of normal running. But the SPI receive seems to be still continuing because I can still see the waveforms in the oscillopscope. The implementation of SPI receive ISR is as follows:void SPI1_IRQHandler(void){static portBASE_TYPE xHigherPriorityTaskWoken;portBASE_TYPE xStatus;xHigherPriorityTaskWoken = pdFALSE;while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);if(index_spi<512){lValueSend[index_spi]=SPI_I2S_ReceiveData(SPI1);index_spi++;SPI_I2S_SendData(SPI1,0x22);}else{index_spi=0;xStatus=xQueueSendToBackFromISR(xQueue,&lValueSend,&xHigherPriorityTaskWoken);portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);}}And inside the task infinite loop:xStatus=xQueueReceive(xQueue,&lValueReceive,xTicksToWait);if(xStatus==pdPASS){dataProcess(lValueReceive);}After some debugging, I found out that the queue becomes full very easily, and the context switch doesn't happen. So it seems that my task just never gets a chance of running again. But if I understand correctly, after the ISR, either we switch the context to the task waiting for queue  if this task waiting for the queue has a higher priority than the current one which was interrupted, either we return to the task which was interrupted. My Datahandling task is already set to configMAX_PRIORITIES, so even if we return to the privious task, my task should have been given control to run.  Someone can help to analyse and understand?And also the interrupt priority thing makes me confused. My SPI IRQ settings is:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; In the FreeRTOS document, there are two which are absolutely not my settings at all. One is,"If you are using an STM32 with the STM32 driver library then ensure all the priority bits are assigned to be preempt priority bits by calling NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); before the RTOS is started."Another one is that the interrupt with priority between configMAX_SYSCALL_INTERRUPT_PRIRITY and configMAX_SYSCALL_INTERRUPT_PRIORITY can use the freeRTOS API functions. My settings are#define configKERNEL_INTERRUPT_PRIORITY       255#define configMAX_SYSCALL_INTERRUPT_PRIORITY      191 /* equivalent to 0xb0, or priority 11. */With the mapping of STM32, the priority between 11 and 15 can use FreeRTOS API. But my settings are completely out of these two rules, but after test, this is the best config. If I put the NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ) and set the PreemptionPriority to 15. The task stops even more quickly. Someone may also help to understand with this?