STM32平台移植uCOSIII

STM32平台uCOSIII移植 使用的软件及硬件平台:

硬件:德飞莱STM32开发板M3S(STM32F103ZE) 软件:keil uVision4 固件库:STM32F10x_StdPeriph_Lib_V3.5.0 uCOSIII版本:Release V3.02.00 (2011/08/01)

建立工作目录 建立目录命名为uCOS,在uCOS目录下建立目录用于存放源文件、中间文件、工程文件及二进制文件。如下:
STM32平台移植uCOSIII
文章图片

在uCOS-III目录下建立如下目录
STM32平台移植uCOSIII
文章图片

工作目录创建完成后就可以将固件库及uCOS中文件复制到相应目录下,具体操作如下
1. 将\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver下的inc目录及src目录复制到FWLib目录下。
2. 将STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport目录下core_cm3.c和core_cm3.h复制到FWLib目录下。
3. 将STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x \startup\arm目录下starup_stm32f10x_hd.s复制到FWLib目录下
4. 将STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x下的.h及.c文件复制到FWLib目录下。
5. 将uCOS-III\Software\EvalBoards\Micrium\uC-Eval-STM32F107\IAR\BSP下的bsp.c及bsp.h复制到BSP目录下。同时在BSP\LED目录下建立led.c及led.h文件。
6. 将uCOS-III\Software\EvalBoards\Micrium\uC-Eval-STM32F107\IAR\uCOS-III-Ex1下的app.c、app_cfg.h复制到APP目录下同时建立app.h文件
7. 将uCOS-III\Software\uCOS-III\Cfg\Template目录下的文件复制到uCOS-III\Cfg目录下
8. uCOS-III\Software\uCOS-III\Ports\ARM-Cortex-M3\Generic\IAR目录下的文件复制到uCOS-III\Ports目录下。
9. 将uCOS-III\Software\uCOS-III\Source目录下文件复制到uCOS-III\Source目录下,同时将Software\EvalBoards\Micrium\ uC-Eval-STM32F107\IAR\uCOS-III-Ex1目录下的os_type.h复制到uCOS-III\Source目录下。
10. 将uCOS-III\Software\uC-CPU目录及下层目录下的所有.c及.h文件复制到uCOS-III\uC-CPU目录下
11. 将uCOS-III\Software\uC-LIB目录及下层目录下所有.c、.h及.asm文件复制到uCOS-III\uC-LIB目录下
12. 最后将STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\SysTick\ TimeBase目录下stm32f10x_conf.h、stm32f10x_it.c及ftm32f10x_it.h复制到BSP目录下。
13. 在User目录下建立main.c文件.
在keil中建立工程 打开keil软件,选择Project/new uvision project,将工程随意命名并放置在uCOSIII\Project目录下,保存之后选择器件,选择STMicroelectronics下的STM32F103ZE芯片。选择OK,在弹出的窗口选择否。不使用keil提供的文件。
选择manage components,建立相关工程目录如下图所示
STM32平台移植uCOSIII
文章图片

uC-LIB目录按需添加。
之后添加文件到工程中,添加完成后如下图
STM32平台移植uCOSIII
文章图片

添加完成后选择target option进行相应设置,如下
output->Create HEX File
c/c++->Define:STM32F10X_HD,USE_STDPERIPH_DRIVER
同时设置包含路径
STM32平台移植uCOSIII
文章图片

修改文件 修改uCOSIII\Ports下os_cpu_a.asm文件
STM32平台移植uCOSIII
文章图片

STM32平台移植uCOSIII
文章图片

【STM32平台移植uCOSIII】修改UCOSIII\uC-CPU下的cpu_a.asm文件
STM32平台移植uCOSIII
文章图片

同时去掉该文件函数名后的”:”
修改FWLib下的startup_stm32f10x_hd.s文件
将文件中的PendSV Handler改为OS_CPU_PendSVHandler
以上修改完成后进行内核裁剪,修改UCOSIII\Cfg下的os_cfg.h文件,按需禁用,这次只保留基础功能
/* ---------------------------- MISCELLANEOUS -------------------------- */ #define OS_CFG_APP_HOOKS_EN0u/* Enable (1) or Disable (0) application specific hooks*/ #define OS_CFG_ARG_CHK_EN1u/* Enable (1) or Disable (0) argument checking*/ #define OS_CFG_CALLED_FROM_ISR_CHK_EN1u/* Enable (1) or Disable (0) check for called from ISR*/ #define OS_CFG_DBG_EN1u/* Enable (1) debug code/variables*/ #define OS_CFG_ISR_POST_DEFERRED_EN1u/* Enable (1) or Disable (0) Deferred ISR posts*/ #define OS_CFG_OBJ_TYPE_CHK_EN0u/* Enable (1) or Disable (0) object type checking*/ #define OS_CFG_TS_EN0u/* Enable (1) or Disable (0) time stamping*/#define OS_CFG_PEND_MULTI_EN1u/* Enable (1) or Disable (0) code generation for multi-pend feature*/#define OS_CFG_PRIO_MAX64u/* Defines the maximum number of task priorities (see OS_PRIO data type) */#define OS_CFG_SCHED_LOCK_TIME_MEAS_EN0u/* Include code to measure scheduler lock time*/ #define OS_CFG_SCHED_ROUND_ROBIN_EN1u/* Include code for Round-Robin scheduling*/ #define OS_CFG_STK_SIZE_MIN64u/* Minimum allowable task stack size*//* ----------------------------- EVENT FLAGS --------------------------- */ #define OS_CFG_FLAG_EN0u/* Enable (1) or Disable (0) code generation for EVENT FLAGS*/ #define OS_CFG_FLAG_DEL_EN1u/*Include code for OSFlagDel()*/ #define OS_CFG_FLAG_MODE_CLR_EN1u/*Include code for Wait on Clear EVENT FLAGS*/ #define OS_CFG_FLAG_PEND_ABORT_EN1u/*Include code for OSFlagPendAbort()*//* -------------------------- MEMORY MANAGEMENT ------------------------ */ #define OS_CFG_MEM_EN1u/* Enable (1) or Disable (0) code generation for MEMORY MANAGER*//* --------------------- MUTUAL EXCLUSION SEMAPHORES ------------------- */ #define OS_CFG_MUTEX_EN0u/* Enable (1) or Disable (0) code generation for MUTEX*/ #define OS_CFG_MUTEX_DEL_EN1u/*Include code for OSMutexDel()*/ #define OS_CFG_MUTEX_PEND_ABORT_EN1u/*Include code for OSMutexPendAbort()*//* --------------------------- MESSAGE QUEUES -------------------------- */ #define OS_CFG_Q_EN0u/* Enable (1) or Disable (0) code generation for QUEUES*/ #define OS_CFG_Q_DEL_EN1u/*Include code for OSQDel()*/ #define OS_CFG_Q_FLUSH_EN1u/*Include code for OSQFlush()*/ #define OS_CFG_Q_PEND_ABORT_EN1u/*Include code for OSQPendAbort()*//* ----------------------------- SEMAPHORES ---------------------------- */ #define OS_CFG_SEM_EN0u/* Enable (1) or Disable (0) code generation for SEMAPHORES*/ #define OS_CFG_SEM_DEL_EN1u/*Include code for OSSemDel()*/ #define OS_CFG_SEM_PEND_ABORT_EN1u/*Include code for OSSemPendAbort()*/ #define OS_CFG_SEM_SET_EN1u/*Include code for OSSemSet()*//* -------------------------- TASK MANAGEMENT -------------------------- */ #define OS_CFG_STAT_TASK_EN1u/* Enable (1) or Disable(0) the statistics task*/ #define OS_CFG_STAT_TASK_STK_CHK_EN1u/* Check task stacks from statistic task*/#define OS_CFG_TASK_CHANGE_PRIO_EN0u/* Include code for OSTaskChangePrio()*/ #define OS_CFG_TASK_DEL_EN1u/* Include code for OSTaskDel()*/ #define OS_CFG_TASK_Q_EN0u/* Include code for OSTaskQXXXX()*/ #define OS_CFG_TASK_Q_PEND_ABORT_EN1u/* Include code for OSTaskQPendAbort()*/ #define OS_CFG_TASK_PROFILE_EN0u/* Include variables in OS_TCB for profiling*/ #define OS_CFG_TASK_REG_TBL_SIZE1u/* Number of task specific registers*/ #define OS_CFG_TASK_SEM_PEND_ABORT_EN0u/* Include code for OSTaskSemPendAbort()*/ #define OS_CFG_TASK_SUSPEND_EN1u/* Include code for OSTaskSuspend() and OSTaskResume()*//* -------------------------- TIME MANAGEMENT -------------------------- */ #define OS_CFG_TIME_DLY_HMSM_EN1u/*Include code for OSTimeDlyHMSM()*/ #define OS_CFG_TIME_DLY_RESUME_EN1u/*Include code for OSTimeDlyResume()*//* ------------------------- TIMER MANAGEMENT -------------------------- */ #define OS_CFG_TMR_EN0u/* Enable (1) or Disable (0) code generation for TIMERS*/ #define OS_CFG_TMR_DEL_EN1u/* Enable (1) or Disable (0) code generation for OSTmrDel()*/

修改os_cpu_c.c文件注释或删除void OS_CPU_SysTickHandler(void)和void OS_CPU_SysTickInit(CPU_INT32U cnts)函数,同时将os_cpu_c.c中这两个函数的声明注释或删除。
在stm32f10x_it.c中添加
#include "os.h"

并将SysTick_Handler函数修改如下
void SysTick_Handler(void) { OSIntEnter(); OSTimeTick(); OSIntExit(); }

LED测试 修改工程中文件
main.c
#include "os.h" #include "app.h"static OS_TCB TASK_START_TCB; static CPU_STK Task_Start_Stk[TASK_START_STK_SIZE]; int main(void) { OS_ERR err; OSInit(&err); OSTaskCreate((OS_TCB *) &TASK_START_TCB, (CPU_CHAR *) "task", (OS_TASK_PTR) Task_Start, (void *)0, (OS_PRIO)TASK_START_PRIO, (CPU_STK *) &Task_Start_Stk, (CPU_STK_SIZE)TASK_START_STK_SIZE/10, (CPU_STK_SIZE)TASK_START_STK_SIZE, (OS_MSG_QTY) 0, (OS_TICK) 0, (void *) 0, (OS_OPT)(OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR), (OS_ERR *) &err); OSStart(&err); }

app.c
#include "os.h" #include "app.h" #include "bsp.h"static OS_TCB LED_TASK_TCB; static CPU_STK Led_Task_Stk[LED_TASK_STK_SIZE]; void Task_Start(void *p_arg) { OS_ERR err; (void)p_arg; BSP_Init(); Create_Task(); while (1) { OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); }}void Create_Task(void) { OS_ERR err; OSTaskCreate((OS_TCB *) &LED_TASK_TCB, (CPU_CHAR *) "Led", (OS_TASK_PTR) Led_Task, (void *)0, (OS_PRIO)LED_TASK_PRIO, (CPU_STK *) &Led_Task_Stk, (CPU_STK_SIZE)LED_TASK_STK_SIZE/10, (CPU_STK_SIZE)LED_TASK_STK_SIZE, (OS_MSG_QTY) 0, (OS_TICK) 0, (void *) 0, (OS_OPT)(OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR), (OS_ERR *) &err); }void Led_Task(void *p_arg) { OS_ERR err; (void)p_arg; while(1) { LED2_ON; LED3_OFF; OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); LED2_OFF; LED3_ON; OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); } }

app.h
#ifndef__APP_H_ #define__APP_H_#include "app_cfg.h"void Task_Start(void *p_arg); void Create_Task(void); void Led_Task(void *p_arg); #endif

app_cfg.h
#ifndef__APP_CFG_H__ #define__APP_CFG_H__/* ************************************************************************************* *TASK PRIORITIES ************************************************************************************* */#defineTASK_START_PRIO2 #defineLED_TASK_PRIO3/* ************************************************************************************* *TASK STACK SIZES ************************************************************************************* */#defineTASK_START_STK_SIZE128 #defineLED_TASK_STK_SIZE128/* ************************************************************************************* *uC/LIB CONFIGURATION ************************************************************************************* *///#include #endif

bsp.c
#include voidBSP_Init (void) { SysTick_Config(75000); LED_Init(); }void LED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOE, &GPIO_InitStructure); }

bsp.h
#ifndef _BSP_H_ #define _BSP_H_#include "stm32f10x.h"#define LED3_ONGPIO_SetBits(GPIOB,GPIO_Pin_5); #define LED3_OFFGPIO_ResetBits(GPIOB,GPIO_Pin_5); #define LED2_ONGPIO_SetBits(GPIOE,GPIO_Pin_5); #define LED2_OFFGPIO_ResetBits(GPIOE,GPIO_Pin_5); voidLED_Init(void); voidBSP_Init(void); #endif

完成之后编译下载,程序执行效果为两LED灯交替闪烁。

    推荐阅读