(2)μC/OS-II系统移植到LPC17xx单片机(简化版)
一、工具
1、IDE:MCUXpresso IDE v11.2.1
2、下载器:J-Link
3、芯片:LPC1768
二、准备移植
到μC/OS官网下载基于μC/OS-II系统的LPC1768工程,下载操作请参考另一篇文章:https://www.cnblogs.com/wenhao-Web/p/12662553.html
三、开始移植
1、准备一个基于LCP17xx的裸机工程,裸机工程的创建请参考另一篇文章:https://www.cnblogs.com/wenhao-Web/p/14269102.html
2、打开下载的例程,参照下载的例程,在个人工程中添加如下图所示的文件夹,并添加对应的文件。在添加文件的时候需要注意,在含有IAR、GNU和RealView三个文件夹的文件夹中,只选择GUN文件夹中的文件进行添加:
3、添加对应的头文件路径,如下图所示:
4、修改驱动文件
引用μC/OS中的两个中断函数,如下图所示:
修改中断向量表,如下图所示:
5、修改μC/OS配置文件os_cfg.h,用户可以根据个人所需进行配置:
/* ********************************************************************************************************* * uC/OS-II * The Real-Time Kernel * uC/OS-II Configuration File for V2.9x * * (c) Copyright 2005-2012, Micrium, Weston, FL * All Rights Reserved * * * File : OS_CFG.H * By : Jean J. Labrosse * Version : V2.92.08 * * LICENSING TERMS: * --------------- * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research. * If you plan on using uC/OS-II in a commercial product you need to contact Micrium to properly license * its use in your product. We provide ALL the source code for your convenience and to help you experience * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a * licensing fee. ********************************************************************************************************* */ #ifndef OS_CFG_H #define OS_CFG_H /* ---------------------- MISCELLANEOUS ----------------------- */ #define OS_APP_HOOKS_EN 1u /* 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 1u /* 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 1u /* Enable names for Sem, Mutex, Mbox and Q */ #define OS_LOWEST_PRIO 63u /* 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 */ #define OS_TLS_TBL_SIZE 5u /* Size of Thread-Local Storage Table */ /* --------------------- 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 1u /* Include code for OSTaskChangePrio() */ #define OS_TASK_CREATE_EN 1u /* Include code for OSTaskCreate() */ #define OS_TASK_CREATE_EXT_EN 1u /* 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 1u /* Include code for OSTaskSwHook() */ /* ----------------------- EVENT FLAGS ------------------------ */ #define OS_FLAG_EN 1u /* 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 1u /* 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 1u /* 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 1u /* 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 1u /* 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 1u /* 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
6、编写main.c文件中的程序,该文件主要是实现main函数的定义以及μC/OS系统初始化、启动和创建任务等操作:
/* =============================================================================== Name : main.c Author : $(author) Version : Copyright : $(copyright) Description : main definition =============================================================================== */ #ifdef __USE_CMSIS #include "LPC17xx.h" #endif #include <cr_section_macros.h> #include "includes.h" #include "bsp.h" /* 定义任务栈大小 */ #define APP_TASK_START_STK_SIZE 128 #define APP_TASK1_STK_SIZE 128 /* 定义任务优先级 */ #define APP_TASK_START_PRIO 4 #define APP_TASK1_PRIO 6 /* 定义任务栈 */ __BSS(RAM2) static OS_STK App_TaskStartStk[APP_TASK_START_STK_SIZE]; __BSS(RAM2) static OS_STK App_Task1Stk[APP_TASK1_STK_SIZE]; /* 函数声明 */ static void app_task_start(void *p_arg); static void app_task1(void *p_arg); /* 定义变量 */ OS_EVENT *operation_semp; /** * @brief 主函数 * @retval none * @author Mr.W * @date 2021年1月12日 */ int main(void) { bsp_init(); OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */ OSTaskCreate((void (*)(void *))app_task_start, /* Create the start task */ (void *)0, (OS_STK *)&App_TaskStartStk[APP_TASK_START_STK_SIZE - 1], (INT8U )APP_TASK_START_PRIO); OSStart(); /* Start multitasking (i.e. give control to uC/OS-II). */ while(1); return 0 ; } /** * @brief 起始任务 * @param none * @retval none * @author Mr.W * @date 2021年1月12日 */ static void app_task_start(void *p_arg) { /* 配置并开启系统时钟 */ SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC); /* 创建信号量,初始可用信号量为0 */ operation_semp = OSSemCreate(0); /* 创建任务 */ OSTaskCreate((void (*)(void *))app_task1, (void *)0, (OS_STK *)&App_Task1Stk[APP_TASK1_STK_SIZE - 1], (INT8U )APP_TASK1_PRIO); while(1) { OSTimeDly(100); /* 发送按键操作信号 */ OSSemPost(operation_semp); } } /** * @brief 任务1 * @param none * @retval none * @author Mr.W * @date 2021年1月12日 */ static void app_task1(void *p_arg) { INT8U err; static INT8U state = 0; while(1) { /* 等待操作信号 */ OSSemPend(operation_semp, 0, &err); if(err == OS_ERR_NONE) { if(state == 0) { state = 1; LPC_GPIO1->FIOSET |= (1 << 30); } else { state = 0; LPC_GPIO1->FIOCLR |= (1 << 30); } } } }
在定义任务栈大小的时候前面加了一个修饰“__BSS(RAM2)”,该修饰的意思是从RAM2中分配资源用于栈的大小(默认是从RAM1中分配资源),需要添加头文件“#include <cr_section_macros.h>”
说到这里有必要解释下原因:该编译器会将LPC1768的内存资源分两部分,一部分称RAM1(32Kb),一部分称RAM2(32Kb),默认情况下使用的是RAM1;如果一直默认设置的话可能会导致RAM资源不够用,所以就需要用户指定一些消耗内存的资源变量到RAM2中。
至于在使用该IDE中遇到的其它不懂的问题可以到IDE的安装路径找到相关的手册进行查找,如下图所示:
至此,μC/OS-II系统移植完成,编译一下,提示的错误也很容易解决,我这里也就不再赘述。
四、总结
相较于我之前的一篇移植文章的介绍,这次移植我去掉了很多基本用不到的东西,使整个工程更加简洁,如果有些功能你恰好用到而我这里没有,不妨再回头参考一下官方提供的例程。移植完成后一定要测试一下任务切换是否正常,系统时钟节拍是否准确。
#end