Demonstration: How to implement a preemptive multitasking OS on embedded devices.
Contribution:
- Contribution
- Development
- What is this about?
- What is it not?
- How to port this to another platform?
- About
- If you want to port this implementation fork me
- If you have improvements at your hand send me an PR
- Hardware: STM32L476G-DISCO Board
- For development the STM32Cube IDE is used
- gcc
- To get a highlevel API the Board BSP from ST is used
- Debug Printouts on UART2:
- can be activated via switch in Usercode/debug.h or with compiler define.
#define DEBUG_PRINT
- Setting:
- Baudrate: 9600
- Databits: 8
- Stopbits: 1
- Parity: None
- This is a demonstration how to create a preemptive multitasking system which can run threads (using setjmp/longjmp, and a timer for preemptive scheduling)
- A implementation to use in production. If you need preemptive multithreading use RTOS or something similar.
- Portable to other platforms (Stackframe definition specific for ARM Cortex-M4)
- You need everything in Usercode/Concurrency
- remove or implement methods according to Usercode/debug.c
- Adaptions in scheduler.c
- redefine t_stackFrame according to your platform
- allocate stackframe correct in allocateStack
- change this to match your platform
- if you not using cmsis_gcc.h find out how to set/get the stackpointer
- yield ... exit pending interrupt
- void TIM1_CC_IRQHandler() - this is the timer interupt for preemptive scheduling
- timers_init() - creating a timer which calls the the timer interrupt at a rate of TIMESLICE_MS
- OS Implementation:
- Preemptive Scheduling: Usercode/Concurrency/scheduler.c
- Synchronisation Mechanism via counting semaphores: Usercode/Concurrency/semaphore.c
- Demo Usage: Usercode/usercode.c
- To enable/disable synchronization change #define SEM_DEMO at start of usercode.c
- Basic Usage Example - printout stuff to console + green and red LED should flash in parallel forever:
void flashGreenThreadForever() {
while (true) {
BSP_LED_Toggle(LED_GREEN);
scheduler_sleep(50);
}
}
void flashRedThreadForever() {
while (true) {
BSP_LED_Toggle(LED_RED);
scheduler_sleep(50);
}
}
void printSome() {
DEBUG_PRINTF("Test 1 ... ");
DEBUG_PRINTF("Test 2 ... ");
DEBUG_PRINTF("Test 3 ... ");
DEBUG_PRINTF("Test 4 ... ");
DEBUG_PRINTF("Test 5 ... ");
}
void runExample() {
scheduler_init();
scheduler_startThread(&printSome);
scheduler_startThread(&flashGreenThreadForever);
scheduler_startThread(&flashRedThreadForever);
}