STM32 在V3.5库函数下移植μCOS-II
<p style="text-indent: 2em;">相信大家使用STM32或者其他芯片的时候,绝大部分时间都是在写裸机程序,不过偶尔会写一些带系统的程序,那么问题来了,这个系统怎么选,又怎么移植呢?今天我给大家讲讲如何移植μCOS-II这个嵌入式系统到STM32下工作,为什么选μCOS-II,因为目前对它比较熟的嘛<img src="/UEditor/dialogs/emotion/images/face/i_f03.gif"/>,而且也适合使用在像STM32这种处理器上。下面就是移植的过程,有图哟<img src="/UEditor/dialogs/emotion/images/face/i_f07.gif"/>。关于移植过程中的一些基础知识,我想大家应该知道上网搜索学习吧。</p><p style="text-indent: 0em;"> 在移植之前,先下载要用到的μCOS-II的官方移植版本。地址:<a class="btn btn-success" href="https://pan.baidu.com/s/1bnk2QLL" target="_blank">点此下载</a> 密码:<span style="color: rgb(255, 0, 0);">euom</span>。</p><p class="artical_littlestyle1">1、首先在Keil下建立STM32库函数工程</p><p style="text-indent: 2em;">如果还不会在Keil下建立STM32库函数工程,或者记不清了,可以参考我的博文:<a href="http://www.only2fire.com/archives/3.html" target="_blank"><span style="color: rgb(0, 112, 192);">Keil下建立STM32库函数工程</span></a>,这里有详细的说明,也是带图教程哦<img src="/UEditor/dialogs/emotion/images/face/i_f29.gif"/>。<br/></p><p class="artical_littlestyle2">2、在工程下,建立存放系统文件的目录</p><p style="text-indent: 2em;">在搭建好的工程下,建立一个UCOS-II的文件夹,然后在该文件夹下,再新建两个文件夹:SRC、PORT,效果如下:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324772300900.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324772300900.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324772238891.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324772238891.png')"/></p><p class="artical_littlestyle3">3、拷贝源码的相关文件到相应的目录</p><p style="text-indent: 2em;">注:<span style="color: rgb(255, 0, 0);">UCOS-II\SRC</span>下的代码是 ucosii 中无需修改部分,<span style="color: rgb(255, 0, 0);">UCOS-II\PORT</span>下的代码是移植时需要修改的。</p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(1)、</span>把<span style="color: rgb(0, 112, 192);">Micrium\Software\uCOS-II\Source</span>下的文件拷贝至<span style="color: rgb(0, 112, 192);">Test\UCOS-II\SRC</span>:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324773745814.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324773745814.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(2)、</span>把<span style="color: rgb(0, 112, 192);">Micrium\Software\uCOS-II\Ports\arm-cortex-m3\Generic\IAR</span>下的文件拷贝至 <span style="color: rgb(0, 112, 192);">Test\UCOS-II\PORT</span>,把<span style="color: rgb(0, 112, 192);">Micrium\Software\EvalBoards\ST\STM32F103ZE-SK\IAR\OS-Probe-LCD</span>下的<span style="color: rgb(255, 0, 0);">os_cfg.h</span>文件拷贝到<span style="color: rgb(0, 112, 192);">Test\UCOS-II\PORT</span>文件夹下:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324772296385.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324772296385.png')"/></p><p class="artical_littlestyle4">4、配置Keil工程</p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(1)、</span>在Keil下建立如下组,然后添加对应文件到组下:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324772953242.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324772953242.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(2)、</span>添加相应的头文件路径:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324772200621.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324772200621.png')"/></p><p class="artical_littlestyle1">5、修改移植时需要修改的文件</p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(1)、</span><span style="color: rgb(255, 0, 0);">os_cpu.h</span>:<br/></p><p style="text-indent: 2em;">注释掉os_cpu.h文件中下面的这三个函数:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">void OS_CPU_SysTickHandler(void); <br/></span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">void OS_CPU_SysTickInit(void); <br/></span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">INT32U OS_CPU_SysTickClkFreq(void);</span></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324774271229.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324774271229.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(2)、</span><span style="color: rgb(255, 0, 0);">os_cpu_c.c</span>:</p><p style="text-indent: 2em;">把下面这些宏定义也注释掉,把 <span style="color: rgb(0, 112, 192);">OS_CPU_SysTickHandler()</span>, OS_CPU_SysTickInit()也注释掉:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324774777380.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324774777380.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324776360085.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324776360085.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324776137374.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324776137374.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(3)、</span><span style="color: rgb(255, 0, 0);">os_cpu_a.asm</span>:<br/></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OS_CPU_SR_Save ; Functions declared in this file</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OS_CPU_SR_Restore</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OSStartHighRdy</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OSCtxSw</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OSIntCtxSw</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PUBLIC OS_CPU_PendSVHandler</span><br/></p><p style="text-indent: 2em;">将以上的<span style="color: rgb(255, 0, 0);">PUBLIC</span>改为<span style="color: rgb(255, 0, 0);">EXPORT</span>,如下图:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324778708690.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324778708690.png')"/></p><p style="text-indent: 2em;">将<span style="color: rgb(255, 0, 0);">RSEG CODE:CODE:NOROOT(2)</span>修改为:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">;RSEG CODE:CODE:NOROOT(2)</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">AREA |.text|, CODE, READONLY, ALIGN=2</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">THUMB</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">REQUIRE8</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">PRESERVE8</span>如下图:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324778409098.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324778409098.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(4)、</span><span style="color: rgb(255, 0, 0);">os_dbg.c</span>:<br/></p><p style="text-indent: 2em;">将该文件中的</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_COMPILER_OPT __root</span>修改为:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_COMPILER_OPT //__root</span>,修改结果如下:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324814707359.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324814707359.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(5)、</span><span style="color: rgb(255, 0, 0);">startup_stm32f10x_hd.s</span>:</p><p style="text-indent: 2em;">搜索<span style="color: rgb(0, 112, 192);">startup_stm32f10x_hd.s</span>中的<span style="color: rgb(255, 0, 0);">PendSV_Handler</span>,然后用<span style="color: rgb(255, 0, 0);">OS_CPU_PendSVHandler</span>把所有出现<span style="color: rgb(255, 0, 0);">PendSV_Handler</span>的地方替换掉,总共三个地方:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324814187054.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324814187054.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324814953556.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324814953556.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(6)、</span><span style="color: rgb(255, 0, 0);">os_cfg.h</span>:</p><p style="text-indent: 2em;">对其做如下修改:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_APP_HOOKS_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_DEBUG_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_EVENT_MULTI_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_SCHED_LOCK_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TICK_STEP_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TASK_CHANGE_PRIO_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TASK_QUERY_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TASK_STAT_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TASK_STAT_STK_CHK_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TASK_SUSPEND_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_FLAG_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_MBOX_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TIME_DLY_RESUME_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TIME_GET_SET_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TIME_TICK_HOOK_EN 0</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#define OS_TMR_EN 0</span></p><p style="text-indent: 2em;">所做的修改主要是把一些功能给去掉,减少内核大小,也利于调试。等移植完成后,如果需要该功能,再做开启。</p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(7)、</span><span style="color: rgb(255, 0, 0);">stm32f10x_it.c</span>:</p><p style="text-indent: 2em;">在该文件引用头文件:<span style="color: rgb(0, 112, 192);">#include “ucos_ii.h”</span></p><p style="text-indent: 2em;">在<span style="color: rgb(255, 0, 0);">void SysTick_Handler(void)</span> 函数中添加如下代码:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">OSIntEnter();</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">OSTimeTick();</span></p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">OSIntExit();</span><br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324815983262.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324815983262.png')"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324814498754.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324814498754.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(8)、</span><span style="color: rgb(255, 0, 0);">ucos_ii.h</span>:</p><p style="text-indent: 2em;">注释掉<span style="color: rgb(0, 112, 192);">#include <app_cfg.h></span>:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324815245983.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324815245983.png')"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(118, 146, 60);">(9)、</span><span style="color: rgb(255, 0, 0);">main.c</span>:<br/></p><p style="text-indent: 2em;">在main.c文件中引用ucos_ii.h文件:</p><p style="text-indent: 2em;"><span style="color: rgb(0, 112, 192);">#include “ucos_ii.h”</span></p><p style="text-indent: 2em;">main.c文件中的代码如下:</p><pre class="brush:cpp;toolbar:false PrismJs">#include "stm32f10x.h"
#include "ucos_ii.h"
#define TASK_SIZE 128
OS_STK StartTask_Stk[TASK_SIZE];
OS_STK Task1_Stk[TASK_SIZE];
OS_STK Task2_Stk[TASK_SIZE];
void SysTick_Init(void)
{
RCC_ClocksTypeDef rcc_clocks;
RCC_GetClocksFreq(&rcc_clocks);
SysTick_Config(rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC);
}
void MyTask1(void *p)
{
while(1)
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);
OSTimeDlyHMSM(0,0,1,0);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
OSTimeDlyHMSM(0,0,1,0);
}
}
void MyTask2(void *p)
{
while(1)
{
GPIO_SetBits(GPIOC,GPIO_Pin_7);
OSTimeDlyHMSM(0,0,1,0);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
OSTimeDlyHMSM(0,0,1,0);
}
}
void StartUpTask(void *p)
{
SysTick_Init();/* Initialize the SysTick. */
#if (OS_TASK_STAT_EN > 0)
OSStatInit();/* Determine CPU capacity. */
#endif
OSTaskCreate(MyTask1,(void *)0,&Task1_Stk[TASK_SIZE-1],2);
OSTaskCreate(MyTask2,(void *)0,&Task2_Stk[TASK_SIZE-1],3);
OSTaskDel(OS_PRIO_SELF);
}
int main(void)
{
GPIO_InitTypeDef GPIO_Pin;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能PC端口时钟
GPIO_Pin.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_Pin.GPIO_Mode=GPIO_Mode_Out_PP;//IO口为推挽输出模式
GPIO_Pin.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHz
GPIO_Init(GPIOC,&GPIO_Pin);//初始化PC口
OSInit();
OSTaskCreate(StartUpTask,(void *)0,&StartTask_Stk[TASK_SIZE - 1],1);
OSStart();
}</pre><p style="text-indent: 2em;">上述代码创建了两个任务,实现了PC6与PC7的IO口电平切换。<br/></p><p style="text-indent: 2em;">仿真结果如下图:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.15/1534324815778544.png" onclick="preview_image('/uploads/AilsonJack/2018.08.15/1534324815778544.png')"/></p><p style="text-indent: 2em;">至此,在STM32上移植μCOS-II的工作就完成了,接下来,你就可以在这个工程下建立自己的应用程序。</p>
暂无评论,要不要来个沙发
发表评论
JLink V9掉固件修复(灯不亮) 3Zephyr笔记2 - 在STM32F429上运行HelloWorld 2计算NandFlash要传入的行地址和列地址 1Linux MMC子系统 - 6.eMMC 5.1工作模式-设备识别模式 0Linux MMC子系统 - 5.eMMC 5.1工作模式-引导模式 0Linux MMC子系统 - 4.eMMC 5.1常用命令说明(2) 0
标签云
Linux嵌入式实用技巧ARM内核学习问题集合CC++编程语言阅读笔记汇编Linux内核完全注释Windows驱动开发计算机基础ARM11ARMv7-ASTM32IDESublimeLinux内核学习eMMCMMC子系统Ubuntu操作系统OfficeVMWareAPUEgccRTOS中断漫游世界随笔感悟开发工具软件应用编程VsCodearmccarmclang编译器ZephyrSPIJLink网卡驱动安装各种芯片库函数NFSμCOS内核sambaFlashUnix命令与脚本输入法Linux内核设计与实现gitRIFFWAVJATGFTPar8161安装centos有线上网μCGUI字库工程建立右键菜单网络文件系统Firefox百度NTFS文件系统CodeBlocksCentOS数据结构算法PhotoShop51KeilQTUltraEditscanfglibc宏定义UIDGID优先级娱乐天地SourceInsight磁盘扇区总线I2CPDFBComparePythonI2SFPUMakefileSWDCPUARP软件推荐FileZilla