Use of ISR - STM32 - Freeze

alexgamo wrote on Monday, June 08, 2015:

Dear all,

I’m trying to use FreeRTOS 6.0.4 on an STM32F3 Discovery Board,
but I encounter some problems.

I want to give a mutex by ISR when I receive a CAN bus message, to wake up another task.
I have a second task which permit a LED to blink.

My problem is before the CAN trame is received, the program runs normally, and I see the LED blinks.
But when a CAN trame arrives, the ISR wakes up and the program blocks in it.
Tasks are stop running, and the LED stops to blink.

Sending and receiving CAN trames are working without use of ISR, only problem is when I try to use ISR.
I thought the problem is because of I set wrong priorities but it seems OK for me…

Here’s a part of code in FreeRTOSConfig.h :

:::C
#define configUSE_PREEMPTION		1
#define configUSE_IDLE_HOOK		0
#define configUSE_TICK_HOOK		0
#define configCPU_CLOCK_HZ		( ( unsigned long ) 72000000 )
#define configTICK_RATE_HZ	        ( ( portTickType ) 1000 )
#define configMAX_PRIORITIES		( ( unsigned portBASE_TYPE ) 10 )
#define configMINIMAL_STACK_SIZE	( ( unsigned short ) 128 )
#define configMAXIMAL_STACK_SIZE	( ( unsigned short ) 8192)
#define configTOTAL_HEAP_SIZE		( 30000 )
#define configMAX_TASK_NAME_LEN		( 16 )
#define configUSE_TRACE_FACILITY	0
#define configUSE_16_BIT_TICKS		0
#define configIDLE_SHOULD_YIELD		1
...
#define configKERNEL_INTERRUPT_PRIORITY         255    // 15
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 	191    // 11

The function below is used to configure ISR parameters :

:::C
void CAN_init_IT(void){
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 13;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}

The ISR and the associated task, and the main function, are here :

:::C
xSemaphoreHandle ACK_message_CAN;
xSemaphoreHandle message_CAN;


void USB_LP_CAN1_RX0_IRQHandler(void) {
	long yield = 0;

	xSemaphoreGiveFromISR(message_CAN, &yield);

        // Only use to debug
	/*GPIO_SetBits(GPIOE, GPIO_Pin_11 | GPIO_Pin_10 | GPIO_Pin_9 | GPIO_Pin_8);
	vTaskDelay(250);
	GPIO_ResetBits(GPIOE, GPIO_Pin_11 | GPIO_Pin_10 | GPIO_Pin_9 | GPIO_Pin_8);
	vTaskDelay(250);*/

	portEND_SWITCHING_ISR(yield);
}


void RX_CAN(void *p)
{
	CAN_init();
	CAN_init_IT();

	CanRxMsg RxMessage;
	RxMessage.Data[0] = 0;

	while(1)
	{
		if(xSemaphoreTake(message_CAN, (portTickType)0 ))
		{
			CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
			if(RxMessage.Data[0] == 0x05)
			{
				...
			}
		}
	}
}


int main()
{
	SystemInit();
	SystemCoreClockUpdate();
	// SysTick end of count event each 1ms
	RCC_ClocksTypeDef RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);
	SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);

        ...

	vSemaphoreCreateBinary(message_CAN);		// Create the semaphore
	xSemaphoreTake(message_CAN, 0);        		// Take semaphore after creating it.

        xTaskCreate(RX_CAN,(signed char *) "receive_CAN_task", 20*configMINIMAL_STACK_SIZE, NULL, 1, NULL);
        xTaskCreate(Blink, (signed char *) "blink", 20*configMINIMAL_STACK_SIZE, NULL, 1, NULL);


        vTaskStartScheduler();

        while(1); // the program should never comes here
        return -1;
}

Maybe I forgot something ?
I searched in this forum before to post but I didn’t find anything.

Sorry for this long post and my bad English, I’m a french student.
Thanks for your help.

rtel wrote on Monday, June 08, 2015:

Yikes that is an old version. Normally the advice would be to define configASSERT() to have the assert check the interrupt configuration for you - but I don’t think that will help in V6.x code. That said, I cannot see anything wrong with what you have posted (thanks for posting useful information!) provided the STM32 in use has 4 priority bits defined.

Can you try changing the semaphore from a mutex to a binary semaphore. You only need change the function used to create the semaphore, nothing else. The reason being that various different FreeRTOS versions have had different rules on giving mutexes from interrupt - the reason being that mutexes use priority inheritance, which has little meaning when they are used from an interrupt. Let me know if that helps.

Regards.

alexgamo wrote on Wednesday, June 10, 2015:

Hello,

I was already using binary semaphore in my code, I was wrong when I wrote “I want to give a mutex by ISR”, sorry.
I tried to comment the “void USB_LP_CAN1_RX0_IRQHandler(void)” handler, and I saw the same thing : the program froze when a CAN trame is received.

So I upgraded my version of FreeRTOS to v8.2.1 : same problem.

I defined configASSERT like this :

:::C
#define configASSERT( x ) if( ( x ) == pdFALSE ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

When I debug, the program stops on this line, in the function xQueueGenericReceive, in the file queue.c :

:::C
configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );

Values are :
- pvBuffer : 0
- uxItemSize : 8

I don’t understand what happen and what I’m doing wrong.

rtel wrote on Wednesday, June 10, 2015:

Inside that function it is only valid for the buffer to be NULL, if zero bytes are going to be copied into the buffer (i.e. the buffer is not going to be accessed). If pxQueue->uxItemSize is zero, then no data is going to be copied to the buffer. pxQueue->uxItemSize will be zero, and pvBuffer will be NULL, if the queue is actually being used as a semaphore.

In your case it is correctly detecting that, if the assert didn’t catch it, the code would attempt to write 8 bytes to a NULL pointer.

When the code stops on that line, look at the function call stack in the debugger. That should tell you the route through the code taken to get to that point, which will help you debug how it came to be.

Did you create the semaphore before using it?

Regards.

alexgamo wrote on Thursday, June 11, 2015:

I solved my problem.

I changed the handler by this :

:::C
void USB_LP_CAN1_RX0_IRQHandler(void) {
	long yield = pdFALSE;

	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

	if((RxMessage.StdId == 0x000) && (RxMessage.IDE == CAN_ID_STD)
	     && (RxMessage.DLC == 1) && (RxMessage.Data[0] == 0x05))
	  {
		xSemaphoreGiveFromISR(message_CAN, &yield);
	  }

	portEND_SWITCHING_ISR(yield);
}

I realized that, when CAN ITs are enabled (CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);), you need to clean FIFO with CAN_receive() in the IRQ (maybe I am wrong ? but this is what I’m observing).
Else program stops working at the first received message.

This problem was a STM32 functionning problem, not a freeRTOS problem.

The program is now working fine. Thanks !