找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1556|回复: 1
打印 上一主题 下一主题
收起左侧

uCOSIII源码分享

[复制链接]
跳转到指定楼层
楼主
分享uCOSIII源码


源程序如下:
  1. /*
  2. ************************************************************************************************************************
  3. *                                                      uC/OS-III
  4. *                                                 The Real-Time Kernel
  5. *
  6. *                                        (c) Copyright 2009, Micrium, Weston, FL
  7. *                                                  All Rights Reserved
  8. *                                                    www.Micrium.com
  9. *
  10. *                                                  ISR QUEUE MANAGEMENT
  11. *
  12. * File    : OS_ISR.C
  13. * By      : JJL
  14. * Version : V3.00.4
  15. *
  16. * LICENSING TERMS:
  17. * ---------------
  18. *       uC/OS-III  is provided in source form to registered licensees.  It is illegal to distribute this source
  19. *       code to any third party unless you receive written permission by an authorized Micrium officer.  
  20. *
  21. *       Knowledge of the source code may NOT be used to develop a similar product.
  22. *
  23. *       Please help us continue to provide the  Embedded  community with the  finest software  available.   Your
  24. *       honesty is greatly appreciated.
  25. *
  26. *       You can contact us at www.micrium.com.
  27. ************************************************************************************************************************
  28. */

  29. #include  <os.h>

  30. #if OS_CFG_ISR_POST_DEFERRED_EN > 0u
  31. /*$PAGE*/
  32. /*
  33. ************************************************************************************************************************
  34. *                                                   POST TO ISR QUEUE
  35. *
  36. * Description: This function places contents of posts into an intermediate queue to help defer processing of interrupts
  37. *              at the task level.
  38. *
  39. * Arguments  : type       is the type of kernel object the post is destined to:
  40. *
  41. *                             OS_OBJ_TYPE_SEM        
  42. *                             OS_OBJ_TYPE_Q         
  43. *                             OS_OBJ_TYPE_FLAG      
  44. *                             OS_OBJ_TYPE_TASK_MSG   
  45. *                             OS_OBJ_TYPE_TASK_SIGNAL
  46. *
  47. *              p_obj      is a pointer to the kernel object to post to.  This can be a pointer to a semaphore,
  48. *              -----      a message queue or a task control clock.
  49. *
  50. *              p_void     is a pointer to a message that is being posted.  This is used when posting to a message
  51. *                         queue or directly to a task.
  52. *
  53. *              msg_size   is the size of the message being posted
  54. *
  55. *              flags      if the post is done to an event flag group then this corresponds to the falgs being
  56. *                         posted
  57. *
  58. *              ts         is a timestamp as to when the post was done
  59. *
  60. *              opt        this corresponds to post options and applies to:
  61. *
  62. *                             OSFlagPost()
  63. *                             OSSemPost()
  64. *                             OSQPost()
  65. *                             OSTaskQPost()
  66. *
  67. *              p_err      is a pointer to a variable that will contain an error code returned by this function.
  68. *
  69. *                             OS_ERR_NONE         if the post to the ISR queue was successful
  70. *                             OS_ERR_INT)Q_FULL   if the ISR queue is full and cannot accepts any further posts.  This
  71. *                                                 generally indicates that you are receiving interrupts faster than you
  72. *                                                 can process them or, that you didn't make the ISR queue large enough.
  73. *
  74. * Returns    : none
  75. *              
  76. * Note(s)    : none
  77. ************************************************************************************************************************
  78. */

  79. void  OS_IntQPost (OS_OBJ_TYPE    type,
  80.                    void          *p_obj,
  81.                    void          *p_void,
  82.                    OS_MSG_SIZE    msg_size,
  83.                    OS_FLAGS       flags,
  84.                    OS_OPT         opt,
  85.                    CPU_TS         ts,
  86.                    OS_ERR        *p_err)
  87. {
  88.     CPU_SR_ALLOC();



  89. #ifdef OS_SAFETY_CRITICAL_RELEASE
  90.     if (p_err == (OS_ERR *)0) {
  91.         OS_SAFETY_CRITICAL_EXCEPTION();
  92.     }
  93. #endif
  94.                                                            
  95.     CPU_CRITICAL_ENTER();
  96.     if (OSIntQNbrEntries < OSCfg_IntQSize) {                /* Make sure we haven't already filled the ISR queue      */
  97.         OSIntQNbrEntries++;
  98.         OSIntQInPtr->Type       = type;
  99.         OSIntQInPtr->ObjPtr     = p_obj;
  100.         OSIntQInPtr->MsgPtr     = p_void;
  101.         OSIntQInPtr->MsgSize    = msg_size;
  102.         OSIntQInPtr->Flags      = flags;
  103.         OSIntQInPtr->Opt        = opt;
  104.         OSIntQInPtr->TS         = ts;
  105.         OSIntQInPtr             =  OSIntQInPtr->NextPtr;
  106.         OSRdyList[0].NbrEntries = (OS_OBJ_QTY)1;            /* Add to ready list                                      */
  107.         OSRdyList[0].HeadPtr    = &OSIntQTaskTCB;
  108.         OSRdyList[0].TailPtr    = &OSIntQTaskTCB;
  109.         OS_PrioInsert(0u);                                  /* Add task priority 0 in the priority table              */
  110.         OSPrioSaved             = OSPrioCur;                /* Save current priority                                  */
  111.         *p_err                  = OS_ERR_NONE;
  112.     } else {
  113.         OSIntQOvfCtr++;                                     /* Count the number of ISR queue overflows                */
  114.         *p_err                  = OS_ERR_INT_Q_FULL;
  115.     }
  116.     CPU_CRITICAL_EXIT();
  117. }
  118.                   
  119. /*$PAGE*/
  120. /*
  121. ************************************************************************************************************************
  122. *                                               INTERRUPT QUEUE MANAGEMENT TASK
  123. *
  124. * Description: This task is created by OS_IntQTaskInit().
  125. *
  126. * Arguments  : p_arg     is a pointer to an optional argument that is passed during task creation.  For this function
  127. *                        the argument is not used and will be a NULL pointer.
  128. *
  129. * Returns    : none
  130. ************************************************************************************************************************
  131. */

  132. void  OS_IntQRePost (void)
  133. {
  134.     void          *p_obj;
  135.     void          *p_void;
  136.     OS_ERR         err;
  137.     OS_FLAGS       flags;
  138.     CPU_TS         ts;
  139.     OS_OBJ_TYPE    type;
  140.     OS_OPT         opt;
  141.     OS_MSG_SIZE    msg_size;
  142.     CPU_SR_ALLOC();



  143.     CPU_CRITICAL_ENTER();
  144.     type         = OSIntQOutPtr->Type;                          /* Get local copy of queue item contents             */
  145.     p_obj        = OSIntQOutPtr->ObjPtr;
  146.     p_void       = OSIntQOutPtr->MsgPtr;
  147.     msg_size     = OSIntQOutPtr->MsgSize;
  148.     flags        = OSIntQOutPtr->Flags;
  149.     opt          = OSIntQOutPtr->Opt;
  150.     ts           = OSIntQOutPtr->TS;   
  151.     OSIntQOutPtr = OSIntQOutPtr->NextPtr;                       /* Point to next item in the ISR queue               */
  152.     CPU_CRITICAL_EXIT();
  153.     switch (type) {                                             /* Re-post to task                                   */
  154.         case OS_OBJ_TYPE_FLAG:
  155. #if OS_CFG_FLAG_EN > 0u                     
  156.              (void)OS_FlagPost((OS_FLAG_GRP *)p_obj,
  157.                                (OS_FLAGS     )flags,
  158.                                (OS_OPT       )opt,
  159.                                (CPU_TS       )ts,
  160.                                (OS_ERR      *)&err);
  161. #endif              
  162.              break;
  163.                                             
  164.         case OS_OBJ_TYPE_Q:
  165. #if OS_CFG_Q_EN > 0u                     
  166.              OS_QPost((OS_Q      *)p_obj,
  167.                       (void      *)p_void,
  168.                       (OS_MSG_SIZE)msg_size,
  169.                       (OS_OPT     )opt,
  170.                       (CPU_TS     )ts,
  171.                       (OS_ERR    *)&err);
  172. #endif            
  173.              break;
  174.                                             
  175.         case OS_OBJ_TYPE_SEM:
  176. #if OS_CFG_SEM_EN > 0u                     
  177.              (void)OS_SemPost((OS_SEM *)p_obj,
  178.                               (OS_OPT  )opt,
  179.                               (CPU_TS  )ts,
  180.                               (OS_ERR *)&err);
  181. #endif              
  182.              break;
  183.                         
  184.         case OS_OBJ_TYPE_TASK_MSG:
  185. #if OS_CFG_TASK_Q_EN > 0u                     
  186.              OS_TaskQPost((OS_TCB    *)p_obj,
  187.                           (void      *)p_void,
  188.                           (OS_MSG_SIZE)msg_size,
  189.                           (OS_OPT     )opt,
  190.                           (CPU_TS     )ts,
  191.                           (OS_ERR    *)&err);
  192. #endif                          
  193.              break;
  194.                                             
  195.         case OS_OBJ_TYPE_TASK_SIGNAL:
  196.              (void)OS_TaskSemPost((OS_TCB *)p_obj,
  197.                                   (OS_OPT  )opt,
  198.                                   (CPU_TS  )ts,
  199.                                   (OS_ERR *)&err);
  200.              break;
  201.                         
  202.         case OS_OBJ_TYPE_TICK:
  203. #if OS_CFG_SCHED_ROUND_ROBIN_EN > 0u   
  204.              OS_SchedRoundRobin(&OSRdyList[OSPrioSaved]);
  205. #endif
  206.    
  207.              (void)OS_TaskSemPost((OS_TCB *)&OSTickTaskTCB,                /* Signal tick task                        */
  208.                                   (OS_OPT  )OS_OPT_POST_NONE,
  209.                                   (CPU_TS  )ts,
  210.                                   (OS_ERR *)&err);
  211. #if OS_CFG_TMR_EN > 0u
  212.              OSTmrUpdateCtr--;
  213.              if (OSTmrUpdateCtr == (OS_CTR)0) {
  214.                  OSTmrUpdateCtr = OSTmrUpdateCnt;
  215.                  ts             = OS_TS_GET();                             /* Get timestamp                           */
  216.                  (void)OS_TaskSemPost((OS_TCB *)&OSTmrTaskTCB,             /* Signal timer task                       */
  217.                                       (OS_OPT  )OS_OPT_POST_NONE,
  218.                                       (CPU_TS  )ts,
  219.                                       (OS_ERR *)&err);
  220.              }
  221. #endif   
  222.              break;
  223.                         
  224.         default:
  225.              break;
  226.     }
  227. }

  228. /*$PAGE*/
  229. /*
  230. ************************************************************************************************************************
  231. *                                               INTERRUPT QUEUE MANAGEMENT TASK
  232. *
  233. * Description: This task is created by OS_IntQTaskInit().
  234. *
  235. * Arguments  : p_arg     is a pointer to an optional argument that is passed during task creation.  For this function
  236. *                        the argument is not used and will be a NULL pointer.
  237. *
  238. * Returns    : none
  239. ************************************************************************************************************************
  240. */

  241. void  OS_IntQTask (void *p_arg)
  242. {
  243.     CPU_BOOLEAN    done;
  244.     CPU_TS         ts_start;
  245.     CPU_TS         ts_end;
  246.     CPU_SR_ALLOC();



  247.     p_arg = p_arg;                                               /* Not using 'p_arg', prevent compiler warning       */
  248.     while (DEF_TRUE) {
  249.         done     = DEF_FALSE;
  250.         while (done == DEF_FALSE) {
  251.             if (OSIntQNbrEntries == (OS_OBJ_QTY)0) {
  252.                 CPU_CRITICAL_ENTER();
  253.                 OSRdyList[0].NbrEntries = (OS_OBJ_QTY)0;         /* Remove from ready list                            */
  254.                 OSRdyList[0].HeadPtr    = (OS_TCB   *)0;
  255.                 OSRdyList[0].TailPtr    = (OS_TCB   *)0;
  256.                 OS_PrioRemove(0u);                               /* Remove from the priority table                    */
  257.                 CPU_CRITICAL_EXIT();                        
  258.                 OSSched();
  259.                 done = DEF_TRUE;                                 /* No more entries in the queue, we are done         */
  260.             } else {
  261.                 ts_start = OS_TS_GET();
  262.                 OS_IntQRePost();
  263.                 ts_end   = OS_TS_GET() - ts_start;               /* Measure execution time of tick task               */
  264.                 if (ts_end > OSIntQTaskTimeMax) {
  265.                     OSIntQTaskTimeMax = ts_end;
  266.                 }
  267.                 CPU_CRITICAL_ENTER();
  268.                 OSIntQNbrEntries--;
  269.                 CPU_CRITICAL_EXIT();
  270.             }
  271.         }
  272.     }
  273. }

  274. /*$PAGE*/
  275. /*
  276. ************************************************************************************************************************
  277. *                                                 INITIALIZE THE ISR QUEUE
  278. *
  279. * Description: This function is called by OSInit() to initialize the ISR queue.  
  280. *
  281. * Arguments  : p_err    is a pointer to a variable that will contain an error code returned by this function.
  282. *
  283. *                           OS_ERR_INT_Q             If you didn't provide an ISR queue in OS_CFG.C
  284. *                           OS_ERR_INT_Q_SIZE        If you didn't specify a large enough ISR queue.
  285. *                           OS_ERR_STK_INVALID       If you specified a NULL pointer for the task of the ISR task
  286. *                                                    handler
  287. *                           OS_ERR_STK_SIZE_INVALID  If you didn't specify a stack sise greate than the minimum
  288. *                                                    specified by OS_CFG_STK_SIZE_MIN
  289. *                           OS_ERR_???               An error code returned by OSTaskCreate().
  290. *
  291. * Returns    : none
  292. *              
  293. * Note(s)    : none
  294. ************************************************************************************************************************
  295. */

  296. void  OS_IntQTaskInit (OS_ERR  *p_err)
  297. {
  298.     OS_INT_Q      *p_int_q;
  299.     OS_INT_Q      *p_int_q_next;
  300.     OS_OBJ_QTY     i;
  301.    
  302.    
  303.    
  304. #ifdef OS_SAFETY_CRITICAL_RELEASE
  305.     if (p_err == (OS_ERR *)0) {
  306.         OS_SAFETY_CRITICAL_EXCEPTION();
  307.     }
  308. #endif
  309.                                                            
  310.     OSIntQOvfCtr = (OS_QTY)0;                               /* Clear the ISR queue overflow counter                   */

  311.     if (OSCfg_IntQBasePtr == (OS_INT_Q *)0) {
  312.         *p_err = OS_ERR_INT_Q;
  313.         return;
  314.     }
  315.       
  316.     if (OSCfg_IntQSize < (OS_OBJ_QTY)2) {
  317.         *p_err = OS_ERR_INT_Q_SIZE;
  318.         return;
  319.     }
  320.    
  321.     OSIntQTaskTimeMax = (CPU_TS)0;
  322.       
  323.     p_int_q           = OSCfg_IntQBasePtr;                  /* Initialize the circular ISR queue                      */
  324.     p_int_q_next      = p_int_q;              
  325.     p_int_q_next++;
  326.     for (i = 0u; i < OSCfg_IntQSize; i++) {               
  327.         p_int_q->Type    =  OS_OBJ_TYPE_NONE;
  328.         p_int_q->ObjPtr  = (void      *)0;
  329. ……………………

  330. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
UCOS-III.zip (115.27 KB, 下载次数: 22)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:292427 发表于 2019-9-6 23:21 | 只看该作者
这是哪个单片机的源码
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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