Implementing a Scheduler

21 Jan 2010

Currently, working with interrupts is a bit of a pain, as you can't really do much in the ISR without breaking anything, and even a ticker will mess things up if it interrupts your code at a bad time, especially if a segment of code is supposed to execute atomically. Currently I have a bitmask thing going on, where the ISR just sets the appropiate interrupt bit and the bit is handled in the main loop, but... without a scheduler an OS, it means you end up polling the mask and the interrupts don't really work like "interrupts" anymore.

For example, I have some basic code that monitors 4 interrupts, and on rise, it updates a HD44780 based display. The problem is that each button starts a task that I'd like to be interrupted if a button is pressed, and the code contains printfs and other stuff that you can't really stuff into the ISR (not without the HD44780 breaking due to corruption anyway)...

So I've been looking to implement a simple scheduler which would provide basic threading and some synchronization primitives, without the fun of a full blown OS.

Features such as:
Threading
Semaphores
Events
Mutexes
Critical Sections

It'd be really nice for example to wrap the HD44780 code in a critical section... and make ISTs to handle interrupts.

Anyone do any work on this? Or if there's a way to take care of this without all this work, I'd like to know!

21 Jan 2010

You probably should look into using some RTOS, e.g. FreeRTOS.

That said, some things you can try:

1) you can just add __WFI() (wait for interrupt) in the polling loop's body, that should reduce cpu idle usage.

2) for the scheduler you can use Cortex-M3's SysTick interrupt ("magic" name is SysTick_Handler). It seems to be unused by the mbed libraries.

3) for synchronization, try my semaphore class (NB: untested). I've got the idea from here.

21 Jan 2010
Igor Skochinsky wrote:

You probably should look into using some RTOS, e.g. FreeRTOS.

That said, some things you can try:

1) you can just add __WFI() (wait for interrupt) in the polling loop's body, that should reduce cpu idle usage.

2) for the scheduler you can use Cortex-M3's SysTick interrupt ("magic" name is SysTick_Handler). It seems to be unused by the mbed libraries.

3) for synchronization, try my semaphore class (NB: untested). I've got the idea from here.

Cool, I'll look into the semaphore class. I kind of want to avoid using a RTOS, but I guess why reinvent the wheel? The only thing is the FreeRTOS post I read involved setting up gnugcc and compiling locally... Which means maybe I lose the mbed libraries? Is there a way to use FreeRTOS with the online compiler? I think last time I tried I got a bunch of ASM issues...

31 Oct 2010

Anyone got any answers to Michael Wei? Im interested in this topic also!

31 Oct 2010

Hi Harry

Tim has posted a simple scheduler referred to here, and in the same thread Igor also mentions his port of scmRTOS (which compiles online). After that, you're into a separate toolchain I think; I don't believe anybody has got FreeRTOS compiling online.

Regards
Daniel