1 The significance of using RTOS on MSP 430
It is understandable that it is meaningless to use RTOS on MSP430. Because the hardware resources of MSP430 are limited (for example, MSP430F149 has only 2KB RAM), it is impossible to port any commercial operating system to MSP430. Currently, the only RTOS used on MSP430 is μC/OS-II, but the use of μC/OS-II requires an expensive C compiler, which severely limits its use on MSP430.
Based on the above situation, the author wrote an RTOS based on MSP430F149 in the process of applying MSP430, tentatively named M430/OS. It occupies less RAM and has short code, and can be applied to most other MSP430 microcontrollers with slight changes .
Using M430/OS on the MSP430 MCU system has the following significance for the system:
① Realize modularization of software design. Different functional modules can be compiled into corresponding tasks, which can be called by the operating system according to the level, without worrying about which function to execute first and which function to execute later.
② It can make more reasonable and effective use of the limited CPU resources. Arranging the level of tasks according to their importance can ensure that the most important tasks can be executed in the most timely manner.
③ Greatly reduce the system failure rate. When a low-priority task is blocked, the execution of a high-priority task is not affected.
2 Implementation of M430/OS on MSP430F149
2.1 M430/OS Features
M430/OS has the following features:
① Use preemptive kernel, that is, high-priority tasks can "grab" CPU control back from low-priority tasks;
② Each task opens a separate task stack;
③ Each task occupies a task stack of dozens to hundreds of bytes. The size of the task stack can be estimated based on the field data, local variables and nested calls in the task;
④ Each task is assigned a priority, and two tasks cannot have the same priority;
⑤ Does not support semaphore and mailbox functions;
⑥ There are only three task states: executing, ready, and suspended;
⑦ The amount of RAM occupied by the system = ((number of tasks + 1) × 4) + 6 bytes, excluding the task stack;
⑧ Small amount of code. The current version of the code has 86 lines of assembly code and 256 bytes of target code;
⑨ Theoretically supports up to 126 tasks;
⑩ Task lock function: In a low-priority code, if you do not want the operating system to switch the CPU to another task, you can lock the code. When running this code, it will not cause task switching;
Task wake-up function: Generate an event in a task to trigger other tasks to run (if the triggered task has a high priority, it will run immediately).
2.2 Introduction to system functions
① OS_Init: Multi-task initialization, initialization of task stack (see Figure 1 for the structure of task stack), task delay count, and task status. After initialization, the system directly switches to the highest priority task, and the multi-task system starts.
② OS_Time_Dly: Suspend the current task for a specified period of time to allow other tasks to run.
③ OS_Sched: Task scheduling, it first reduces the delay number of each task by 1, then finds the ready task with the highest priority and switches to this ready task. If there is no ready task, it switches to the idle task.
④ OS_Free_Task: Idle task is a very important system task. When all tasks are suspended, this task is run. It mainly accumulates a counter Free_Count. Users can calculate the CPU utilization based on this counter.
⑤ OS_Task_LOC k : Lock task scheduling and prohibit task scheduling. Mainly used to lock some reentrant codes or some important codes in low priority.
⑥ OS_Task_Unlock: Unlock task scheduling, which is the opposite of the above subroutine function.
⑦ OS_Task_Wakeup: Wake up the task of specified priority and generate a task scheduling. If the priority of the awakened task is higher than the priority of the currently running task, the task will switch to the awakened task, otherwise wait for the next scheduling opportunity.
2.3 Implementation of main functions
(1) Task initialization
After the system is powered on, the hardware resources are initialized first, and then the multi-tasks are initialized. The main thing is to initialize the task stack of each task, the number of clock ticks of each task, and the stack pointer position. We initialize each task stack as shown in Figure 1.
The task stack is initialized as follows (r11 is a pointer used to initialize the task stack, and r10 is a loop counter):
mov.w #(stack bottom + 2), r11
clr.w Task_T IC k(r10); clear the clock tick count
mov.w #task first address, 0(r11); push the task address into the stack
mov.w SR , -2(r11) ; put the flag register into the task stack
mov.w r11 , Task_SP(r10)
sub.w #Number of bytes occupied by the scene, Task_SP(r10); SP position is placed
; Push into stack
After initializing the task stack, the stack pointer is pointed to the first address of the task stack with the highest task priority, and then ret is executed. In this way, multitasking is started, and the program is as follows:
mov.w #09feh , sp ; The first task in the task stack with the highest priority
;Address location
ret ; return to the highest priority task
The process of task initialization is shown in Figure 2.
(2) Clock beat
The clock beat is generated by TimerA of MSP 430F149. TimerA works in rising mode, and CC R0 contains the maximum value of TimerA count. The TimerA initialization code is as follows:
bis.w #(TASSEL1+TACLR+MC_1),&TACTL
mov.w 2(sp),&CCR0; count the maximum value, this value determines the clock beat
bis.w #CCIE,&CCTL0
(3) Task Scheduling
After the application calls OS_init for initialization, it directly switches to the highest priority task.
Each task executes OS_ Time_Dly after running a cycle. This is achieved by filling the delay number of the task into the Task_ Tick of the task and then executing the task scheduler.
Task scheduling is to reduce the Task_Tick of all tasks by 1 when the timer interrupt occurs, and then search for the task whose Task_Tick is reduced to 0 in order of priority, and jump directly to the task switching program.
The following is the task switching procedure (the content of r10 is the flag of the ready task, which is found by the scheduler):
pushALL; push the current task scene into the stack
mov.b Now_Task,r11; put the current task flag in r11
mov.w sp,Task_SP(r11); save the current task stack pointer
mov.b r10,Now_Task; The ready task flag becomes the current task flag
mov.w Task_SP(r10),sp ; put the task stack pointer of the ready task into SP
;At this time, the stack operation is the task stack operation of the ready task.
popALL; Pop the ready task's scene out of the stack
reti; interrupt return, return to the ready task
There are two scheduling opportunities for task scheduling: one is when the task is suspended, and the other is when the timer interrupts. Task scheduling when the task is suspended will definitely cause task switching, but the timer interrupt will not necessarily cause task switching. This is because if the ready task is the currently running task, it will not cause switching. As such, task scheduling is the most frequently executed function in RTOS and is also the most important function, so its code size must be reduced as much as possible, and reliable scheduling algorithms must be used as much as possible to reduce the time occupied by task scheduling. The flow of this subroutine is shown in Figure 3.
(4) Implementation of task lock and other functions
The purpose of locking and unlocking tasks is to prevent non-reentrant code in low-priority tasks or I/O operations with high real-time requirements from causing task switching during execution. This function is achieved by setting a flag. When the scheduler detects that a task is locked, even if there is a ready task, it must wait until the lock is unlocked before switching.
If the system suddenly generates an event that needs to be handled by a suspended task, you can call task wakeup in the program that generates the event. Its principle is to clear Task_TICk to 0 and then execute a task scheduling. If the priority of this task is higher, it will directly switch to this task for execution.
3 Conclusion
M430/OS has been applied to the system based on MSP430F149 developed by the author, and it runs stably and reliably. This operating system can be applied to other MSP430 microcontrollers with slight modifications. Of course, its functions are still very limited, and there may be some problems that have not yet been exposed; but in any case, it proves to us that it is entirely possible to use RTOS in MSP430 microcontroller systems.
Previous article:Design and implementation of voice signal acquisition based on SPCE061A single chip microcomputer
Next article:Detailed explanation of the application of single chip microcomputer
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- RFID unattended intelligent weighing software system solution
- 【TI recommended course】#Live replay: TI's new generation of integrated PA Zigbee 3.0 and multi-protocol solutions#
- 200 Examples of Monolithic Switching Power Supply Design (2nd Edition)
- Physical Design Analysis of PCB Board in Switching Power Supply Design
- Application and development of filters in anti-EMI
- PCB Design
- Why can't I see the bluetooth function when running micropython on EPS32?
- Does anyone know how this kind of colored PCB is made? ?
- How to solve the problems of waterproofing, liquid level fluctuation, and water droplets sticking to the wall of the liquid level sensor?
- EEWORLD University Hall----Live Replay: Comprehensive explanation of TI MSP Academy tutorial