uc/os-II 原理及应用(八) 系统裁减以及移植到51单片机上

两个习题

  1. 先了解下CPU上函数调用的过程:

    • 一个程序取得函数地址,先保护现场将局部变量及参数压栈,再将调用函数的参数压栈,然后跳转到函数位置,将参数出栈,执行代码,结束后返回到调用位置,再怖复现场。
    • 在多任务系统中,如果多个任务并发的调用一个函数,可能会产生干扰,简单的说是线程不安全。
      如果一个函数被多个任务并发的调用而不产生干扰(线程安全),就是可重入函数。
  2. 各个任务用来存储各自任务堆栈内容的空间是任务堆栈;各个任务在运行时使用的堆栈是系统堆栈。

    在任务切换时,系统堆栈的内容要到重新保存到被中止任务的堆栈空间,要运行的任务的堆栈内存复制到系统堆栈上来。

    任务没有在运行时任务堆栈空间的内容只是备份,所以是那些内容是任务堆栈映像。

移植记录

先使用Keil4 建立一个工程 ucosii_c51,建立两组User与uCOS-II,在User中加入main.c,在uCOS-II中加入uCOS-II的源码


的uCOS-II的目录下新建 os_cfg.h,os_cpu.h,app_cfg.h 三个头文件,把os_cfg_r.h的内容先复制到os_cfg.h中来

然后修改os_cfg.h来对uCOS-II系统进行裁减,先做到最小配置

c 复制代码
#ifndef __OS_CFG_H__
#define __OS_CFG_H__


                                       /* ---------------------- MISCELLANEOUS ----------------------- */
#define OS_APP_HOOKS_EN           0u   /* Application-defined hooks are called from the uC/OS-II hooks */
#define OS_ARG_CHK_EN             0u   /* Enable (1) or Disable (0) argument checking                  */
#define OS_CPU_HOOKS_EN           0u   /* uC/OS-II hooks are found in the processor port files         */

#define OS_DEBUG_EN               0u   /* Enable(1) debug variables                                    */

#define OS_EVENT_MULTI_EN         1u   /* Include code for OSEventPendMulti()                          */
#define OS_EVENT_NAME_EN          0u   /* Enable names for Sem, Mutex, Mbox and Q                      */

#define OS_LOWEST_PRIO           20u   /* Defines the lowest priority that can be assigned ...         */
                                       /* ... MUST NEVER be higher than 254!                           */

#define OS_MAX_EVENTS            10u   /* Max. number of event control blocks in your application      */
#define OS_MAX_FLAGS              5u   /* Max. number of Event Flag Groups    in your application      */
#define OS_MAX_MEM_PART           5u   /* Max. number of memory partitions                             */
#define OS_MAX_QS                 4u   /* Max. number of queue control blocks in your application      */
#define OS_MAX_TASKS             20u   /* Max. number of tasks in your application, MUST be >= 2       */

#define OS_SCHED_LOCK_EN          1u   /* Include code for OSSchedLock() and OSSchedUnlock()           */

#define OS_TICK_STEP_EN           1u   /* Enable tick stepping feature for uC/OS-View                  */
#define OS_TICKS_PER_SEC        100u   /* Set the number of ticks in one second                        */


                                       /* --------------------- TASK STACK SIZE ---------------------- */
#define OS_TASK_TMR_STK_SIZE    128u   /* Timer      task stack size (# of OS_STK wide entries)        */
#define OS_TASK_STAT_STK_SIZE   128u   /* Statistics task stack size (# of OS_STK wide entries)        */
#define OS_TASK_IDLE_STK_SIZE   128u   /* Idle       task stack size (# of OS_STK wide entries)        */


                                       /* --------------------- TASK MANAGEMENT ---------------------- */
#define OS_TASK_CHANGE_PRIO_EN    0u   /*     Include code for OSTaskChangePrio()                      */
#define OS_TASK_CREATE_EN         1u   /*     Include code for OSTaskCreate()                          */
#define OS_TASK_CREATE_EXT_EN     0u   /*     Include code for OSTaskCreateExt()                       */
#define OS_TASK_DEL_EN            1u   /*     Include code for OSTaskDel()                             */
#define OS_TASK_NAME_EN           0u   /*     Enable task names                                        */
#define OS_TASK_PROFILE_EN        1u   /*     Include variables in OS_TCB for profiling                */
#define OS_TASK_QUERY_EN          1u   /*     Include code for OSTaskQuery()                           */
#define OS_TASK_REG_TBL_SIZE      1u   /*     Size of task variables array (#of INT32U entries)        */
#define OS_TASK_STAT_EN           0u   /*     Enable (1) or Disable(0) the statistics task             */
#define OS_TASK_STAT_STK_CHK_EN   1u   /*     Check task stacks from statistic task                    */
#define OS_TASK_SUSPEND_EN        1u   /*     Include code for OSTaskSuspend() and OSTaskResume()      */
#define OS_TASK_SW_HOOK_EN        0u   /*     Include code for OSTaskSwHook()                          */


                                       /* ----------------------- EVENT FLAGS ------------------------ */
#define OS_FLAG_EN                0u   /* Enable (1) or Disable (0) code generation for EVENT FLAGS    */
#define OS_FLAG_ACCEPT_EN         1u   /*     Include code for OSFlagAccept()                          */
#define OS_FLAG_DEL_EN            1u   /*     Include code for OSFlagDel()                             */
#define OS_FLAG_NAME_EN           1u   /*     Enable names for event flag group                        */
#define OS_FLAG_QUERY_EN          1u   /*     Include code for OSFlagQuery()                           */
#define OS_FLAG_WAIT_CLR_EN       1u   /* Include code for Wait on Clear EVENT FLAGS                   */
#define OS_FLAGS_NBITS           16u   /* Size in #bits of OS_FLAGS data type (8, 16 or 32)            */


                                       /* -------------------- MESSAGE MAILBOXES --------------------- */
#define OS_MBOX_EN                0u   /* Enable (1) or Disable (0) code generation for MAILBOXES      */
#define OS_MBOX_ACCEPT_EN         1u   /*     Include code for OSMboxAccept()                          */
#define OS_MBOX_DEL_EN            1u   /*     Include code for OSMboxDel()                             */
#define OS_MBOX_PEND_ABORT_EN     1u   /*     Include code for OSMboxPendAbort()                       */
#define OS_MBOX_POST_EN           1u   /*     Include code for OSMboxPost()                            */
#define OS_MBOX_POST_OPT_EN       1u   /*     Include code for OSMboxPostOpt()                         */
#define OS_MBOX_QUERY_EN          1u   /*     Include code for OSMboxQuery()                           */


                                       /* --------------------- MEMORY MANAGEMENT -------------------- */
#define OS_MEM_EN                 0u   /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */
#define OS_MEM_NAME_EN            1u   /*     Enable memory partition names                            */
#define OS_MEM_QUERY_EN           1u   /*     Include code for OSMemQuery()                            */


                                       /* ---------------- MUTUAL EXCLUSION SEMAPHORES --------------- */
#define OS_MUTEX_EN               0u   /* Enable (1) or Disable (0) code generation for MUTEX          */
#define OS_MUTEX_ACCEPT_EN        1u   /*     Include code for OSMutexAccept()                         */
#define OS_MUTEX_DEL_EN           1u   /*     Include code for OSMutexDel()                            */
#define OS_MUTEX_QUERY_EN         1u   /*     Include code for OSMutexQuery()                          */


                                       /* ---------------------- MESSAGE QUEUES ---------------------- */
#define OS_Q_EN                   0u   /* Enable (1) or Disable (0) code generation for QUEUES         */
#define OS_Q_ACCEPT_EN            1u   /*     Include code for OSQAccept()                             */
#define OS_Q_DEL_EN               1u   /*     Include code for OSQDel()                                */
#define OS_Q_FLUSH_EN             1u   /*     Include code for OSQFlush()                              */
#define OS_Q_PEND_ABORT_EN        1u   /*     Include code for OSQPendAbort()                          */
#define OS_Q_POST_EN              1u   /*     Include code for OSQPost()                               */
#define OS_Q_POST_FRONT_EN        1u   /*     Include code for OSQPostFront()                          */
#define OS_Q_POST_OPT_EN          1u   /*     Include code for OSQPostOpt()                            */
#define OS_Q_QUERY_EN             1u   /*     Include code for OSQQuery()                              */


                                       /* ------------------------ SEMAPHORES ------------------------ */
#define OS_SEM_EN                 0u   /* Enable (1) or Disable (0) code generation for SEMAPHORES     */
#define OS_SEM_ACCEPT_EN          1u   /*    Include code for OSSemAccept()                            */
#define OS_SEM_DEL_EN             1u   /*    Include code for OSSemDel()                               */
#define OS_SEM_PEND_ABORT_EN      1u   /*    Include code for OSSemPendAbort()                         */
#define OS_SEM_QUERY_EN           1u   /*    Include code for OSSemQuery()                             */
#define OS_SEM_SET_EN             1u   /*    Include code for OSSemSet()                               */


                                       /* --------------------- TIME MANAGEMENT ---------------------- */
#define OS_TIME_DLY_HMSM_EN       1u   /*     Include code for OSTimeDlyHMSM()                         */
#define OS_TIME_DLY_RESUME_EN     1u   /*     Include code for OSTimeDlyResume()                       */
#define OS_TIME_GET_SET_EN        1u   /*     Include code for OSTimeGet() and OSTimeSet()             */
#define OS_TIME_TICK_HOOK_EN      1u   /*     Include code for OSTimeTickHook()                        */


                                       /* --------------------- TIMER MANAGEMENT --------------------- */
#define OS_TMR_EN                 0u   /* Enable (1) or Disable (0) code generation for TIMERS         */
#define OS_TMR_CFG_MAX           16u   /*     Maximum number of timers                                 */
#define OS_TMR_CFG_NAME_EN        1u   /*     Determine timer names                                    */
#define OS_TMR_CFG_WHEEL_SIZE     8u   /*     Size of timer wheel (#Spokes)                            */
#define OS_TMR_CFG_TICKS_PER_SEC 10u   /*     Rate at which timer management task runs (Hz)            */

#endif
	 	   	  		 			 	    		   		 		 	 	 			 	    		   	 			 	  	 		 				 		  			 		 					 	  	  		      		  	   		      		  	 		 	      		   		 		  	 		 	      		  		  		  
 		 			 	    		   		 		 	 	 			 	    		   	 			 	  	 		 				 		  			 		 					 	  	  		      		  	   		      		  	 		 	      		   		 		  	 		 	      		  		  		  

在os_cpu.h 中定义一些必要的数据类型及CPU相关配置

c 复制代码
#ifndef __OS_CPU_H__
#define __OS_CPU_H__

#include <REGX52.H>


#define OS_STK_GROWTH	    1u				   // 栈增长方向

#define OS_CRITICAL_METHOD  0u				   // 不安全模式下

#define OS_ENTER_CRITICAL() EA=0			   // 关中断
#define OS_EXIT_CRITICAL()  EA=1			   // 开中断

#define OS_TASK_SW()        OSCtxSw()


typedef unsigned char     BOOLEAN;
typedef unsigned char     INT8U;
typedef signed char       INT8S;
typedef unsigned int      INT16U;
typedef signed int        INT16S;
typedef unsigned long int INT32U;
typedef signed long int   INT32S;
typedef float 			  FP32;
typedef double            FP64;

typedef INT8U             OS_STK;


#endif

添加一个源文件os_cpu_c.c

在里面实现任务堆栈初始化函数OSTaskStkInit

c 复制代码
#include <ucos_ii.h>

OS_STK *OSTaskStkInit(void (*task)(void *p_arg),
                      void *p_arg,
                      OS_STK *ptos,
                      INT16U opt)
{
	(void)(task);
	(void)(p_arg);
	(void)(opt);
    OS_STK* stk = ptos;
    *stk++ = 15;                 	// user stack length
	*stk++ = (INT16U)task & 0xff; 	// task ptr lower 8
	*stk++ = (INT16U)task >> 8;   	// task ptr high 8
	*stk++ = 0xD0;               	// PSW define at regx52.h
	*stk++ = 0xE0;               	// ACC define at regx52.h
	*stk++ = 0xF0;               	// B define at regx52.h
	*stk++ = 0x82;               	// DPL define at regx52.h
	*stk++ = 0x83;               	// DPH define at regx52.h
	*stk++ = (INT16U)p_arg & 0xff;  // arg ptr lower 8
	*stk++ = (INT16U)p_arg >> 8;    // arg ptr high 8
	*stk++ = (INT16U)ptos>>8;     // stack image high 8
	*stk++ = (INT16U)ptos&0xff;       // stack image lower 8
    return ptos;
}

添加一个源文件 os_cpu_asm.c

在里面加入三个函数定义

c 复制代码
void OSStartHighRdy(void) {}
void OSIntCtxSw(void) {}
void OSCtxSw(void) {}

然后点开options配置下面这样

然后translate

就生成了os_cpu_asm.SRC,改后缀为.s 就是汇编源文件,把os_cpu_asm.s 加入到工程来

asm 复制代码
; .\os_cpu_asm.SRC generated from: uCOS-II\Source\os_cpu_asm.c
; COMPILER INVOKED BY:
;        E:\Keil\C51\BIN\C51.EXE uCOS-II\Source\os_cpu_asm.c BROWSE INCDIR(.\uCOS-II\Source;.\User) DEBUG OBJECTEXTEND CODE PRINT(.\os_cpu_asm.lst) SRC(.\os_cpu_asm.SRC)


NAME	OS_CPU_ASM

?PR?OSStartHighRdy?OS_CPU_ASM            SEGMENT CODE 
?PR?OSIntCtxSw?OS_CPU_ASM                SEGMENT CODE 
?PR?OSCtxSw?OS_CPU_ASM                   SEGMENT CODE 
	PUBLIC	OSCtxSw
	PUBLIC	OSIntCtxSw
	PUBLIC	OSStartHighRdy
; void OSStartHighRdy(void) {}

	RSEG  ?PR?OSStartHighRdy?OS_CPU_ASM
OSStartHighRdy:
			; SOURCE LINE # 1
	RET  	
; END OF OSStartHighRdy

; void OSIntCtxSw(void) {}

	RSEG  ?PR?OSIntCtxSw?OS_CPU_ASM
OSIntCtxSw:
			; SOURCE LINE # 2
	RET  	
; END OF OSIntCtxSw

; void OSCtxSw(void) {}
	RSEG  ?PR?OSCtxSw?OS_CPU_ASM
OSCtxSw:
			; SOURCE LINE # 3
	RET  	
; END OF OSCtxSw

	END

现在编译一下,出现了 error C249: 'DATA': SEGMENT TOO LARGE,可以试着把编译内存模式改为大型


下期继续

相关推荐
魂兮-龙游13 小时前
二、模型训练与优化(4):模型优化-实操
嵌入式硬件·mcu·ai·性能优化·tensorflow·keras·neo4j
7yewh1 天前
自制红外热像仪(二) MLX90640移植 RP2040 STM32 ESP32
驱动开发·stm32·单片机·嵌入式硬件·mcu·计算机视觉
网易独家音乐人Mike Zhou2 天前
【TI毫米波雷达】DCA1000不使用mmWave Studio的数据采集方法,以及自动化实时数据采集
c语言·单片机·mcu·物联网·嵌入式·iot·毫米波雷达
洋葱鳞片3 天前
点亮一个esp32 的led
mcu·物联网·iot
WPG大大通3 天前
加速物联网HMI革命,基于TouchGFX的高效GUI显示方案
stm32·mcu·物联网·gui·工业·hmi·大大通
7yewh8 天前
LVGL 移植到 Arduino IDE(适用SP32 Arduino RP系列)
arm开发·驱动开发·嵌入式硬件·mcu·物联网·ui
MounRiver_Studio8 天前
基于VSCode软件框架的RISC-V IDE MRS2正式上线发布
ide·vscode·mcu·risc-v
supermodule8 天前
micropython framebuf库的blit函数使用详解
python·单片机·嵌入式硬件·mcu·物联网·iot
zhuohui3073176849 天前
FCM32F103C8T6开发指引
stm32·单片机·mcu