stm32f4移植ucosii

前些时候把ucosii移植到stm32f4上,记录一下过程。
移植参考了《STM32F407全套资料》
移植实现的功能:在ucosii系统下驱动lcd!


具体的原理,可以去看看  Ucos中文书-邵贝贝、任哲-嵌入式实时操作系统μCOS-II原理及应用


首先去这个网站下载源码
http://micrium.com/downloadcenter/


解压源码包,移植所需要的源码在以下几个文件夹下:
..\Software\uCOS-II\Ports\ARM-Cortex-M4\Generic\RealView
os_cpu.h
os_cpu_a.asm
os_cpu_c.c
os_dbg.c



..\Software\uCOS-II\Source
os_core.c
os_task.c   
os_flag.c 
os_mbox.c 
os_mutex.c 
os_q.c 
os_sem.c   
os_mem.c   
os_time.c   
os_tmr.c 
ucos_ii.c
ucos_ii.h


..\Software\EvalBoards\ST\STM3240G-EVAL\uCOS-II
os_cfg.h
includes.h
app_cfg.h


其中,需要使用者修改或者编写的文件如下:
bsp.c(介于硬件和操作系统中驱动层程序之间的一层)
bsp.h
main.c
main.h
stm32f4xxit.c (配置时钟)
startup_stm32f4xx.s(至于启动文件,后边会提到)
os_cfg.h
includes.h
app_cfg.h

下面的文件有些教程会提到怎么修改,但我下载的源码是已经修改好,且合适M4使用就不做修改了!
或者参考《STM32F407全套资料》中的ucos的工程源码。
os_cpu.h
os_cpu_a.asm
os_cpu_c.c
app_cfg.h
os_cfg.h



工程结构图:

stm32f4移植ucosii_第1张图片stm32f4移植ucosii_第2张图片

board文件夹中的文件是之前做的lcd的驱动文件,直接拿来用就可以了,不需要修改!


发下修改的源码:

main.c

#include 

static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
static OS_STK AppTaskUserIFStk[APP_TASK_USER_IF_STK_SIZE];
int ExTick;

static void AppTaskCreate(void);
static void AppTaskStart(void *p_arg);
static void AppTaskUserIF(void *p_arg);
u32 sd_Capacity;
volatile  INT32U  SoftTimer;
		 
int  main (void)
{
    INT8U  err;	 
	NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    OSInit();//初始化uC/OSII,实时内核*/
   //建立开始任务
	OSTaskCreateExt(AppTaskStart,
									(void *)0,
									(OS_STK *)&AppTaskStartStk[APP_TASK_START_STK_SIZE-1],
									APP_TASK_START_PRIO,
									APP_TASK_START_PRIO,
									(OS_STK *)&AppTaskStartStk[0],
									APP_TASK_START_STK_SIZE,
									(void *)0,
									OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);

	#if (OS_TASK_NAME_SIZE > 13)
    OSTaskNameSet(APP_TASK_START_PRIO, "STask", &err);
	#endif
    OSStart();//开始多任务                              
}

static  void  AppTaskStart (void *p_arg)
{
  	(void)p_arg;
   	BSP_Init();//初始化BSP函数
   	#if(OS_TASK_STAT_EN > 0)
    OSStatInit();
   	#endif
		AppTaskCreate();
   	while(DEF_TRUE){  
	   	OSTaskSuspend(OS_PRIO_SELF);
    }
}												 

static  void  AppTaskCreate(void)
{
  	INT8U  err;
  	OSTaskCreateExt(AppTaskUserIF,
										(void *)0,
										(OS_STK *)&AppTaskUserIFStk[APP_TASK_USER_IF_STK_SIZE-1],
										APP_TASK_USER_IF_PRIO,
										APP_TASK_USER_IF_PRIO,
										(OS_STK *)&AppTaskUserIFStk[0],
										APP_TASK_USER_IF_STK_SIZE,(void *)0,
										OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);								  
}


static  void  AppTaskUserIF (void *p_arg)
{
	int n=0;
 	(void)p_arg;
 	while(DEF_TRUE){
			n++;
//		if(n>30000) n==0;
			LCD_Num(20, 20, n, 6, RED);
  }							  
}

main.h

#ifndef __MAIN_H
#define __MAIN_H

#include 
#include "stm32f4xx.h"//不能使用系统路径上的
#include "stm32f4xx_conf.h"

#include 
#include 
#include 

#include //在stm32f4xx_it.c中修改了某个函数,所以需要加上这个头文件。

#include "delay.h"
#include "lcd.h"


#define   DEF_FALSE 0
#define   DEF_TRUE  1


#endif

bsp.c

#define  BSP_GLOBALS
#include 

static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
						   

void  BSP_Init(void)
{
	LCD_Init(); 	
  	SysTick_Init();
}

void  SysTick_Init(void)//配置uC/OS-II的时间节拍
{
    INT32U  cnts;
    RCC_ClocksTypeDef  rcc_clocks;

    RCC_GetClocksFreq(&rcc_clocks);
    cnts=(INT32U)rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC;
    SysTick_Config(cnts);
}

bsp.h

#ifndef  __BSP_H__
#define  __BSP_H__

#ifdef   BSP_GLOBALS
#define  BSP_EXT
#else
#define  BSP_EXT  extern
#endif

#define TP_CS	PBout(12)

void BSP_Init(void);
void Touch_Init(void);
void SysTick_Init(void);

#endif

stm32f4xx_it.c

//注释掉pendSV_Handle这个函数
void SysTick_Handler(void)
{
    OS_CPU_SR  cpu_sr;
    OS_ENTER_CRITICAL();//保存全局中断标志,关总中断,告知uC/OS-II正要进入中断
    OSIntNesting++;
    OS_EXIT_CRITICAL();//恢复全局中断标志
    OSTimeTick();     //调用uC/OS-II的OSTimeTick()函数,在os_core.c文件里定义,主要判断延时的任务是否计时到
    OSIntExit();  //在os_core.c文件里定义,如果有更高优先级的任务就绪了,则执行一次任务切换            
}

到此,编译没有报错没有警告。

但是下载运行后会发现,功能没有实现出来。

于是,我跟踪调试。

发现程序停在启动文件startup_stm32f4xx.s的221行


然后,查了下资料!

只需要将startup_stm32f4xx.s文件中的PendSV_Handler改为OSPendSV

修改的位置在73行、219、220行,一共三处!

stm32f4移植ucosii_第3张图片


在移植的过程中,出现过这个警告:


需要修改工程设置:

stm32f4移植ucosii_第4张图片

你可能感兴趣的:(单片机)