UC/OS-II kernel Architecture Analysis (3)-uC/OS-II System Core

Source: Internet
Author: User

It is mainly included in the C source file OS _core.c.

1. uC/OS-II Task Scheduling (1) uC/OS-II Scheduling Algorithm

UC/OS-II uses the priority-based scheduling algorithm, always select the current ready state of the highest priority task scheduling. UC/OS-II is a preemptible, real-time OS that allows new job scheduling upon completion of interruptions.

UC/OS-II has two Scheduling Methods: task-level task scheduling, interrupt-level task scheduling.

(2) task readiness table

Int8u const osunmaptbl [1, 256] = {...};

OS _ext int8u osrdygrp;

OS _ext int8u osrdytbl [OS _rdy_tbl_size];

  • Add a ready task to the ready table;
  • Delete a ready task from the ready table;
  • Find the highest priority ready task OS _schednew ();
(3) task-level Task Scheduling

Task Scheduling is performed when the return is not interrupted. Generally, the current task is blocked or suspended due to time delay or waiting for an event, or the task with a higher priority is in the ready state.

Basic Task information:

  • PC register of the CPU: the current position of the task;
  • General registers of the CPU: temporary data involved in the current execution of the task;
  • CPU Status Register: stores the current CPU status.

Task-level task switching: You can directly switch from one task to another without switching the CPU status. OS _task_sw () not only saves the current task context, but also restores the new task context.

Process:OS _sched ()-> OS _schednew ()-> OS _task_sw ()

 

 

(4) interrupt-level Task Scheduling

 

 

Interruption-level task switching: After the interrupt processing is completed, you can use osintexit () to determine whether a higher priority ready task exists. If yes, call osintctxsw () to restore the new task context. Note: In interrupt handling, the context of the interrupted task has been saved. Therefore, only the context is restored here.

Process:Osintext ()-> Osintenter ()-> ISR-> Osintexit ()-> osintctxsw ()

(5) Lock and unlock the Scheduler

The uC/OS-II provides the scheduler lock feature, which does not allow task scheduling during the lock. The uC/OS-II uses the global variable oslocknesting to identify whether the task scheduler is locked.

  • OS _ext int8u oslocknesting;
  • Void osschedlock (void );
  • Void osschedunlock (void );
(6) interrupt management functions

During interrupt handling, task management, event management, and task scheduling are not allowed. The uC/OS-II identifies whether the current state is interrupted using the global variable osintnesting. All tasks and event management programs have statements for osintnesting judgment.

  • Void osintenter (void );
  • Void osintexit (void );

 

(7) interrupt Problems
  • OS _enter_critical ()
  • OS _exit_critical ()

Guanzhong disconnection enables uC/OS-II to avoid other tasks or service interruptions from entering critical code segments at the same time. When calling the uC/OS-II function, the interrupt should always be open.

  • How does uC/OS-II disable scheduling?
  • Can scheduling be performed during an interruption? Why?
  • UC/OS-II how to shield interruptions?
2. uC/OS-II system startup

UC/OS-II first calls osinit () for initialization, then creates a task (the system is not started yet, just allocates resources for it), then calls osstart () to start the system, give control of the CPU to the uC/OS-II, And the OS selects which task to start executing Based on the task priority, or creates a new task.

(1) initialization function osinit ()

Osinit () mainly completes initialization, including initializing global variables (in OS _initmisc (), task readiness table, TCB, ECB, FCB, memory unit, and message queue, and create idle tasks. Create a statistical task if necessary.

  • OS _initmisc ();//Initialize some global variables
  • OS _initrdylist ();//Initialization task readiness table
  • OS _inittcblist ();//Initialize idle TCBLinked List
  • OS _initeventlist ();//Initialize ECBLinked List
  • OS _flaginit ();//Initialize the event group flag Structure
  • OS _meminit ();//Initialize memory management
  • OS _qinit ();//Initialize Message Queue
  • OS _inittaskidle ();//Create idle task
  • OS _inittaskstat ();//Create a statistical task

 

 

The uC/OS-II initializes 5 empty Data Structure buffers, each of which is a one-way linked list that allows the uC/OS-II to quickly retrieve or release elements in a buffer from the buffer.

 

The variables and data structures after uC/OS-II calls osinit () are shown in:

 

(2) Start the osstart function ()

Osstart () starts uC/OS-II after all the tasks that are ready and need to be created first are created. It looks up the highest priority ready task from the ready table and resumes its context for execution.

Process: osstart ()-> OS _schednew ()-> osstarthighrdy ()

Q: When a task is called for the first time, which context is used for restoration? When creating a task, ostaskstkinit () is called to initialize the task stack. However, this function does not contain the context of the task?

The variables and data structures after uC/OS-II calls osstart () are shown in:

(3) statistical task ostaskstat

Ostaskstat is used to calculate the CPU usage. Set OS _task_stat_en in OS _cfg.h to 1, create a statistical task, and remain in the ready state after the system starts. At the beginning, the idle task runs for 1 s, providing a baseline value for calculating CPU utilization, and saving it in the stack of the statistical task. This value will not change unless the CPU is restarted. After that, the counters in the idle CPU will directly record the idle time of the CPU every time another task grabs the CPU.

3. uC/OS-II system clock

The clock hardware of any real-time system generates a hardware interrupt at intervals (a system tick). After the OS receives the interrupt, It updates the time counter and all program codes that depend on the clock, to maintain the orderly and stable operation of the system.

It is mainly included in the C source file OS _time.c.

  • # Define OS _ticks_per_sec 100 //System clock interruption Interval
  • OS _ext volatile int32u ostime ;//System running time value

  

  • Void ostimetick (void );//Clock interruption Service Program
  • Void ostimedly (int16u ticks );//Delay specified clock cycle
  • Int8u ostimedlyhmsm (...); //Delay length
  • Int8u ostimedlyresume (PRIO );//Recovery wait (latency/Blocking)Task
  • Int32u ostimeget (void );//Current read time
  • Void ostimeset (int32u ticks );//Set current time
4. uC/OS-II Event Management (1) Event Control Block
  • Int8u oseventtype ;//Event Type
  • Void * oseventptr ;//Point to mboxOr queue
  • Int16u oseventcnt ;//Semaphore counter (Note: mutex)
  • Int8u oseventgrp ;//Event wait group flag
  • Int8u oseventtbl []; //Time task wait table
  • Int8u oseventname []; //Time name

(2) ECB management mechanism

  • OS _ext OS _event * oseventfreelist ;//Idle ECBLinked List pointer
  • OS _ext OS _event oseventtbl []; // ECBStruct Array

(3) ECB management functions

  • OS _initeventlist (): Initialize ECB;
  • OS _eventwaitlistinit (): called when an event is created. initialize the ECB task wait table;
  • OS _eventtaskrdy (): called when an event occurs. Modify the TCB member variable of the highest-priority task in this event, add this task in the task readiness table, and pass the relevant information to the task TCB, delete the task from the event task representatives;
  • OS _eventtaskwait (): it is called when the current task is paused due to resource application failure. The task is deleted from the task readiness table and added to the task representatives of the event;
  • OS _eventto (): called when the event waits for timeout. This task is deleted from the event task representatives and the TCB member variable of the task is modified;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.