freertos hanging on vListInsert on STM32

woshiwowo wrote on Tuesday, June 08, 2010:

Hello

I am still new to FreeRTOS, and my program is hanging always in the same line of code after random times (minutes or seconds).
The code always hangs on this line:
for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
in vListInsert , filename: list.c

pxIterator->pxNext points to itself, so it hangs there.
And I am using Cortex-M3 .

Here is some of my application.
In FreeRTOSConfig.h
#define configMAX_PRIORITIES                         ( ( unsigned portBASE_TYPE ) 6 )
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY         191 /* equivalent to 0xb0, or priority 11. */
in main.c I set the webtask the highest priopity as 5(the highest in freertos)
xTaskCreate( vWEBTask, ( signed portCHAR * )“WEB”,    1024,     NULL, 5,NULL );
The interrupt priopity is 0(the highest priopity in Cortex-M3)
And i think it is not the stack problem.Because it happened after i add a semaphore to deal whit a task and an interruption.
The web task and interrup is below:
void vWEBTask(void *pvParameters )
{
  vSemaphoreCreateBinary(xWEBSemphore);
  vSemaphoreCreateBinary(xTUNERSemphore);
  vSemaphoreCreateBinary(xQAMSemphore);
  while(1)
  {
    unsigned short len=0;
    const portTickType xBlockTime = ( portTickType )0x500;
    portENTER_CRITICAL();
    status=getSn_SR(0);
    portEXIT_CRITICAL();
    if(status==S_SSR_CLOSE_WAIT)//client close first
    {
         portENTER_CRITICAL();
          status=getSn_SR(0);
        portEXIT_CRITICAL();
        if(status1==status)
        {
          close(0);
          Socket_Listen(0);
        }
      }
      else if(status==S_SSR_CLOSED)
      {
        Socket_Listen(0);
      }
    // vTaskDelay(5);
    if(xSemaphoreTake(xWEBSemphore, portMAX_DELAY)==pdTRUE)
    {
     
      /* 处理W5100中断 */
          portENTER_CRITICAL();
      if(W5100_Interrupt)
        W5100_Interrupt_Process();
         portEXIT_CRITICAL();
      //    portENTER_CRITICAL();
      //    len=getSn_RX_RSR(0);
      //    portEXIT_CRITICAL();
      /* 如果Socket0接收到数据 */
      //if((S_Data & S_RECEIVE) == S_RECEIVE)
      if(recv_flag==1 || (len>0 && status==0x17))
      {
        S_Data&=~S_RECEIVE;
        Rx_start=TIM2_Get();
        portENTER_CRITICAL();
        len=getSn_RX_RSR(0);
        portEXIT_CRITICAL();
        if(Rx_Buffer==‘f’)
        {
          while(len<page_length)
          {
            portENTER_CRITICAL();
            len=getSn_RX_RSR(0);
            portEXIT_CRITICAL();
            page_length=0;
          }
        }
        file_end=TIM2_Get();
        if (len > 0)
        {
          recv(0,Rx_Buffer,len);
          Rx_end=TIM2_Get();
          if(Rx_Buffer==‘f’)
          {
            if(file_updata_change==1)
            {
              Tx_start=TIM2_Get();
              portENTER_CRITICAL();
              Rx_Data_Process(0,Rx_Buffer,len);
              portEXIT_CRITICAL();
            }
          }
          else if(Rx_Buffer==‘P’ || Rx_Buffer==‘G’)
          {
            Tx_start=TIM2_Get();
            portENTER_CRITICAL();
            Rx_Data_Process(0,Rx_Buffer,len);
            portEXIT_CRITICAL();
          }
          Tx_end=TIM2_Get();
          //   Process_Socket_Data(0);
        }
        recv_flag=0;
        FIN_Flag=0;
      }
      if(sendok_flag==1)
        //if((S_Data & S_TRANSMITOK) == S_TRANSMITOK)
      {
        sendok_flag=0;
        S_Data&=~S_TRANSMITOK;
        Socket_Disconnect(0);
        Close_start=TIM2_Get();
        close(0);
        Socket_Listen(0);
        if(Rx_Buffer==‘t’)
       {
          xSemaphoreGive(xTUNERSemphore);
          FIN_Flag=1;
        }
        else if(Rx_Buffer==‘q’) 
       {
           xSemaphoreGive(xQAMSemphore);
          FIN_Flag=1;
       }
        Close_end=TIM2_Get();
      
      }
    }
}
}
void EXTI4_IRQHandler(void)//ISR
{
  if(EXTI_GetITStatus(EXTI_Line4) != RESET)
  {  
    portENTER_CRITICAL();
    EXTI_ClearFlag(EXTI_Line4);
    W5100_Interrupt = 1;
    portEXIT_CRITICAL();
     xSemaphoreGiveFromISR(xWEBSemphore, &xHigherPriorityTaskWoken );
    /* We may want to switch to the vTc_interupt task, if this message has made it the highest priority task that is ready to execute. */
     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken  );
  }
}

As this code is “inside” freeRTOS, what could have causing this? Where should I look for?
Thank you very much, and sorry if it is a stupid question.

woshiwowo wrote on Tuesday, June 08, 2010:

Hello I am still new to FreeRTOS, and my program is hanging always in the same line of code after random times (minutes or seconds). The code always hangs on this line:
for( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
in vListInsert , filename: list.c
pxIterator->pxNext points to itself, so it hangs there. And I am using Cortex-M3 .
Here is some of my application. In FreeRTOSConfig.h
#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 6 )
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191 /* equivalent to 0xb0, or priority 11. */
in main.c I set the webtask the highest priopity as 5(the highest in freertos)
xTaskCreate( vWEBTask, ( signed portCHAR * )“WEB”, 1024, NULL, 5,NULL );
The interrupt priopity is 0(the highest priopity in Cortex-M3) And i think it is not the stack problem.Because it happened after i add a semaphore to deal whit a task and an interruption.
The web task and interrup is below:
void vWEBTask(void *pvParameters )
{
vSemaphoreCreateBinary(xWEBSemphore);
vSemaphoreCreateBinary(xTUNERSemphore);
vSemaphoreCreateBinary(xQAMSemphore);
while(1) {
unsigned short len=0;
const portTickType xBlockTime = ( portTickType )0x500;
portENTER_CRITICAL();
status=getSn_SR(0);
portEXIT_CRITICAL();
if(status==S_SSR_CLOSE_WAIT)//client close first
{
portENTER_CRITICAL();
status=getSn_SR(0);
portEXIT_CRITICAL();
if(status1==status)
{
close(0);
Socket_Listen(0);
}
}
else if(status==S_SSR_CLOSED)
{ Socket_Listen(0); } // vTaskDelay(5);**
if(xSemaphoreTake(xWEBSemphore, portMAX_DELAY)==pdTRUE)
{
portENTER_CRITICAL();
if(W5100_Interrupt)
W5100_Interrupt_Process();
portEXIT_CRITICAL(); // portENTER_CRITICAL(); // len=getSn_RX_RSR(0); // portEXIT_CRITICAL
if(recv_flag==1 || (len>0 && status==0x17))
{ S_Data&=~S_RECEIVE;
Rx_start=TIM2_Get();
portENTER_CRITICAL();
len=getSn_RX_RSR(0);
portEXIT_CRITICAL();
if(Rx_Buffer==‘f’)
{ while(len<page_length)
{ portENTER_CRITICAL();
len=getSn_RX_RSR(0);
portEXIT_CRITICAL();
page_length=0; } }
file_end=TIM2_Get();
if (len > 0) {
recv(0,Rx_Buffer,len);
Rx_end=TIM2_Get();
if(Rx_Buffer==‘f’)
{ if(file_updata_change==1)
{ Tx_start=TIM2_Get();
portENTER_CRITICAL();
Rx_Data_Process(0,Rx_Buffer,len);
portEXIT_CRITICAL(); } }
else if(Rx_Buffer==‘P’ || Rx_Buffer==‘G’)
{ Tx_start=TIM2_Get();
portENTER_CRITICAL();
Rx_Data_Process(0,Rx_Buffer,len);
portEXIT_CRITICAL(); }
Tx_end=TIM2_Get(); // Process_Socket_Data(0);
}
recv_flag=0; FIN_Flag=0;
}
if(sendok_flag==1)
{ sendok_flag=0; S_Data&=~S_TRANSMITOK;
Socket_Disconnect(0); Close_start=TIM2_Get();
close(0); Socket_Listen(0);
if(Rx_Buffer==‘t’)
{

xSemaphoreGive(xTUNERSemphore)**; FIN_Flag=1;
}
else if(Rx_Buffer==‘q’)
{
xSemaphoreGive(xQAMSemphore);
FIN_Flag=1;
}
Close_end=TIM2_Get();
} } } }
void EXTI4_IRQHandler(void)//ISR
{ if(EXTI_GetITStatus(EXTI_Line4) != RESET)
{ portENTER_CRITICAL();
EXTI_ClearFlag(EXTI_Line4);
W5100_Interrupt = 1;
portEXIT_CRITICAL(); **
xSemaphoreGiveFromISR(xWEBSemphore, &xHigherPriorityTaskWoken **);
/* We may want to switch to the vTc_interupt task, if this message has made it the highest priority task that is ready to execute. */ portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); }
}

As this code is “inside” freeRTOS, what could have causing this? Where should I look for? Thank you very much, and sorry if it is a stupid question.

rtel wrote on Tuesday, June 08, 2010:

I have not looked at your code, but immediately above the line you say you are getting stuck on you will find the comment:

/* *** NOTE ***********************************************************
If you find your application is crashing here then likely causes are:
1) Stack overflow -
   see http://www.freertos.org/Stacks-and-stack-overflow-checking.html
2) Incorrect interrupt priority assignment, especially on Cortex M3
   parts where numerically high priority values denote low actual
   interrupt priories, which can seem counter intuitive.  See
   configMAX_SYSCALL_INTERRUPT_PRIORITY on http://www.freertos.org/a00110.html
3) Calling an API function from within a critical section or when
   the scheduler is suspended.
4) Using a queue or semaphore before it has been initialised or
   before the scheduler has been started (are interrupts firing
   before vTaskStartScheduler() has been called?).
See http://www.freertos.org/FAQHelp.html for more tips.
**********************************************************************/

Have you taken this into account?  Number 2 is the most likely cause of your problem. 

If you are using interrupts

+ have you set the interrupt priority to be at or below configMAX_SYSCALL_INTERRUPT_PRIORITY?  You can **NOT **leave the priority at its default value.

+ Have you defined the interrupt priority using the method expected by whichever function you are using.  Some functions want the priority shifted up into the most significant bits, some want it left in the least significant bits.  If you get that wrong then you will most likely hang on the line you highlighted.

Regards.

woshiwowo wrote on Tuesday, June 08, 2010:

Hello ,thank you for you advise but i donot understand  priority clearly.I use a semphore to synchronize an interrupt and a task.
i set tha task the highest  priority in freertos as 5,and i set the interrupt the highest priority in STM32 as 0. So is there any problem._
In FreeRTOSConfig_.h
#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 6 )
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191 /* equivalent to 0xb0, or priority 11. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY 15
_

in mai_n.c
I set the webtask the highest priopity as 5(the highest in freertos)
xTaskCreate( vWEBTask, ( signed portCHAR * )“WEB”, 1024, NULL, 5,NULL );
The interrupt priopity is 0(the highest priopity in Cortex-M3)

So is priopity right set in my application ,Thank you very much, and sorry if it is a stupid question.

rtel wrote on Tuesday, June 08, 2010:

Yes there absolutely is a problem with your interrupt priority - it must be at or below configMAX_SYSCALL_INTERRUPT_PRIORITY - so at or below (ie numerically higher) than 191 (or 0xb0 if shifted to the most significant bits, or 11 if not shifted).

Regards.

woshiwowo wrote on Wednesday, June 09, 2010:

Thank you for your advice .Do you mean that if i use API in ISR i must set interrup priority numerically higher than 191 that means 192 for example?But we all know in STM32 interrup priority is 0~15 .So i should set configMAX_SYSCALL_INTERRUPT_PRIORITY  0~15 for example 7 and set my interrup priority  numerically higher than 7 for example 9.Am i right ???Thank you very much, and sorry if it is a stupid question.

rtel wrote on Wednesday, June 09, 2010:

sorry if it is a stupid question

No, it is not a stupid question.  Nearly all support requests on CM3 devices is because of this.  It is confusing, and to add to the confusion, there is inconsistency in API semantics.

But we all know in STM32 interrup priority is 0~15

Actually, only the top four bits of the interrupt priority bytes are implemented (on the STM32, in any case, that is not true for all CM3 implementations).  Therefore the priorities are 0 (highest) and 1<<4 to 15<<4.  Or in binary, 0 and 00010000 to 11110000.  The inconsistency comes from the fact that some API functions (CMSIS for example) want the priority stated without the shift, while others require the shift.  If you use a shifted priority with a function that does not want the value shifted your actual interrupt priority will end up as being zero, and visa versa.

Regards.

woshiwowo wrote on Friday, June 11, 2010:

I guess i know how to do .this time i set preemptionPriority=3,SubPriority=0 and as a result  interrupt priority is 11000000B(numerically higher than 191).Is that right?
One another question In FreeRTOSConfig.h
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191
Can i change 191  to a higher number or lower number?Thanks