stm32_ucos学习---Osinit()

uC/OS-II主函数如下:

main(){

OSInit();    //初始化uC/OS_II

TaskCreate(...);    //创建用户任务

OSStart();   //启动任务且之前不易打开中断

}

下面首先对uC/OS-II的初始化函数OSInit()进行分析:

OSInit(): 最先看看OSInit完成哪些初始化:

void OSInit (void)

{

#if OS_VERSION >= 204

    OSInitHookBegin();                                    /* Call port specific initialization code   */

#endif

    OS_InitMisc();                                        /* Initialize miscellaneous variables       */

    OS_InitRdyList();                                      /* Initialize the Ready List                */

    OS_InitTCBList();                                     /* Initialize the free list of OS_TCBs      */

    OS_InitEventList();                                    /* Initialize the free list of OS_EVENTs    */

#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)

    OS_FlagInit();                                       /* Initialize the event flag structures     */

#endif

#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)

    OS_MemInit();                                      /* Initialize the memory manager            */

#endif

#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)

    OS_QInit();                                        /* Initialize the message queue structures */

#endif

    OS_InitTaskIdle();                                   /* Create the Idle Task 初始化空闲任务 */

#if OS_TASK_STAT_EN > 0

    OS_InitTaskStat();                                  /* Create the Statistic Task初始化统计任务*/

#endif

#if OS_VERSION >= 204

    OSInitHookEnd();                                  /* Call port specific init. code            */

#endif

#if OS_VERSION >= 270 && OS_DEBUG_EN > 0

    OSDebugInit();

#endif

}

static void OS_InitMisc (void)
{
#if OS_TIME_GET_SET_EN > 0   
    OSTime        = 0L;                                          /* Clear the 32-bit system clock            */
#endif

    OSIntNesting = 0;                                           /* Clear the interrupt nesting counter      */
    OSLockNesting = 0;                                           /* Clear the scheduling lock counter        */

    OSTaskCtr     = 0;                                           /* Clear the number of tasks                */

    OSRunning     = FALSE;                                       /* Indicate that multitasking not started   */
    
    OSCtxSwCtr    = 0;                                           /* Clear the context switch counter         */
    OSIdleCtr     = 0L;                                          /* Clear the 32-bit idle counter            */

#if (OS_TASK_STAT_EN > 0) && (OS_TASK_CREATE_EXT_EN > 0)
    OSIdleCtrRun = 0L;
    OSIdleCtrMax = 0L;
    OSStatRdy     = FALSE;                                       /* Statistic task is not ready              */
#endif
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~void  OSInit (void) 

#if OS_VERSION >= 204 
    OSInitHookBegin();                                           /* 调用用户特定的初始化代码(通过一个接口函数实现用户要求的插件式进入系统中)*/
#endif 
    OS_InitMisc();                                               /* 初始化变量*/
    OS_InitRdyList();                                           /* 初始化就绪列表*/
    OS_InitTCBList();                                             /* 初始化OS_TCB空闲列表*/
    OS_InitEventList();                                          /* 初始化OS_EVENT空闲列表*/
    OS_InitTaskIdle();                                            /*创建空闲任务*/
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0) 
    OS_FlagInit();                                              /* 初始化事件标志结构*/
#endif 
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0) //允许内存管理
    OS_MemInit();                                               /* 初始化内存管理器*/
#endif 
#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)  //允许消息队列
    OS_QInit();                                                /* 初始化消息队列结构*/
#endif 
#if OS_TASK_STAT_EN > 0 
    OS_InitTaskStat();                                          /* 创建统计任务*/
#endif 
#if OS_VERSION >= 204 
    OSInitHookEnd();                                            /*调用用户特定的初始化代码(参考OSInitHookBegin())*/
#endif 

/*$PAGE*/ 
static  void  OS_InitMisc (void)
{
#if OS_TIME_GET_SET_EN > 0
    OSTime        = 0L;                                    /* 32位的系统时钟清零*/
#endif
    OSIntNesting  = 0;                                     /* 中断嵌套层数计数器清零*/
    OSLockNesting = 0;                                     /* 调度器锁的嵌套层数计数器清零*/
    OSTaskCtr     = 0;                                     /* 任务数清零*/
    OSRunning     = OS_FALSE;                              /*指明多任务未开始*/
    OSCtxSwCtr    = 0;                                     /* 任务切换次数计数器清零*/
    OSIdleCtr     = 0L;                                    /*32位空闲计数器清零*/
#if OS_TASK_STAT_EN > 0                                      /*运行统计任务*/
    OSIdleCtrRun  = 0L;                                       /* 空闲计数器过去S内达到的最大值 */ 
    OSIdleCtrMax  = 0L;                                        /*保存空闲计数器S内达到的最大值,一般是在初始化的时候*/ 
    OSStatRdy     = OS_FALSE;                              /* 统计任务未就绪*/
#endif
}
static  void  OS_InitRdyList (void)
{
    INT16U   i;
    INT8U   *prdytbl;
    OSRdyGrp      = 0x00;                                        /* Clear the ready list                     */
    prdytbl       = &OSRdyTbl[0];
    for (i = 0; i < OS_RDY_TBL_SIZE; i++) {//OS_RDY_TBL_SIZE=((OS_LOWEST_PRIO) / 8 + 1)
        *prdytbl++ = 0x00;
    }
    OSPrioCur     = 0;           /*当前任务的优先级*/
    OSPrioHighRdy = 0;           /*最高优先级就绪任务的优先级*/
    OSTCBHighRdy  = (OS_TCB *)0; /*最高优先级就绪任务*/                                
    OSTCBCur      = (OS_TCB *)0; /*当前任务*/
}
static  void  OS_InitTCBList (void)
{
    INT8U    i;
    OS_TCB  *ptcb1;
    OS_TCB  *ptcb2;
    OSTCBList     = (OS_TCB *)0;                                 /* TCB Initialization                       */
    for (i = 0; i < (OS_LOWEST_PRIO + 1); i++) {                 /* Clear the priority table                 */
        OSTCBPrioTbl = (OS_TCB *)0;
    }
    ptcb1 = &OSTCBTbl[0];
    ptcb2 = &OSTCBTbl[1];
    for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) {  /* Init. list of free TCBs                  */
        ptcb1->OSTCBNext = ptcb2;
        ptcb1++;
        ptcb2++;
    }
    ptcb1->OSTCBNext = (OS_TCB *)0;                              /* Last OS_TCB                              */
    OSTCBFreeList    = &OSTCBTbl[0];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于将uC/OS移植到STM32微控制器上的步骤,你可以按照以下指导进行操作: 1. 配置件:根据您的具体STM32型号和外设需求,配置引脚、时钟、中断等硬件设置。 2. 准备uC/OS库和源代码:从Micrium官方网站(https://www.micrium.com/)下载最新版本的uC/OS,然后解压缩文件。 3. 添加文件到工程:将uC/OS的源代码和头文件添加到您的STM32工程中。确保包含ucos_ii.h头文件,以便使用uC/OS的API函数。 4. 配置uC/OS:打开ucos_cfg.h文件,并根据您的需求进行配置。这包括任务数量、堆栈大小、时间片等。确保正确配置硬件抽象层(HAL)以兼容STM32。 5. 初始化uC/OS:在main函数中,调用OSInit函数来初始化uC/OS内核。确保正确初始化任务控制块(TCB)和其他必要的数据结构。 6. 创建任务:使用OSTaskCreate函数创建您的任务。为每个任务分配堆栈空间,并指定任务的优先级和入口函数。 7. 启动uC/OS:在main函数中,调用OSStart函数来启动uC/OS内核。这将开始任务调度,并使第一个任务开始执行。 8. 编写任务函数:为每个任务编写相应的任务函数。在函数中,使用uC/OS的API函数来管理任务和资源。 9. 构建和下载:使用适当的编译器和开发环境,将工程编译为可执行文件,并通过调试器或烧录器将其下载到STM32微控制器中。 以上是一个大致的步骤指导,具体细节可能根据您的项目需求和STM32型号而有所不同。请参考uC/OS的官方文档和示例代码,以获取更详细的信息和指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值