当前位置:   article > 正文

UCOSII学习笔记-1.ucosii移植_下载一个ucos-ii在c51上移植的源码,里面会有的,这个头文件是includes.h

下载一个ucos-ii在c51上移植的源码,里面会有的,这个头文件是includes.h

基本信息

	硬件平台:stm32f103
	软件环境:MDK5
	
	移植ucosii花了我很多时间,首先看了正点哥的视频,看不太懂。接下来又看野火哥,野火哥用了一堆从
	自己例程中复制过来的代码,看的有点蒙。接下来我就去看了cortex-m3中文权威指南,大概了解了m3的
	通用寄存器、特殊寄存器、中断原理、滴答定时器的内容(cosii的函数都是基于这部分内容编写的,比如
	临界区的概念建议要了解下),然后我就开始看cosii系统要编写的几个函数,最后还是回到原子哥的视频
	着手移植。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

STEP1 准备一个没有错误LED例程

为保证移植后编译错误只与ucosii有关,一定要保证移植前的程序无误。
在这里插入图片描述
在这里插入图片描述
以上用的例程来自原子哥的库函数版本跑马灯实验

1.在目录中创建文件夹UCOSII、UCOSII/CORE、UCOSII/PORT、UCOSII/CONFIG。

	CORE用来存UCOSII源码
	PORT存接口文件,移植于不同芯片,裁剪ucosii系统需要进行修改
	CONFIG存配置文件
  • 1
  • 2
  • 3

STEP2 下载UCOSII源码

官方链接:(stm32fi03)

https://www.micrium.com/download/micrium_stm32f103-sk_ucos-ii/
  • 1

下载得到文件目录如下
在这里插入图片描述
主要用到software\ucosII中的文件
在这里插入图片描述

UCOSII移植

1.添加文件到自己的工程文件中

  • PORT

\Ports\arm-cortex-m3\Generic\IAR
在这里插入图片描述
复制的我的工程目录UCOSII/PORT

  • CORE

\Source下文件复制到UCOSII/CORE
在这里插入图片描述

  • CONFIG
    复制Software\EvalBoards\ST\STM32F103ZE-SK\IAR\OS-Probe-LCD的os_cfg.h到UCOSII/CONFIG

2.文件加载到工程

添加文件与添加头文件路径最终结果:
在这里插入图片描述
在这里插入图片描述

3.修改文件

1.修改includes.h,这是ucos源码中通用的头文件,由于我复制的是其他工程项目的头文件,有些文件我并没有,所以改为以下代码:

>#ifndef __INCLUDES_H__
	#define __INCLUDES_H__	
//以上防重定义
//ucosii所用到的头文件
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include "usart.h"
#include "ucos_ii.h"
#include "os_cpu.h"
#include "os_cfg.h"
#include <stm32f10x.h>	    
#include "sys.h"
//main例程要用的头文件
#include "delay.h"
#include "led.h"
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2.修改os_cfg.h,这是用来配置和裁剪内核的,也是跟上面一样,摘抄原子哥的例程,这里很多都不懂的,先跑通一次,成功看到效果再来就这正确的模板深入学习

#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           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         0u   /* Include code for OSEventPendMulti()                          */
#define OS_EVENT_NAME_EN          0u   /* 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           0u   /* Max. number of memory partitions                             */
#define OS_MAX_QS                 5u   /* Max. number of queue control blocks in your application      */
#define OS_MAX_TASKS             10u   /* 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       	200u   /* 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    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           1u   /*     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           1u   /*     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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

3.打开sys.h,打开ucosii:
在这里插入图片描述
4.注释掉stm32f10x_it.c中与os_cpu.asm重了的两个函数
在这里插入图片描述

5.编译,应该能通过了,

至此,ucosii的移植就完成了,接下来就是软件设计部分

软件设计

#include <includes.h>
//****START任务*****
//设置任务优先级
#define START_TASK_PRIO 			10		//开始任务优先级为最低
//设置任务堆栈大小
#define START_STK_SIZE 				128
//任务堆栈
OS_STK START_TASK_STK[START_STK_SIZE];
//任务函数
void start_task(void *pdata);

//******LED0任务*****

#define LED0_TASK_PRIO				7
#define LED0_STK_SIZE 				64
OS_STK 	LED0_TASK_STK[LED0_STK_SIZE];
void led0_task(void *pdata);

//****LED1任务*****

#define LED1_TASK_PRIO				6
#define LED1_STK_SIZE 				128
OS_STK 	LED1_TASK_STK[LED1_STK_SIZE];
void led1_task(void *pdata);

//*****浮点测试任务****

#define FLOAT_TASK_PRIO				5
#define FLOAT_STK_SIZE 				128
__align(8)OS_STK 	FLOAT_TASK_STK[FLOAT_STK_SIZE];
void float_task(void *pdata);

int main(void ){
	delay_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	uart_init(115200);
	LED_Init();
	
	OSInit();
	OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],\
		START_TASK_PRIO);
	OSStart();
	return 0;
}

void start_task(void*pdata){
	OS_CPU_SR cpu_sr = 0;
	pdata=pdata;
	OSStatInit();	//开启统计任务
	
	OS_ENTER_CRITICAL();//进入临界区
	//创建任务
	OSTaskCreate(led0_task,(void*)0,(OS_STK*)&LED0_TASK_STK[LED0_STK_SIZE-1],\
		LED0_TASK_PRIO);
	OSTaskCreate(led1_task,(void*)0,(OS_STK*)&LED1_TASK_STK[LED0_STK_SIZE-1],\
		LED1_TASK_PRIO);
	OSTaskCreate(float_task,(void*)0,(OS_STK*)&FLOAT_TASK_STK[LED0_STK_SIZE-1],\
		FLOAT_TASK_PRIO);
	
	OSTaskSuspend(START_TASK_PRIO);//挂起开始任务
	OS_EXIT_CRITICAL();	//退出临界区
}

void led0_task(void *pdata){
	while(1){
		LED0 = 0;
		delay_ms(80);
		LED0 = 1;
		delay_ms(400);
	}
}

void led1_task(void *pdata){
	while(1){
		LED1 = 0;
		delay_ms(300);
		LED1 = 1;
		delay_ms(300);
	}
}

void float_task(void *pdata){
	OS_CPU_SR cpu_sr = 0;
	static float float_num = 0.01;
	while(1){
		float_num += 0.01f;
		OS_ENTER_CRITICAL();
		printf("float_num 的值为:%4f\r\n",float_num);
		OS_EXIT_CRITICAL();
		delay_ms(500);
	}


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

编译下载

LED任务直接可视
浮点测试则在串口调试助手中可以看

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/654976
推荐阅读
相关标签
  

闽ICP备14008679号