找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6039|回复: 6
收起左侧

基于Cortex-M0的ucos ii可运行源码工程

[复制链接]
ID:329972 发表于 2018-5-14 21:57 | 显示全部楼层 |阅读模式
编译通过可以

单片机源程序如下:
  1. /**************************************************************************
  2. 功    能: Cortex-M0上跑V2.86版的ucosii                                                          
  3. 编译环境: MDKV4.12                                                   
  4. 时    钟: 外部12Mhz                                                                                           
  5. 日    期: 11/03/14                                                           
  6. by      : AVR_DIY(苹果另一半)                                                                                 
  7. email   : syyhcl@163.com                                                                                                 
  8. **************************************************************************/
  9. #include "LPC11xx.h"                        /* LPC11xx definitions */
  10. #include "gpio.h"
  11. #include "config.h"
  12. #include "device_init.h"
  13. #include "ucos_ii.h"

  14. OS_STK TaskStartStk1[MaxStkSize];              //定义任务堆栈大小

  15. void TaskLed(void *nouse);
  16. /****************************************
  17.               main
  18. 函数功能 : 主函数
  19. 参数描述 : 无
  20. 返回值   : 无
  21. ****************************************/
  22. int main (void)
  23. {
  24.     CPU_IntDis();                                        //禁止所有中断
  25.         DeviceInit();                                        //初始化设备
  26.     OSInit();                                                //初始化OS
  27.         OSTaskCreate(TaskLed, (void *)0, &TaskStartStk1[MaxStkSize-1],1);            //创建Led显示任务
  28.         OSStart();                                                             //启动ucos ii 永远不返回
  29. }

  30. /***************************************
  31.               LED任务
  32. ***************************************/
  33. void TaskLed(void *nouse)
  34. {
  35.     nouse=nouse;                                                                         //防止编译器警告
  36.     CPU_IntEn();                                     //开时钟节拍中断
  37.     while(1)
  38.     {
  39.                 LED_TOG;
  40.         OSTimeDlyHMSM(0,0,0,50);
  41.     }   
  42. }
复制代码

所有资料51hei提供下载:
在cortex——M0上移植移植的Ucosii系统.rar (473.33 KB, 下载次数: 49)
回复

使用道具 举报

ID:162136 发表于 2018-12-31 14:40 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:493724 发表于 2019-3-19 09:43 | 显示全部楼层
M0、M3的ucOS II的移植都没难度
回复

使用道具 举报

ID:493724 发表于 2019-3-19 10:25 | 显示全部楼层
cortex移植ucOS II主要在于os_cpu_a.asm的改写

M3的os_cpu_a.asm在Micrium有官方移植例程

M0的当时没找到,不知现在有没有

附件是我写的os_cpu_a.asm,M3版本改写的

写了两个版本MSP和PSP,一般用PSP的

M0和M3的指令集有差异,需要修改STM和LDM的用法,参考PendSV_Handler的中文注释


  1. ;********************************************************************************************************
  2. ;                                               uC/OS-II
  3. ;                                         The Real-Time Kernel
  4. ;
  5. ;                               (c) Copyright 1992-2006, Micrium, Weston, FL
  6. ;                                          All Rights Reserved
  7. ;
  8. ;                                           ARM Cortex-M3 Port
  9. ;
  10. ; File      : OS_CPU_A.ASM
  11. ; Version   : V2.89
  12. ; By        : Jean J. Labrosse
  13. ;             Brian Nagel
  14. ;
  15. ; For       : ARMv7M Cortex-M3
  16. ; Mode      : Thumb2
  17. ; Toolchain : IAR EWARM
  18. ;********************************************************************************************************

  19. ;********************************************************************************************************
  20. ;                                           PUBLIC FUNCTIONS
  21. ;********************************************************************************************************
  22.                                                                 ; External references.
  23.     EXTERN  OSRunning
  24.     EXTERN  OSPrioCur
  25.     EXTERN  OSPrioHighRdy
  26.     EXTERN  OSTCBCur
  27.     EXTERN  OSTCBHighRdy
  28.     EXTERN  OSIntNesting
  29.     EXTERN  OSIntExit
  30.     EXTERN  OSTaskSwHook
  31. ;        EXTERN        __vector_table

  32.                                                                 ; Functions declared in this file.
  33.     PUBLIC  OS_CPU_SR_Save
  34.     PUBLIC  OS_CPU_SR_Restore
  35.     PUBLIC  OSStartHighRdy
  36.     PUBLIC  OSCtxSw
  37.     PUBLIC  OSIntCtxSw
  38.     PUBLIC  PendSV_Handler

  39. ;********************************************************************************************************
  40. ;                                                EQUATES
  41. ;********************************************************************************************************

  42. NVIC_INT_CTRL   EQU     0xE000ED04                              ; Interrupt control state register.
  43. NVIC_SYSPRI14   EQU     0xE000ED22                              ; System priority register (priority 14).
  44. NVIC_PENDSV_PRI EQU           0xFF                              ; PendSV priority value (lowest).
  45. NVIC_PENDSVSET  EQU     0x10000000                              ; Value to trigger PendSV exception.


  46. ;********************************************************************************************************
  47. ;                                      CODE GENERATION DIRECTIVES
  48. ;********************************************************************************************************

  49.                 RSEG CSTACK:DATA:NOROOT(2)
  50.         RSEG CODE:CODE:NOROOT(2)
  51.         THUMB

  52. ;*********************************************************************************************************
  53. ;                                  CRITICAL SECTION METHOD 3 FUNCTIONS
  54. ;
  55. ; Description: Disable/Enable interrupts by preserving the state of interrupts.  Generally speaking you
  56. ;              would store the state of the interrupt disable flag in the local variable 'cpu_sr' and then
  57. ;              disable interrupts.  'cpu_sr' is allocated in all of uC/OS-II's functions that need to
  58. ;              disable interrupts.  You would restore the interrupt disable state by copying back 'cpu_sr'
  59. ;              into the CPU's status register.
  60. ;
  61. ; Prototypes : OS_CPU_SR  OS_CPU_SR_Save    (void);
  62. ;              void       OS_CPU_SR_Restore (OS_CPU_SR  os_cpu_sr);
  63. ;
  64. ;
  65. ; Note(s)    : (1) These functions are used in general like this:
  66. ;
  67. ;                 void Task (void  *p_arg)
  68. ;                 {
  69. ;                                                               /* Allocate storage for CPU status register.            */
  70. ;                 #if (OS_CRITICAL_METHOD == 3)
  71. ;                      OS_CPU_SR  os_cpu_sr;
  72. ;                 #endif
  73. ;
  74. ;                          :
  75. ;                          :
  76. ;                      OS_ENTER_CRITICAL();                     /* os_cpu_sr = OS_CPU_SR_Save();                        */
  77. ;                          :
  78. ;                          :
  79. ;                      OS_EXIT_CRITICAL();                      /* OS_CPU_SR_Restore(cpu_sr);                           */
  80. ;                          :
  81. ;                          :
  82. ;                 }
  83. ;*********************************************************************************************************

  84. OS_CPU_SR_Save
  85.     MRS     R0, PRIMASK                                         ; Set prio int mask to mask all (except faults)
  86.     CPSID   I
  87.     BX      LR

  88. OS_CPU_SR_Restore
  89.     MSR     PRIMASK, R0
  90.     BX      LR

  91. ;*********************************************************************************************************
  92. ;                                           START MULTITASKING
  93. ;                                       void OSStartHighRdy(void)
  94. ;
  95. ; Note(s) : 1) OSStartHighRdy() MUST:
  96. ;              a) Call OSTaskSwHook() then,
  97. ;              b) Set OSRunning to TRUE,
  98. ;              c) Switch to the highest priority task.
  99. ;*********************************************************************************************************
  100. OSStartHighRdy
  101.         CPSID   I                                                   ; Prevent interruption during OSStartHighRdy
  102.     LDR     R0, =0xE000ED20                                     ; Set the PendSV exception priority, must lowest
  103.     LDR                R1, =0x00FF0000
  104.         LDR                R2, [R0]
  105.         ORRS        R2, R2, R1
  106.         STR                R2, [R0]

  107.     LDR     R0, =OSRunning                                      ; OSRunning = TRUE
  108.     MOVS    R1, #1
  109.     STRB    R1, [R0]
  110.        
  111.         LDR     R0, =OSTaskSwHook                                   ; OSTaskSwHook();
  112.     BLX     R0

  113.     LDR     R0, =OSTCBCur                                       ; OSTCBCur  = OSTCBHighRdy;
  114.     LDR     R1, =OSTCBHighRdy
  115.     LDR     R2, [R1]
  116.     STR     R2, [R0]
  117.    
  118.     LDR     R0, [R2]                                            ; R0 is new process SP; SP = OSTCBHighRdy->OSTCBStkPtr;
  119.     MSR     PSP, R0                                                                                                ; PSP = R0
  120.     MOVS    R0, #2
  121.     MSR     CONTROL, R0                                                                                        ; SP used PSP
  122.     ISB
  123.    
  124.         LDR     R0,        =SFE(CSTACK)
  125. ;        LDR                R0, =__vector_table
  126. ;        LDR                R0, [R0]
  127.     MSR                MSP, R0                                                                                                ; MSP = CSTACK
  128.    
  129.         POP     {R0-R7}                                                                                                ; R8-R11, R4-R7
  130.         MOV     R8,        R0
  131.         MOV     R9,        R1
  132.         MOV     R10,R2
  133.         MOV     R11,R3

  134.         ADD     SP,        SP,        #0x10                                                                                ; R12, LR, PC, PSR
  135.         POP     {R0-R3}
  136.         MOV     R12,R0
  137.         MOV     LR,        R1
  138.         MSR     PSR,R3
  139.         ISB
  140.        
  141.         PUSH        {R2}                                                                                                ; PUSH PC
  142.        
  143.         SUB     SP,        SP,        #0x1C                                                                                ; R0-R3
  144.         POP     {R0-R3}
  145.         ADD     SP,        SP,        #0x0C
  146.        
  147.         CPSIE   I     
  148.     POP                {PC}
  149. OSStartHang
  150.     B       OSStartHang                                         ; Should never get here


  151. ;********************************************************************************************************
  152. ;                               PERFORM A CONTEXT SWITCH (From task level)
  153. ;                                           void OSCtxSw(void)
  154. ;
  155. ; Note(s) : 1) OSCtxSw() is called when OS wants to perform a task context switch.  This function
  156. ;              triggers the PendSV exception which is where the real work is done.
  157. ;********************************************************************************************************

  158. OSCtxSw
  159.     LDR     R0, =NVIC_INT_CTRL                                  ; Trigger the PendSV exception (causes context switch)
  160.     LDR     R1, =NVIC_PENDSVSET
  161.     STR     R1, [R0]
  162.     BX      LR

  163. ;********************************************************************************************************
  164. ;                             PERFORM A CONTEXT SWITCH (From interrupt level)
  165. ;                                         void OSIntCtxSw(void)
  166. ;
  167. ; Notes:    1) OSIntCtxSw() is called by OSIntExit() when it determines a context switch is needed as
  168. ;              the result of an interrupt.  This function simply triggers a PendSV exception which will
  169. ;              be handled when there are no more interrupts active and interrupts are enabled.
  170. ;********************************************************************************************************

  171. OSIntCtxSw
  172.     LDR     R0, =NVIC_INT_CTRL                                  ; Trigger the PendSV exception (causes context switch)
  173.     LDR     R1, =NVIC_PENDSVSET
  174.     STR     R1, [R0]
  175.     BX      LR

  176. ;********************************************************************************************************
  177. ;                                         HANDLE PendSV EXCEPTION
  178. ;                                     void PendSV_Handler(void)
  179. ;
  180. ; Note(s) : 1) PendSV is used to cause a context switch.  This is a recommended method for performing
  181. ;              context switches with Cortex-M0.  This is because the Cortex-M0 auto-saves half of the
  182. ;              processor context on any exception, and restores same on return from exception.  So only
  183. ;              saving of R4-R11 is required and fixing up the stack pointers.  Using the PendSV exception
  184. ;              this way means that context saving and restoring is identical whether it is initiated from
  185. ;              a thread or occurs due to an interrupt or exception.
  186. ;
  187. ;           2) Pseudo-code is:
  188. ;              a) Get the process SP, if 0 then skip (goto d) the saving part (first context switch);
  189. ;              b) Save remaining regs r4-r11 on process stack;
  190. ;              c) Save the process SP in its TCB, OSTCBCur->OSTCBStkPtr = SP;
  191. ;              d) Call OSTaskSwHook();
  192. ;              e) Get current high priority, OSPrioCur = OSPrioHighRdy;
  193. ;              f) Get current ready thread TCB, OSTCBCur = OSTCBHighRdy;
  194. ;              g) Get new process SP from TCB, SP = OSTCBHighRdy->OSTCBStkPtr;
  195. ;              h) Restore R4-R11 from new process stack;
  196. ;              i) Perform exception return which will restore remaining context.
  197. ;
  198. ;           3) On entry into PendSV handler:
  199. ;              a) The following have been saved on the process stack (by processor):
  200. ;                 xPSR, PC, LR, R12, R0-R3
  201. ;              b) Processor mode is switched to Handler mode (from Thread mode)
  202. ;              c) Stack is Main stack (switched from Process stack)
  203. ;              d) OSTCBCur      points to the OS_TCB of the task to suspend
  204. ;                 OSTCBHighRdy  points to the OS_TCB of the task to resume
  205. ;
  206. ;           4) Since PendSV is set to lowest priority in the system (by OSStartHighRdy() above), we
  207. ;              know that it will only be run when no other exception or interrupt is active, and
  208. ;              therefore safe to assume that context being switched out was using the process stack (PSP).
  209. ;
  210. ;        2) b) save remaining regs r4-r11 on process stack;
  211. ;        先将PSP赋给R0作为目标地址
  212. ;        方法一:
  213. ;        先偏移4个字,传输R4-R7;继续偏移8个字,传输R8-R11
  214. ;        SUBS    R0, R0, #0x10                                       ; Save remaining regs r4-11 on process stack
  215. ;        STM     R0!, {R4-R7}
  216. ;        SUBS    R0, R0, #0x20
  217. ;        MOV     R4,        R8
  218. ;        MOV     R5,        R9
  219. ;        MOV     R6, R10
  220. ;        MOV     R7, R11
  221. ;        STM     R0!, {R4-R7}
  222. ;        SUBS    R0, R0, #0x10
  223. ;        方法二:
  224. ;        先偏移8个字,借助R1传输R8;借助R1-R3,传输R9-R11,R4-R7
  225. ;        SUBS    R0, R0, #0x20                                       ; Save remaining regs r4-11 on process stack
  226. ;        MOV     R1,        R8
  227. ;        STM     R0!, {R1}
  228. ;        MOV     R1,        R9
  229. ;        MOV     R2, R10
  230. ;        MOV     R3, R11
  231. ;        STM     R0!, {R1-R7}
  232. ;        SUBS    R0, R0, #0x20
  233. ;********************************************************************************************************
  234. PendSV_Handler
  235.     CPSID   I                                                   ; Prevent interruption during context switch
  236.     PUSH    {LR}                                                ; Save LR exc_return value
  237.    
  238.     MRS     R0, PSP                                             ; PSP is process stack pointer
  239.         SUBS    R0, R0, #0x20
  240.         LDR     R1, =OSTCBCur                                       ; OSTCBCur->OSTCBStkPtr = SP;
  241.     LDR     R1, [R1]
  242.     STR     R0, [R1]                                            ; R0 is SP of process being switched out
  243.        
  244.         MOV     R1,        R8                                                                                                ; Save remaining regs r4-11 on process stack
  245.         STM     R0!, {R1}
  246.         MOV     R1,        R9
  247.         MOV     R2, R10
  248.         MOV     R3, R11
  249.         STM     R0!, {R1-R7}
  250.         SUBS    R0, R0, #0x20
  251.    
  252. ;        LDR     R0, =OSTaskSwHook                                   ; OSTaskSwHook();
  253. ;        BLX     R0

  254.     LDR     R0, =OSPrioCur                                      ; OSPrioCur = OSPrioHighRdy;
  255.     LDR     R1, =OSPrioHighRdy
  256.     LDRB    R2, [R1]
  257.     STRB    R2, [R0]

  258.     LDR     R0, =OSTCBCur                                       ; OSTCBCur  = OSTCBHighRdy;
  259.     LDR     R1, =OSTCBHighRdy
  260.     LDR     R2, [R1]
  261.     STR     R2, [R0]

  262.     LDR     R0, [R2]                                            ; R0 is new process SP; SP = OSTCBHighRdy->OSTCBStkPtr;

  263.     LDM                R0!,{R1}                                                                                        ; Restore R8-11, R4-7 from new process stack
  264.     MOV                R8, R1
  265.     LDM     R0!,{R1-R7}
  266.         MOV     R9,        R1
  267.         MOV     R10,R2
  268.         MOV     R11,R3

  269.     MSR     PSP, R0                                             ; Load PSP with new process SP
  270.     CPSIE   I
  271.     POP     {PC}                                                ; Exception return will restore remaining context

  272.     END
复制代码



os_cpu_a.rar

4.4 KB, 下载次数: 7, 下载积分: 黑币 -5

os_cpu_a.asm MSP及PSP版本

回复

使用道具 举报

ID:598797 发表于 2019-8-10 12:56 | 显示全部楼层
我下载看看,主要是汇编部分不会弄
回复

使用道具 举报

ID:76719 发表于 2019-8-11 23:03 | 显示全部楼层
很好的资料,也正在学习UCOS,还不知道如何去用.
回复

使用道具 举报

ID:383905 发表于 2019-12-17 11:00 | 显示全部楼层
学习,学习,希望更多的大神分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表