# Tickless context switch before going to sleep EM2

**URL:** <https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280>\
**Category:** Kernel\
**Created:** [March 19, 2020, 4:25pm UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280 "2020-03-19T16:25:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gougoul](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gougoul](https://forums.freertos.org/u/gougoul)\
**Post date:** [March 19, 2020, 4:25pm UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280/1 "2020-03-19T16:25:34Z")

</div>

Hello,  
I am new on FreeRTOS and I cannot figure what is my mistake.  
I am working on a EFM32TG11B (ARM M0+) with FreeRTOS V10.1.1 The project use the tickless mode of freertos.  
My problem is the following.  
I enter an interrupt, in this interrupt I send a signal to a task

> BaseType\_t xHigherPriorityTaskWoken = pdFALSE;  
> (void) xTaskNotifyFromISR(g\_ISSITcb,ulValue,eSetBits,&xHigherPriorityTaskWoken);  
> portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken );

The task is waiting for the signal

> (void) xTaskNotifyWait(0x00,ULONG\_MAX,ulNotifiedValue,portMAX\_DELAY);

So when I exit the interrupt, I expect the OS to go and execute the task. But it seems it go directly to the task IDLE and my task is in READY state.

Moreover, in the void vPortSuppressTicksAndSleep( TickType\_t xExpectedIdleTime )  
I go in EM2 like bellow :

> \_\_enable\_irq();  
> EMU\_EnterEM2(true);  
> \_\_disable\_irq();

Important informations, if I pause and re run the debugger, it is working, I go into the task.  
If I comment the EMU\_EnterEM2(true), it is also working.

Do you have any idea what could be the problem ?  
Thank you very much

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [March 19, 2020, 5:40pm UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280/2 "2020-03-19T17:40:50Z")

</div>

> [@gougoul](#):
>
> \_\_enable\_irq();  
> EMU\_EnterEM2(true);  
> \_\_disable\_irq();

Did you add this code? I don’t think it’s right. In Cortex-M, you should actually go to sleep with interrupts disabled. Otherwise the CPU might handle the interrupt that is supposed to wake you up and _then_ go to sleep. You don’t want that.

---

<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:** [March 19, 2020, 5:49pm UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280/3 "2020-03-19T17:49:08Z")

</div>

Concur - looks like the enabling and disabling of interrupts are the wrong way around.

---

<div class="post-metadata">

**Author:** ![gougoul](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@gougoul](https://forums.freertos.org/u/gougoul)\
**Post date:** [March 20, 2020, 9:06am UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280/4 "2020-03-20T09:06:52Z")

</div>

Hello, true I did a mistake here. So now my I2C is working well but vTaskDelay from the RTOS are now stucked and never end. Do you have an idea or work around ?

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [March 20, 2020, 4:05pm UTC](https://forums.freertos.org/t/tickless-context-switch-before-going-to-sleep-em2/9280/5 "2020-03-20T16:05:50Z")

</div>

If you have modified vPortSuppressTicksAndSleep(), you may have other errors there. Try going back to the “official” version of port.c and see if the problems go away. By the same token, if you have defined configPRE\_SLEEP\_PROCESSING and/or configPOST\_SLEEP\_PROCESSING, eliminate those too.
