Bill Greiman
Well-known member
I have ported the ChibiOS/RT RTOS kernel to Teensy 3.0 as a library. Here is a link to ChibiOS http://www.chibios.org/dokuwiki/doku.php?id=start.
Porting the RTOS has required several patches to the Teensy 3.0 core. I have tried to minimize these patches and would appreciate any advice on a better way.
I need three features to run a preemptive kernel. Here are changes to the beta7 code.
A hook in the SysTick handler. I modified systick_isr() like this.
Access to the Supervisor Call exception. I added a weak symbol like this:
Ability to use thread mode and handler mode. I added this code at the start of void ResetHandler(void).
I would appreciate any advice on a better way to implement the above features.
Here is an example of how ChibiOS can be used with Teensy 3.0. This example runs two high priority tasks and the normal Arduino loop().
Porting the RTOS has required several patches to the Teensy 3.0 core. I have tried to minimize these patches and would appreciate any advice on a better way.
I need three features to run a preemptive kernel. Here are changes to the beta7 code.
A hook in the SysTick handler. I modified systick_isr() like this.
Code:
void (*systick_hook)() = 0;
void systick_isr(void)
{
timer0_millis_count++;
if (systick_hook) systick_hook();
}
Access to the Supervisor Call exception. I added a weak symbol like this:
Code:
diff -r org/mk20dx128.c mod/mk20dx128.c
28a29,30
> void svcall_isr(void) __attribute__ ((weak, alias("fault_isr")));
>
92c94
< fault_isr, // 11 ARM: Supervisor call (SVCall)
---
> svcall_isr, // 11 ARM: Supervisor call (SVCall)
Code:
164a167,168
> int unused_handler_stack() {return 0;}
> int handler_stack_size(void) __attribute__ ((weak, alias("unused_handler_stack")));
167a172,182
> int mstk = handler_stack_size();
> if (mstk) {
> uint32_t psp, reg;
> /* Process Stack initialization - allow mstk entries for handler stack */
> asm volatile ("cpsid i");
> psp = (uint32_t)(&_estack - mstk);
> asm volatile ("msr PSP, %0" : : "r" (psp));
> reg = 2;
> asm volatile ("msr CONTROL, %0" : : "r" (reg));
> asm volatile ("isb");
> }
I would appreciate any advice on a better way to implement the above features.
Here is an example of how ChibiOS can be used with Teensy 3.0. This example runs two high priority tasks and the normal Arduino loop().
Code:
// Simple demo of three threads
// LED blink thread, print thread, and idle loop
#include <ChibiOSTeensy3.h>
const uint8_t LED_PIN = 13;
volatile uint32_t count = 0;
//------------------------------------------------------------------------------
// thread 1 - high priority for blinking LED
// 64 byte stack beyond task switch needs
static WORKING_AREA(waThread1, 64);
static msg_t Thread1(void *arg) {
pinMode(LED_PIN, OUTPUT);
while (TRUE) {
digitalWrite(LED_PIN, HIGH);
chThdSleepMilliseconds(50);
digitalWrite(LED_PIN, LOW);
chThdSleepMilliseconds(150);
}
return 0;
}
//------------------------------------------------------------------------------
// thread 2 - print idle loop count every second
// 200 byte stack beyond task switch needs
static WORKING_AREA(waThread2, 200);
static msg_t Thread2(void *arg) {
while (TRUE) {
Serial.println(count);
chThdSleepMilliseconds(1000);
}
return 0;
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
while(!Serial) {};
delay(2000);
Serial.println("chBegin");
// initialize ChibiOS
chBegin();
// start blink thread
chThdCreateStatic(waThread1, sizeof(waThread1),
NORMALPRIO + 2, Thread1, NULL);
// start print thread
chThdCreateStatic(waThread2, sizeof(waThread2),
NORMALPRIO + 1, Thread2, NULL);
}
//------------------------------------------------------------------------------
// idle loop runs at NORMALPRIO
void loop() {
count++;
}