Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


Suspending FreeRTOS for precise timing

Posted by simonhaines on September 1, 2016

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, taskDISABLEINTERRUPTS and taskENTERCRITICAL. 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.

Suspending FreeRTOS for precise timing

Posted by xz8987f on September 1, 2016

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/ 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/). 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: taskENTERCRITICAL on Cortex-M3/4/7 only masks interrupts up to configMAXSYSCALLINTERRUPTPRIORITY, 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: ~~~

define portDISABLEALLINTERRUPTS() asm volatile("cpsid i")
define portENABLEALLINTERRUPTS() 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: https://mcuoneclipse.com/2016/08/14/arm-cortex-m-interrupts-and-freertos-part-1/ https://mcuoneclipse.com/2016/08/20/arm-cortex-m-interrupts-and-freertos-part-2/ https://mcuoneclipse.com/2016/08/28/arm-cortex-m-interrupts-and-freertos-part-3/

I hope this helps, Erich


alternate (1064 bytes)

Suspending FreeRTOS for precise timing

Posted by simonhaines on September 3, 2016

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.


alternate (1064 bytes)

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists