Problem with taskEXIT_CRITICAL()

pelibz wrote on Monday, October 22, 2012:

Hello,
I am trying to use the taskENTER_CRITICAL() and taskEXIT_CRITICAL() macros to perform some lines of critical and very time-dependant code inside one task (I have three running in total). It looks working fine but when I try to exit the critical section with taskEXIT_CRITICAL() it always crashes afterwards (HardFault_Handler error).
Please, any idea why happens and how to solve it?? Or what can I do?

Thanks a lot in advance and kind regards,
Peli

richard_damon wrote on Monday, October 22, 2012:

It would be helpful to know more about how you are using the calls (especially what processor!).

One thing that comes to mind is that these functions can only be used at the “task” level, they can NOT be used inside an ISR, doing so might cause the HardFault.

rtel wrote on Monday, October 22, 2012:

…if it is a Cortex-M processor then the following link may help: http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

Regards.

pelibz wrote on Tuesday, October 23, 2012:

Hello,
Thanks for the answers. I see I didn’t give much information, sorry.
I am using a Cortex-M3 from NXP (LPC1758). I have a function that is called from a task, and inside the function I call taskENTER and taskEXIT_CRITICAL macros, I don’t know if I can do that. Like this:

TASK:
void genTcpIpTask(void *pvParameters)
{
for( ;; )
{
IncomingGenericFramesCheck();
}
}

FUNCTION:
void IncomingGenericFramesCheck(void)
{
   …
   …
   taskENTER_CRITICAL();
   if (OW_WriteButton() != 0)
      trama = 6; // Error de lectura/escritura.
   taskEXIT_CRITICAL();
   …
   …
}

After taskEXIT_CRITICAL is called the Hard_Fault happens on PortPendSVHandler() (apparently).

Thanks for your help!
Peli

rtel wrote on Tuesday, October 23, 2012:

There is nothing wrong with your code, from what you have posted, assuming OW_WriteButton() is not itself using FreeRTOS API functions (which should not be called from a critical section).  I suspect the problem will lies elsewhere.  Have you taken note of the points here:

http://www.freertos.org/FAQHelp.html

Regards.

pelibz wrote on Tuesday, October 23, 2012:

Hello Richard,
I checked the FAQ and I don’t see anything related. Actually my application runs fine, but it crashes in the very few cases I call taskENTER_CRITICAL() / taskEXIT_CRITICAL(). Normally it has three tasks running fine, performing some communications via Ethernet and UART1, checking some ports, switching leds, etc.
As said, in really few times I have to call to “OW_WriteButton()” function, that writes into a iButton from Dallas connected via One-Wire interface. This operation is very time dependant, that’s why I want to make it in a critical section.
Inside “OW_WriteButton” I don’t use any FreeRTOS function, I just start the Timer1, write iButton and disable Timer1. This function takes some time, maybe too much and it is affecting somehow.

When back from OW_WriteButton I call to taskEXIT_CRITICAL() and after a short time I get the Hard_Fault. On that point Debugger gives this info (I don’t know if it helps):
6 HasdFault_Handler() cr_startup_lpc17.c: 298 0x00000164
5 <signal handler called> () 0xfffffff1
4 xPortPendSVHandler() port.c: 237 0x0000a1a2
3 <signal handler called> () 0xfffffffd
2 <symbol is not available> 0x10007ef0
1 <symbol is not available> 0x100011cc

Thanks and kind regards,
Peli

pelibz wrote on Thursday, October 25, 2012:

Hello,
Richard is right, my problem happens due to other reason although taskEXIT_CRITICAL makes it show later… If I remove taskENTER / taskEXIT the problem happens anyway.
I reduced my function to this:

uint8_t OW_WriteButton()
{
uint8_t result;

TicksOW=0;
while(TicksOW<=G){} // G useg wait
        outp(0x00); // Drives DQ low

TicksOW=0;
while(TicksOW<=H){} //  H useg wait
        outp(0x01);

TicksOW=0;
while(TicksOW<=I){} //  I useg wait
        result = inp() & 0x01;

TicksOW=0;
while(TicksOW<=J){} //J useg wait

        return result; // Return sample presence pulse result
}

Where TicksOW is defined in a timer.c class and counts 1usg ticks. As you can see there are some waits done with while loops. “inp()” and “outp()” just read/write on one pin.
I cannot see the exact point causing the Hard_Fault, but it is inside this function.

Any idea please???

Thanks,
Peli

davedoors wrote on Thursday, October 25, 2012:

There is nothing in that file that looks suspicious at all. Have you tried it with the inp() and outp() commented out?

jdurand wrote on Thursday, October 25, 2012:

I presume you’re not having a stack overflow causing this?  I didn’t see that mentioned so I thought I’d mention it.

pelibz wrote on Thursday, October 25, 2012:

It was indeed an stack overflow…… I increased the task stack size and it is apparently working fine now.

Thanks a lot for your help!!

edwards3 wrote on Thursday, October 25, 2012:

I recommend always having stack overflow detection turned on during development
http://www.freertos.org/Stacks-and-stack-overflow-checking.html