Uc/os-ii Embedded OS porting

Source: Internet
Author: User
Tags constant
The porting of the operating system means that the kernel of the real-time system can run on the microprocessor. The uc/os-ii should be ported to the CPU used before it can be applied. In the uc/os-ii transplant, the processor must meet some requirements: L processor C compiler can generate reentrant code L Processor support interrupt, and can produce a timing interrupt L in C can be on/off interrupt L processor can Supports a certain number of data storage hardware stacks l processors have instructions to read the stack pointer and other CPU registers, and store them in the stack or in memory. In fact, Uc/os-ii can simply be seen as a multi-tasking scheduler that adds a number of systems related to multitasking operating systems on this task scheduler. Services, such as semaphores, mailboxes, etc. Its 90% code is written in C and can be ported directly to a processor with a C language compiler. Migration is mainly focused on the implementation of multi-task switching, because this part of the code to save and restore the CPU field (that is, write/read related registers), not in C language, can only be done in assembly language, that is, writing OS_CPU_A.S files. There is also a need to modify the OS_CPU associated with ARM architecture. h files and user-defined task stack initialization structure of the OS_CPU_C.C file. The transplant of UC/OS-II is relatively easy, and the specific transplant process is as follows: includes. H includes. H is a master header file, which is independent of the porting process, but because every uc/os-ii C file needs it, here's a brief introduction. Includes. H contains the main three header files in the program, namely: OS_CPU. H,os_cfg. H,ucos_ii. H. It makes every C file in a project unnecessary to consider which header files it actually needs and enhances readability. Alternatively, you can re-edit this file to add a user-defined header file. os_cpu. H This header file includes the processor-related constants/macros and types defined by the # define statement, including the critical segment Code macro definition: Os_enter_critical () and os_exit_critical (), and provides three ways to switch interrupts. The specific code is as follows:   #include "arm.h"   #ifdef  os_cpu_globals #define OS_CPU_EXT #else #define os_cpu_ext  extern #endif   typedef unsigned char boolean;   /**/typedef unsigned char int8u; typedef signed char   int8s; typedef unsigned Short  INT16U; typedef signed     short int16s; typedef unsigned long  int32u; typedef signed long    int32s; typedef float          FP32; typedef double        FP64; typedef unsigned int   OS_STK; typedef unsigned int   OS_CPU_SR;   #define byte           int8s #define ubyte           int8u #define word        &Nbsp; int16s #define uword         int16u #define LONG           int32s #define ulong          INT32U   #define  OS_CRITICAL_METHOD    1 /**/#if       os_ Critical_method = = 1 #define  os_enter_critical ()  armdisableint1 ()   /* Disable interrupts*/                          #define  os_exit_critical ()    ARMEnableInt1 ()    /* Enable interrupts*/#endif #if       Os_critical_method = = 2 #define  OS_ENTER_CRITICAL ()  armdisableint2 ()   /* Disable interrupts*/                         #define  os_exit_critical ()    ARMEnableInt2 ()    /* Enable interrupts*/#endif #if        Os_critical_method = = 3 #define  os_enter_critical ()  armdisableint3 ()   /* Disable interrupts*/                         #define  os_exit_critical ()    ARMEnableInt3 ()     /* Enable interrupts*/#endif/*, */#define os_stk_growth    1 #define  OS_TASK_SW () &NB sp;      OSTASKSW() #ifdef semihosted #define OS_IDLE_STK_SIZE (64+semihosted_stack_needs) #else #define OS_IDLE_STK_SIZE 64 #endif/* */extern voidOSTASKSW(void); extern void osintctxsw (void); extern void ARMDisableInt1 (void); extern void ARMEnableInt1 (void); extern void ARMDisableInt2 (void); extern void ARMEnableInt2 (void); extern void ARMDisableInt3 (void); extern void ARMEnableInt3 (void); extern void Ostickisr (void); extern void sp_compensate (void);   Porting first includes a series of definitions of data types to accommodate the word lengths of different microprocessors. The UC/OS-II code does not use data types such as the C language Short,int, because these data types are processor-dependent and non-portable. The length of the data type defined in the program code should depend on the length of the data type specified by the processor C compiler. The above data type porting is based on the ads compiler's definition of the word length requirement for each type of C language. Where OS_STK is the task stack data type, it is 32 bits in arm, OS_CPU_SR is the CPU status register width, it is also 32 bits. Os_enter_critical () and os_exit_critical () are functions used to set the critical segment code. Os_enter_critical () causes the system to enter the critical segment, which requires the system core to shut down the interrupt, and then dispose of the critical section code, the Protection of critical section code is not affected by multi-tasking or interrupt Service sub-program damage. When the critical section is launched, the os_exit_critical () is called and the system restarts to interrupt for normal task scheduling. Usually the above two functions can be implemented in 3 ways, Os_cpu_h defines a Os_critical_method constant, which is used to select the method of function implementation. With a constant value of 1 o'clock, the simplest method of os_enter_critical () calls the processor directive to shut down interrupts, os_exit_critical () calls the corresponding processor instruction interrupt (detailed code is defined in the Os_cpu_a.s file). There is a problem with this: if the system disables interrupts when the function is called, then the system interrupt becomes allowed when the critical section code is executed. When the constant value is 2 o'clock, executing os_enter_critical () will first save the break state to the stack, then close the interrupt, and perform os_enter_critical () to restore the original from the stack when theThe state of the fault. This resolves the problem that exists in method 1. A constant value of 3 requires more than one CPU_SR local variable to be used for some compilers that can directly get the current processor state Word, save the status word, and use it to recover the PSW. Because the application is simple, the method is sufficient, so the specific code of Method 3 is not implemented in this article, and the Code implemented by method 3 and Method 2 is consistent. This can be expanded later. The Os_stk_growth constant defines the direction of the stack, with a value of 0 indicating that the stack is incremented from the bottom up, and 1 indicates that the stack is descending from the top down. The stack structure in this migration is decremented from the top down, so the macro value is defined as 1. OS_TASK_SW () is a macro that is used when uc/os-ii switches from a low priority to a high-priority task, defined here as a task dispatch functionOSTASKSW() to work on switching from a low-priority task to a high-priority task. OS_CPU_C.C This file requires the user to write 10 simple functions, where the necessary function is Ostaskstkinit (), and the other functions are declared only. The code is as follows: Os_stk *ostaskstkinit (void (*task) (void *pd), void *pdata, Os_stk *ptos, int16u opt) {    unsigned int *STK;     opt = opt;       /* */    STK = (unsigned int *) PTOs;    /**/    /* */    *--STK = (unsigned int) task;              /* LR */    *--STK = 0;                          /* R12 */    *--STK = 0;                         /* R11 */     *--STK = 0;                         /* R10 */    *--STK = 0;                          /* R9 * *      *--STK = 0;                         /* R8 */    *--STK = 0;                          /* R7 */ 

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.