ARM Cortex RTOS: Part 1

Here are some resources which are useful if you want to learn about developing an OS on ARM Cortex-M

https://interrupt.memfault.com/blog/cortex-m-rtos-context-switching

There is also a great resource for an OS called uOS++, an RTOS in C++.

http://micro-os-plus.github.io/

The Joseph Yiu book on Cortex M is really good too, which comes with sample OS code.

I recently wanted an OS for a particular application where there would be a bit of IO bound processing. After looking at the options for an RTOS, it did seem that many were a bit overkill and complex.

For example, FreeRTOS for the M3/M4 has thousands of lines of code, albeit many are commented out with #if’s so you can configure your OS. While configuration and customisation is good, it comes at the cost of having bloated hard to read code.

FreeRTOS runs on an X86 platform, something I don’t need for my Arm Cortex-M. What really want is an OS which can run complex IO bound tasks, such as writing to standard SPI Flash drivers without having to restructure the code to be re-entrant.

In the usual superloop programs, you’d structure the SPI Flash Access routines so that each pass of the superloop would do a little bit of the SPI Flash routine.

This would mean implementing a state machine, which *could* be a lot more complex than a while(1) loop which waited on an object to perform the necessary task.

There’s nothing wrong with the state machine method, but what if you’re given a comms library (eg. TCPIP that you didn’t write, or a FatFS) and asked to integrate that into your app.

Are you going to break that up into a state machine?

OS Types:

  • Preemptive
  • Cooperative

Yes you can configure FreeRTOS to be cooperative, but as an ‘academic’ exercise it seemed easier to write my own OS.

In my case using a Cooperative OS is fine because having tasks being preempted will cause you a lot of synchronisation issues if you are not careful.

The idea is that each IO bound operation will start it’s DMA (or IO operation) and then pass control to the next thread while waiting for the IO operation to complete.

OS Writing Tips:

  • Use a debugger like the JLink which supports GDB. You can also contact them and they will provide you with an RTOS SDK to write a GDB plugin so you get thread aware debugging.
  • Understand how C callstacks work. Knowing how the exception frame stacks and unstacks

 

Check out Part 2 (coming soon) if you would like to keep reading.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s