# Suspending FreeRTOS for precise timing

**URL:** <https://forums.freertos.org/t/suspending-freertos-for-precise-timing/6135>\
**Category:** Kernel\
**Created:** [September 1, 2016, 5:47am UTC](https://forums.freertos.org/t/suspending-freertos-for-precise-timing/6135 "2016-09-01T05:47:54Z")\
**Posts on this page:** 3\
**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:** [September 1, 2016, 5:47am UTC](https://forums.freertos.org/t/suspending-freertos-for-precise-timing/6135/1 "2016-09-01T05:47:54Z")

</div>

**[simonhaines](https://sourceforge.net/u/simonhaines/)** wrote on [Thursday, September 01, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/aff69464/#b437):

My Cortex-M4 project uses a bank of WS2812 LEDs that require precise delays  
in the nanosecond range when modulating a data pin. The typical approach  
when using this part is to suspend all interrupts, pull the pin high, issue  
a certain number of NOP instructions depending on the value to display,  
then pull the pin low and issue another set of NOP instructions, repeating  
the process until all values are displayed and finally re-enabling  
interrupts.

There are several options available in FreeRTOS for exclusive access:  
vTaskSuspendScheduler, taskDISABLE\_INTERRUPTS and taskENTER\_CRITICAL.  
Considering my requirement is to have exclusive use of the chip for timing  
(not access to a shared resource), which is the preferred approach? If I  
use critical sections, will I need to set the priority of all interrupts to  
be at least BASEPRI so they are masked? Will the PendSV and SysTick  
interrupts still run, possibly throwing the timing out of whack?

Thanks, Simon.

---

<div class="post-metadata">

**Author:** ![McuOnEclipse](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mcuoneclipse/32/32_2.png) [@McuOnEclipse](https://forums.freertos.org/u/McuOnEclipse)\
**Post date:** [September 1, 2016, 6:13am UTC](https://forums.freertos.org/t/suspending-freertos-for-precise-timing/6135/2 "2016-09-01T06:13:25Z")

</div>

**[xz8987f](https://sourceforge.net/u/xz8987f/)** wrote on [Thursday, September 01, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/aff69464/#7137):

Hi Simon,  
first off, I do not recommend to drive WS2812 with such a bit-banging approach you describe: this is doable for a system where you do only this, as this requires a lot of CPU cycles. The typical way to drive WS2812 is with hardware like SPI or DMA (see [https://mcuoneclipse.com/2014/11/10/neoshield-ws2812-rgb-led-shield-with-dma-and-nrf24l01/](https://mcuoneclipse.com/2014/11/10/neoshield-ws2812-rgb-led-shield-with-dma-and-nrf24l01/) and the multi-part tutorial here: [https://mcuoneclipse.com/2015/08/01/tutorial-adafruit-ws2812b-neopixels-with-the-freescale-frdm-k64f-board-part-1-hardware/](https://mcuoneclipse.com/2015/08/01/tutorial-adafruit-ws2812b-neopixels-with-the-freescale-frdm-k64f-board-part-1-hardware/)). I’m sucessfully using the above approaches with FreeRTOS.

If you inisist on your bit-banging approach, then you need to make sure that your bit banging is not interrupted at all during the data transfer. If doing this from a task, you need to turn off all interrupts. You need to check your port how it deals with the interrupts:  
taskENTER\_CRITICAL on Cortex-M3/4/7 only masks interrupts up to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY, so other interrupts with higher urgency still run.  
dito for taskDISABLE\_INTERRUPTS: it uses BASEPRI for interrupt masking.

What you need to use is to disable all interrupts which usually is provided in the FreeRTOS port like this:

```auto
#define portDISABLE_ALL_INTERRUPTS() asm volatile("cpsid i")
#define portENABLE_ALL_INTERRUPTS() asm volatile("cpsie i")

```

I recently wrote an article series about Cortex-M interrupt system and how it is used in FreeRTOS, so this might be helpful for you:

> **[ARM Cortex-M, Interrupts and FreeRTOS: Part 1](https://mcuoneclipse.com/2016/08/14/arm-cortex-m-interrupts-and-freertos-part-1/)**
>
> The ARM Cortex-M microcontroller are very popular. And it has a very flexible and powerful nested vectored interrupt controller (NVIC) on it. But for many, including myself, the Cortex-M interrupt …

  

> **[ARM Cortex-M, Interrupts and FreeRTOS: Part 2](https://mcuoneclipse.com/2016/08/20/arm-cortex-m-interrupts-and-freertos-part-2/)**
>
> In “ARM Cortex-M, Interrupts and FreeRTOS: Part 1”  I started with the ARM Cortex-M interrupt system. Because the ARM implementation cann be very confusing, I confused myself and had to…

  

> **[ARM Cortex-M Interrupts and FreeRTOS: Part 3](https://mcuoneclipse.com/2016/08/28/arm-cortex-m-interrupts-and-freertos-part-3/)**
>
> This is the third part about ARM Cortex-M and how the interrupts are used. In Part 1 I discussed the Cortex-M interrupt system and in Part 2 I showed nested interrupt examples. This part is about F…

I hope this helps,  
Erich

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 3, 2016, 12:44am UTC](https://forums.freertos.org/t/suspending-freertos-for-precise-timing/6135/3 "2016-09-03T00:44:19Z")

</div>

**[simonhaines](https://sourceforge.net/u/simonhaines/)** wrote on [Saturday, September 03, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/aff69464/#d9c0):

Thanks Erich, yes I’m an avid reader of your blog and have been following your interrupts series. I did not know you had already covered the particular requirements of the WS2812, and I’ll give the FTM/DMA approach a shot.  
Simon.
