OS_CPU_C.C
Upload User: qqy_2008
Upload Date: 2020-04-21
Package Size: 4k
Code Size: 19k
Category:

OS Develop

Development Platform:

Visual C++

  1. /*---------------------------------------------------------------------------
  2. //
  3. //  FILE          : $Workfile: Os_cpu_c.c $
  4. //
  5. //  ORIGINATOR    : Martin Rodier
  6. //  CREATION DATE : March, 7 2002
  7. //  REVISION      : 1.10
  8. //  PURPOSE       : This uC/OS-II port is intended for Infineon Technologies and ST10R167
  9. //                  C16x Extended Architecture Micro Controller Targets (C167CR-LM)
  10. //  PART OF       : uC/OS-II version 2.00
  11. //  COMPILE WITH  : KEIL C166/ST10 V4.20 Ansi C Compiler
  12. //
  13. //---------------------------------------------------------------------------
  14. //                               uC/OS-II
  15. //                         The Real-Time Kernel
  16. //                        Infineon  C16x/C167CR
  17. //                   Extended Architecture Specific Code
  18. //                          HLARGE MEMORY MODEL
  19. //---------------------------------------------------------------------------
  20. //
  21. // Revision history:
  22. //
  23. // $Log: /.../UCOS-II/C167/Os_cpu_c.c $
  24.  * 
  25.  * 5     3/07/02 3:49p Mrodier
  26.  * Revised Version of the port that include a copy of the interrupting
  27.  * task PSW to fix the problem of the multiplication when the interrupted
  28.  * task had a multiplication in progress and the interrupting task do not
  29.  * return to it.
  30.  * 
  31.  * Addition into OSTaskBuildStk of the PSW at position -2E of the task
  32.  * stack to let the interrupting task memorize the state of PSW into the
  33.  * task stack.
  34.  *
  35.  * 4     6/13/01 10:42a Mrodier
  36.  * Revised to final version as relased in uCOSII port
  37. //
  38. //-------------------------------------------------------------------------
  39. */
  40. #define  OS_CPU_GLOBALS
  41. #include "includes.h"
  42. #include <intrins.h>
  43. #pragma SRC
  44. /*
  45. *********************************************************************************************************
  46. *                                        INITIALISE A TASK'S STACK
  47. *
  48. * Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialise the
  49. *              stack frame of the task being created.  This function is highly processor specific.
  50. *
  51. * Arguments  : task          is a pointer to the task code
  52. *
  53. *              pdata         is a pointer to a user supplied data area that will be passed to the task
  54. *                            when the task first executes.
  55. *
  56. *              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to
  57. *                            a 'free' entry on the task stack.  If OS_STK_GROWTH is set to 1 then
  58. *                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if
  59. *                            OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
  60. *                            of the stack.
  61. *
  62. *              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
  63. *
  64. *                                                TASK STACK AREA                              (High Memory)
  65. *                                           +14  TASK DATA PARAMETER SEGMENT pointer of task
  66. *                                           +12  TASK DATA PARAMETER OFFSET pointer of task
  67. *                                           +10  SEGMENT of task code address
  68. *                                           +0E  OFFSET  of task code address
  69. *                                           +0C  SP of System stack of task
  70. *                                           +0A  USER STACK OFFSET POINTER (R0) of task
  71. *                                           +08  USER STACK PAGE POINTER (DPP2) of task
  72. *                                           +06  PSW of task
  73. *                                           +04  OFFSET  of task return address (IP)
  74. *                                           +02  SEGMENT of task return address (CSP)
  75. *                                             0                                               (Low Memory)
  76. *
  77. * Returns    : Always returns the location of the new top-of-stack' once the processor registers have
  78. *              been placed on the stack in the proper order.
  79. *
  80. * Note(s)    : Interrupts are enabled when your task starts executing. You can change this by setting the
  81. *              PSW to 0x0800 instead.  In this case, interrupts would be disabled upon task startup.  The
  82. *              application code would be responsible for enabling interrupts at the beginning of the task
  83. *              code.  You will need to modify OSTaskIdle() and OSTaskStat() so that they enable
  84. *              interrupts.  Failure to do this will make your system crash!
  85. *
  86. *********************************************************************************************************
  87. */
  88. OS_STK *OSTaskStkInit (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt)
  89. {
  90.     INT16U  *stk;
  91.     INT32U   usr;
  92.     INT16U   page;
  93.     INT16U   offset;
  94.     INT16U   data_seg;
  95.     INT16U   data_sof;
  96.     opt      = opt;                                          /* 'opt' is not used, prevent warning     */
  97.     data_seg = (INT16U)((INT32U)pdata >> 16);
  98.     data_sof = ((INT16U) pdata & 0xFFFF);
  99.     stk      = (INT16U *)ptos;                               /* Load stack pointer                     */
  100.     *stk--   = data_seg;                                     /* TASK DATA PARAMETER SEGMENT pointer of task   */
  101.     *stk--   = data_sof;                                     /* TASK DATA PARAMETER OFFSET pointer of task */
  102.     *stk--   = (INT16U)((INT32U)task >> 16);                 /* Task segment start address             */
  103.     *stk--   = ((INT16U)task & 0xFFFF);                      /* Task offset start address              */
  104.     *stk--   = (INT16U)0xFC00;                               /* Task SP of System stack of task        */
  105.     usr      = (INT32U)stk;                                  /* Keep a copy of STACK OFFSET POINTER location */
  106.     offset   = (INT16U)((((usr) & 0x3FFF) - 0x0A) | 0x4000); /* First adress of stack offset pointer of the task */
  107.     *stk--   = offset;                                       /* Task user stack offset R0              */
  108.     page     = (INT16U)(usr >> 0x000E);                      /* Task user stack page   DPP2            */
  109.     *stk--   = page;                                         /* Task user stack page   DPP2            */
  110.     *stk--   = (INT16U)0x0800;                               /* Task PSW = Interrupt enabled           */
  111.     *stk--   = ((INT16U)task & 0xFFFF);                      /* Task offset return address             */
  112.     *stk--   = (INT16U)((INT32U)task >> 16);                 /* Task segment return address            */
  113.     OSTaskBuildStk(page, offset, data_seg, data_sof);
  114.     return ((OS_STK *)stk);
  115. }
  116. /*$PAGE*/
  117. #if OS_CPU_HOOKS_EN
  118. /*
  119. *********************************************************************************************************
  120. *                                          TASK CREATION HOOK
  121. *
  122. * Description: This function is called when a task is created.
  123. *
  124. * Arguments  : ptcb   is a pointer to the task control block of the task being created.
  125. *
  126. * Note(s)    : 1) Interrupts are disabled during this call.
  127. *********************************************************************************************************
  128. */
  129. void OSTaskCreateHook (OS_TCB *ptcb)
  130. {
  131.     ptcb = ptcb;                       /* Prevent compiler warning                                     */
  132. }
  133. /*
  134. *********************************************************************************************************
  135. *                                           TASK DELETION HOOK
  136. *
  137. * Description: This function is called when a task is deleted.
  138. *
  139. * Arguments  : ptcb   is a pointer to the task control block of the task being deleted.
  140. *
  141. * Note(s)    : 1) Interrupts are disabled during this call.
  142. *********************************************************************************************************
  143. */
  144. void OSTaskDelHook (OS_TCB *ptcb)
  145. {
  146.     ptcb = ptcb;                       /* Prevent compiler warning                                     */
  147. }
  148. /*
  149. *********************************************************************************************************
  150. *                                           TASK SWITCH HOOK
  151. *
  152. * Description: This function is called when a task switch is performed.  This allows you to perform other
  153. *              operations during a context switch.
  154. *
  155. * Arguments  : none
  156. *
  157. * Note(s)    : 1) Interrupts are disabled during this call.
  158. *              2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
  159. *                 will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
  160. *                 task being switched out (i.e. the preempted task).
  161. *********************************************************************************************************
  162. */
  163. void OSTaskSwHook (void)
  164. {
  165. }
  166. /*
  167. *********************************************************************************************************
  168. *                                           STATISTIC TASK HOOK
  169. *
  170. * Description: This function is called every second by uC/OS-II's statistics task.  This allows your
  171. *              application to add functionality to the statistics task.
  172. *
  173. * Arguments  : none
  174. *********************************************************************************************************
  175. */
  176. void OSTaskStatHook (void)
  177. {
  178. }
  179. /*
  180. *********************************************************************************************************
  181. *                                               TICK HOOK
  182. *
  183. * Description: This function is called every tick.
  184. *
  185. * Arguments  : none
  186. *
  187. * Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
  188. *********************************************************************************************************
  189. */
  190. void OSTimeTickHook (void)
  191. {}
  192. /*
  193. *********************************************************************************************************
  194. *
  195. *Description :Those function is added by donghegang
  196. *
  197. *Notes       :There are not those function at  the  version before 200.
  198. *
  199. *********************************************************************************************************
  200. */
  201. void OSInitHookBegin(void)
  202. {}
  203. void OSInitHookEnd(void)
  204. {}
  205. void OSTaskIdleHook(void)
  206. {}
  207. void OSTCBInitHook(OS_TCB *ptcb)
  208. {
  209.  ptcb=ptcb;
  210. }
  211. #endif
  212. /*
  213. *********************************************************************************************************
  214. *                                        BUILD A TASK'S STACK AREA
  215. *
  216. * Description: This function is called by OSTaskStkInit to initialise the
  217. *              stack frame of the task being created.
  218. *
  219. * Arguments  : page          is a pointer to the current task user stack page.
  220. *
  221. *              offset        is a pointer to the current task user stack offset.
  222. *
  223. *              datapag       is a pointer to a user supplied data area page when the task first executes.
  224. *
  225. *              datapof       is a pointer to a user supplied data area offset when the task first executes.
  226. *
  227. *                                                TASK STACK AREA                              (High Memory)
  228. *                                             0
  229. *                                           -02  R[1 ..15] General Purpose registers of task
  230. *                                           -20  CP     Context pointer of task
  231. *                                           -22  DPP3   Data page pointer 3 of task
  232. *                                           -24  DPP2   Data page pointer 1 of task
  233. *                                           -26  DPP0   Data page pointer 0 of task
  234. *                                           -28  MDC    Multiply/Divide Control of task
  235. *                                           -2A  MDH    Multiply/Divide High register of task
  236. *                                           -2C  MDL    Multiply/Divide Low register of task  (Low Memory)
  237. *                                           -2E  PSW    OF the interrupting task to preserve the MULIP flag
  238. *
  239. * Returns    : None
  240. *********************************************************************************************************
  241. */
  242. void OSTaskBuildStk (INT16U page, INT16U offset, INT16U datapag, INT16U datapof)
  243. {
  244.     page=page;                              // The compiler assign page to R8
  245.     offset=offset;                          // The compiler assign offset to R9
  246.     datapag=datapag;                        // The compiler assign datapag to R10
  247.     datapof=datapof;                        // The compiler assign datapof to R11
  248. #pragma asm
  249.                                             ; SAVE USED REGISTERS
  250.     PUSH   R1
  251.     PUSH   R2
  252.     PUSH   R3
  253.     PUSH   R4
  254.     PUSH   R10
  255.     PUSH   R11
  256.     PUSH   R12
  257.                                             ; LOAD INITIAL TASK STACK.
  258.     MOV    R4,R9                            ; Get pointer to Task Stack
  259.     MOV    R2,R10                           ; Page pointer to passed parameter
  260.     MOV    R3,R11                           ; Offset pointer to passed parameter
  261.                                             ; ADJUST THE TASK USER OFFSET POINTER
  262.     EXTP   R8,#1
  263.     MOV    R1,[R4+#0x0A]                    ; Get initial user offset pointer
  264.     SUB    R1,#0x2E                         ; adjust user offset pointer to save task registers
  265.     EXTP   R8,#1
  266.     MOV    [R4+#0x0A],R1                     ; Save true user offset pointer
  267.                                             ; INITIALISE REGISTER VALUES
  268.     MOV    R9 ,#0x1111                      ; R1 initialised to 1111
  269.     MOV    R10,#0x2222                      ; R2 initialised to 2222
  270.     MOV    R11,#0x3333                      ; R3 initialised to 3333
  271.     MOV    R12,#0x4444                      ; R4 initialised to 4444
  272.     EXTP   R8,#4
  273.     MOV    [-R4],R9
  274.     MOV    [-R4],R10
  275.     MOV    [-R4],R11
  276.     MOV    [-R4],R12
  277.     MOV    R9, #0x5555                      ; R5 initialised to 5555
  278.     MOV    R10,#0x6666                      ; R6 initialised to 6666
  279.     MOV    R11,#0x7777                      ; R7 initialised to 7777
  280.     MOV    R12,R3                           ; R8 initialised to point @ POF of pdata
  281.     EXTP   R8,#4
  282.     MOV    [-R4],R9
  283.     MOV    [-R4],R10
  284.     MOV    [-R4],R11
  285.     MOV    [-R4],R12
  286.     MOV    R9 ,R2                           ; R9  initialised to point @ PAG of pdata
  287.     MOV    R10,#0xAAAA                      ; R10 initialised to AAAA
  288.     MOV    R11,#0xBBBB                      ; R11 initialised to BBBB
  289.     MOV    R12,#0xCCCC                      ; R12 initialised to CCCC
  290.     EXTP   R8,#4
  291.     MOV    [-R4],R9
  292.     MOV    [-R4],R10
  293.     MOV    [-R4],R11
  294.     MOV    [-R4],R12
  295.     MOV    R9 ,#0xDDDD                      ; R13 initialised to DDDD
  296.     MOV    R10,#0xEEEE                      ; R14 initialised to EEEE
  297.     MOV    R11,#0xFFFF                      ; R15 initialised to FFFF
  298.     MOV    R12,CP                           ; Get the Context Pointer (CP)
  299.     EXTP   R8,#4
  300.     MOV    [-R4],R9
  301.     MOV    [-R4],R10
  302.     MOV    [-R4],R11
  303.     MOV    [-R4],R12
  304.     MOV    R9 ,DPP3                         ; Get Data Page Pointer 3 (DPP3)
  305.     MOV    R10,DPP2                         ; Get Data Page Pointer 2 (DPP2)
  306.     MOV    R11,DPP0                         ; Get Data Page Pointer 0 (DPP0)
  307.     EXTP   R8,#3
  308.     MOV    [-R4],R9                         ; Put it on the user stack
  309.     MOV    [-R4],R10                        ; Put it on the user stack
  310.     MOV    [-R4],R11                        ; Put it on the user stack
  311.     MOV    R9,#0x00                         ; R10 initialised to 0
  312.     EXTP   R8,#4
  313.     MOV    [-R4],R9                         ; Set Multiply/Divide Control (MDC)
  314.     MOV    [-R4],R9                         ; Set Multiply/Divide High (MDH)
  315.     MOV    [-R4],R9                         ; Set Multiply/Divide Low (MDL)
  316.     MOV    [-R4],R9                         ; Set Temporary PSW (Cleared)
  317.                                             ; RESTORE USED REGISTERS
  318.     POP    R12
  319.     POP    R11
  320.     POP    R10
  321.     POP    R4
  322.     POP    R3
  323.     POP    R2
  324.     POP    R1
  325.  #pragma endasm
  326. }
  327. /*$PAGE*/
  328. /*
  329. *********************************************************************************************************
  330. *                                       INITIALIZE SYSTEM TICK
  331. *
  332. * Description: This function is called to initialize and configure the system interrupt tick.
  333. *
  334. * Arguments  : none
  335. *********************************************************************************************************
  336. */
  337. void  OSTickISRInit (void)
  338. {
  339.   
  340.  
  341.   ///  -----------------------------------------------------------------------
  342.   ///  Configuration of Timer Block Prescaler 1:
  343.   ///  -----------------------------------------------------------------------
  344.   ///  - prescaler for timer block 1 is 8
  345.   ///  -----------------------------------------------------------------------
  346.   ///  Configuration of the GPT1 Core Timer 3:
  347.   ///  -----------------------------------------------------------------------
  348.   ///  - timer 3 works in timer mode
  349.   ///  - external up/down control is disabled
  350.   ///  - prescaler factor is 8
  351.   ///  - up/down control bit is reset
  352.   ///  - alternate output function T3OUT (P3.3) is disabled
  353.   ///  - timer 3 output toggle latch (T3OTL) is set to 0
  354.   ///  - timer 3 run bit is reset
  355.   GPT12E_T3CON   =  0x0000;      // load timer 3 control register
  356.   GPT12E_T3      =  0x0000;      // load timer 3 register
  357.   ///  -----------------------------------------------------------------------
  358.   ///  Configuration of the GPT1 Auxiliary Timer 2:
  359.   ///  -----------------------------------------------------------------------
  360.   ///  - timer 2 works in timer mode
  361.   ///  - external up/down control is disabled
  362.   ///  - prescaler factor is 16
  363.   ///  - up/down control bit is reset
  364.   GPT12E_T2CON   =  0x0001;      // load timer 2 control register
  365.   GPT12E_T2      =  0x0000;      // load timer 2 register
  366.   ///  -----------------------------------------------------------------------
  367.   ///  Configuration of the GPT1 Auxiliary Timer 4:
  368.   ///  -----------------------------------------------------------------------
  369.   ///  - timer 4 works in timer mode
  370.   ///  - external up/down control is disabled
  371.   ///  - prescaler factor is 8
  372.   ///  - up/down control bit is reset
  373.   ///  - timer 4 run bit is reset
  374.   GPT12E_T4CON   =  0x0000;      // load timer 4 control register
  375.   GPT12E_T4      =  0x0000;      // load timer 4 register
  376.   ///  -----------------------------------------------------------------------
  377.   ///  Configuration of the used GPT1 Port Pins:
  378.   ///  -----------------------------------------------------------------------
  379.   ///  -----------------------------------------------------------------------
  380.   ///  Configuration of the used GPT1 Interrupts:
  381.   ///  -----------------------------------------------------------------------
  382.   ///  timer 2 service request node configuration:
  383.   ///  - timer 2 interrupt priority level (ILVL) = 13
  384.   ///  - timer 2 interrupt group level (GLVL) = 1
  385.   ///  - timer 2 group priority extension (GPX) = 0
  386.   GPT12E_T2IC    =  0x0075;     
  387.   // USER CODE BEGIN (GPT1_Function,3)
  388.   // USER CODE END
  389.   GPT12E_T2CON_T2R  =  1;        // set timer 2 run bit
  390. } //  End of function GPT1_vInit
  391.