μC/OS-II移植

下面对μC/OSII移植到STM32F103的步骤进行总结。

1、准备一份STM32F103裸机工程

μC/OS-II移植_第1张图片

2、μC/OSII文件移入裸机工程

进入micrium官网获取μC/OSII文件μC/OS-II移植_第2张图片

 

选择stm32f103相关工程点击进行下载

μC/OS-II移植_第3张图片

将下载文件解压后的uCOS-II文件拷贝到裸机工程目录下

μC/OS-II移植_第4张图片
μC/OS-II移植_第5张图片

3、向工程添加ucos源码

μC/OS-II移植_第6张图片

另外将Micrium\Software\EvalBoards\ST\STM32F103ZE-SK\IAR\OS-Probe-LCD下的os_cfg.h拷贝到工程文件下。

4、修改文件

(1)将os_cpu_c.c的#include "app_cfg.h"注释

(2)将os_dbg.c的#define  OS_COMPILER_OPT  __root 改为#define  OS_COMPILER_OPT

(3)修改启动文件startup_stm32f10x_hd.s

μC/OS-II移植_第7张图片(4)修改UCOSII-Ports\os_cpu_a.asm文件

 

μC/OS-II移植_第8张图片

μC/OS-II移植_第9张图片

5、时钟节拍函数

/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name          : stm32f10x_systick.c
* Author             : MCD Application Team
* Version            : V2.0
* Date               : 05/23/2008
* Description        : This file provides all the SysTick firmware functions.
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED 
* IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_systick.h"
#include "os_cpu.h"
#include "ucos_ii.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* ---------------------- SysTick registers bit mask -------------------- */
/* CTRL TICKINT Mask */
#define CTRL_TICKINT_Set      ((u32)0x00000002)
#define CTRL_TICKINT_Reset    ((u32)0xFFFFFFFD)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : SysTick_CLKSourceConfig
* Description    : Configures the SysTick clock source.
* Input          : - SysTick_CLKSource: specifies the SysTick clock source.
*                    This parameter can be one of the following values:
*                       - SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8
*                         selected as SysTick clock source.
*                       - SysTick_CLKSource_HCLK: AHB clock selected as
*                         SysTick clock source.
* Output         : None
* Return         : None
* ¹¦ÄÜ           : ÉèÖÃSysTickʱÖÓÔ´ £»
*******************************************************************************/
//void SysTick_CLKSourceConfig(u32 SysTick_CLKSource)
//{
//  /* Check the parameters */
//  assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));

//  if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
//  {
//    SysTick->CTRL |= SysTick_CLKSource_HCLK;
//  }
//  else
//  {
//    SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
//  }
//}

/*******************************************************************************
* Function Name  : SysTick_SetReload
* Description    : Sets SysTick Reload value.
* Input          : - Reload: SysTick Reload new value.
*                    This parameter must be a number between 1 and 0xFFFFFF.
* Output         : None
* Return         : None
* ¹¦ÄÜ           : ÉèÖÃSysTickÖØ×°ÔØÖµ £»
*******************************************************************************/
void SysTick_SetReload(u32 Reload)
{
  /* Check the parameters */
  assert_param(IS_SYSTICK_RELOAD(Reload));

  SysTick->LOAD = Reload;
}

/*******************************************************************************
* Function Name  : SysTick_CounterCmd
* Description    : Enables or disables the SysTick counter.
* Input          : - SysTick_Counter: new state of the SysTick counter.
*                    This parameter can be one of the following values:
*                       - SysTick_Counter_Disable: Disable counter
*                       - SysTick_Counter_Enable: Enable counter
*                       - SysTick_Counter_Clear: Clear counter value to 0
* Output         : None
* Return         : None
* ¹¦ÄÜ           : ʹÄÜ»òÕßʧÄÜSysTick¼ÆÊýÆ÷ £»
*******************************************************************************/
void SysTick_CounterCmd(u32 SysTick_Counter)
{
  /* Check the parameters */
  assert_param(IS_SYSTICK_COUNTER(SysTick_Counter));

  if (SysTick_Counter == SysTick_Counter_Enable)
  {
    SysTick->CTRL |= SysTick_Counter_Enable;
  }
  else if (SysTick_Counter == SysTick_Counter_Disable) 
  {
    SysTick->CTRL &= SysTick_Counter_Disable;
  }
  else /* SysTick_Counter == SysTick_Counter_Clear */
  {
    SysTick->VAL = SysTick_Counter_Clear;
  }    
}

/*******************************************************************************
* Function Name  : SysTick_ITConfig
* Description    : Enables or disables the SysTick Interrupt.
* Input          : - NewState: new state of the SysTick Interrupt.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
* ¹¦ÄÜ           : ʹÄÜ»òÕßʧÄÜSysTickÖÐ¶Ï £»
*******************************************************************************/
void SysTick_ITConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    SysTick->CTRL |= CTRL_TICKINT_Set;
  }
  else
  {
    SysTick->CTRL &= CTRL_TICKINT_Reset;
  }
}

/*******************************************************************************
* Function Name  : SysTick_GetCounter
* Description    : Gets SysTick counter value.
* Input          : None
* Output         : None
* Return         : SysTick current value
* ¹¦ÄÜ           : »ñÈ¡SysTick¼ÆÊýÆ÷µÄÖµ £»
*******************************************************************************/
u32 SysTick_GetCounter(void)
{
  return(SysTick->VAL);
}

/*******************************************************************************
* Function Name  : SysTick_GetFlagStatus
* Description    : Checks whether the specified SysTick flag is set or not.
* Input          : - SysTick_FLAG: specifies the flag to check.
*                    This parameter can be one of the following values:
*                       - SysTick_FLAG_COUNT
*                       - SysTick_FLAG_SKEW
*                       - SysTick_FLAG_NOREF
* Output         : None
* Return         : None
* ¹¦ÄÜ           : ¼ì²éÖ¸¶¨µÄSysTick±ê־λÉèÖÃÓë·ñ £»
*******************************************************************************/
FlagStatus SysTick_GetFlagStatus(u8 SysTick_FLAG)
{
  u32 statusreg = 0, tmp = 0 ;
  FlagStatus bitstatus = RESET;

  /* Check the parameters */
  assert_param(IS_SYSTICK_FLAG(SysTick_FLAG));

  /* Get the SysTick register index */
  tmp = SysTick_FLAG >> 3;

  if (tmp == 2) /* The flag to check is in CTRL register */
  {
    statusreg = SysTick->CTRL;
  }
  else          /* The flag to check is in CALIB register */
  {
    statusreg = SysTick->CALIB;
  }

  if ((statusreg & ((u32)1 << SysTick_FLAG)) != (u32)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;
}




void SysTickHandler(void)
{ 
//OSPendSV
    OS_CPU_SR  cpu_sr;

    OS_ENTER_CRITICAL();  //±£´æÈ«¾ÖÖжϱêÖ¾,¹Ø×ÜÖжÏ/* Tell uC/OS-II that we are starting an ISR*/
    OSIntNesting++;
    OS_EXIT_CRITICAL();	  //»Ö¸´È«¾ÖÖжϱêÖ¾

    OSTimeTick();     /* Call uC/OS-II's OSTimeTick(),ÔÚos_core.cÎļþÀﶨÒå,Ö÷ÒªÅжÏÑÓʱµÄÈÎÎñÊÇ·ñ¼Æʱµ½*/

    OSIntExit();  //ÔÚos_core.cÎļþÀﶨÒå,Èç¹ûÓиü¸ßÓÅÏȼ¶µÄÈÎÎñ¾ÍÐ÷ÁË,ÔòÖ´ÐÐÒ»´ÎÈÎÎñÇл»  
}



void  OS_CPU_SysTickInit(void)
{
    RCC_ClocksTypeDef  rcc_clocks;
    INT32U         cnts;


    RCC_GetClocksFreq(&rcc_clocks);

    cnts = (INT32U)rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC;

    SysTick_SetReload(cnts);
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    SysTick_CounterCmd(SysTick_Counter_Enable);
    SysTick_ITConfig(ENABLE);
}



/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

 

6、测试

创建两个控制IO引脚定时变化的任务,结果运行正常。

int main()
{
 
  OSInit();     
  OSTaskCreate((void (*) (void *))LE0_task, (void *) 0,(OS_STK *)&LD0stk[63],  3);
  OSTaskCreate((void (*) (void *))LE1_task, (void *) 0,(OS_STK *)&LD1stk[63],  4);
  OS_CPU_SysTickInit();
  OSStart(); 
  while(1)
  {
 		
  }
}

 

你可能感兴趣的:(μC/OS-II)