os_view.c
上传用户:yj_qqy
上传日期:2017-01-28
资源大小:2911k
文件大小:42k
源码类别:

uCOS

开发平台:

C/C++

  1. /*
  2. *********************************************************************************************************
  3. *                                           uC/OS-View
  4. *
  5. *                             (c) Copyright 2005, Micrium, Weston, FL
  6. *                                       All Rights Reserved
  7. *
  8. *
  9. * Filename   : OS_VIEW.C
  10. * Version    : V1.33
  11. * Programmer : JJL
  12. *********************************************************************************************************
  13. */
  14. #define  OS_VIEW_GLOBALS
  15. #include <includes.h>
  16. #if OS_VIEW_MODULE == DEF_ENABLED
  17. /*
  18. *********************************************************************************************************
  19. *                                               CONSTANTS
  20. *********************************************************************************************************
  21. */
  22. #ifndef  OS_VIEW_SUPPORT_MEM
  23. #define  OS_VIEW_SUPPORT_MEM               0                /* Current version doesn't support memory Rd/Wr    */
  24. #endif
  25.                                                             /* Inbound  packets (from PC)                      */
  26. #define  OS_VIEW_PROTOCOL_RX_SD0        0xED                /* Start delimiters                                */
  27. #define  OS_VIEW_PROTOCOL_RX_SD1        0x8C
  28. #define  OS_VIEW_PROTOCOL_RX_ED         0x8D                /* End   delimiter                                 */
  29.                                                             /* Outbound packets (to PC)                        */
  30. #define  OS_VIEW_PROTOCOL_TX_SD0        0x8C                /* Start delimiters                                */
  31. #define  OS_VIEW_PROTOCOL_TX_SD1        0xED
  32. #define  OS_VIEW_PROTOCOL_TX_ED         0x8D                /* End   delimiter                                 */
  33. #define  OS_VIEW_RX_STATE_SD0              0                /* waiting for start first  start delimiter (SD0)  */
  34. #define  OS_VIEW_RX_STATE_SD1              1                /* waiting for start second start delimiter (SD1)  */
  35. #define  OS_VIEW_RX_STATE_LEN              2                /* waiting for len byte                            */
  36. #define  OS_VIEW_RX_STATE_DATA             3                /* waiting for data                                */
  37. #define  OS_VIEW_RX_STATE_CHKSUM           4                /* waiting for checksum                            */
  38. #define  OS_VIEW_RX_STATE_ED               5                /* waiting for end delimiter                       */
  39. #define  OS_VIEW_TX_STATE_SD0              0                /* Transmit state machine states                   */
  40. #define  OS_VIEW_TX_STATE_SD1              1
  41. #define  OS_VIEW_TX_STATE_DATA_LEN         2
  42. #define  OS_VIEW_TX_STATE_DATA             3
  43. #define  OS_VIEW_TX_STATE_CHKSUM           4
  44. #define  OS_VIEW_TX_STATE_ED               5
  45. #define  OS_VIEW_TX_STATE_STR_LEN          6
  46. #define  OS_VIEW_TX_STATE_STR_TYPE         7
  47. #define  OS_VIEW_TX_STATE_STR_DATA         8
  48. #define  OS_VIEW_CMD_STEP_TICK_ON          0                /* Commands for tick stepping                      */
  49. #define  OS_VIEW_CMD_STEP_TICK_OFF         1
  50. #define  OS_VIEW_CMD_STEP_TICK_NEXT        2
  51. #define  OS_VIEW_CPU_NAME_SIZE            30
  52. /*$PAGE*/
  53. /*
  54. *********************************************************************************************************
  55. *                                           LOCAL VARIABLES
  56. *********************************************************************************************************
  57. */
  58. #if OS_VIEW_PARSE_TASK > 0
  59. static  OS_STK     OSView_TaskStk[OS_VIEW_TASK_STK_SIZE];
  60. #if OS_SEM_EN > 0
  61. static  OS_EVENT  *OSView_Sem;
  62. #endif
  63. #endif
  64. /*
  65. *********************************************************************************************************
  66. *                                      LOCAL FUNCTION PROTOTYPES
  67. *********************************************************************************************************
  68. */
  69. #if OS_VIEW_PARSE_TASK > 0
  70. static  void    OSView_InitOS(void);
  71. static  void    OSView_Task(void *p_arg);
  72. #endif
  73. static  INT8U   OSView_CmdParsePkt(void);
  74. static  void    OSView_CmdSendToApp(void);
  75. static  void    OSView_CmdGetSysInfo(void);
  76. static  void    OSView_CmdGetTaskList(void);
  77. static  void    OSView_CmdGetTaskInfo(void);
  78. static  void    OSView_CmdGetTaskInfo_InvalidTask(void);
  79. static  INT8U   OSView_CmdGetTaskInfo_TCBStat(OS_TCB *ptcb);
  80. static  void    OSView_CmdMemRdINT8U(void);
  81. static  void    OSView_CmdMemRdINT16U(void);
  82. static  void    OSView_CmdMemRdINT32U(void);
  83. static  void    OSView_CmdMemWrINT8U(void);
  84. static  void    OSView_CmdMemWrINT16U(void);
  85. static  void    OSView_CmdMemWrINT32U(void);
  86. static  void    OSView_CmdTickStep(void);
  87. static  void    OSView_CmdTrace(void);
  88. static  void    OSView_RxPkt(void);
  89. static  void    OSView_RxStoINT8U(INT8U rx_data);
  90. static  void    OSView_RxBufClr(void);
  91. static  INT8U   OSView_RxGetINT8U(void);
  92. static  INT16U  OSView_RxGetINT16U(void);
  93. static  INT32U  OSView_RxGetINT32U(void);
  94. static  void    OSView_TxStart(void);
  95. static  void    OSView_TxStoINT8U(INT8U tx_data);
  96. static  void    OSView_TxStoINT16U(INT16U tx_data);
  97. static  void    OSView_TxStoINT32U(INT32U tx_data);
  98. static  void    OSView_TxStoPtr(void *ptr);
  99. static  void    OSView_TxStoStr(INT8U *s);
  100. /*$PAGE*/
  101. /*
  102. *********************************************************************************************************
  103. *                                       INITIALIZE uC/OS-View
  104. *********************************************************************************************************
  105. */
  106. void  OSView_Init (INT32U baud_rate)
  107. {
  108. #if OS_TICK_STEP_EN > 0
  109.     OSTickStepState     = OS_TICK_STEP_DIS;                 /* Disable the tick stepping mode                  */
  110. #endif
  111.     OSView_CyclesCtr    = 0;
  112.     OSView_RxState      = OS_VIEW_RX_STATE_SD0;             /* Setup Rx and Tx state machines                  */
  113.     OSView_TxState      = OS_VIEW_TX_STATE_SD0;
  114.     OSView_TxLen        = 0;
  115.     OSView_TxStrLen     = 0;
  116.     OSView_TxActiveFlag = OS_FALSE;
  117.     OSView_TerminalRxSetCallback((void (*)(INT8U))0);       /* Force terminal callback function to 'nothing'   */
  118.     OSView_TmrCntsPrev  = 0;
  119. #if OS_VIEW_PARSE_TASK > 0
  120.     OSView_InitOS();
  121. #endif
  122.     OSView_InitTarget(baud_rate);                           /* Initialize target specific code                 */
  123. }
  124. /*$PAGE*/
  125. /*
  126. *********************************************************************************************************
  127. *                             INITIALIZE THE uC/OS-View TASK AND OS OBJECTS
  128. *********************************************************************************************************
  129. */
  130. #if OS_VIEW_PARSE_TASK > 0
  131. static  void  OSView_InitOS (void)
  132. {
  133. #if OS_TASK_NAME_EN > 0 || OS_EVENT_NAME_SIZE > 7
  134.     INT8U  err;
  135. #endif
  136. #if OS_SEM_EN > 0
  137.     OSView_Sem = OSSemCreate(0);
  138. #if OS_EVENT_NAME_EN > 0
  139.     OSEventNameSet(OSView_Sem, (INT8U *)"uC/OS-View Signal", &err);
  140. #endif
  141. #endif
  142. #if OS_TASK_CREATE_EXT_EN > 0
  143.     #if OS_STK_GROWTH == 1
  144.     (void)OSTaskCreateExt(OSView_Task,
  145.                           (void *)0,                                  /* No arguments passed to OSView_Task() */
  146.                           &OSView_TaskStk[OS_VIEW_TASK_STK_SIZE - 1], /* Set Top-Of-Stack                     */
  147.                           OS_VIEW_TASK_PRIO,                          /* Lowest priority level                */
  148.                           OS_VIEW_TASK_ID,
  149.                           &OSView_TaskStk[0],                         /* Set Bottom-Of-Stack                  */
  150.                           OS_VIEW_TASK_STK_SIZE,
  151.                           (void *)0,                                  /* No TCB extension                     */
  152.                           OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear stack  */
  153.     #else
  154.     (void)OSTaskCreateExt(OSView_Task,
  155.                           (void *)0,                                  /* No arguments passed to OSView_Task() */
  156.                           &OSView_TaskStk[0],                         /* Set Top-Of-Stack                     */
  157.                           OS_VIEW_TASK_PRIO,                          /* Lowest priority level                */
  158.                           OS_VIEW_TASK_ID,
  159.                           &OSView_TaskStk[OS_VIEW_TASK_STK_SIZE - 1], /* Set Bottom-Of-Stack                  */
  160.                           OS_VIEW_TASK_STK_SIZE,
  161.                           (void *)0,                                  /* No TCB extension                     */
  162.                           OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear stack  */
  163.     #endif
  164. #else
  165.     #if OS_STK_GROWTH == 1
  166.     (void)OSTaskCreate(OSView_Task,
  167.                        (void *)0,
  168.                        &OSView_TaskStk[OS_VIEW_TASK_STK_SIZE - 1],
  169.                        OS_VIEW_TASK_PRIO);
  170.     #else
  171.     (void)OSTaskCreate(OSView_Task,
  172.                        (void *)0,
  173.                        &OSView_TaskStk[0],
  174.                        OS_VIEW_TASK_PRIO);
  175.     #endif
  176. #endif
  177. #if OS_TASK_NAME_EN > 10
  178.     OSTaskNameSet(OS_VIEW_TASK_PRIO, (INT8U *)"uC/OS-View", &err);
  179. #endif
  180. }
  181. #endif
  182. /*$PAGE*/
  183. /*
  184. *********************************************************************************************************
  185. *                                           uC/OS-View TASK
  186. *********************************************************************************************************
  187. */
  188. #if OS_VIEW_PARSE_TASK > 0
  189. static  void  OSView_Task (void *p_arg)
  190. {
  191.     INT16U     len;
  192.     INT8U      err;
  193. #if OS_SEM_EN == 0 && OS_MBOX_EN > 0
  194.     void      *msg;
  195. #endif
  196. #if OS_CRITICAL_METHOD == 3                       /* Allocate storage for CPU status register          */
  197.     OS_CPU_SR  cpu_sr = 0;
  198. #endif
  199.     (void)p_arg;
  200.     while (1) {
  201. #if OS_SEM_EN > 0
  202.         (void)OSSemPend(OSView_Sem, 0, &err);     /* Wait for a packet to be received                  */
  203. #else
  204. #if OS_MBOX_EN > 0
  205.         (void)OSMboxPend(OSView_Mbox, 0, &err);   /* Wait for a packet to be received                  */
  206. #endif
  207. #endif
  208.         len = OSView_CmdParsePkt();               /* Parse packet and formulate a response             */
  209.         if (len > 0) {                            /* If we have a response                             */
  210.             OS_ENTER_CRITICAL();                  /* Setup transmission of the response                */
  211.             OSView_TxLen = len;
  212.             OS_EXIT_CRITICAL();
  213.             OSView_TxStart();
  214.         }
  215.     }
  216. }
  217. #endif
  218. /*$PAGE*/
  219. /*
  220. *********************************************************************************************************
  221. *                                     uC/OS-View TASK CREATE HOOK
  222. *
  223. * Description: This function MUST be called by uC/OS-II's Task Create Hook function: OSTaskCreateHook().
  224. *********************************************************************************************************
  225. */
  226. void  OSView_TaskCreateHook (OS_TCB *ptcb)
  227. {
  228.     ptcb->OSTCBCyclesStart = OSView_TimeGetCycles();        /* Get the current start time for this task.       */
  229.     ptcb->OSTCBCyclesTot   = 0;                             /* Update the task's total execution time          */
  230. }
  231. /*$PAGE*/
  232. /*
  233. *********************************************************************************************************
  234. *                                     uC/OS-View TASK SWITCH HOOK
  235. *
  236. * Description: This function MUST be called by uC/OS-II's Task Switch Hook function: OSTaskSwHook().
  237. *********************************************************************************************************
  238. */
  239. void  OSView_TaskSwHook (void)
  240. {
  241.     INT32U  cycles;
  242.     cycles                         = OSView_TimeGetCycles();    /* This task is done                           */
  243.     OSTCBCur->OSTCBCyclesTot      += cycles - OSTCBCur->OSTCBCyclesStart;
  244.     OSTCBHighRdy->OSTCBCyclesStart = cycles;                    /* Save absolute #cycles at task activation    */
  245. }
  246. /*
  247. *********************************************************************************************************
  248. *                                       Update 32-bits cycles counter
  249. *
  250. * Description: This function must be called by uC/OS-II's OSTimeTickHook() and is used to maintain a
  251. *              32-bit counter of clock or other cycles.
  252. *
  253. * Returns    : None
  254. *
  255. * Note(s)    : Changes the global variable OSView_CyclesCtr
  256. *********************************************************************************************************
  257. */
  258. void  OSView_TickHook (void)
  259. {
  260.     (void)OSView_TimeGetCycles();
  261. }
  262. /*$PAGE*/
  263. /*
  264. *********************************************************************************************************
  265. *                                           Get time [cycles]
  266. *
  267. * Description: This routine is required for task execution time measurement.  This function needs to
  268. *              return time as accurately as possible and in a 32-bit variable.
  269. *
  270. * Returns    : A 32-bit representation of time.
  271. *********************************************************************************************************
  272. */
  273. INT32U  OSView_TimeGetCycles (void)
  274. {
  275.     INT32U     cycles;
  276. #if OS_VIEW_TMR_32_BITS > 0
  277.     INT32U     cnts32;
  278.     INT32U     cnts32_delta;
  279. #else
  280.     INT16U     cnts16;
  281.     INT16U     cnts16_delta;
  282. #endif
  283. #if OS_CRITICAL_METHOD == 3                                    /* Allocate storage for CPU status register         */
  284.     OS_CPU_SR  cpu_sr = 0;
  285. #endif
  286.     OS_ENTER_CRITICAL();
  287. #if OS_VIEW_TMR_32_BITS > 0
  288.     cnts32              = OSView_TmrRd();                      /* Read current counts of the free running counter  */
  289.     cnts32_delta        = cnts32 - OSView_TmrCntsPrev;
  290.     OSView_TmrCntsPrev  = cnts32;                              /* Save current counts for next time                */
  291.     OSView_CyclesCtr   += cnts32_delta;
  292. #else
  293.     cnts16              = (INT16U)OSView_TmrRd();              /* Read current counts of the free running counter  */
  294.     cnts16_delta        = cnts16 - OSView_TmrCntsPrev;
  295.     OSView_TmrCntsPrev  = cnts16;                              /* Save current counts for next time                */
  296.     OSView_CyclesCtr   += (INT32U)cnts16_delta;
  297. #endif
  298.     cycles              = OSView_CyclesCtr;
  299.     OS_EXIT_CRITICAL();
  300.     return (cycles);
  301. }
  302. /*$PAGE*/
  303. /*
  304. *********************************************************************************************************
  305. *                                       uC/OS-View COMMAND HANDLER
  306. *                                            (Parse Packet)
  307. *********************************************************************************************************
  308. */
  309. static  INT8U  OSView_CmdParsePkt (void)
  310. {
  311.     INT8U cmd0;
  312.     cmd0 = OSView_RxGetINT8U();                             /* First byte contains command                     */
  313.     if (cmd0 == 'C') {
  314.         OSView_CmdSendToApp();
  315.         OSView_TxBufCnt = 0;
  316.         return (0);
  317.     }
  318.     if (OSView_TxBufCnt > 0) {                              /* Do cmds only if Tx of the last one is done      */
  319.         return (0);
  320.     }
  321.     switch (cmd0) {
  322.         case 's':
  323.              OSView_TxStoINT8U(cmd0);
  324.              OSView_CmdGetSysInfo();
  325.              break;
  326.         case 't':
  327.              OSView_TxStoINT8U(cmd0);
  328.              OSView_CmdGetTaskInfo();
  329.              break;
  330.         case 'l':
  331.              OSView_TxStoINT8U(cmd0);
  332.              OSView_CmdGetTaskList();
  333.              break;
  334.         case 'S':
  335.              OSView_TxStoINT8U(cmd0);
  336.              OSView_CmdTickStep();
  337.              break;
  338.         case 'T':
  339.              OSView_TxStoINT8U(cmd0);
  340.              OSView_CmdTrace();
  341.              break;
  342.         case 'b':
  343.              OSView_TxStoINT8U(cmd0);
  344.              OSView_CmdMemRdINT8U();
  345.              break;
  346.         case 'w':
  347.              OSView_TxStoINT8U(cmd0);
  348.              OSView_CmdMemRdINT16U();
  349.              break;
  350.         case 0:
  351.              OSView_TxStoINT8U(cmd0);
  352.              OSView_CmdMemRdINT32U();
  353.              break;
  354.         case '1':
  355.              OSView_TxStoINT8U(cmd0);
  356.              OSView_CmdMemWrINT8U();
  357.              break;
  358.         case '2':
  359.              OSView_TxStoINT8U(cmd0);
  360.              OSView_CmdMemWrINT16U();
  361.              break;
  362.         case '4':
  363.              OSView_TxStoINT8U(cmd0);
  364.              OSView_CmdMemWrINT32U();
  365.              break;
  366.         default:
  367.              break;
  368.     }
  369.     return (OSView_TxBufCnt);
  370. }
  371. /*$PAGE*/
  372. /*
  373. *********************************************************************************************************
  374. *                                       uC/OS-View COMMAND HANDLER
  375. *                                         (Send to Application)
  376. *
  377. * Description: This routine transfers a character to the application.
  378. *********************************************************************************************************
  379. */
  380. static  void  OSView_CmdSendToApp (void)
  381. {
  382.     INT8U  rx_data;
  383.     rx_data = OSView_RxGetINT8U();                               /* Read data typed at terminal                */
  384.     if ((void *)OSView_TerminalRxCallbackFnct != (void *)0) {    /* See if callback is defined                 */
  385.         (*OSView_TerminalRxCallbackFnct)(rx_data);               /* Pass character received to application     */
  386.     }
  387. }
  388. /*
  389. *********************************************************************************************************
  390. *                                       uC/OS-View COMMAND HANDLER
  391. *                                        (Get System Information)
  392. *********************************************************************************************************
  393. */
  394. static  void  OSView_CmdGetSysInfo (void)
  395. {
  396.     INT8U   s[OS_VIEW_CPU_NAME_SIZE+1];
  397.     INT8U   i;
  398.     INT32U  stk_base;
  399.     INT32U  stk_size;
  400.     INT32U  time;
  401.     time = OSTimeGet();                                     /* Send current value of tick counter              */
  402.     OSView_TxStoINT32U(time);
  403.     OSView_TxStoPtr(OSTCBCur);                              /* Current task's OS_TCB                           */
  404.     OSView_TxStoINT16U(OS_VERSION);                         /* Send uC/OS-II's version number                  */
  405.     OSView_TxStoINT8U(1);                                   /* Indicate profiling information will be sent     */
  406.     for (i = 0; i < OS_VIEW_CPU_NAME_SIZE; i++) {           /* Clear buffer to help with debug.                */
  407.         s[i] = 0x00;
  408.     }
  409.     OSView_GetCPUName(s);                                   /* Get Processor Name in string 's'                */
  410.     OSView_TxStoStr(s);
  411.     stk_base = OSView_GetIntStkBase();                      /* Interrupt stack base from port specific file    */
  412.     OSView_TxStoINT32U(stk_base);
  413.     stk_size = OSView_GetIntStkSize();                      /* Interrupt stack size from port specific file    */
  414.     OSView_TxStoINT32U(stk_size);
  415. }
  416. /*$PAGE*/
  417. /*
  418. *********************************************************************************************************
  419. *                                       uC/OS-View COMMAND HANDLER
  420. *                                            (Get Task List)
  421. *********************************************************************************************************
  422. */
  423. static  void  OSView_CmdGetTaskList (void)
  424. {
  425.     INT8U   prio;
  426.     OS_TCB *ptcb;
  427.     OSView_TxStoINT8U('t');                                 /* Identifier for data type                        */
  428.     for (prio = 0; prio <= OS_LOWEST_PRIO; prio++) {
  429.         ptcb = OSTCBPrioTbl[prio];
  430.         if (ptcb != (OS_TCB *)0 && ptcb != (OS_TCB *)1) {   /* Does task exist? (Not a Mutex reserved task)    */
  431.             OSView_TxStoINT32U((INT32U)ptcb);               /* Yes, send the task's TCB address                */
  432.         }
  433.     }
  434. }
  435. /*
  436. *********************************************************************************************************
  437. *                                       uC/OS-View COMMAND HANDLER
  438. *                                         (Get Task Information)
  439. *********************************************************************************************************
  440. */
  441. static  void  OSView_CmdGetTaskInfo (void)
  442. {
  443.     OS_TCB  *ptcb;
  444.     INT32U   cycles;
  445.     INT8U    stat;
  446.     ptcb   = (OS_TCB *)OSView_RxGetINT32U();                         /* Get the desired task's TCB address     */
  447.     cycles = OSView_TimeGetCycles();                                 /* Get timestamp                          */
  448.     OSView_TxStoINT32U(cycles);
  449.     if (ptcb != (OS_TCB *)0 && ptcb != (OS_TCB *)1) {                /* Make sure we have a valid TCB pointer  */
  450.         if (ptcb == OSTCBCur) {
  451.             ptcb->OSTCBCyclesTot  += cycles - ptcb->OSTCBCyclesStart;
  452.             ptcb->OSTCBCyclesStart = cycles;
  453.         }
  454.         OSView_TxStoStr(ptcb->OSTCBTaskName);   /* Task name                              */
  455.         OSView_TxStoINT8U(ptcb->OSTCBPrio);                          /* Prio                                   */
  456.         stat = OSView_CmdGetTaskInfo_TCBStat(ptcb);                  /* Task status                            */
  457.         OSView_TxStoINT8U(stat);
  458.         OSView_TxStoINT32U((INT32U)ptcb->OSTCBEventPtr);             /* Pointer to event task is waiting for.  */
  459.         OSView_TxStoINT32U((INT32U)ptcb->OSTCBDly);                  /* Timeout (i.e. ticks of delayed task)   */
  460.         OSView_TxStoINT32U(ptcb->OSTCBCyclesTot);                    /* ExecTime                               */
  461.         OSView_TxStoINT32U(ptcb->OSTCBCtxSwCtr);                     /* NumActivations                         */
  462.         OSView_TxStoINT32U((INT32U)ptcb->OSTCBStkBase);              /* Stack base address                     */
  463.         OSView_TxStoINT32U(ptcb->OSTCBStkSize * sizeof(OS_STK));     /* Stack size (in #bytes)                 */
  464.         OSView_TxStoINT32U(ptcb->OSTCBStkUsed);                      /* Number of bytes used                   */
  465.         OSView_TxStoINT32U((INT32U)ptcb->OSTCBStkPtr);               /* Current stack pointer address          */
  466.     } else {
  467.         OSView_CmdGetTaskInfo_InvalidTask();
  468.     }
  469.     OSView_TxStoINT16U((INT16U)OSTimeGet());
  470. }
  471. /*$PAGE*/
  472. /*
  473. *********************************************************************************************************
  474. *                                       uC/OS-View COMMAND HANDLER
  475. *                                             (Invalid Task)
  476. *********************************************************************************************************
  477. */
  478. static  void  OSView_CmdGetTaskInfo_InvalidTask (void)
  479. {
  480.     OSView_TxStoStr((INT8U *)"INVALID TASK!");         /* Task name                                       */
  481.     OSView_TxStoINT8U(0);                                   /* Prio                                            */
  482.     OSView_TxStoINT8U(0);                                   /* Stat                                            */
  483.     OSView_TxStoINT32U(0);                                  /* Data                                            */
  484.     OSView_TxStoINT32U(0);                                  /* Timeout                                         */
  485.     OSView_TxStoINT32U(0);                                  /* StackBase                                       */
  486.     OSView_TxStoINT32U(0);                                  /* StackSize                                       */
  487.     OSView_TxStoINT32U(0);                                  /* #Bytes used on stack                            */
  488.     OSView_TxStoINT32U(0);                                  /* Current SP address                              */
  489.     OSView_TxStoINT32U(0);                                  /* ExecTime                                        */
  490.     OSView_TxStoINT32U(0);                                  /* NumActivations                                  */
  491. }
  492. /*
  493. *********************************************************************************************************
  494. *                                       uC/OS-View COMMAND HANDLER
  495. *                                         (Get ptcb->OSTCBStat)
  496. *********************************************************************************************************
  497. */
  498. static  INT8U  OSView_CmdGetTaskInfo_TCBStat (OS_TCB *ptcb)
  499. {
  500.     INT8U stat;
  501.     if (ptcb->OSTCBStat != OS_STAT_RDY) {
  502.         stat = ptcb->OSTCBStat;
  503.     } else {
  504.         if (ptcb->OSTCBDly > 0) {                           /* Task status                                     */
  505.             stat = 0xF0;
  506.         } else {
  507.             stat = ptcb->OSTCBStat;
  508.         }
  509.     }
  510.     return (stat);
  511. }
  512. /*$PAGE*/
  513. /*
  514. *********************************************************************************************************
  515. *                                       uC/OS-View COMMAND HANDLER
  516. *                                              (Memory)
  517. *********************************************************************************************************
  518. */
  519. static  void  OSView_CmdMemRdINT8U (void)
  520. {
  521.     INT32U addr;
  522.     INT8U  nbytes;
  523.     addr   = OSView_RxGetINT32U();
  524.     nbytes = OSView_RxGetINT8U();
  525.     while (nbytes--) {
  526.         OSView_TxStoINT8U(*(INT8U *)addr);
  527.         addr++;
  528.     }
  529. }
  530. static  void  OSView_CmdMemRdINT16U (void)
  531. {
  532.     INT32U addr;
  533.     INT8U  nbytes;
  534.     addr   = OSView_RxGetINT32U();
  535.     nbytes = OSView_RxGetINT8U();
  536.     while (nbytes--) {
  537.         OSView_TxStoINT16U(*(INT16U *)addr);
  538.         addr += 2;
  539.     }
  540. }
  541. static  void  OSView_CmdMemRdINT32U (void)
  542. {
  543.     INT32U addr;
  544.     INT8U  nbytes;
  545.     addr   = OSView_RxGetINT32U();
  546.     nbytes = OSView_RxGetINT8U();
  547.     while (nbytes--) {
  548.         OSView_TxStoINT32U(*(INT32U *)addr);
  549.         addr += 4;
  550.     }
  551. }
  552. /*$PAGE*/
  553. /*
  554. *********************************************************************************************************
  555. *                                       uC/OS-View COMMAND HANDLER
  556. *                                           (Write Functions)
  557. *********************************************************************************************************
  558. */
  559. static  void  OSView_CmdMemWrINT8U (void)
  560. {
  561.     INT8U *p;
  562.     p    = (INT8U *)OSView_RxGetINT32U();
  563.     (*p) = OSView_RxGetINT8U();
  564. }
  565. static  void  OSView_CmdMemWrINT16U (void)
  566. {
  567.     INT16U *p;
  568.     p    = (INT16U *)OSView_RxGetINT32U();
  569.     (*p) = OSView_RxGetINT16U();
  570. }
  571. static  void  OSView_CmdMemWrINT32U (void)
  572. {
  573.     INT32U *p;
  574.     p    = (INT32U *)OSView_RxGetINT32U();
  575.     (*p) = OSView_RxGetINT32U();
  576. }
  577. /*$PAGE*/
  578. /*
  579. *********************************************************************************************************
  580. *                                       uC/OS-View COMMAND HANDLER
  581. *                                          (Tick Step command)
  582. *********************************************************************************************************
  583. */
  584. static  void  OSView_CmdTickStep (void)
  585. {
  586. #if OS_TICK_STEP_EN > 0
  587.     INT8U sub_cmd;
  588.     sub_cmd = OSView_RxGetINT8U();
  589.     switch (sub_cmd) {
  590.         case 0:                                             /* Enable stepping                                 */
  591.              OSTickStepState = OS_TICK_STEP_WAIT;
  592.              break;
  593.         case 1:                                             /* Disable stepping, let tick ISR handle the ticks */
  594.              OSTickStepState = OS_TICK_STEP_DIS;
  595.              break;
  596.         case 2:                                             /* Simulate the occurrence of one tick             */
  597.              OSTickStepState = OS_TICK_STEP_ONCE;
  598.              break;
  599.     }
  600. #endif
  601. }
  602. /*
  603. *********************************************************************************************************
  604. *                                       uC/OS-View COMMAND HANDLER
  605. *                                             (Trace command)
  606. *
  607. * Note(s): Command not currently supported.
  608. *********************************************************************************************************
  609. */
  610. static  void  OSView_CmdTrace (void)
  611. {
  612. }
  613. /*$PAGE*/
  614. /*
  615. *********************************************************************************************************
  616. *                                              Rx Handler
  617. *
  618. * Description: This routine is called from the Rx interrupt service handler.
  619. *********************************************************************************************************
  620. */
  621. void  OSView_RxHandler (INT8U rx_data)
  622. {
  623.     OSView_RxCtr++;
  624.     switch (OSView_RxState) {
  625.         case OS_VIEW_RX_STATE_SD0:                          /* waiting for start first  start delimiter (SD0)  */
  626.              if (rx_data == OS_VIEW_PROTOCOL_RX_SD0) {
  627.                  OSView_RxState = OS_VIEW_RX_STATE_SD1;
  628.                  OSView_RxBufClr();
  629.              }
  630.              break;
  631.         case OS_VIEW_RX_STATE_SD1:                          /* waiting for start second start delimiter (SD1)  */
  632.              if (rx_data == OS_VIEW_PROTOCOL_RX_SD1) {
  633.                  OSView_RxState = OS_VIEW_RX_STATE_LEN;
  634.              } else {
  635.                  OSView_RxState = OS_VIEW_RX_STATE_SD0;
  636.              }
  637.              break;
  638.         case OS_VIEW_RX_STATE_LEN:                          /* waiting for 'len' byte                          */
  639.              if ((rx_data == 0x00) | (rx_data > OS_VIEW_RX_BUF_SIZE)) {
  640.                  OSView_RxState  = OS_VIEW_RX_STATE_SD0;    /* Can not handle this size ...                    */
  641.              } else {
  642.                  OSView_RxChkSum    = rx_data;
  643.                  OSView_RxRemainLen = rx_data;
  644.                  OSView_RxState     = OS_VIEW_RX_STATE_DATA;
  645.              }
  646.              break;
  647.         case OS_VIEW_RX_STATE_DATA:                         /* waiting for data                                */
  648.              OSView_RxStoINT8U(rx_data);
  649.              OSView_RxChkSum += rx_data;
  650.              if (--OSView_RxRemainLen == 0) {
  651.                  OSView_RxState = OS_VIEW_RX_STATE_CHKSUM;
  652.              }
  653.              break;
  654.         case OS_VIEW_RX_STATE_CHKSUM:                       /* waiting for checksum                            */
  655.              OSView_RxChkSum += rx_data;
  656.              if ((OSView_RxChkSum & 0xFF) == 0x00) {
  657.                  OSView_RxState = OS_VIEW_RX_STATE_ED;
  658.              } else {
  659.                  OSView_RxState = OS_VIEW_RX_STATE_SD0;
  660.              }
  661.              break;
  662.         case OS_VIEW_RX_STATE_ED:
  663.              if (rx_data == OS_VIEW_PROTOCOL_RX_ED) {       /* End delimiter ?                                 */
  664.                  OSView_RxPktCtr++;
  665.                  OSView_RxPkt();                            /* Parse received packet                           */
  666.              }
  667.              OSView_RxState = OS_VIEW_RX_STATE_SD0;
  668.              break;
  669.         default:
  670.              OSView_RxState = OS_VIEW_RX_STATE_SD0;
  671.              break;
  672.     }
  673. }
  674. /*$PAGE*/
  675. /*
  676. *********************************************************************************************************
  677. *                                        PARSE RECEIVED PACKET
  678. *
  679. * Description: This function is called when a packet has been received and needs to be processed.
  680. *********************************************************************************************************
  681. */
  682. static  void  OSView_RxPkt (void)
  683. {
  684. #if OS_VIEW_PARSE_TASK > 0
  685. #if OS_SEM_EN > 0
  686.     OSSemPost(OSView_Sem);                         /* We have a whole packet, signal task to parse it! */
  687. #else
  688. #if OS_MBOX_EN > 0
  689.     OSMboxPost(OSView_Mbox, (void *)1);
  690. #endif
  691. #endif
  692. #else
  693.     INT16U   len;
  694.     len = OSView_CmdParsePkt();                    /* We have a whole packet, parse it!                */
  695.     if (len > 0) {
  696.         OSView_TxLen = len;
  697.         OSView_TxStart();
  698.     }
  699. #endif
  700. }
  701. /*$PAGE*/
  702. /*
  703. *********************************************************************************************************
  704. *                                        STORE DATA IN Rx BUFFER
  705. *
  706. * Description: This routine is called whenever a valid byte has been received.
  707. *********************************************************************************************************
  708. */
  709. static  void  OSView_RxStoINT8U (INT8U rx_data)
  710. {
  711.     if (OSView_RxBufCnt < OS_VIEW_RX_BUF_SIZE) {
  712.         OSView_RxBuf[OSView_RxBufCnt++] = rx_data;
  713.     }
  714. }
  715. /*
  716. *********************************************************************************************************
  717. *                                           CLEAR Rx BUFFER
  718. *********************************************************************************************************
  719. */
  720. static  void  OSView_RxBufClr (void)
  721. {
  722.     OSView_RxBufCnt = 0;
  723.     OSView_RxRdIx   = 0;
  724. }
  725. /*
  726. *********************************************************************************************************
  727. *                                     GET 8, 16 or 32-bit DATA
  728. *********************************************************************************************************
  729. */
  730. static  INT8U  OSView_RxGetINT8U (void)
  731. {
  732.     return (OSView_RxBuf[OSView_RxRdIx++]);
  733. }
  734. static  INT16U  OSView_RxGetINT16U (void)
  735. {
  736.     INT16U  lowbyte;
  737.     INT16U  highbyte;
  738.     highbyte = OSView_RxGetINT8U();
  739.     lowbyte  = OSView_RxGetINT8U();
  740.     return ((highbyte << 8) | lowbyte);
  741. }
  742. static  INT32U  OSView_RxGetINT32U (void)
  743. {
  744.     INT32U  highword;
  745.     INT32U  lowword;
  746.     highword = OSView_RxGetINT16U();
  747.     lowword  = OSView_RxGetINT16U();
  748.     return ((highword << 16) | lowword);
  749. }
  750. /*$PAGE*/
  751. /*
  752. *********************************************************************************************************
  753. *                                  START TRANSMISSION OF REPLY PACKET
  754. *********************************************************************************************************
  755. */
  756. void  OSView_TerminalRxSetCallback (void (*call_back)(INT8U rx_data))
  757. {
  758.     OSView_TerminalRxCallbackFnct = call_back;
  759. }
  760. /*$PAGE*/
  761. /*
  762. *********************************************************************************************************
  763. *                                 SEND AN ASCII STRING TO TERMINAL WINDOW
  764. *
  765. * Description: This function allows an application to send strings to the TERMINAL window in the viewer.
  766. *
  767. * Arguments  : s     is a pointer to the string to send.
  768. *
  769. *              dly   allows the calling task to delay itself for 'dly' ticks until the current string
  770. *                    is sent.  If 'dly' is set to 0, then the string will not be sent if a string is
  771. *                    currently in the process of being sent.  In other words, if there is a string currently
  772. *                    being sent and you sent 'dly' to 0, OSView_TxStr() will return to the caller and the
  773. *                    string will not be sent.
  774. *
  775. * Note(s)    : 1) This function MUST be called from a TASK and NOT an ISR.
  776. *********************************************************************************************************
  777. */
  778. void  OSView_TxStr (INT8U *s, INT16U dly)
  779. {
  780.     INT16U  len;
  781.     len = (INT16U)OS_StrLen(s);                           /* Make sure string fits in buffer           */
  782.     if (len >= OS_VIEW_TX_STR_SIZE) {                     /* Force end of string if string too long    */
  783.         len    = OS_VIEW_TX_STR_SIZE - 1;
  784.         s[len] = '';
  785.     }
  786.     if (OSView_TxStrLen > 0) {                            /* Are currently busy sending a string?      */
  787.         if (dly > 0) {                                    /* Yes, does caller want to wait?            */
  788.             while (OSView_TxStrLen > 0) {                 /* Yes, let other tasks run                  */
  789.                 OSView_TxStrDlyCtr++;
  790.                 OSTimeDly(dly);
  791.             }
  792.             OSView_TxStrLen = len;                        /* Update the string length                  */
  793.             strcpy((char *)&OSView_TxStrBuf[0], (char *)s);           /* Copy string to send to buffer             */
  794.             if (OSView_TxStrLen > 0) {
  795.                 OSView_TxStart();
  796.             }
  797.         }
  798.     } else {
  799.         OSView_TxStrLen = len;                            /* Update the string length                  */
  800.         strcpy((char *)&OSView_TxStrBuf[0], (char *)s);               /* Copy string to send to buffer             */
  801.         if (OSView_TxStrLen > 0) {
  802.             OSView_TxStart();
  803.         }
  804.     }
  805. }
  806. /*$PAGE*/
  807. /*
  808. *********************************************************************************************************
  809. *                                  START TRANSMISSION OF REPLY PACKET
  810. *********************************************************************************************************
  811. */
  812. static  void  OSView_TxStart (void)
  813. {
  814. #if OS_CRITICAL_METHOD == 3
  815.     OS_CPU_SR  cpu_sr = 0;
  816. #endif
  817.     OS_ENTER_CRITICAL();
  818.     if (OSView_TxActiveFlag == OS_FALSE) {
  819.         OSView_TxHandler();
  820.         OSView_TxIntEn();
  821.     }
  822.     OS_EXIT_CRITICAL();
  823. }
  824. /*
  825. *********************************************************************************************************
  826. *                                               Tx Handler
  827. *
  828. * Description: This routine is called from the transmitter buffer empty interrupt service handler.
  829. *              It will send out the next byte in the buffer.
  830. *
  831. * Returns:     none
  832. *********************************************************************************************************
  833. */
  834. void  OSView_TxHandler (void)
  835. {
  836.     INT8U  tx_data;
  837.     switch (OSView_TxState) {
  838.         case OS_VIEW_TX_STATE_SD0:
  839.              if (OSView_TxLen > 0 || OSView_TxStrLen > 0) { /* Packet in buffer or string waiting to be send ? */
  840.                  OSView_Tx1(OS_VIEW_PROTOCOL_TX_SD0);
  841.                  OSView_TxCtr++;
  842.                  OSView_TxActiveFlag = OS_TRUE;
  843.                  OSView_TxState      = OS_VIEW_TX_STATE_SD1;
  844.                  OSView_TxIx         = 0;
  845.              } else {                                       /* If there is nothing to do end transmission      */
  846.                  OSView_TxActiveFlag = OS_FALSE;
  847.                  OSView_TxIntDis();                         /* No more data to send, disable Tx interrupts     */
  848.                  break;
  849.              }
  850.              break;
  851.         case OS_VIEW_TX_STATE_SD1:
  852.              OSView_Tx1(OS_VIEW_PROTOCOL_TX_SD1);
  853.              OSView_TxCtr++;
  854.              if (OSView_TxLen > 0) {
  855.                  OSView_TxState = OS_VIEW_TX_STATE_DATA_LEN;
  856.              } else {
  857.                  OSView_TxState = OS_VIEW_TX_STATE_STR_LEN;
  858.              }
  859.              break;
  860.         case OS_VIEW_TX_STATE_DATA_LEN:                       /* Include the packet length in the packet         */
  861.              OSView_Tx1(OSView_TxLen);
  862.              OSView_TxCtr++;
  863.              OSView_TxState  = OS_VIEW_TX_STATE_DATA;
  864.              OSView_TxChkSum = OSView_TxLen;
  865.              break;
  866.         case OS_VIEW_TX_STATE_DATA:
  867.              tx_data = OSView_TxBuf[OSView_TxIx];
  868.              OSView_Tx1(tx_data);
  869.              OSView_TxCtr++;
  870.              OSView_TxChkSum += tx_data;
  871.              OSView_TxIx++;
  872.              if (OSView_TxIx >= OSView_TxLen) {               /* See if we are done sending the packet           */
  873.                  OSView_TxState  = OS_VIEW_TX_STATE_CHKSUM;
  874.                  OSView_TxLen    = 0;
  875.              }
  876.              break;
  877.         case OS_VIEW_TX_STATE_STR_LEN:                        /* String sending                                  */
  878.              OSView_Tx1(OSView_TxStrLen + 1);
  879.              OSView_TxCtr++;
  880.              OSView_TxState  = OS_VIEW_TX_STATE_STR_TYPE;
  881.              OSView_TxChkSum = OSView_TxStrLen + 1;
  882.              break;
  883.         case OS_VIEW_TX_STATE_STR_TYPE:
  884.              OSView_Tx1('C');
  885.              OSView_TxCtr++;
  886.              OSView_TxState   = OS_VIEW_TX_STATE_STR_DATA;
  887.              OSView_TxChkSum += 'C';
  888.              break;
  889.         case OS_VIEW_TX_STATE_STR_DATA:
  890.              tx_data = OSView_TxStrBuf[OSView_TxIx];
  891.              OSView_Tx1(tx_data);
  892.              OSView_TxCtr++;
  893.              OSView_TxChkSum += tx_data;
  894.              if (++OSView_TxIx >= OSView_TxStrLen) {
  895.                  OSView_TxState  = OS_VIEW_TX_STATE_CHKSUM;
  896.                  OSView_TxStrLen = 0;
  897.              }
  898.              break;
  899.         case OS_VIEW_TX_STATE_CHKSUM:                         /* Epilog ... checksum & end delimiter             */
  900.              OSView_Tx1(OSView_TxChkSum);
  901.              OSView_TxCtr++;
  902.              OSView_TxState = OS_VIEW_TX_STATE_ED;
  903.              break;
  904.         case OS_VIEW_TX_STATE_ED:
  905.              OSView_Tx1(OS_VIEW_PROTOCOL_TX_ED);
  906.              OSView_TxCtr++;
  907.              OSView_TxState  = OS_VIEW_TX_STATE_SD0;
  908.              OSView_TxBufCnt = 0;                             /* Clear the Tx buffer                             */
  909.              OSView_TxPktCtr++;
  910.              break;
  911.         default:
  912.              OSView_TxState      = OS_VIEW_TX_STATE_SD0;
  913.              OSView_TxActiveFlag = OS_FALSE;
  914.              OSView_TxIntDis();                               /* No more data to send, disable Tx interrupts     */
  915.              break;
  916.     }
  917. }
  918. /*$PAGE*/
  919. /*
  920. *********************************************************************************************************
  921. *                                     STORE 8, 16 or 32-bit DATA
  922. *********************************************************************************************************
  923. */
  924. static  void  OSView_TxStoINT8U (INT8U tx_data)
  925. {
  926.     if (OSView_TxBufCnt < OS_VIEW_TX_BUF_SIZE) {
  927.         OSView_TxBuf[OSView_TxBufCnt++] = tx_data;
  928.     }
  929. }
  930. static  void  OSView_TxStoINT16U (INT16U tx_data)
  931. {
  932.     OSView_TxStoINT8U(tx_data >> 8);
  933.     OSView_TxStoINT8U(tx_data & 0x00FF);
  934. }
  935. static  void  OSView_TxStoINT32U (INT32U tx_data)
  936. {
  937.     OSView_TxStoINT16U(tx_data >> 16);
  938.     OSView_TxStoINT16U(tx_data & 0x0000FFFFL);
  939. }
  940. static  void  OSView_TxStoPtr (void *ptr)
  941. {
  942.     INT32U  tx_data;
  943.     tx_data = (INT32U)ptr;
  944.     OSView_TxStoINT16U(tx_data >> 16);
  945.     OSView_TxStoINT16U(tx_data & 0x0000FFFFL);
  946. }
  947. static  void  OSView_TxStoStr (INT8U *s)
  948. {
  949.     INT8U   len;
  950.     INT8U   i;
  951.     INT8U  *ps;
  952.     len = 0;
  953.     ps  = s;
  954.     if (ps != (INT8U *)0) {                                 /* Calculate length of string                      */
  955.         while (*ps) {
  956.             ps++;
  957.             len++;
  958.         }
  959.     }
  960.     OSView_TxStoINT8U(len);                                 /* Store string, length first                      */
  961.     ps = s;
  962.     for (i = 0; i < len; i++) {
  963.         OSView_TxStoINT8U((INT8U)*ps);
  964.         ps++;
  965.     }
  966. }
  967. #endif