# Mutex in ISR

**URL:** <https://forums.freertos.org/t/mutex-in-isr/3210>\
**Category:** Kernel\
**Created:** [May 29, 2015, 9:48pm UTC](https://forums.freertos.org/t/mutex-in-isr/3210 "2015-05-29T21:48:35Z")\
**Posts on this page:** 5\
**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:** [May 29, 2015, 9:48pm UTC](https://forums.freertos.org/t/mutex-in-isr/3210/1 "2015-05-29T21:48:35Z")

</div>

**[roujesky](https://sourceforge.net/u/roujesky/)** wrote on [Friday, May 29, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/#a831):

I know this had to have been discussed before, but searching the forum fails me…

PIC32 and FreeRTOS application. I have a hardware interrupt (INT0) which increments a long variable everytime it fires. I have a task that runs every 100ms which reads and clears that variable.  
I thought I could use xSemaphoreTake and xSemaphoreGive in the ISR, but that crashes the program. Then I looked at xSemaphoreGiveFromISR, but that doesnt work either.  
So how do expert FreeRTOS developers create exclusion between the task and ISR. I have the “Using the FreeRTOS Kernel… PIC32 edition”, but the example doesnt seem to fit.

thanks!

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 29, 2015, 9:55pm UTC](https://forums.freertos.org/t/mutex-in-isr/3210/2 "2015-05-29T21:55:43Z")

</div>

**[amsffrtos](https://sourceforge.net/u/amsffrtos/)** wrote on [Friday, May 29, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/#d54d):

The “FromISR” versions should certainly work, but check your FreeRTOSConfig.h to make sure that configMAX\_SYSCALL\_INTERRUPT\_PRIORITY is set at least as high as the priority level of your interrupt handler.

---

<div class="post-metadata">

**Author:** ![tlafleur](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tlafleur](https://forums.freertos.org/u/tlafleur)\
**Post date:** [May 29, 2015, 11:23pm UTC](https://forums.freertos.org/t/mutex-in-isr/3210/3 "2015-05-29T23:23:05Z")

</div>

**[tlafleur](https://sourceforge.net/u/tlafleur/)** wrote on [Friday, May 29, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/#a831/788c):

You can also turn off interrupts for a very short time to read variable…

volatile long myVarable  
long myNewVarable

task…

taskDISABLE\_INTERRUPTS ();  
myNewVarable = myVarable;  
taskENABLE\_INTERRUPTS ();

On Fri, May 29, 2015 at 2:48 PM, Robert H. Oujesky [roujesky@users.sf.net](mailto:roujesky@users.sf.net)  
wrote:

> I know this had to have been discussed before, but searching the forum  
> fails me…
> 
> PIC32 and FreeRTOS application. I have a hardware interrupt (INT0) which  
> increments a long variable everytime it fires. I have a task that runs  
> every 100ms which reads and clears that variable.  
> I thought I could use xSemaphoreTake and xSemaphoreGive but that crashes  
> the program. Then I looked at xSemaphoreGiveFromISR, but that doesnt work  
> either.  
> So how do expert FreeRTOS developers create exclusion between the task and  
> ISR. I have the “Using the FreeRTOS Kernel… PIC32 edition”, but the  
> example doesnt seem to fit.
> 
> ## thanks!
> 
> ## Mutex in ISR [https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/?limit=25#a831](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/?limit=25#a831)
> 
> Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/p/freertos/discussion/382005/)
> 
> To unsubscribe from further messages, please visit  
> [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/auth/subscriptions/)

–

~~\_/) `_/)` \_/) ``` \_/)~~

Tom Lafleur

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 30, 2015, 9:14am UTC](https://forums.freertos.org/t/mutex-in-isr/3210/4 "2015-05-30T09:14:27Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Saturday, May 30, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/#3350):

I would vote for Tom’s solution:

```auto

uint32_t ulTickCount = 0ul;

void a_task( void *pvParameters
{
    for( ;; )
    {
        /* Reading a 32-bit variable in 32-bit memory
        should be safe. */
        if( ulTickCount != 0ul )
        {
            taskDISABLE_INTERRUPTS ();
            ulTickCount--;
            taskENABLE_INTERRUPTS ();
        }
    }
}

void an_isr()
{
    /* The task has protected itself against possible
    interrupts. We're not expecting a nested interrupt
    that also accesses 'ulTickCount'. */
    ulTickCount++;
}

```

Programmers dislike disabling interrupts, but in the above case I see no better solution. As you’ll know the compiler will create at least 3 instructions for a de- or increment in memory:

```auto
  ulTickCount--;
      ldr r2, [r3] // Load from memory
      sub r0, r2, #1 // Decrease with 1
      str r0, [r3] // Store in memory

```

Getting an interrupt in-between these instructions would be fatal, `ulTickCount` gets corrupted.

About `xSemaphoreGive` :  
When you are sharing resources/objects among tasks, a semaphore or a mutex can be very useful. Before using the resource you `take` the semaphore, when ready you `give` the semaphore:

```auto
    if( xSemaphoreTake( xUSARTAccessSemaphore, ( TickType_t ) 10 ) )
    {
        vUSARTWrite( pcMessage, xLength );
        xSemaphoreGive( xUSARTAccessSemaphore );
    }

```

About `xSemaphoreGiveFromISR`:  
Sometime a resource becomes available when an interrupt fires, for instance when data is received or sent.

Suppose a USART receives characters and an ISR stores them into buffers. `xSemaphoreGiveFromISR` is called as soon as one buffer is full:

```auto
void an_isr()
{
BaseType_t xHigherPriorityTaskWoken = psFALSE;

    /* Read a character from the register: */
    ucUSARTBUffer[xUSARTIndex][xUSARTHead] = UDR0;
    if( ++xUSARTHead == USART_BUF_LENGTH )
    {
        xUSARTHead = 0;
        if( ++xUSARTIndex == USART_BUF_COUNT )
        {
            xUSARTIndex = 0;
        }
        /* As noticed above: the priority of this ISR should not
        be higher than configMAX_SYSCALL_INTERRUPT_PRIORITY. */
        xSemaphoreGiveFromISR( xUSARTSemaphore, &xHigherPriorityTaskWoken );
    }
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

```

Regards,  
Hein

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [May 30, 2015, 1:52pm UTC](https://forums.freertos.org/t/mutex-in-isr/3210/5 "2015-05-30T13:52:10Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Saturday, May 30, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/9442dd83/#fa46):

My first modification would be to use taskENTER\_CRITICAL/taskEXIT\_CRITICAL if the interrupt has a priority below configMAX\_SYSCALL\_INTERRUPT\_PRIORITY (as would be required to call any FreeRTOSAPI). This typically only disables those interrupts and not higher priority interrupts, it also nests properly incase your code that needs protection might be in a function called within a bigger critical section.
