采用別人移植好的代碼。即所需函數(shù)已寫好。
準備工作:
1、ucos ii源碼
2、stm32庫
步驟:
1、建工程。
工程目錄為:
2、配置ucos ii
在os_cfg.h中配置相關(guān)功能
3、修改相關(guān)函數(shù)
ucos ii需要一個定時器,在stm32中采用systick定時器,因此需要配置systick定時器
void SysTick_Configuration(void)
{
//關(guān)計數(shù)
//配置SysTick的時鐘源 此時為AHB時鐘
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
//配置指系統(tǒng)句柄的優(yōu)先級(Systick句柄 3號搶占優(yōu)先級,0號子優(yōu)先級)
NVIC_SystemHandlerPriorityCon fig(SystemHandler_SysTick, 3, 0);
//設(shè)置重載值1ms中斷 HCLK=8M時
SysTick_SetReload(800000);
//使能Systick中斷
SysTick_ITConfig(ENABLE);
SysTick_CounterCmd(SysTick_Counter_Enable);
}
需要systick定時器中斷,在中斷里面調(diào)用ucos的相關(guān)函數(shù),以此來作為ucos的時鐘。
void SysTickHandler(void)
{
OS_CPU_SR cpu_sr;
// OS_ENTER_CRITICAL(); //保存全局中斷標志,關(guān)總中斷
// OSIntNesting++;
// OS_EXIT_CRITICAL(); //恢復全局中斷標志
OSIntEnter();
OSTimeTick();
OSIntExit();
}
配置pendsv函數(shù)
ucos通過pendsv來實現(xiàn)任務(wù)調(diào)試,因此需要在啟動代碼中更改pendsv
將原來的PendSV改為
OSPendSV
否則,系統(tǒng)會卡在
OSStartHang
處。
4、includes.h的編寫
#ifndef __INCLUDES_H__
#define __INCLUDES_H__
#define DEF_FALSE 0
#define DEF_TRUE 1
#include "ucos_ii.h"
#include "os_cpu.h"
#include "bsp.h"
#include "app.h"
#include "app_cfg.h"
#include "stm32f10x_conf.h"
#include
#if (uC_PROBE_OS_PLUGIN > 0)
#include
#endif
#if (uC_PROBE_COM_MODULE > 0)
#include
#if (PROBE_COM_METHOD_RS232 > 0)
#include
#endif
#endif
#endif
5、bsp函數(shù)配置
stm32的相關(guān)硬件配置包括RCC、systick、NVIC、等,需要注意的是,systick定時器必須在OS啟動后才能啟動,否則,系統(tǒng)會崩潰。(這里有疑問,在OSStart執(zhí)行前使能systick中斷,系統(tǒng) 還是能運行)
6、編寫相關(guān)任務(wù)函數(shù)。
總結(jié),在移植UCOS時,需要編寫的函數(shù)文件有includes.h、os_cpu.c、os_cpu_a.asm、os_cpu.h、需要配置的文件有os_cgf.h、中斷和啟動代碼。