Atomthreads是开源的实时操作系统。诞生之初就是给STM8s设计的,而且作者还在不断更新,我看最近的主要修改是加入更多MCU的支持。算法上没有变化。所以我取了1.3的版本,足够用了。
我使用的是STM8S105K4的最小系统。有16Kflash可以使用。这个大小放下原生的atomthreads是够的。
这个实时系统包含了操作系统所有最基本的接口 - mutex
- semaphore
- timer
- queue
- 255级优先级的强占是调度
- 同优先级时间片轮转
等等。绝对算是完整的操作系统。 并且源代码有所有API调用的例子,这绝对是福利,节约大家时间。要造汽车,绝对不需要每次都从车轮造起。当今世界要站在巨人的肩膀上前进。
回到atomthreads的内部,它需要一个心跳timer,系统默认使用了TIM1这个STM8中功能最强的timer。如果你的系统中要用TIM1做更复杂的事情,那么你可以改用其他的TIM2/TIM3来做心跳。
[cpp] view plain copy
- NO_REG_SAVE void main ( void )
- {
- int8_t status;
-
- /**
- * Note: to protect OS structures and data during initialisation,
- * interrupts must remain disabled until the first thread
- * has been restored. They are reenabled at the very end of
- * the first thread restore, at which point it is safe for a
- * reschedule to take place.
- */
-
- /* Initialise the OS before creating our threads */
- status = atomOSInit(&idle_thread_stack[IDLE_STACK_SIZE_BYTES - 1], IDLE_STACK_SIZE_BYTES);
- if (status == ATOM_OK)
- {
- /* Enable the system tick timer */
- archInitSystemTickTimer();
-
- /* Create an application thread */
- status = atomThreadCreate(&main_tcb,
- TEST_THREAD_PRIO, main_thread_func, 0,
- &main_thread_stack[MAIN_STACK_SIZE_BYTES - 1],
- MAIN_STACK_SIZE_BYTES);
- if (status == ATOM_OK)
- {
- /**
- * First application thread successfully created. It is
- * now possible to start the OS. Execution will not return
- * from atomOSStart(), which will restore the context of
- * our application thread and start executing it.
- *
- * Note that interrupts are still disabled at this point.
- * They will be enabled as we restore and execute our first
- * thread in archFirstThreadRestore().
- */
- atomOSStart();
- }
- }
-
- /* There was an error starting the OS if we reach here */
- while (1)
- {
- }
-
- }
另外内核默认是会打印debug message。提供printf函数。底层是通过UART2实现。所以调试atomthreads,你需要把UART接出来,通过PL2303转接到PC USB。
[cpp] view plain copy
- static void main_thread_func (uint32_t param)
- {
- uint32_t test_status;
- int sleep_ticks;
-
- /* Compiler warnings */
- param = param;
-
- /* Initialise UART (9600bps) */
- if (uart_init(9600) != 0)
- {
- /* Error initialising UART */
- }
-
- /* Put a message out on the UART */
- printf("Go\n");
添加自己的中断服务程序 ISR,要注意,在进入和退出时,分别调用atomIntEnter()和atomIntExit(TRUE)。这两个函数时系统调度器要用到的 [cpp] view plain copy
- #if defined(__IAR_SYSTEMS_ICC__)
- #pragma vector = 13
- #endif
- INTERRUPT void TIM1_SystemTickISR (void)
- #if defined(__RCSTM8__)
- interrupt 11
- #endif
- {
- /* Call the interrupt entry routine */
- atomIntEnter();
-
- /* Call the OS system tick handler */
- atomTimerTick();
-
- /* Ack the interrupt (Clear TIM1:SR1 register bit 0) */
- TIM1->SR1 = (uint8_t)(~(uint8_t)TIM1_IT_UPDATE);
-
- /* Call the interrupt exit routine */
- atomIntExit(TRUE);
- }
另外atomthreads的底层硬件操作实际是调用意法半导体的标准库函数。只不过作者为了让代码精简,只拿出了用到的函数。
为了对代码管理,本人在Kelvin Lawson的基础上开发了自己的分支。又由于我主要使用IAR编译器,所有所有的修改都基于这个开发环境。
参考链接
|