RTOS hooks

It would be nice to have hooks in Due so popular RTOSs could be ported to Due as libraries.

The main requirement is to have access to the SVC_Handler, PendSV_Handler, and SysTick_Handler vectors.

A call to a weak dummy function at the end of these handles would be sufficient. Currently SVC_Handler and PendSV_Handler are just for() loops that stop execution.

I understand that full preemptive RTOSs are not appropriate for most Arduino users but a few advanced users want the functionally of a full RTOS.

There are a lot of first class open source RTOSs that would run well on Due.

Paul Stoffregen provided these hooks for Teensy 3.0 and I ported ChibiOS/RT and FreeRTOS to Teensy 3.0 using these hooks. I may port additional systems since I enjoy seeing how RTOSs are implemented and how they perform.

I would like to provide these libraries to Due users in a way that would not require modification of the Arduino core.

Hello fat16lib. Right now I am porting CAN protocol in Due. To test transmission (receiving messages) I am using a couple of interrupt handlers that read CAN controllers addresses at 0x400000000 in the memory-mapped interrupts of the CORTEX-M3. After a message is received, some I/O are managed. Like you said, a couple of calls to weak (aliases) dummy functions were enough. All these with third-party CMSIS gnu gcc exceptions. Regarding your RTOS hooks, I believe they could add more value for Due but from what I understand, as the more we are attached to the Arduino IDE libraries, the better for the Arduino community. Some 'exceptions' may apply. Regards.

fat16lib,
I'm actually resolving an issue with weak-symbols in interrupt handlers, I guess that at the end of my refactoring you can "hook" the interrupts directly.

cmaglie,

That sounds great. Teensy 3.0 has these three weak handlers, SVC_Handler, PendSV_Handler, and SysTick_Handler.

The only question is how to handle SysTick. In Teensy 3.0 I replace the handler with one that that duplicates the Teensy 3.0 functionality and the RTOS needs.

I want the standard Arduino functionality to continue to work and have the option for a few high priority tasks and ISRs to use the RTOS functionality.

This way an ISR can cause a high priority task to execute in about 3 microseconds while loop() does low priority stuff like a normal Arduino sketch.

fat16lib:
cmaglie,
The only question is how to handle SysTick. In Teensy 3.0 I replace the handler with one that that duplicates the Teensy 3.0 functionality and the RTOS needs.

I want the standard Arduino functionality to continue to work and have the option for a few high priority tasks and ISRs to use the RTOS functionality.

I see the issue with SysTick, i've found the following solution to avoid code duplication:

you just need to define a function called:

int sysTickHook(void)

that overrides the empty sysTickHook. If you return 0 the Arduino's handler is runned afterward, any other value instead prevents it from running (I don't know if this could be useful for something but its for free).

fat16lib:
I understand that full preemptive RTOSs are not appropriate for most Arduino users but a few advanced users want the functionally of a full RTOS.

Until the changes needed are so small and doesn't affect anything, IMHO its worth trying. Of course, don't expect support for more advanced stuff about RTOS... from now on you're alone! :wink:

(but I'm curious to see what you can do it :))

cmaglie,

Perfect!

I understand this may not be supported in the future or may change.

Soon after I get my Due, I will post a version of ChibiOS/RT and FreeRTOS for Due with a number of examples.

I also have a version of SdFat ready for testing. I have optimized it for chips with large memory.

I wish there were fast native SPI functions for reading and writing blocks of memory on Due. On Teensy 3.0 with 24 MHz native SPI I get file read and write rates over 2 MB/sec for large reads and writes. Due should be even faster but I plan to use the official SPI library

Thanks for access to systick, I can think of a couple of uses for that.

Now what happens if two people want to use it?


Rob

Graynomad:
Now what happens if two people want to use it?

Good point, I guess they remains incompatible for now. :wink:

C

I have a pin debounce function that I'd like to port to the Due and it needs access to SysTick. So the obvious problem is that if you use that you can't use anything else and vv.

In my LPC framework I allow up to 3 SysTick handlers and there's similar system to interrupts ie

attachEventHandler(EVENT_SYS_TICK0, debounceHandler);
attachEventHandler(EVENT_SYS_TICK1, someOtherHandler);
attachEventHandler(EVENT_SYS_TICK2, yetAnotherHandler);

Obviously it's up to the programmer to keep these very short.


Rob

I realize that this thread is a bit old, but I quickly tried getting this running today on 1.52 and wasn't having much luck. I added the following code as a cpp file to my project:

#include <Arduino.h>

int count = 0;

int sysTickHook(void)
{
  count++;
  return 0;
}

It doesn't seem that this code is being called. What am I missing? Thanks for the help...

delsauce:
I realize that this thread is a bit old, but I quickly tried getting this running today on 1.52 and wasn't having much luck. I added the following code as a cpp file to my project:

#include <Arduino.h>

int count = 0;

int sysTickHook(void)
{
  count++;
  return 0;
}




It doesn't seem that this code is being called. What am I missing? Thanks for the help...

It needs to be in an extern "C" { ... } block, eg

extern "C" {
  int sysTickHook(void)
  {
    count++;
    return 0;
  }
}

stimmer:
It needs to be in an extern "C" { ... } block, eg

extern "C" {

int sysTickHook(void)
  {
    count++;
    return 0;
  }
}

Yep, that did the trick. Thanks for the quick reply...

Thank You stimmer, this was the information I needed also.
And Your contribution was the reason for me to register here.
Have a nice weekend.