Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23875
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-16 10:01
文章分类
文章存档

2016年(7)

我的朋友

分类: C/C++

2016-09-01 18:40:04

uCosII移植到stm32上的文章和demo已经很多了,细节上建议大家可以看官方的移植文档(难过当 然是E文的)。网上流传的各种移植版本基本都是基于官方的移植版本做了小改进。这些改进基本都限制在更适合自己的项目或自己的使用习惯上。当然我也一样, 我的改进是为了搭建一个平台,只要stm32+ucos平台都使用这个版本,无论是我使用或是一起开发者,能更快上手。

  uCosII V2.86版本在cortex-m3的移植上面有bug,具体可以自己google一下。我目前使用的是V2.91版本,建立在stm32 V3.40的库基本上。我是强烈要求(或是强制要求)协同开发者使用库函数,除非在速度要求高的情况下或中断里需要直接操作寄存器,也必须使用库的地址定 义,并要求在后面注明对应的库函数。因为程序不是你一个人看的……例如:


  1. /* Clear the selected DMA interrupt pending bits */  
  2. DMA1->IFCR = DMA1_IT_TC4;  //DMA_ClearITPendingBit(DMA1_IT_TC4);  
  3. /* Disable the selected DMAy Channelx */  
  4. DMA1_Channel4->CCR &= (u16)(~DMA_CCR1_EN);//DMA_Cmd(DMA1_Channel4, DISABLE);  

好了,进入正题。uCos在stm32上的移植官方的版本是默认不支持中断嵌套的(个人理解),因为其没有调用 NVIC_PriorityGroupConfig这一函数或使用相同的功能语句。stm32在复位的状态下默认进去 NVIC_PriorityGroup_0,即只要子优先级没有主优先级,即先到先处理,同时到达再按优先级处理。 官方在这上面也是留有空间,因为NVIC_PriorityGroupConfig只能调用一次,采用默认状态,开发者可以加入并完善使用中断嵌套。 


官方的移植把所有的应用中断入口都指向了 BSP_IntHandler这个函数,并定义了一个函数数组static  CPU_FNCT_VOID  BSP_IntVectTbl[BSP_INT_SRC_NBR];因此,如果要加入自己的中断函数的话,只需把自己写的中断函数的指针存入这个数组就 OK了,在初始化中断前调用这函数BSP_IntVectSet。官方的中断优先级设置方式,我全删除了,使用库函数来设置将更直接,但这种中断方式保存 下来,这样中断函数更自由,写中断函数时也不用考虑太多东西,只负责自己要做的东西。


  1. /* 
  2. ********************************************************************************************************* 
  3. *                                             INCLUDE FILES 
  4. ********************************************************************************************************* 
  5. */  
  6.   
  7. #define  BSP_INTERRUPT_MODULE  
  8.   
  9. #include   
  10. #include  //STM32芯片内部寄存器定议  
  11.   
  12. #include   
  13.   
  14. #include   
  15.   
  16. /* 
  17. ********************************************************************************************************* 
  18. *                                            LOCAL DEFINES 
  19. ********************************************************************************************************* 
  20. */  
  21. #define  BSP_INT_SRC_NBR                                 60  
  22.   
  23.   
  24. /* 
  25. ********************************************************************************************************* 
  26. *                                            LOCAL TABLES 
  27. ********************************************************************************************************* 
  28. */  
  29.   
  30. static  CPU_FNCT_VOID  BSP_IntVectTbl[BSP_INT_SRC_NBR];  
  31.   
  32.   
  33. /* 
  34. ********************************************************************************************************* 
  35. *                                      LOCAL FUNCTION PROTOTYPES 
  36. ********************************************************************************************************* 
  37. */  
  38.   
  39. static  void  BSP_IntHandler(u16  int_id);  
  40. static  void  BSP_IntHandlerDummy(void);  
  41.   
  42.   
  43. /* 
  44. ********************************************************************************************************* 
  45. *                                            BSP_IntVectSet() 
  46. * 
  47. * Description : Assign ISR handler. 
  48. * 
  49. * Argument(s) : int_id      Interrupt for which vector will be set. 
  50. * 
  51. *               isr         Handler to assign 
  52. * 
  53. * Return(s)   : none. 
  54. * 
  55. * Caller(s)   : Application. 
  56. * 
  57. * Note(s)     : none. 
  58. ********************************************************************************************************* 
  59. */  
  60.   
  61. void  BSP_IntVectSet (u16       int_id,  
  62.                       CPU_FNCT_VOID  isr)  
  63. {  
  64.   
  65. #if OS_CRITICAL_METHOD == 3u                            /* Allocate storage for CPU status register    */  
  66.     OS_CPU_SR     cpu_sr = 0u;  
  67. #endif  
  68.       
  69.   
  70.     if(int_id < BSP_INT_SRC_NBR){  
  71.         OS_ENTER_CRITICAL();  
  72.         BSP_IntVectTbl[int_id] = isr;  
  73.         OS_EXIT_CRITICAL();  
  74.     }  
  75. }  
  76.   
  77.   
  78.   
  79. /* 
  80. ********************************************************************************************************* 
  81. ********************************************************************************************************* 
  82. *                                           INTERNAL FUNCTIONS 
  83. ********************************************************************************************************* 
  84. ********************************************************************************************************* 
  85. */  
  86.   
  87. /* 
  88. ********************************************************************************************************* 
  89. *                                              BSP_IntInit() 
  90. * 
  91. * Description : Initialize interrupts: 
  92. * 
  93. * Argument(s) : none. 
  94. * 
  95. * Return(s)   : none. 
  96. * 
  97. * Caller(s)   : BSP_Init(). 
  98. * 
  99. * Note(s)     : none. 
  100. ********************************************************************************************************* 
  101. */  
  102.   
  103. void  BSP_IntInit (void)  
  104. {  
  105.     u16  int_id;  
  106.     OS_CPU_SR  cpu_sr;  
  107.   
  108.   
  109.     OS_ENTER_CRITICAL();                         /* Tell uC/OS-II that we are starting an ISR          */  
  110.     OSIntNeedSW = OSIntSW_Disable;               // 需退出中断时进行任务调度切换  
  111.     OS_EXIT_CRITICAL();  
  112.   
  113.     for (int_id = 0; int_id < BSP_INT_SRC_NBR; int_id++) {  
  114.         BSP_IntVectSet(int_id, BSP_IntHandlerDummy);  
  115.     }  
  116. }  
  117.   
  118.   
  119. /* 
  120. ********************************************************************************************************* 
  121. *                                        BSP_IntHandler####() 
  122. * 
  123. * Description : Handle an interrupt. 
  124. * 
  125. * Argument(s) : none. 
  126. * 
  127. * Return(s)   : none. 
  128. * 
  129. * Caller(s)   : This is an ISR. 
  130. * 
  131. * Note(s)     : none. 
  132. ********************************************************************************************************* 
  133. */  
  134. void  BSP_IntHandlerWWDG          (void)  { BSP_IntHandler(WWDG_IRQn);            }  
  135. void  BSP_IntHandlerPVD           (void)  { BSP_IntHandler(PVD_IRQn);             }  
  136. void  BSP_IntHandlerTAMPER        (void)  { BSP_IntHandler(TAMPER_IRQn);          }  
  137. void  BSP_IntHandlerRTC           (void)  { BSP_IntHandler(RTC_IRQn);             }  
  138. void  BSP_IntHandlerFLASH         (void)  { BSP_IntHandler(FLASH_IRQn);           }  
  139. void  BSP_IntHandlerRCC           (void)  { BSP_IntHandler(RCC_IRQn);             }  
  140. void  BSP_IntHandlerEXTI0         (void)  { BSP_IntHandler(EXTI0_IRQn);           }  
  141. void  BSP_IntHandlerEXTI1         (void)  { BSP_IntHandler(EXTI1_IRQn);           }  
  142. void  BSP_IntHandlerEXTI2         (void)  { BSP_IntHandler(EXTI2_IRQn);           }  
  143. void  BSP_IntHandlerEXTI3         (void)  { BSP_IntHandler(EXTI3_IRQn);           }  
  144. void  BSP_IntHandlerEXTI4         (void)  { BSP_IntHandler(EXTI4_IRQn);           }  
  145. void  BSP_IntHandlerDMA1_CH1      (void)  { BSP_IntHandler(DMA1_Channel1_IRQn);        }  
  146. void  BSP_IntHandlerDMA1_CH2      (void)  { BSP_IntHandler(DMA1_Channel2_IRQn);        }  
  147. void  BSP_IntHandlerDMA1_CH3      (void)  { BSP_IntHandler(DMA1_Channel3_IRQn);        }  
  148. void  BSP_IntHandlerDMA1_CH4      (void)  { BSP_IntHandler(DMA1_Channel4_IRQn);        }  
  149. void  BSP_IntHandlerDMA1_CH5      (void)  { BSP_IntHandler(DMA1_Channel5_IRQn);        }  
  150. void  BSP_IntHandlerDMA1_CH6      (void)  { BSP_IntHandler(DMA1_Channel6_IRQn);        }  
  151. void  BSP_IntHandlerDMA1_CH7      (void)  { BSP_IntHandler(DMA1_Channel7_IRQn);        }  
  152. void  BSP_IntHandlerADC1_2        (void)  { BSP_IntHandler(ADC1_2_IRQn);          }  
  153. void  BSP_IntHandlerUSB_HP_CAN_TX (void)  { BSP_IntHandler(USB_HP_CAN1_TX_IRQn);   }  
  154. void  BSP_IntHandlerUSB_LP_CAN_RX0(void)  { BSP_IntHandler(USB_LP_CAN1_RX0_IRQn);  }  
  155. void  BSP_IntHandlerCAN_RX1       (void)  { BSP_IntHandler(CAN1_RX1_IRQn);         }  
  156. void  BSP_IntHandlerCAN_SCE       (void)  { BSP_IntHandler(CAN1_SCE_IRQn);         }  
  157. void  BSP_IntHandlerEXTI9_5       (void)  { BSP_IntHandler(EXTI9_5_IRQn);         }  
  158. void  BSP_IntHandlerTIM1_BRK      (void)  { BSP_IntHandler(TIM1_BRK_IRQn);        }  
  159. void  BSP_IntHandlerTIM1_UP       (void)  { BSP_IntHandler(TIM1_UP_IRQn);         }  
  160. void  BSP_IntHandlerTIM1_TRG_COM  (void)  { BSP_IntHandler(TIM1_TRG_COM_IRQn);    }  
  161. void  BSP_IntHandlerTIM1_CC       (void)  { BSP_IntHandler(TIM1_CC_IRQn);         }  
  162. void  BSP_IntHandlerTIM2          (void)  { BSP_IntHandler(TIM2_IRQn);            }  
  163. void  BSP_IntHandlerTIM3          (void)  { BSP_IntHandler(TIM3_IRQn);            }  
  164. void  BSP_IntHandlerTIM4          (void)  { BSP_IntHandler(TIM4_IRQn);            }  
  165. void  BSP_IntHandlerI2C1_EV       (void)  { BSP_IntHandler(I2C1_EV_IRQn);         }  
  166. void  BSP_IntHandlerI2C1_ER       (void)  { BSP_IntHandler(I2C1_ER_IRQn);         }  
  167. void  BSP_IntHandlerI2C2_EV       (void)  { BSP_IntHandler(I2C2_EV_IRQn);         }  
  168. void  BSP_IntHandlerI2C2_ER       (void)  { BSP_IntHandler(I2C2_ER_IRQn);         }  
  169. void  BSP_IntHandlerSPI1          (void)  { BSP_IntHandler(SPI1_IRQn);            }  
  170. void  BSP_IntHandlerSPI2          (void)  { BSP_IntHandler(SPI2_IRQn);            }  
  171. void  BSP_IntHandlerUSART1        (void)  { BSP_IntHandler(USART1_IRQn);          }  
  172. void  BSP_IntHandlerUSART2        (void)  { BSP_IntHandler(USART2_IRQn);          }  
  173. void  BSP_IntHandlerUSART3        (void)  { BSP_IntHandler(USART3_IRQn);          }  
  174. void  BSP_IntHandlerEXTI15_10     (void)  { BSP_IntHandler(EXTI15_10_IRQn);       }  
  175. void  BSP_IntHandlerRTCAlarm      (void)  { BSP_IntHandler(RTCAlarm_IRQn);        }  
  176. void  BSP_IntHandlerUSBWakeUp     (void)  { BSP_IntHandler(USBWakeUp_IRQn);       }  
  177. void  BSP_IntHandlerTIM8_BRK      (void)  { BSP_IntHandler(TIM8_BRK_IRQn);        }  
  178. void  BSP_IntHandlerTIM8_UP       (void)  { BSP_IntHandler(TIM8_UP_IRQn);         }  
  179. void  BSP_IntHandlerTIM8_TRG_COM  (void)  { BSP_IntHandler(TIM8_TRG_COM_IRQn);    }  
  180. void  BSP_IntHandlerTIM8_CC       (void)  { BSP_IntHandler(TIM8_CC_IRQn);         }  
  181. void  BSP_IntHandlerADC3          (void)  { BSP_IntHandler(ADC3_IRQn);            }  
  182. void  BSP_IntHandlerFSMC          (void)  { BSP_IntHandler(FSMC_IRQn);            }  
  183. void  BSP_IntHandlerSDIO          (void)  { BSP_IntHandler(SDIO_IRQn);            }  
  184. void  BSP_IntHandlerTIM5          (void)  { BSP_IntHandler(TIM5_IRQn);            }  
  185. void  BSP_IntHandlerSPI3          (void)  { BSP_IntHandler(SPI3_IRQn);            }  
  186. void  BSP_IntHandlerUART4         (void)  { BSP_IntHandler(UART4_IRQn);           }  
  187. void  BSP_IntHandlerUART5         (void)  { BSP_IntHandler(UART5_IRQn);           }  
  188. void  BSP_IntHandlerTIM6          (void)  { BSP_IntHandler(TIM6_IRQn);            }  
  189. void  BSP_IntHandlerTIM7          (void)  { BSP_IntHandler(TIM7_IRQn);            }  
  190. void  BSP_IntHandlerDMA2_CH1      (void)  { BSP_IntHandler(DMA2_Channel1_IRQn);        }  
  191. void  BSP_IntHandlerDMA2_CH2      (void)  { BSP_IntHandler(DMA2_Channel2_IRQn);        }  
  192. void  BSP_IntHandlerDMA2_CH3      (void)  { BSP_IntHandler(DMA2_Channel3_IRQn);        }  
  193. void  BSP_IntHandlerDMA2_CH4_5    (void)  { BSP_IntHandler(DMA2_Channel4_5_IRQn);      }  
  194.   
  195.   
  196. /* 
  197. ********************************************************************************************************* 
  198. ********************************************************************************************************* 
  199. *                                           LOCAL FUNCTIONS 
  200. ********************************************************************************************************* 
  201. ********************************************************************************************************* 
  202. */  
  203.   
  204. /* 
  205. ********************************************************************************************************* 
  206. *                                          BSP_IntHandler() 
  207. * 
  208. * Description : Central interrupt handler. 
  209. * 
  210. * Argument(s) : int_id          Interrupt that will be handled. 
  211. * 
  212. * Return(s)   : none. 
  213. * 
  214. * Caller(s)   : ISR handlers. 
  215. * 
  216. * Note(s)     : none. 
  217. ********************************************************************************************************* 
  218. */  
  219.   
  220. static  void  BSP_IntHandler (u16  int_id)  
  221. {  
  222.   
  223. #if OS_CRITICAL_METHOD == 3u                            /* Allocate storage for CPU status register    */  
  224.     OS_CPU_SR     cpu_sr = 0u;  
  225. #endif  
  226.   
  227.     OS_ENTER_CRITICAL();                                      /* 不支持中断嵌套,无需关中断    */  
  228.     OSIntNesting++;  
  229.     OS_EXIT_CRITICAL();  
  230.   
  231.     if (int_id < BSP_INT_SRC_NBR) {  
  232.         BSP_IntVectTbl[int_id]();  
  233.     }  
  234.     OSIntExit();  
  235. }  
  236.   
  237.   
  238. /* 
  239. ********************************************************************************************************* 
  240. *                                        BSP_IntHandlerDummy() 
  241. * 
  242. * Description : Dummy interrupt handler. 
  243. * 
  244. * Argument(s) : none. 
  245. * 
  246. * Return(s)   : none. 
  247. * 
  248. * Caller(s)   : BSP_IntHandler(). 
  249. * 
  250. * Note(s)     : none. 
  251. ********************************************************************************************************* 
  252. */  
  253.   
  254. static  void  BSP_IntHandlerDummy (void)  
  255. {  
  256.   
  257. }  




从上代码可以看到,增加了一个全局变量OSIntNeedSW。这个变量的使用是判断退出中断的时候需不需要任务调度,因为你在中断里没有做改变任务优先
级的事,在退出的时候就无需重新调试,如需调度,只需加一个语句 OSIntNeedSW =OSIntSW_Enable; 
 当然这是临界代码,需关中断。
  1. void  BSP_IntInit (void)  

这一函数必须在开中断前调用一次,初始化必要的变量。

还有两个系统函数必须改动,以实现这功能。在滴答时钟中断中,

  1. void  OS_CPU_SysTickHandler (void)  



  1. {  
  2.     OS_CPU_SR  cpu_sr;  
  3.   
  4.   
  5.     OS_ENTER_CRITICAL();                         /* Tell uC/OS-II that we are starting an ISR          */  
  6.     OSIntNesting++;  
  7.     OSIntNeedSW = OSIntSW_Enable;               // 需退出中断时进行任务调度切换  
  8.     OS_EXIT_CRITICAL();  
  9.   
  10.     OSTimeTick();                                /* Call uC/OS-II's OSTimeTick()                       */  
  11.   
  12.     OSIntExit();                                 /* Tell uC/OS-II that we are leaving the ISR          */  
  13. }  
最后需改动的一个函数是OSIntExit(),




  1. void  OSIntExit (void)  
  2. {  
  3. #if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */  
  4.     OS_CPU_SR  cpu_sr = 0u;  
  5. #endif  
  6.   
  7.     if (OSRunning == OS_TRUE) {  
  8.         OS_ENTER_CRITICAL();  
  9.         if (OSIntNesting > 0u) {                           /* Prevent OSIntNesting from wrapping       */  
  10.             OSIntNesting--;  
  11.         }  
  12.         if (OSIntNesting == 0u) {                          /* Reschedule only if all ISRs complete ... */  
  13.             if(OSIntNeedSW == OSIntSW_Enable){            // 在这判断是否需要任务切换调试  
  14.                 OSIntNeedSW = OSIntSW_Disable;  
  15.                 if (OSLockNesting == 0u) {                     /* ... and not locked.                      */  
  16.                     OS_SchedNew();  
  17.                     OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];  
  18.                     if (OSPrioHighRdy != OSPrioCur) {          /* No Ctx Sw if current task is highest rdy */  
  19. #if OS_TASK_PROFILE_EN > 0u  
  20.                         OSTCBHighRdy->OSTCBCtxSwCtr++;         /* Inc. # of context switches to this task  */  
  21. #endif  
  22.                         OSCtxSwCtr++;                          /* Keep track of the number of ctx switches */  
  23.                         OSIntCtxSw();                          /* Perform interrupt level ctx switch       */  
  24.                     }                      
  25.                 }                  
  26.             }  
  27.         }  
  28.         OS_EXIT_CRITICAL();  
  29.     }  
  30. }  
最终目的,通过增加一个OSIntNeedSW全局变量,来判断退出中断时是否需要进行任务调试切换。这样的话,在一般的中断处理函数处理的退出过程更快,缩短无改变任务优先级中断的处理时间。其实这功能在新的版本ucos III中已经实现了。


之前使用的版本我还按网上一些人的做法更改了ucos的移植文件,如下图,这是任务切换的时候,如果不使用OSTaskSwHook可以屏蔽下面四行汇编。大笑,省四条语句。

[plain] view plaincopy
  1.                                                             ; At this point, entire context of process has been saved  
  2. PU_PendSVHandler_nosave  
  3. ;PUSH    {R14}                                               ; Save LR exc_return value  
  4. ;LDR     R0, =OSTaskSwHook                                   ; OSTaskSwHook();  
  5. ;BLX     R0  
  6. ;POP     {R14}  
  7.   
  8. LDR     R0, =OSPrioCur                                      ; OSPrioCur = OSPrioHighRdy;  
  9. LDR     R1, =OSPrioHighRdy  
  10. LDRB    R2, [R1]  
  11. STRB    R2, [R0]  
  12.   
  13. LDR     R0, =OSTCBCur                                       ; OSTCBCur  = OSTCBHighRdy;  
  14. LDR     R1, =OSTCBHighRdy  
  15. LDR     R2, [R1]  
  16. STR     R2, [R0]  
  17.   
  18. LDR     R0, [R2]                                            ; R0 is new process SP; SP = OSTCBHighRdy->OSTCBStkPtr;  
  19. LDM     R0, {R4-R11}                                        ; Restore r4-11 from new process stack  
  20. ADDS    R0, R0, #0x20  
  21. MSR     PSP, R0                                             ; Load PSP with new process SP  
  22. ORR     LR, LR, #0x04                                       ; Ensure exception return uses process stack  
  23. CPSIE   I  
  24. BX      LR                                                  ; Exception return will restore  


强烈推荐在调试阶段加入,并开启这宏定义


  1. #ifdef USE_FULL_ASSERT  
  2. /****************************************************************************** 
  3. * 
  4. * Function Name  : assert_failed 
  5. * Description    : Reports the name of the source file and the source line  
  6. number 
  7. *                  where the assert_param error has occurred. 
  8. * Input          : - file: pointer to the source file name 
  9. *                  - line: assert_param error line source number 
  10. * Output         : None 
  11. * Return         : None 
  12. *******************************************************************************/  
  13. void assert_failed(uint8_t* file, uint32_t line)  
  14. {  
  15.   /* User can add his own implementation to report the file name and line number, 
  16.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)  
  17. */  
  18.   
  19.   /* Infinite loop */  
  20.     while (1)  
  21.     {}  
  22. }  
  23. #endif  
阅读(2125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~