# xSemaphoreTake gets blocked

**URL:** <https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251>\
**Category:** Kernel\
**Created:** [July 20, 2009, 11:31am UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251 "2009-07-20T11:31:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 11:31am UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/1 "2009-07-20T11:31:56Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc):

I’ve created a simple semaphore mutex into one of the tasks in order to avoid silmultaneous access to a global variable. The code is identical to the one explained in the online tutorial:

xSemaphoreHandle xSemaphore = NULL;

void myTask(void \*pvParameters)  
{  
&nbsp;&nbsp; xSemaphore = xSemaphoreCreateMutex();  
&nbsp;&nbsp; …  
}

void myfunction(void)  
{  
…  
&nbsp;&nbsp; if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )  
&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Access to the global resource  
&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreGive(epSemaphore);  
&nbsp;&nbsp; }  
&nbsp;&nbsp; …  
}  
…

I’ve verified that the semaphore is correctly created. During the first accesses, it’s even “taken” and “released” without problems. However, after some successful accesses, myfunction gets blocked at xSemaphoreTake. Doing xBlockTime = 0 solves the problem but I’d like myfunction to wait at least some ticks before abandoning the semaphore. I’m thinking that maybe FreeRTOS doesn’t know what a tick is in my system so that it waits indefinitely. I’ve maybe missed some definition elsewhere…

I understand that my descriptions are quite vague but… has anyone an idea about what could be happening with my setup?

Thank you very much,

Daniel.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 11:43am UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/2 "2009-07-20T11:43:52Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/a59d):

First, put a break point in vTaskIncrementTick() in tasks.c. If the break point is triggered repeatedly then your tick interrupt is working ok.

Also do the usual things like checking for stack overflows, things like that.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 2:17pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/3 "2009-07-20T14:17:02Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/2eb4):

Thanks Dave for your help.

vTaskIncrementTick() is called several times at the beginning of the execution. Then, it’s not called again, matching with the blockage at xSemaphoreTake in myfunction() . I’ve also verified that there is no stack overflow through the use of the vApplicationStackOverflowHook function.

Something curious IMO, If I add an instruction in vTaskIncrementTick() that prints a text message on my Dev Board’s OLED display, then vTaskIncrementTick gets called without problems all the time and my program works smoothly.&nbsp; This is not a valid solution though but I wanted to explain this behavior, just in case this is a symptom about what’s happening.

My hardware platform is a Luminary LM3S8962 Eval board.

Thanks again!

Daniel.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 4:47pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/4 "2009-07-20T16:47:43Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/c7d8):

I’ve found the source of the problem. I was using a timer interrupt in order to run ADC conversions on my Stellaris platform. The trigger of this interrupt was hanging the task I think. I now know that custom interrupts have to be avoided under FreeRTOS 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 5:03pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/5 "2009-07-20T17:03:51Z")

</div>

**[spacewrench](https://sourceforge.net/u/spacewrench/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/02d7):

I’ve written several FreeRTOS programs that run on Luminary chips.&nbsp; Interrupts work correctly, but you have to be careful about priority.&nbsp; Specifically, in the NVIC, lower-numbered priorities are higher.&nbsp; Also, there’s an intermediate priority level (configMAX\_SYSCALL\_INTERRUPT\_PRIORITY) above which you can’t call FreeRTOS routines.&nbsp; There’s a lot more information on the FreeRTOS website, but it’s slightly difficult to find.&nbsp; (It would be nice if there was a FAQ about it, or a more-prominent link in a section talking about Cortex procs.)&nbsp; Here’s where to start reading:

> **[FreeRTOS - Market leading RTOS (Real Time Operating System) for embedded...](https://www.freertos.org/index.html?http:%2F%2Fwww.freertos.org%2Fportcortexgcc.html)**
>
> Portable operating system interfaces (POSIX) for FreeRTOS

Near the bottom of the main frame, under the heading "RTOS Port specific configuration"

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [July 20, 2009, 5:09pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/6 "2009-07-20T17:09:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/c7d8/d8a4):

I’m not sure what you mean by “custom interrupts have to be avoided”.&nbsp; Its fine to have any interrupts you want provided there are no resource conflicts.

Was your interrupt using the FreeRTOS API?&nbsp; If so then ensure only API functions that end in FromISR were being used and that the interrupt priority was less than or equal to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY (remembering that low number mean high priorities on the Cortex M3).

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 5:49pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/7 "2009-07-20T17:49:33Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/13bc):

Thanks both for your remarks. I’m quite new in FreeRTOS and have still a lot to learn… and I’m really happy to know that I can use interrupts together with RfeeRTOS.

My custom interrupt (the one currently triggering the ADC reads) has now the lowest priority (255) but it still makes the system crash. I’m now thinking that maybe the problem is in the timer used to trigger these ADC conversions periodically. I did verify that this timer was not used anywhere by the Os but maybe I should do a second check (I’m not the author of the FreeRTOS port)

More news tomorrow…

Daniel.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 6:22pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/8 "2009-07-20T18:22:12Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/6319):

My application no longer uses a timer to trigger ADC conversions… and the problem persists.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 6:24pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/9 "2009-07-20T18:24:07Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/6322):

I forgot to say that the interrupt is not calling any function from the FreeRTOS API. It just reads the ADC values.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 20, 2009, 6:34pm UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/10 "2009-07-20T18:34:38Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Monday, July 20, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/6322/d42b):

Can you post the interrupt code?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 21, 2009, 8:25am UTC](https://forums.freertos.org/t/xsemaphoretake-gets-blocked/3251/11 "2009-07-21T08:25:34Z")

</div>

**[dberenguer](https://sourceforge.net/u/dberenguer/)** wrote on [Tuesday, July 21, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/90f8d98d/#66cc/c5c5):

Of course!

TaskIO.c runs in a separate task, all the routines regarding the IO management. setup\_analog\_inps() is the function that sets up the ADC sequencer (sequencer 0) and programs Timer1 to trigger the sequencer every 0.1 sec:

static void setup\_analog\_inps(void)  
{  
&nbsp;&nbsp;&nbsp; SysCtlPeripheralEnable(SYSCTL\_PERIPH\_ADC);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Enable ADC  
&nbsp;&nbsp;&nbsp; SysCtlPeripheralEnable(SYSCTL\_PERIPH\_TIMER1);&nbsp;&nbsp;&nbsp; // Enable Timer 1

&nbsp;&nbsp;&nbsp; ADCSequenceDisable(ADC\_BASE, 1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Disable ADC sequencer 1

&nbsp;&nbsp;&nbsp; // Configure the ADC to sample when the timer expires  
&nbsp;&nbsp;&nbsp; // After sampling, the ADC will interrupt the processor  
&nbsp;&nbsp;&nbsp; ADCSequenceConfigure(ADC\_BASE, 0, ADC\_TRIGGER\_TIMER, 255);  
&nbsp;&nbsp;&nbsp; ADCSequenceStepConfigure(ADC\_BASE, 0, 0,ADC\_CTL\_CH0 );  
&nbsp;&nbsp;&nbsp; ADCSequenceStepConfigure(ADC\_BASE, 0, 1,ADC\_CTL\_CH1 );  
&nbsp;&nbsp;&nbsp; ADCSequenceStepConfigure(ADC\_BASE, 0, 2,ADC\_CTL\_CH2 );  
&nbsp;&nbsp;&nbsp; ADCSequenceStepConfigure(ADC\_BASE, 0, 3,ADC\_CTL\_CH3 | ADC\_CTL\_IE | ADC\_CTL\_END);

&nbsp;&nbsp;&nbsp; ADCSequenceEnable(ADC\_BASE, 0);

&nbsp;&nbsp;&nbsp; ADCIntEnable(ADC\_BASE, 0);  
&nbsp;&nbsp;&nbsp; IntEnable(INT\_ADC0);

&nbsp;&nbsp;&nbsp; // Configure Timer 1 to generate triggers to the ADC  
&nbsp;&nbsp;&nbsp; TimerConfigure(TIMER1\_BASE, TIMER\_CFG\_32\_BIT\_PER);  
&nbsp;&nbsp;&nbsp; TimerLoadSet(TIMER1\_BASE, TIMER\_A, SysCtlClockGet()/10);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Trigger every 0.1 secs  
&nbsp;&nbsp;&nbsp; TimerControlStall(TIMER1\_BASE, TIMER\_A, true);  
&nbsp;&nbsp;&nbsp; TimerControlTrigger(TIMER1\_BASE, TIMER\_A, true);  
&nbsp;&nbsp;&nbsp; TimerEnable(TIMER1\_BASE, TIMER\_A);  
}

ADCIntHandler is a callback function called whenever the ADC Sequencer 0 reads new analog data (once every 0.1 sec)

void ADCIntHandler(void)  
{  
&nbsp;&nbsp;&nbsp; unsigned long ulData[8];  
&nbsp;&nbsp;&nbsp; unsigned long ulmVolt;  
&nbsp;&nbsp;&nbsp; unsigned long ulValue;

&nbsp;&nbsp;&nbsp; BYTE bState;  
&nbsp;&nbsp;&nbsp; int i;

&nbsp;&nbsp;&nbsp; // Clear the ADC interrupt.  
&nbsp;&nbsp;&nbsp; ADCIntClear(ADC\_BASE, 0);

&nbsp;&nbsp;&nbsp; // Read the data from the ADC.  
&nbsp;&nbsp;&nbsp; ADCSequenceDataGet(ADC\_BASE, 0, ulData);

&nbsp;&nbsp;&nbsp; // For every analog input  
&nbsp;&nbsp;&nbsp; for(i=0 ; i\<NBOF\_INPANA ; i++)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Get ADC value in voltage (mV) format  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ulmVolt = (ADC\_VOLT\_RANGE \* 1000 \* ulData[i]) / 1024;&nbsp;&nbsp;&nbsp; // Milivolts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Apply factor and offset for each ADC endpoint  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ulValue = ulmVolt \* aEndpoint[0].factor + aEndpoint[0].offset;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Set state  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ulValue \< 10)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bState = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bState = 1;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; epUpdateValue(i, ‘a’, ulValue, bState);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Update value in the array of binary endpoints  
&nbsp;&nbsp;&nbsp; }  
}

Apart from here, there is no other place where a timer or interrupt is set.

Thanks again,

Daniel.
