Interrupt management/clock management and memory management of μC/OS-II system

  1. Interrupt management

There is no separate C language file for interrupt handling in μC/OS-II. Because the writing of interrupt service routines is completely different for different hardware systems, the interrupt management function provided in the operating system is still in the kernel os core.c, if the running task does not close the interrupt, when the interrupt arrives, the operating system will The interrupt will be responded to and the interrupt service routine will be entered. At this time, the running environment of the task has not been saved, so the running environment of the task needs to be saved, and the task enters the suspended state due to the arrival of the interrupt.

Enter the interrupt service routine, first save the CPU environment of the currently running task, and then add 1 to an important global variable OSIntNesting used by the interrupt, indicating that the interrupt is nested one level deeper. The implementation of this function is the OSIntEnter function, and the program for this function to achieve this function is as follows:

It can be seen from the OSIntEnter code that the number of nesting levels of interrupts is required to be less than 255. During the initialization process of the operating system, OSIntNesting is initialized to 0. If there is no interrupt service routine running, obviously the value of OSIntNesting is still 0. service routine, so it should be increased by 1, otherwise, other running interrupt service routines will be suspended and this interrupt service routine will be run, that is, interrupt nesting occurs. If the interrupt service routine does not want to be interrupted, just execute the shutdown interrupt. Therefore, the operating system allows interrupt nesting.
According to the idea of ​​interrupt management, interrupt processing in uC/OS-11 should follow the flow shown in Figure 1.

Figure 1-uC/OS-II interrupt processing flow
Figure 1-uC/OS-II interrupt processing flow
  1. Clock management

The content of clock management is in the code os time.c, including the setting of the operating system time and obtaining the delay to the task. The main function of clock management is to delay tasks.
1) Time setting and acquisition
The setting and acquisition of time are all about the assignment of OSTime. The code is relatively simple. The program is as follows:

The time acquisition function OSTimeGet simply returns the value of OSTime. It should be noted that the operation of OSTime must use the critical section. The time setting function assigns the value of the parameter ticks to OSTime, and these two functions are not commonly used.
2) Task delay function OSTimeDly The task delay function OSTimeDly is used to block the task for a certain time, and this time is given in the form of parameters. If the value of this parameter is N, then after N time slices (clock ticks), the task can return to the ready state and get a chance to continue running. If the value of the parameter is 0, the task will not be blocked.
The simple process of the task delay function OSTimeDly is shown in Figure 2.

Figure 2 - Task delay function OSTimeDly process
Figure 2 – Task delay function OSTimeDly process
  1. Memory management of μC/OS-II

The memory management method in μC/OS-II belongs to the fixed-length partition management method.
In an embedded system, memory resources are very precious. If the memory partitioning method adopted is unreasonable, after a period of memory allocation and release, re-allocation and re-release, many scattered memory fragments will be generated. Space is hard to use. Therefore, how to solve the problem of fragmentation in the process of memory allocation is the key problem of memory management.

μC/OS-II uses the partition method to manage memory, that is, the continuous large block of memory is managed by partition. There are several such partitions in each system, and each partition contains several identical small memory blocks. In this way, when allocating memory, several memory blocks are obtained from different partitions as needed, and when releasing, these memory blocks are re-released back to their original partitions. In this way, the problem that the memory becomes more and more chaotic and there is no continuous partition of the whole block will not occur.

  1. Memory management data structures
    The core data structure in memory management is the memory control block. Each memory partition in the system has its own memory control block. The data structure of the memory control block is as follows:

OSMemAddr in the memory control block is the address of the memory partition. The free memory block of each partition is managed by a free memory block linked list. When there is a new partition request, the partition will be taken out from the free memory block. OSMemFreelist is the first address of the free memory block linked list of the partition. OSMemBlkSize is the size of each memory block, and the size of all memory blocks in the partition is the same. OSMemNBIks is the total number of memory blocks, OSMemNFree
is the total number of free partitions, that is, the number of memory blocks in the free memory block list. In addition, the memory block can be given a name through OSMemName.

2, memory control block initialization
If you want to use memory management in the operating system, you need to set the switch OS MEM EN in the OS-CFG H file to 1. so. When the operating system starts, the memory manager will be initialized, and the system will automatically call the OS Memlnit function to achieve this. The user application program is not allowed to call this function.

  1. Create a memory partition
    Memory partitions do not exist when the operating system is initialized. Before using a memory partition, a two-dimensional array must be defined, but this two-dimensional array has not yet become a memory partition. It becomes a memory partition by calling the function OSMemCreate and using a memory control block (MCB) to manage it.
    OSMemCreate returns a pointer to a memory control block for other operation functions of memory management to call.
  2. Memory partition acquisition

After the out-of-memory area is created, a linked list of free memory blocks is formed, and the linked list is managed by the memory control block. Then the operating system can apply for a memory partition, and the memory partition acquisition function is OSMemGet.

  1. Memory release

The release of the memory partition is to return the memory block to the free memory block list, which can also be understood as returning the memory partition. The memory partition release function is OSMemPut, which has two parameters, one is the address of the memory control block, and the other is the address of the memory block to be released. OSMemPut returns the message number of the operation procedure in integer type.

  1. Query the status of the memory partition

The operating system can use the OSMemQuery function to query the information of a specific memory partition to obtain information such as the memory block size, the number of available memory blocks, and the number of memory blocks in use in the memory partition. The obtained query information is stored in a variable whose data structure is the OSMEM DATA structure. The data structure of OS MEM DATA is as follows:

Read more: How to calculate the energy economy of pure electric vehicles