UC/OS II study notes

It is a ROM-based, cuttable, preemptive, real-time, multitasking OS kernel;

The deprivable real-time kernel runs the highest priority tasks that are ready at any time.

A task, also called a thread, is a simple program. The program can be considered that the CPU belongs to the program.

The OS includes basic functions such as task scheduling, task management, time management, memory management, and communication and synchronization between tasks. No additional services such as input and output management, file system, network, etc. are provided. However, due to the good scalability and open source code of uC/OS-II, these non-essential functions can be implemented separately by users according to their needs.

When the task is executed (time slice is up), he saves all the contents of his CPU register to his own stack, and at the same time gives up the CPU to other tasks, then the task that has the right to use the CPU will take its own CPU register from its own. The stack is placed in the real CPU register and started to run, and so on again and again.

Task management: priority and stack when creating tasks

Time management: realized by timing interrupt

Memory management: Give a first address, block size, and block number. Link the continuous address array into a list.
Get: Specify a memory control block, return the address of the first memory block of the linked list, and move the pointer of the linked list to the next free memory block;
Release: Specify a memory control block and the first address of the memory block to be released. This memory block stores the address of the first free memory block in the linked list, and assigns itself as the first free memory block.

Mailbox: Each task encapsulates its own data and sends it to other tasks in the form of a mailbox, so as to achieve the purpose of communication between tasks. A pointer variable, usually the pointer points to a specific data structure that contains the "message".


1. µC/OS-II is multitasking, and a stack is opened for each task

2. µC/OS-II requires users to call OSInit() [L1.5(2)] before using any service. It will create two tasks:
idle tasks and statistical tasks, the former runs when no other tasks are in the ready state; the latter calculates the CPU utilization.

3. The user must create at least one task before starting the multitasking OSStart()

4. To use the stack check operation, OSTaskCreateExt() must be used to create a task

5. The OSMemCreate() function creates and initializes a memory area. The memory area can be created using a static array or using the malloc() function during initialization.
Insert picture description here

msg = OSMemGet(CommMem, &err);
if (msg != (INT8U *)0) {
    
    
        ./* 内存块已经分配 */
}

6、

typedef struct {
    
                           /* MEMORY CONTROL BLOCK                                         */
    void   *OSMemAddr;                 /* Pointer to beginning of memory partition                     */
    void   *OSMemFreeList;             /* Pointer to list of free memory blocks                        */
    INT32U  OSMemBlkSize;              /* Size (in bytes) of each block of memory                      */
    INT32U  OSMemNBlks;                /* Total number of blocks in this partition                     */
    INT32U  OSMemNFree;                /* Number of memory blocks remaining in this partition          */
} OS_MEM;
#define  OS_EVENT_TYPE_UNUSED      0
#define  OS_EVENT_TYPE_MBOX        1
#define  OS_EVENT_TYPE_Q           2
#define  OS_EVENT_TYPE_SEM         3
#define  OS_EVENT_TYPE_MUTEX       4
#define  OS_EVENT_TYPE_FLAG        5

typedef struct {
    
    
    INT8U   OSEventType;                   /* Type of event control block (see OS_EVENT_TYPE_???)      */
    INT8U   OSEventGrp;                    /* Group corresponding to tasks waiting for event to occur  */
    INT16U  OSEventCnt;                    /* Semaphore Count (not used if other EVENT type)           */
    void   *OSEventPtr;                    /* Pointer to message or queue structure                    */
    INT8U   OSEventTbl[OS_EVENT_TBL_SIZE]; /* List of tasks waiting for event to occur                 */
} OS_EVENT;

7. The task that calls the OSSchedLock() function monopolizes the CPU, regardless of whether there are other high-priority ready tasks. In this case, the interrupt can still be accepted and executed (interrupts must be allowed). OSSchedLock() function and OSSchedUnlock() function must be used in pairs.
After the task calls the OSSchedLock() function, it must not call any system functions that may cause the current task to hang: OSTimeDly(), OSTimeDlyHMSM(), OSSemPend(), OSMboxPend(), OSQPend(). Because task scheduling has been disabled, other tasks cannot run, which will cause system deadlock.

8. The OSSemCreate() function creates and initializes a semaphore. The function of the semaphore is as follows: to
allow a task to synchronize with other tasks or interrupts .
Obtain the right to use the equipment to
mark the occurrence of the event

OS_EVENT *DispSem;
void main(void)
{
    
    
.
OSInit(); /* 初始化μC/OS-Ⅱ */
.
DispSem = OSSemCreate(1); /* 建立显示设备的信号量 */
.
OSStart(); /* 启动多任务内核 */
}

Define global variables-initialization-post-pend-flush

CAN2_GLOBAL	  OS_EVENT	  *CAN2Receive;  

CAN2Receive 			= OSQCreate(pCAN2RevBuf,USE_CAN2_RX_BUF_SIZE);       //CAN2接收消息队列  

stcRxBUF msg;
msg = RxBUF(j);
CAN2Rx_OSQPost(CAN2Receive,&msg);

mg=OSQPend(CAN2Receive,0,&err); 
curRxBuf   = *(stcRxBUF *) mg;

OSQFlush(CAN2Receive);

9. CPU usage

void FirstAndOnlyTask (void *pdata)
{
    
    
.
OSStatInit(); /* 计算CPU使用率 */
.
OSTaskCreate(); /* 建立其他任务 */
OSTaskCreate();
.
for (;;) {
    
    
    }
}

10、

INT8U OSTaskChangePrio (INT8U oldprio, INT8U newprio);

11. OSTaskCreate() creates a new task. The task can be established before the multitasking environment is started, or it can be established in the running task. No task can be created in the interrupt handler. A task must be an infinite loop structure and cannot have a return point.

12. Regardless of whether an interrupt is generated in the user program or not, when initializing the task stack, the stack structure must be the same as the sequence structure of the register stack after the CPU interrupt.

13.
In the task, one of the following processes provided by μC/OS must be called: delay waiting, task suspension, waiting for event occurrence (waiting for semaphore, message mailbox, message queue), so that other tasks can get the CPU.

Guess you like

Origin blog.csdn.net/LIU944602965/article/details/112526294