【ALIENTEK 战舰STM32开发板例程系列连载+教学】第五十八章 UCOSII实验1-任务调度

2019-10-16 06:14发布

<p> {if 编辑问题=='编辑问题'} &nbsp; </p><h1 align="center"> <a name="_Toc342394406"></a><span style="font-family: 宋体;">第五十八章</span> <span>UCOSII</span><span style="font-family: 宋体;">实验</span><span>1-</span><span style="font-family: 宋体;">任务调度</span> </h1><p> <span style="font-family: 宋体;">&nbsp;&nbsp;&nbsp;&nbsp; 前面我们所有的例程都是跑的裸机程序(裸奔),从本章开始,我们将分</span><span>3</span><span style="font-family: 宋体;">个章节向大家介绍</span><span>UCOSII</span><span style="font-family: 宋体;">(实时多任务操作系统内核)的使用。本章,我们将向大家介绍</span><span>UCOSII</span><span style="font-family: 宋体;">最基本也是最重要的应用:任务调度。本章分为如下几个部分:</span> <span>58.1 UCOSII</span><span style="font-family: 宋体;">简介</span> <span>58.2 </span><span style="font-family: 宋体;">硬件设计</span> <span>58.3 </span><span style="font-family: 宋体;">软件设计</span> <span>58.4 </span><span style="font-family: 宋体;">下载验证</span> <b><span style='font-family: "Times New Roman","serif"; font-size: 12pt;'><br> </span></b> <a name="_Toc342394407"></a><span>58.1 UCOSII</span><span style="font-family: 宋体;">简介</span> <span>UCOSII</span><span style="font-family: 宋体;">的前身是</span><span>UCOS</span><span style="font-family: 宋体;">,最早出自于</span><span>1992 </span><span style="font-family: 宋体;">年美国嵌入式系统专家</span><span>Jean J.Labrosse </span><span style="font-family: 宋体;">在《嵌入式系统编程》杂志的</span><span>5</span><span style="font-family: 宋体;">月和</span><span>6</span><span style="font-family: 宋体;">月刊上刊登的文章连载,并把</span><span>UCOS </span><span style="font-family: 宋体;">的源码发布在该杂志的</span><span>BBS </span><span style="font-family: 宋体;">上。目前最新的版本:</span><span>UCOSIII</span><span style="font-family: 宋体;">已经出来,但是现在使用最为广泛的还是</span><span>UCOSII</span><span style="font-family: 宋体;">,本章我们主要针对</span><span>UCOSII</span><span style="font-family: 宋体;">进行介绍。</span> <span>UCOSII</span><span style="font-family: 宋体;">是一个可以基于</span><span>ROM</span><span style="font-family: 宋体;">运行的、可裁减的、抢占式、实时多任务内核,具有高度可移植性,特别适合于微处理器和控制器,是和很多商业操作系统性能相当的实时操作系统</span><span>(RTOS)</span><span style="font-family: 宋体;">。为了提供最好的移植性能,</span><span>UCOSII</span><span style="font-family: 宋体;">最大程度上使用</span><span>ANSI C</span><span style="font-family: 宋体;">语言进行开发,并且已经移植到近</span><span>40</span><span style="font-family: 宋体;">多种处理器体系上,涵盖了从</span><span>8</span><span style="font-family: 宋体;">位到</span><span>64</span><span style="font-family: 宋体;">位各种</span><span>CPU(</span><span style="font-family: 宋体;">包括</span><span>DSP)</span><span style="font-family: 宋体;">。</span> <a name="OLE_LINK4"></a><a name="OLE_LINK3"></a><span>UCOSII</span><span style="font-family: 宋体;">是专门为计算机的嵌入式应用设计的,</span> <span style="font-family: 宋体;">绝大部分代码是用</span><span>C</span><span style="font-family: 宋体;">语言编写的。</span><span>CPU </span><span style="font-family: 宋体;">硬件相关部分是用汇编语言编写的、总量约</span><span>200</span><span style="font-family: 宋体;">行的汇编语言部分被压缩到最低限度,为的是便于移植到任何一种其它的</span><span>CPU </span><span style="font-family: 宋体;">上。用户只要有标准的</span><span>ANSI </span><span style="font-family: 宋体;">的</span><span>C</span><span style="font-family: 宋体;">交叉编译器,有汇编器、连接器等软件工具,就可以将</span><span>UCOSII</span><span style="font-family: 宋体;">嵌人到开发的产品中。</span><span>UCOSII</span><span style="font-family: 宋体;">具有执行效率高、占用空间小、实时性能优良和可扩展性强等特点,</span> <span style="font-family: 宋体;">最小内核可编译至</span><span> 2KB </span><span style="font-family: 宋体;">。</span><span>UCOSII</span><span style="font-family: 宋体;">已经移植到了几乎所有知名的</span><span>CPU </span><span style="font-family: 宋体;">上。</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UCOSII</span><span style="font-family: 宋体;">构思巧妙。结构简洁精练,可读性强,同时又具备了实时操作系统的全部功能,虽然它只是一个内核,但非常适合初次接触嵌入式实时操作系统的朋友,可以说是麻雀虽小,五脏俱全。</span><span>UCOSII</span><span style="font-family: 宋体;">(</span><span>V2.91</span><span style="font-family: 宋体;">版本)体系结构如图</span><span>58.1.1</span><span style="font-family: 宋体;">所示:</span> <ignore_js_op> <img title=" " alt=" " src="data/attach/1910/we2qj1yzrfud1h5maxrle2vwy7n4pp6c.jpg"> </ignore_js_op> <span style="font-family: 宋体;">图</span><span>58.1.1 UCOSII</span><span style="font-family: 宋体;">体系结构图</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family: 宋体;">注意本章我们使用的是</span><span>UCOSII</span><span style="font-family: 宋体;">的最新版本:</span><span>V2.91</span><span style="font-family: 宋体;">版本,该版本</span><span>UCOSII</span><span style="font-family: 宋体;">比早期的</span><span>UCOSII</span><span style="font-family: 宋体;">(如</span><span>V2.52</span><span style="font-family: 宋体;">)多了很多功能(比如多了软件定时器,支持任务数最大达到</span><span>255</span><span style="font-family: 宋体;">个等),而且修正了很多已知</span><span>BUG</span><span style="font-family: 宋体;">。不过,有两个文件:</span><span>os_dbg_r.c</span><span style="font-family: 宋体;">和</span><span>os_dbg.c</span><span style="font-family: 宋体;">,我们没有在上图列出,也不将其加入到我们的工程中,这两个主要用于对</span><span>UCOS</span><span style="font-family: 宋体;">内核进行调试支持,比较少用到。</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family: 宋体;">从上图可以看出,</span><span>UCOSII</span><span style="font-family: 宋体;">的移植,我们只需要修改:</span><span>os_cpu.h</span><span style="font-family: 宋体;">、</span><span>os_cpu_a.asm</span><span style="font-family: 宋体;">和</span><span>os_cpu.c</span><span style="font-family: 宋体;">等三个文件即可,其中:</span><span>os_cpu.h</span><span style="font-family: 宋体;">,进行数据类型的定义,以及处理器相关代码和几个函数原型;</span><span>os_cpu_a.asm</span><span style="font-family: 宋体;">,是移植过程中需要汇编完成的一些函数,主要就是任务切换函数;</span><span>os_cpu.c</span><span style="font-family: 宋体;">,定义一些用户</span><span>HOOK</span><span style="font-family: 宋体;">函数。</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family: 宋体;">图中定时器的作用是为</span><span>UCOSII</span><span style="font-family: 宋体;">提供系统时钟节拍,实现任务切换和任务延时等功能。这个时钟节拍由</span><span>OS_TICKS_PER_SEC</span><span style="font-family: 宋体;">(在</span><span>os_cfg.h</span><span style="font-family: 宋体;">中定义)设置,一般我们设置</span><span>UCOSII</span><span style="font-family: 宋体;">的系统时钟节拍为</span><span>1ms~100ms</span><span style="font-family: 宋体;">,具体根据你所用处理器和使用需要来设置。本章,我们利用</span><span>STM32</span><span style="font-family: 宋体;">的</span><span>SYSTICK</span><span style="font-family: 宋体;">定时器来提供</span><span>UCOSII</span><span style="font-family: 宋体;">时钟节拍。</span> <span style="font-family: 宋体;">关于</span><span>UCOSII</span><span style="font-family: 宋体;">在</span><span>STM32</span><span style="font-family: 宋体;">的详细移植,请参考光盘资料(《</span><span>UCOSII</span><span style="font-family: 宋体;">在</span><span>STM32</span><span style="font-family: 宋体;">的移植详解</span><span>.pdf</span><span style="font-family: 宋体;">》),这里我们就不详细介绍了。</span> <span>UCOSII</span><span style="font-family: 宋体;">早期版本只支持</span><span>64</span><span style="font-family: 宋体;">个任务,但是从</span><span>2.80</span><span style="font-family: 宋体;">版本开始,支持任务数提高到</span><span>255</span><span style="font-family: 宋体;">个,不过对我们来说一般</span><span>64</span><span style="font-family: 宋体;">个任务都是足够多了,一般很难用到这么多个任务。</span><span>UCOSII</span><span style="font-family: 宋体;">保留了最高</span><span>4</span><span style="font-family: 宋体;">个优先级和最低</span><span>4</span><span style="font-family: 宋体;">个优先级的总共</span><span>8</span><span style="font-family: 宋体;">个任务,用于拓展使用,单实际上,</span><span>UCOSII</span><span style="font-family: 宋体;">一般只占用了最低</span><span>2</span><span style="font-family: 宋体;">个优先级,分别用于空闲任务(倒数第一)和统计任务(倒数第二),所以剩下给我们使用的任务最多可达</span><span>255-2=253</span><span style="font-family: 宋体;">个(</span><span>V2.91</span><span style="font-family: 宋体;">)。</span> <span style="font-family: 宋体;">所谓的任务,其实就是一个死循环函数,该函数实现一定的功能,一个工程可以有很多这样的任务(最多</span><span>255</span><span style="font-family: 宋体;">个),</span><span>UCOSII</span><span style="font-family: 宋体;">对这些任务进行调度管理,让这些任务可以并发工作(注意不是同时工作!!,并发只是各任务轮流占用</span><span>CPU</span><span style="font-family: 宋体;">,而不是同时占用,任何时候还是只有</span><span>1</span><span style="font-family: 宋体;">个任务能够占用</span><span>CPU</span><span style="font-family: 宋体;">),这就是</span><span>UCOSII</span><span style="font-family: 宋体;">最基本的功能。</span> <span style="font-family: 宋体;">前面我们学习的所有实验,都是一个大任务(死循环),这样,有些事情就比较不好处理,比如:</span><span>MP3</span><span style="font-family: 宋体;">实验,在</span><span>MP3</span><span style="font-family: 宋体;">播放的时候,我们还希望显示歌词,如果是</span><span>1</span><span style="font-family: 宋体;">个死循环(一个任务),那么很可能在显示歌词的时候,</span><span>MP3</span><span style="font-family: 宋体;">声音出现停顿(尤其是高码率的时候),这主要是歌词显示占用太长时间,导致</span><span>VS1053</span><span style="font-family: 宋体;">由于不能及时得到数据而停顿。而如果用</span><span>UCOSII</span><span style="font-family: 宋体;">来处理,那么我们可以分</span><span>2</span><span style="font-family: 宋体;">个任务,</span><span>MP3</span><span style="font-family: 宋体;">播放一个任务(优先级高),歌词显示一个任务(优先级低)。这样,由于</span><span>MP3</span><span style="font-family: 宋体;">任务的优先级高于歌词显示任务,</span><span>MP3</span><span style="font-family: 宋体;">任务可以打断歌词显示任务,从而及时给</span><span>VS1053</span><span style="font-family: 宋体;">提供数据,保证音频不断,而显示歌词又能顺利进行。这就是</span><span>UCOSII</span><span style="font-family: 宋体;">带来的好处。</span> <span>UCOSII</span><span style="font-family: 宋体;">的任何任务都是通过一个叫任务控制块(</span><span>TCB</span><span style="font-family: 宋体;">)的东西来控制的,每个任务管理块有</span><span>3</span><span style="font-family: 宋体;">个最重要的参数:</span><span>1</span><span style="font-family: 宋体;">,任务函数指针;</span><span>2</span><span style="font-family: 宋体;">,任务堆栈指针;</span><span>3</span><span style="font-family: 宋体;">,任务优先级;任务控制块就是任务在系统里面的身份证(</span><span>UCOSII</span><span style="font-family: 宋体;">通过优先级识别任务),任务控制块我们就不再详细介绍了,详细介绍请参考任哲老师的《嵌入式实时操作系统</span><span>UCOSII</span><span style="font-family: 宋体;">原理及应用》一书第二章。</span> <span style="font-family: 宋体;">在</span><span>UCOSII</span><span style="font-family: 宋体;">中,使用</span><span>CPU</span><span style="font-family: 宋体;">的时候,优先级高(数值小)的任务比优先级低的任务具有优先使用权,即任务就绪表中总是优先级最高的任务获得</span><span>CPU</span><span style="font-family: 宋体;">使用权,只有高优先级的任务让出</span><span>CPU</span><span style="font-family: 宋体;">使用权(比如延时)时,低优先级的任务才能获得</span><span>CPU</span><span style="font-family: 宋体;">使用权。</span><span>UCOSII</span><span style="font-family: 宋体;">不支持多个任务优先级相同,也就是每个任务的优先级必须不一样。</span> <span style="font-family: 宋体;">任务的调度其实就是</span><span>CPU</span><span style="font-family: 宋体;">运行环境的切换,即:</span><span>PC</span><span style="font-family: 宋体;">指针、</span><span>SP</span><span style="font-family: 宋体;">指针和寄存器组等内容的存取过程,关于任务调度的详细介绍,请参考《嵌入式实时操作系统</span><span>UCOSII</span><span style="font-family: 宋体;">原理及应用》一书第三章相关内容。</span> <span>UCOSII</span><span style="font-family: 宋体;">的每个任务都是一个死循环。每个任务都处在以下</span><span> 5</span><span style="font-family: 宋体;">种状态之一的状态下,这</span><span>5</span><span style="font-family: 宋体;">种状态是:睡眠状态、</span> <span style="font-family: 宋体;">就绪状态、</span> <span style="font-family: 宋体;">运行状态、</span> <span style="font-family: 宋体;">等待状态</span><span>(</span><span style="font-family: 宋体;">等待某一事件发生</span><span>)</span><span style="font-family: 宋体;">和中断服务状态。</span> <span style="font-family: 宋体;">睡眠状态,任务在没有被配备任务控制块或被剥夺了任务控制块时的状态。</span> <span style="font-family: 宋体;">就绪状态,系统为任务配备了任务控制块且在任务就绪表中进行了就绪登记,任务已经准备好了,但由于该任务的优先级比正在运行的任务的优先级低,</span> <span style="font-family: 宋体;">还暂时不能运行,这时任务的状态叫做就绪状态。</span> <span>&nbsp;</span> <span style="font-family: 宋体;">运行状态,该任务获得</span><span>CPU</span><span style="font-family: 宋体;">使用权,并正在运行中,此时的任务状态叫做运行状态。</span> <span style="font-family: 宋体;">等待状态,正在运行的任务,需要等待一段时间或需要等待一个事件发生再运行时,该任务就会把</span><span>CPU</span><span style="font-family: 宋体;">的使用权让给别的任务而使任务进入等待状态。</span> <span style="font-family: 宋体;">中断服务状态,一个正在运行的任务一旦响应中断申请就会中止运行而去执行中断服务程序,这时任务的状态叫做中断服务状态。</span> <span>UCOSII</span><span style="font-family: 宋体;">任务的</span><span>5</span><span style="font-family: 宋体;">个状态转换关系如图</span><span>58.1.2</span><span style="font-family: 宋体;">所示:</span> <ignore_js_op> </ignore_js_op> </p><p><span style="font-family: 宋体;">图</span><span>58.1.2 UCOSII</span><span style="font-family: 宋体;">任务状态转换关系</span> <span style="font-family: 宋体;">接下来,我们看看在</span><span>UCOSII</span><span style="font-family: 宋体;">中,与任务相关的几个函数:</span> <span>1)<span>&nbsp; </span></span><span style="font-family: 宋体;">建立任务函数</span> <span style="font-family: 宋体;">如果想让</span><span>UCOSII</span><span style="font-family: 宋体;">管理用户的任务,必须先建立任务。</span><span>UCOSII</span><span style="font-family: 宋体;">提供了我们</span><span>2</span><span style="font-family: 宋体;">个建立任务的函数:</span><span>OSTaskCreat</span><span style="font-family: 宋体;">和</span><span>OSTaskCreatExt</span><span style="font-family: 宋体;">,我们一般用</span><span>OSTaskCreat</span><span style="font-family: 宋体;">函数来创建任务,该函数原型为:</span><span>OSTaskCreate(void(*task)(void*pd),void*pdata,OS_STK*ptos,INTU prio)</span><span style="font-family: 宋体;">。该函数包括</span><span>4</span><span style="font-family: 宋体;">个参数:</span><span>task</span><span style="font-family: 宋体;">:是指向任务代码的指针;</span><span>pdata</span><span style="font-family: 宋体;">:是任务开始执行时,传递给任务的参数的指针;</span><span>ptos</span><span style="font-family: 宋体;">:是分配给任务的堆栈的栈顶指针;</span><span>prio</span><span style="font-family: 宋体;">是分配给任务的优先级。</span> <span style="font-family: 宋体;">每个任务都有自己的堆栈,堆栈必须申明为</span><span>OS_STK</span><span style="font-family: 宋体;">类型,并且由连续的内存空间组成。可以静态分配堆栈空间,也可以动态分配堆栈空间。</span> <span>OSTaskCreatExt</span><span style="font-family: 宋体;">也可以用来创建任务,详细介绍请参考《嵌入式实时操作系统</span><span>UCOSII</span><span style="font-family: 宋体;">原理及应用》</span><span>3.5.2</span><span style="font-family: 宋体;">节。</span> <span>2)<span>&nbsp; </span></span><span style="font-family: 宋体;">任务删除函数</span> <span style="font-family: 宋体;">所谓的任务删除,其实就是把任务置于睡眠状态,并不是把任务代码给删除了。</span><span>UCOSII</span><span style="font-family: 宋体;">提供的任务删除函数原型为:</span><span>INT8U OSTaskDel(INT8U prio)</span><span style="font-family: 宋体;">,其中参数</span><span>prio</span><span style="font-family: 宋体;">就是我们要删除的任务的优先级,可见该函数是通过任务优先级来实现任务删除的。</span> <span style="font-family: 宋体;">特别注意:任务不能随便删除,必须在确保被删除任务的资源被释放的前提下才能删除!</span> <span>3)<span>&nbsp; </span></span><span style="font-family: 宋体;">请求任务删除函数</span> <span style="font-family: 宋体;">前面提到,必须确保被删除任务的资源被释放的前提下才能将其删除,所以我们通过向被删除任务发送删除请求,来实现任务释放自身占用资源后再删除。</span><span>UCOSII</span><span style="font-family: 宋体;">提供的请求删除任务函数原型为:</span><span>INT8U OSTaskDelReq(INT8U prio)</span><span style="font-family: 宋体;">,同样还是通过优先级来确定被请求删除任务。</span> <span>4)<span>&nbsp; </span></span><span style="font-family: 宋体;">改变任务的优先级函数</span> <span>UCOSII</span><span style="font-family: 宋体;">在建立任务时,会分配给任务一个优先级,但是这个优先级并不是一成不变的,而是可以通过调用</span><span>UCOSII</span><span style="font-family: 宋体;">提供的函数修改。</span><span>UCOSII</span><span style="font-family: 宋体;">提供的任务优先级修改函数原型为:</span><span>INT8U OSTaskChangePrio(INT8U oldprio,INT8U newprio)</span><span style="font-family: 宋体;">。</span> <span>5)<span>&nbsp; </span></span><span style="font-family: 宋体;">任务挂起函数</span> <span style="font-family: 宋体;">任务挂起和任务删除有点类似,但是又有区别,任务挂起只是将被挂起任务的就绪标志删除,并做任务挂起记录,并没有将任务控制块任务控制块链表里面删除,也不需要释放其资源,而任务删除则必须先释放被删除任务的资源,并将被删除任务的任务控制块也给删了。被挂起的任务,在恢复(解挂)后可以继续运行。</span><span>UCOSII</span><span style="font-family: 宋体;">提供的任务挂起函数原型为:</span><span>INT8U OSTaskSuspend(INT8U prio)</span><span style="font-family: 宋体;">。</span> <span>6)<span>&nbsp; </span></span><span style="font-family: 宋体;">任务恢复函数</span> <span style="font-family: 宋体;">有任务挂起函数,就有任务恢复函数,通过该函数将被挂起的任务恢复,让调度器能够重新调度该函数。</span><span>UCOSII</span><span style="font-family: 宋体;">提供的任务恢复函数原型为:</span><span>INT8U OSTaskResume(INT8U prio)</span><span style="font-family: 宋体;">。</span> <span>UCOSII</span><span style="font-family: 宋体;">与任务相关的函数我们就介绍这么多。最后,我们来看看在</span><span>STM32</span><span style="font-family: 宋体;">上面运行</span><span>UCOSII</span><span style="font-family: 宋体;">的步骤:</span> <b><span>1)<span> </span></span></b><b><span style="font-family: 宋体;">移植</span><span>UCOSII</span></b> <span style="font-family: 宋体;">要想</span><span>UCOSII</span><span style="font-family: 宋体;">在</span><span>STM32</span><span style="font-family: 宋体;">正常运行,当然首先是需要移植</span><span>UCOSII</span><span style="font-family: 宋体;">,这部分我们已经为大家做好了(参考光盘源码,想自己移植的,请参考光盘</span><span>UCOSII</span><span style="font-family: 宋体;">资料)。</span> <span style="font-family: 宋体;">这里我们要特别注意一个地方,</span><span>ALIENTEK</span><span style="font-family: 宋体;">提供的</span><span>SYSTEM</span><span style="font-family: 宋体;">文件夹里面的系统函数直接支持</span><span>UCOSII</span><span style="font-family: 宋体;">,只需要在</span><span>sys.h</span><span style="font-family: 宋体;">文件里面将:</span><span>SYSTEM_SUPPORT_UCOS</span><span style="font-family: 宋体;">宏定义改为</span><span>1</span><span style="font-family: 宋体;">,即可通过</span><span>delay_init</span><span style="font-family: 宋体;">函数初始化</span><span>UCOSII</span><span style="font-family: 宋体;">的系统时钟节拍,为</span><span>UCOSII</span><span style="font-family: 宋体;">提供时钟节拍。</span> <b><span>2)<span> </span></span></b><b><span style="font-family: 宋体;">编写任务函数并设置其堆栈大小和优先级等参数。</span></b> <span style="font-family: 宋体;">编写任务函数,以便</span><span>UCOSII</span><span style="font-family: 宋体;">调用。</span> <span style="font-family: 宋体;">设置函数堆栈大小,这个需要根据函数的需求来设置,如果任务函数的局部变量多,嵌套层数多,那么相应的堆栈就得大一些,如果堆栈设置小了,很可能出现的结果就是</span><span>CPU</span><span style="font-family: 宋体;">进入</span><span>HardFault</span><span style="font-family: 宋体;">,遇到这种情况,你就必须把堆栈设置大一点了。另外,有些地方还需要注意堆栈字节对齐的问题,如果任务运行出现莫名其妙的错误(比如用到</span><span>sprintf</span><span style="font-family: 宋体;">出错),请考虑是不是字节对齐的问题。</span> <span style="font-family: 宋体;">设置任务优先级,这个需要大家根据任务的重要性和实时性设置,记住高优先级的任务有优先使用</span><span>CPU</span><span style="font-family: 宋体;">的权利。</span> <b><span>3)<span> </span></span></b><b><span style="font-family: 宋体;">初始化</span><span>UCOSII</span></b><b><span style="font-family: 宋体;">,并在</span><span>UCOSII</span></b><b><span style="font-family: 宋体;">中创建任务</span></b> <span style="font-family: 宋体;">调用</span><span>OSInit</span><span style="font-family: 宋体;">,初始化</span><span>UCOSII</span><span style="font-family: 宋体;">,通过调用</span><span>OSTaskCreate</span><span style="font-family: 宋体;">函数创建我们的任务。</span> <b><span>4)<span> </span></span></b><b><span style="font-family: 宋体;">启动</span><span>UCOSII</span></b> <span style="font-family: 宋体;">调用</span><span>OSStart</span><span style="font-family: 宋体;">,启动</span><span>UCOSII</span><span style="font-family: 宋体;">。</span> <span style="font-family: 宋体;">通过以上</span><span>4</span><span style="font-family: 宋体;">个步骤,</span><span>UCOSII</span><span style="font-family: 宋体;">就开始在</span><span>STM32</span><span style="font-family: 宋体;">上面运行了,这里还需要注意我们必须对</span><span>os_cfg.h</span><span style="font-family: 宋体;">进行部分配置,以满足我们自己的需要。</span> <a name="_Toc342394408"></a><span>58.2 </span><span style="font-family: 宋体;">硬件设计</span> <span style="color: black; font-family: 宋体;">本节实验功能简介:本章我们在</span><span style='color: black; font-family: "Arial","sans-serif";'>UCOSII</span><span style="color: black; font-family: 宋体;">里面创建</span><span style='color: black; font-family: "Arial","sans-serif";'>3</span><span style="color: black; font-family: 宋体;">个任务:开始任务、</span><span style='color: black; font-family: "Arial","sans-serif";'>LED0</span><span style="color: black; font-family: 宋体;">任务和</span><span style='color: black; font-family: "Arial","sans-serif";'>LED1</span><span style="color: black; font-family: 宋体;">任务,开始任务用于创建其他(</span><span style='color: black; font-family: "Arial","sans-serif";'>LED0</span><span style="color: black; font-family: 宋体;">和</span><span style='color: black; font-family: "Arial","sans-serif";'>LED1</span><span style="color: black; font-family: 宋体;">)任务,之后挂起;</span><span style='color: black; font-family: "Arial","sans-serif";'>LED0</span><span style="color: black; font-family: 宋体;">任务用于控制</span><span style='color: black; font-family: "Arial","sans-serif";'>DS0</span><span style="color: black; font-family: 宋体;">的亮灭,</span><span style='color: black; font-family: "Arial","sans-serif";'>DS0</span><span style="color: black; font-family: 宋体;">每秒钟亮</span><span style='color: black; font-family: "Arial","sans-serif";'>80ms</span><span style="color: black; font-family: 宋体;">;</span><span style='color: black; font-family: "Arial","sans-serif";'>LED1</span><span style="color: black; font-family: 宋体;">任务用于控制</span><span style='color: black; font-family: "Arial","sans-serif";'>DS1</span><span style="color: black; font-family: 宋体;">的亮灭,</span><span style='color: black; font-family: "Arial","sans-serif";'>DS1</span><span style="color: black; font-family: 宋体;">亮</span><span style='color: black; font-family: "Arial","sans-serif";'>300ms</span><span style="color: black; font-family: 宋体;">,灭</span><span style='color: black; font-family: "Arial","sans-serif";'>300ms</span><span style="color: black; font-family: 宋体;">,依次循环。</span><span style='color: black; font-family: "Arial","sans-serif";'> </span> <span style="font-family: 宋体;">所要用到的硬件资源如下:</span> <span>1)<span>&nbsp; </span></span><span style="font-family: 宋体;">指示灯</span><span>DS0 </span><span style="font-family: 宋体;">、</span><span>DS1 &nbsp;</span><span>&nbsp;</span> <a name="_Toc342394409"></a><span>58.3 </span><span style="font-family: 宋体;">软件设计</span> <span style="font-family: 宋体;">本章,我们在第六章实验</span> <span style="font-family: 宋体;">(实验</span><span>1 </span><span style="font-family: 宋体;">)的基础上修改,在该工程源码下面加入</span><span>UCOSII</span><span style="font-family: 宋体;">文件夹,存放</span><span>UCOSII</span><span style="font-family: 宋体;">源码(我们已经将</span><span>UCOSII</span><span style="font-family: 宋体;">源码分为三个文件夹:</span><span>CORE</span><span style="font-family: 宋体;">、</span><span>PORT</span><span style="font-family: 宋体;">和</span><span>CONFIG</span><span style="font-family: 宋体;">)。</span> <span style="font-family: 宋体;">打开工程,新建</span><span>UCOSII-CORE</span><span style="font-family: 宋体;">、</span><span>UCOSII-PORT</span><span style="font-family: 宋体;">和</span><span>UCOSII-CONFIG</span><span style="font-family: 宋体;">三个分组,分别添加</span><span>UCOSII</span><span style="font-family: 宋体;">三个文件夹下的源码,并将这三个文件夹加入头文件包含路径,最后得到工程如图</span><span>58.3.1</span><span style="font-family: 宋体;">所示:</span> <ignore_js_op> <img title=" " alt=" " src="data/attach/1910/2mslijkd09fh0lynuvnj2i2qkjuef0tl.jpg"> </ignore_js_op> <span style="font-family: 宋体;">图</span><span>58.3.1 </span><span style="font-family: 宋体;">添加</span><span>UCOSII</span><span style="font-family: 宋体;">源码后的工程</span> <span>UCOSII-CORE</span><span style="font-family: 宋体;">分组下面是</span><span>UCOSII</span><span style="font-family: 宋体;">的核心源码,我们不需要做任何变动。</span> <span>UCOSII-PORT</span><span style="font-family: 宋体;">分组下面是我们移植</span><span>UCOSII</span><span style="font-family: 宋体;">要修改的</span><span>3</span><span style="font-family: 宋体;">个代码,这个在移植的时候完成。</span> <span>UCOSII-CONFIG</span><span style="font-family: 宋体;">分组下面是</span><span>UCOSII</span><span style="font-family: 宋体;">的配置部分,主要由用户根据自己的需要对</span><span>UCOSII</span><span style="font-family: 宋体;">进行裁剪或其他设置。</span> <span style="font-family: 宋体;">本章,我们对</span><span>os_cfg.h</span><span style="font-family: 宋体;">里面定义</span><span>OS_TICKS_PER_SEC</span><span style="font-family: 宋体;">的值为</span><span>200</span><span style="font-family: 宋体;">,也就是设置</span><span>UCOSII</span><span style="font-family: 宋体;">的时钟节拍为</span><span>5ms</span><span style="font-family: 宋体;">,同时设置</span><span>OS_MAX_TASKS</span><span style="font-family: 宋体;">为</span><span>10</span><span style="font-family: 宋体;">,也就是最多</span><span>10</span><span style="font-family: 宋体;">个任务(包括空闲任务和统计任务在内),其他配置我们就不详细介绍了,请参考本实验源码。</span> <span style="font-family: 宋体;">前面提到,我们需要在</span><span>sys.h</span><span style="font-family: 宋体;">里面设置</span><span>SYSTEM_SUPPORT_UCOS</span><span style="font-family: 宋体;">为</span><span>1</span><span style="font-family: 宋体;">,以支持</span><span>UCOSII</span><span style="font-family: 宋体;">,通过这个设置,我们不仅可以实现利用</span><span>delay_init</span><span style="font-family: 宋体;">来初始化</span><span>SYSTICK</span><span style="font-family: 宋体;">,产生</span><span>UCOSII</span><span style="font-family: 宋体;">的系统时钟节拍,还可以让</span><span>delay_us</span><span style="font-family: 宋体;">和</span><span>delay_ms</span><span style="font-family: 宋体;">函数在</span><span>UCOSII</span><span style="font-family: 宋体;">下能够正常使用(实现原理请参考</span><span>5.1</span><span style="font-family: 宋体;">节),这使得我们之前的代码,可以十分方便的移植到</span><span>UCOSII</span><span style="font-family: 宋体;">下。虽然</span><span>UCOSII</span><span style="font-family: 宋体;">也提供了延时函数:</span><span style="font-family: 宋体;">OSTimeDly</span><span style="font-family: 宋体;">和<span>OSTimeDLyHMSM</span></span><span style="font-family: 宋体;">,但是这两个函数的最少延时单位只能是</span><span>1</span><span style="font-family: 宋体;">个</span><span>UCOSII</span><span style="font-family: 宋体;">时钟节拍,在本章,即</span><span>5ms</span><span style="font-family: 宋体;">,显然不能实现</span><span>us</span><span style="font-family: 宋体;">级的延时,而</span><span>us</span><span style="font-family: 宋体;">级的延时在很多时候非常有用:比如</span><span>IIC</span><span style="font-family: 宋体;">模拟时序,</span><span>DS18B20</span><span style="font-family: 宋体;">等单总线器件操作等。而通过我们提供的</span><span>delay_us</span><span style="font-family: 宋体;">和</span><span>delay_ms</span><span style="font-family: 宋体;">,则可以方便的提供</span><span>us</span><span style="font-family: 宋体;">和</span><span>ms</span><span style="font-family: 宋体;">的延时服务,这比</span><span>UCOSII</span><span style="font-family: 宋体;">本身提供的延时函数更好用。</span> <span style="font-family: 宋体;">在设置</span><span>SYSTEM_SUPPORT_UCOS</span><span style="font-family: 宋体;">为</span><span>1</span><span style="font-family: 宋体;">之后,</span><span>UCOSII</span><span style="font-family: 宋体;">的时钟节拍由</span><span>SYSTICK</span><span style="font-family: 宋体;">的中断服务函数提供,该部分代码如下:</span> <span>//systick</span><span style="font-family: 宋体;">中断服务函数</span><span>,</span><span style="font-family: 宋体;">使用</span><span>ucos</span><span style="font-family: 宋体;">时用到</span> <span>void SysTick_Handler(void)</span> <span>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSIntEnter();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">进入中断</span> <span>&nbsp;&nbsp;&nbsp; OSTimeTick();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">调用</span><span>ucos</span><span style="font-family: 宋体;">的时钟服务程序</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp; OSIntExit();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; //</span><span style="font-family: 宋体;">触发任务切换软中断</span> <span>}</span> <span style="font-family: 宋体;">以上代码,其中</span><span>OSIntEnter</span><span style="font-family: 宋体;">是进入中断服务函数,用来记录中断嵌套层数(</span><span>OSIntNesting</span><span style="font-family: 宋体;">增加</span><span>1</span><span style="font-family: 宋体;">);</span><span>OSTimeTick</span><span style="font-family: 宋体;">是系统时钟节拍服务函数,在每个时钟节拍了解每个任务的延时状态,使已经到达延时时限的非挂起任务进入就绪状态;</span><span>OSIntExit</span><span style="font-family: 宋体;">是退出中断服务函数,该函数可能触发一次任务切换(当</span><span>OSIntNesting==0&amp;&amp;</span><span style="font-family: 宋体;">调度器未上锁</span><span>&amp;&amp;</span><span style="font-family: 宋体;">就绪表最高优先级任务!</span><span>=</span><span style="font-family: 宋体;">被中断的任务优先级时),否则继续返回原来的任务执行代码(如果</span><span>OSIntNesting</span><span style="font-family: 宋体;">不为</span><span>0</span><span style="font-family: 宋体;">,则减</span><span>1</span><span style="font-family: 宋体;">)。</span> <span style="font-family: 宋体;">事实上,任何中断服务函数,我们都应该加上</span><span>OSIntEnter</span><span style="font-family: 宋体;">和</span><span>OSIntExit</span><span style="font-family: 宋体;">函数,这是因为</span><span>UCOSII</span><span style="font-family: 宋体;">是一个可剥夺型的内核,中断服务子程序运行之后,系统会根据情况进行一次任务调度去运行优先级别最高的就绪任务,而并不一定接着运行被中断的任务!</span> <span style="font-family: 宋体;">最后,我们打开</span><span>test.c</span><span style="font-family: 宋体;">,输入如下代码:</span> <span>/////////////////////////UCOSII</span><span style="font-family: 宋体;">任务设置</span><span>///////////////////////////////////</span> <span>//START </span><span style="font-family: 宋体;">任务</span> <span>#define START_TASK_PRIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务优先级</span> <span>#define START_STK_SIZE&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务堆栈大小</span> <span>OS_STK START_TASK_STK[START_STK_SIZE]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务堆栈</span><span>&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>void start_task(void *pdata);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务函数</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span>//LED0</span><span style="font-family: 宋体;">任务</span> <span>#define LED0_TASK_PRIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务优先级</span> <span>#define LED0_STK_SIZE&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务堆栈大小</span> <span>OS_STK LED0_TASK_STK[LED0_STK_SIZE]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务堆栈</span><span>&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>void led0_task(void *pdata); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务函数</span> &nbsp; <span>//LED1</span><span style="font-family: 宋体;">任务</span> <span>#define LED1_TASK_PRIO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务优先级</span> <span>#define LED1_STK_SIZE&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">设置任务堆栈大小</span> <span>OS_STK LED1_TASK_STK[LED1_STK_SIZE]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务堆栈</span><span>&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>void led1_task(void *pdata); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">任务函数</span> <span>////////////////////////////////////////////////////////////////////////////// </span> <span>int main(void)</span> <span>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stm32_Clock_Init(9); //</span><span style="font-family: 宋体;">系统时钟设置</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay_init(72);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">延时初始化</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED_Init();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED_Init();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//</span><span style="font-family: 宋体;">初始化与</span><span>LED</span><span style="font-family: 宋体;">连接的硬件接口</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSInit();&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSTaskCreate(start_task,(void *)0,(OS_STK *)&amp;START_TASK_STK[START_STK_SIZE</span> <span>-1],START_TASK_PRIO );//</span><span style="font-family: 宋体;">创建起始任务</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSStart();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;</span> <span>}&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span> <span>//</span><span style="font-family: 宋体;">开始任务</span> <span>void start_task(void *pdata)</span> <span>{</span> <span>&nbsp;&nbsp;&nbsp; OS_CPU_SR cpu_sr=0;</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdata = pdata; </span> <span>&nbsp; &nbsp;&nbsp;&nbsp; OS_ENTER_CRITICAL();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">进入临界区</span><span>(</span><span style="font-family: 宋体;">无法被中断打断</span><span>)&nbsp;&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSTaskCreate(led0_task,(void *)0,(OS_STK*)&amp;LED0_TASK_STK[LED0_STK_SIZE-1],</span> <span>LED0_TASK_PRIO);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSTaskCreate(led1_task,(void *)0,(OS_STK*)&amp;LED1_TASK_STK[LED1_STK_SIZE-1],</span> <span>LED1_TASK_PRIO);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OSTaskSuspend(START_TASK_PRIO);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">挂起起始任务</span><span>.</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OS_EXIT_CRITICAL();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-family: 宋体;">退出临界区</span><span>(</span><span style="font-family: 宋体;">可以被中断打断</span><span>)</span> <span>} </span> <span>//LED0</span><span style="font-family: 宋体;">任务</span> <span>void led0_task(void *pdata)</span> <span>{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(1)</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED0=0; delay_ms(80); </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED0=1; delay_ms(920); </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</span> <span>}</span> &nbsp; <span>//LED1</span><span style="font-family: 宋体;">任务</span> <span>void led1_task(void *pdata)</span> <span>{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(1)</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED1=0; delay_ms(300); </span> <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LED1=1; delay_ms(300);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
38条回答
冬迹
1楼-- · 2019-10-20 07:53
回复【32楼】冬迹:
---------------------------------
1000/200
正点原子
2楼-- · 2019-10-20 11:14
回复【33楼】正点原子:
---------------------------------
再弱弱地问一句,为什么我用jlink无法仿真呢?
冬迹
3楼-- · 2019-10-20 12:58
回复【34楼】冬迹:
---------------------------------
设置有问题吧
正点原子
4楼-- · 2019-10-20 17:09
原子哥,在ucosii测试统计任务OS_TaskStat,遇到一个问题,编译器是eclipse,其中OSCPUUsage、OSStatRdy等等这些变量都没有定义,因为只在ucosii.h中extern了,并没有c文件进行声明,keil使用正常,但是eclipse就无法读取当前的OSCPUUsage值了,求帮助!谢谢。
linbeithree
5楼-- · 2019-10-20 20:46
 精彩回答 2  元偷偷看……
孔凤明
6楼-- · 2019-10-20 21:39
回复【37楼】孔凤明:
---------------------------------
这个我也不熟...

一周热门 更多>