UCOS--II移植到stm32上

1.从官网上下载UCOS-II源码。到自己的工程路径下新建文件夹UCOS-II。然后将官网上下载的UCOS-II源码的一些文件复制到自己工程的UCOS-II文件夹内。详细如下:

    1.1 Source:     

        |  os_core.c
        |  os: os_flag.c    os_mbox.c
        |  os_mem.c        os_mutex.c
        |  os_q.c              os_sem.c
        |  os_task.c         os_time.c
        |  os_tmr.c
        |  ucos_ii.c          ucos_ii.h

    1.2  Ports

        | os_cpu_c.c          os_cpu.h      os_cpu_a.asm

        | os_dbg.c              os_dbg_r.c

    1.3 User

        | app_cfg.c            app_cfg.h

        | os_cfg.h              os_cfg_r.h

扫描二维码关注公众号,回复: 3048201 查看本文章

2.  修剪os_cpu_c.c

    2.1 注释掉一些不用的宏 

        #if 0
        #define  OS_CPU_CM3_NVIC_ST_CTRL    (*((volatile INT32U *)0xE000E010uL)) /* SysTick Ctrl & Status Reg. */
        #define  OS_CPU_CM3_NVIC_ST_RELOAD  (*((volatile INT32U *)0xE000E014uL)) /* SysTick Reload  Value Reg. */
        #define  OS_CPU_CM3_NVIC_ST_CURRENT (*((volatile INT32U *)0xE000E018uL)) /* SysTick Current Value Reg. */
        #define  OS_CPU_CM3_NVIC_ST_CAL     (*((volatile INT32U *)0xE000E01CuL)) /* SysTick Cal     Value Reg. */
        #define  OS_CPU_CM3_NVIC_PRIO_ST    (*((volatile INT8U  *)0xE000ED23uL)) /* SysTick Handler Prio  Reg. */


        #define  OS_CPU_CM3_NVIC_ST_CTRL_COUNT                    0x00010000uL   /* Count flag.                */
        #define  OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC                  0x00000004uL   /* Clock Source.              */
        #define  OS_CPU_CM3_NVIC_ST_CTRL_INTEN                    0x00000002uL   /* Interrupt enable.          */
        #define  OS_CPU_CM3_NVIC_ST_CTRL_ENABLE                   0x00000001uL   /* Counter mode.              */
        #define  OS_CPU_CM3_NVIC_PRIO_MIN                               0xFFu    /* Min handler prio.          */
        #endif

    2.2 注释掉void  OS_CPU_SysTickHandler (void)、void  OS_CPU_SysTickInit (INT32U  cnts) 这两个函数

        /*
        *********************************************************************************************************
        *                                         OS_CPU_SysTickHandler()
        *
        * Description: Handle the system tick (SysTick) interrupt, which is used to generate the uC/OS-II tick
        *              interrupt.
        *
        * Arguments  : none.
        *
        * Note(s)    : 1) This function MUST be placed on entry 15 of the Cortex-M3 vector table.
        *********************************************************************************************************
        */
        #if 0
        void  OS_CPU_SysTickHandler (void)
        {
            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()                       */

            OSIntExit();                                 /* Tell uC/OS-II that we are leaving the ISR          */
        }
        #endif
        /*
        *********************************************************************************************************
        *                                          OS_CPU_SysTickInit()
        *
        * Description: Initialize the SysTick.
        *
        * Arguments  : none.
        *
        * Note(s)    : 1) This function MUST be called after OSStart() & after processor initialization.
        *********************************************************************************************************
        */
        #if 0
        void  OS_CPU_SysTickInit (void)
        {
            INT32U  cnts;


            cnts = OS_CPU_SysTickClkFreq() / OS_TICKS_PER_SEC;

            OS_CPU_CM3_NVIC_ST_RELOAD = (cnts - 1);
             /* Set prio of SysTick handler to min prio.           */
            OS_CPU_CM3_NVIC_PRIO_ST   = OS_CPU_CM3_NVIC_PRIO_MIN;
             /* Enable timer.                                      */

 OS_CPU_CM3_NVIC_ST_CTRL  |= OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC | OS_CPU_CM3_NVIC_ST_CTRL_ENABLE;
               /* Enable timer interrupt.                    

       */
              OS_CPU_CM3_NVIC_ST_CTRL  |= OS_CPU_CM3_NVIC_ST_CTRL_INTEN;
        }
        #endif

3. 修剪os_cpu.h

    3.1 注释掉下面几个函数的声明。

         #if 0
         /* See OS_CPU_C.C                                    */
        void       OS_CPU_SysTickHandler(void);
        void       OS_CPU_SysTickInit(void);
         /* See BSP.C                                         */
        INT32U     OS_CPU_SysTickClkFreq(void); 
        #endif   

4. 修剪 os_cpu_a.asm。这个是汇编代码。

    4.1   将所有的PUBLIC 改为 EXPORT

            EXPORT  OS_CPU_SR_Save                                      ; Functions declared in this file
            EXPORT  OS_CPU_SR_Restore
            EXPORT  OSStartHighRdy
            EXPORT  OSCtxSw
            EXPORT  OSIntCtxSw
            EXPORT  OS_CPU_PendSVHandler

    4.2  

           ;        RSEG CODE:CODE:NOROOT(2)

                    AREA |.text|, CODE, READONLY, ALIGN=2     

           THUMB
           REQUIRE8
           PRESERVE8

           注:AREA 一点不能顶头写,这是规定,不然回编译出错;

5.  修剪os_dbg.c

    5.1把#define OS_COMPILER_OPT __root的宏定义注释掉,如下:

         #define OS_COMPILER_OPT //__root

6.   修剪startup_stm32f10x_hd.s

        将 PendSV_Handler 替换成 OS_CPU_PendSVHandler

         DCD     OS_CPU_PendSVHandler             ; PendSV Handler(76行)

         OS_CPU_PendSVHandler  PROC(192行)
                        EXPORT  OS_CPU_PendSVHandler             [WEAK]
                        B       .
                        ENDP       

        注:这一步很重要,但也是很容易遗漏的步骤。     

7. 修剪 stm32f10x_it.c

    7.1 加头文件: ucos_ii.h

        #include <ucos_ii.h>

    7.2 修改void SysTick_Handler(void)函数,如下:

        /**
          * @brief  This function handles SysTick Handler.
          * @param  None
          * @retval None
          */
        void SysTick_Handler(void)
        {
            OSIntEnter();
            OSTimeTick();
            OSIntExit();
        }

8. 修剪os_cfg.h

    对部分功能进行剪裁;

        #define OS_FLAG_EN0
        #define OS_MBOX_EN 0
        #define OS_MEM_EN 0
        #define OS_MUTEX_EN 0
        #define OS_Q_EN 0
        #define OS_SEM_EN 0
        #define OS_TMR_EN 0
        #define OS_DEBUG_EN 0
        #define OS_APP_HOOKS_EN 0
        #define OS_EVENT_MULTI_EN 0

至此,移植方面的工作就完成了。下面是编写点亮LED的代码了。

9.   编写app_cfg.c

#include "includes.h"
#include "app_cfg.h"
#include "task_led.h"

uint8_t error=0;

__align(8) OS_STK  led1_task_stk[LED1_TASK_STK_SIZE];                       //LEDÈÎÎñÕ»


void Task_Start(void *p_arg)
{
		(void)p_arg;                				// 

		OSStatInit();
		OSTaskCreate(task_led1,(void *)0,&led1_task_stk[LED1_TASK_STK_SIZE-1], LED1_TASK_PRIO); 

		OSTaskDel(STARTUP_TASK_PRIO);
}

10. 编写app_cfg,h

#ifndef  __APP_CFG_H__
#define  __APP_CFG_H__

#include "includes.h"
#include "uCOS_II.h"
/*******************ÉèÖÃÈÎÎñÓÅÏȼ¶*******************/
#define STARTUP_TASK_PRIO            	 4	

#define	AVOID_OBSTACLE_TASK_PRIO			 5
#define	LED1_TASK_PRIO			           6


/************ÉèÖÃÕ»´óС£¨µ¥Î»Îª OS_STK £©************/
#define STARTUP_TASK_STK_SIZE              128

#define	LED1_TASK_STK_SIZE		             128


extern unsigned char error;

void Task_Start(void *p_arg);
	
#endif

11. 编写main.c

#include "stm32f10x.h"
#include "Includes.h"
#include <stdio.h>
#include "ucos_ii.h"
#include "task_led.h"

OS_STK startup_task_stk[STARTUP_TASK_STK_SIZE];		  
int main(void)
{
	SystemInit();
	SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC);
	if (SysTick_Config(SystemCoreClock/OS_TICKS_PER_SEC))	
	{ 
	 /* Capture error */ 
	 while (1);
	}
	led_gpio_init();
	OSInit();
	OSTaskCreate(Task_Start,(void *)0,
	 &startup_task_stk[STARTUP_TASK_STK_SIZE-1], STARTUP_TASK_PRIO);	
	OSStart();
	return 0;
}

12 编写task_led.c

#include "task_led.h"
#include "Includes.h"
#include "Bsp_Led.h"

void task_led1(void *p_arg)
{ 
    (void)p_arg; 
        OSTimeDlyHMSM(0, 0,3,0);
    while (1)
    {	
				OSTimeDlyHMSM(0, 0,1,0);	
		    Bsp_Led_on_off(LED3, ON );			  // ÁÁ
		    OSTimeDlyHMSM(0, 0,1,0);	
		    Bsp_Led_on_off(LED3, OFF );		  // Ãð					
    }
}

13.task_led.h

#ifndef __TASK_LED_H__
#define __TASK_LED_H__

#include "includes.h"
#include "stm32f10x.h"
#include "ucos_ii.h"


void task_led1(void *p_arg);

#endif

14. Bsp_Led.c

#include "Includes.h"
#include "Bsp_Led.h"

void led_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_Init_Param;
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOF, ENABLE); 
	
    GPIO_Init_Param.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_Init_Param.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init_Param.GPIO_Speed = GPIO_Speed_50MHz;
	
	  GPIO_Init(GPIOC, &GPIO_Init_Param);
	  GPIO_SetBits(GPIOC,GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3); 
	  //GPIO_ResetBits(GPIOC,GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
}

int Bsp_Led_on_off(int led_number,int on_off)
{
    int ret = 0;
	  switch(led_number) {
			case 0:
				  if(ON == on_off) {
					    GPIO_ResetBits(GPIOC, GPIO_Pin_0);
					}else {
					    GPIO_SetBits(GPIOC,GPIO_Pin_0);
					}
			break;

			case 1:
				  if(ON == on_off) {
					    GPIO_ResetBits(GPIOC, GPIO_Pin_1);
					}else {
					    GPIO_SetBits(GPIOC,GPIO_Pin_1);
					}
			break;					
			
			case 2:
				  if(ON == on_off) {
					    GPIO_ResetBits(GPIOC, GPIO_Pin_2);
					}else {
					    GPIO_SetBits(GPIOC,GPIO_Pin_2);
					}
			break;					
		
			case 3:
				  if(ON == on_off) {
					    GPIO_ResetBits(GPIOC, GPIO_Pin_3);
					}else {
					    GPIO_SetBits(GPIOC,GPIO_Pin_3);
					}
			break;

      default:
          ret = 1;				
		}
	
    return ret;
}

15. Bsp_Led.h

#ifndef __LED__H
#define __LED__H


#define ON  0
#define OFF 1

#define LED0 0
#define LED1 1
#define LED2 2
#define LED3 3

int Bsp_Led_on_off(int led_number,int on_off);

void led_gpio_init(void);

#endif

至此,一个完整的点亮LED的UCOS-II工程就完成了。

猜你喜欢

转载自blog.csdn.net/Deren_lin/article/details/81145568