Stm32 Ctext-M3 kernel's simplest multitasking RTOS

RTOS real-time operating system, I am doing a multi-tasking operating system, can run multiple tasks at the same time, the maximum number of tasks I set here is 32.

It is estimated that there are very few embedded devices running more than 30 tasks, and 32 tasks are sufficient.

OS I call it HesOS, the main functions are as follows, relatively simple.

1--implement multitasking scheduling

2--Implement task scheduling lock

3--Achieve critical section protection

4--Implementation of reading cpu usage (this function is more important)

5--implement us delay (for simulation such as IIC bus simulation)

For general embedded applications, these features are almost enough.

Porting HesOS is also very simple, not as large as other OSs, and there are many modifications and complicated to use.

HesOS is very simple to port and use, similar to the use of PC threads.

Main function initialization: The system ran a total of 10 tasks.

Part of the task definition:

Print cpu usage task:

Serial port printing cannot be done in all tasks, because serial port printing is very CPU-intensive, unless it is transmitted using dma.

Speaking of porting, you only need to implement the following code.

_uint32 fac_us;
//SysTick timer
Void set_systick(_uint32 ms)
Fac_us=SystemCoreClock/1000000; //fac_us needs to be used regardless of OS usage
SysTick->LOAD = ms * SystemCoreClock/1000-1;
SysTick->VAL = 0;


/*delay us

Cropped atomic brother here

Void delay_us(_uint32 nus)
_uint32 ticks;
_uint32 told, tnow, tcnt=0;
_uint32 reload=SysTick->LOAD; //value of LOAD
Ticks=nus*fac_us; //The number of beats needed
Told=SysTick->VAL; //Counter value just entered
If(tnow<told)tcnt+=told-tnow; //Note here that SYSTICK is a decrementing counter.
Else tcnt+=reload-tnow+told;
If(tcnt>=ticks)break; //Time exceeds/equal to the time to delay, then exit.

/ / Tick interrupt must call the following code, is about task scheduling
Void SysTick_Handler()

If(now_task!=0) //Execute task scheduling if the current task is not empty

/ / Task scheduling

Add HesOS.lib under the project to the project, only 3 of the HesOS system files.

Description: This RTOS is based on the freeRTOS and atomic part of the code, the demonstration project is the atomic brother's library function lighting project.