UCOS_TI_LM3S_Keil
 全部 结构体 文件 函数 变量 类型定义 宏定义 
os_cpu_c.c
浏览该文件的文档.
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 *
6 *
7 * (c) Copyright 2006, Micrium, Weston, FL
8 * All Rights Reserved
9 *
10 * ARM Cortex-M3 Port
11 *
12 * File : OS_CPU_C.C
13 * Version : V2.89
14 * By : Jean J. Labrosse
15 * Brian Nagel
16 *
17 * For : ARMv7M Cortex-M3
18 * Mode : Thumb2
19 * Toolchain : IAR EWARM
20 *********************************************************************************************************
21 */
22 
23 #define OS_CPU_GLOBALS
24 #include <ucos_ii.h>
25 
26 /*
27 *********************************************************************************************************
28 * LOCAL VARIABLES
29 *********************************************************************************************************
30 */
31 
32 #if OS_TMR_EN > 0u
34 #endif
35 
36 /*
37 *********************************************************************************************************
38 * SYS TICK DEFINES
39 *********************************************************************************************************
40 */
41 
42 #define OS_CPU_CM3_NVIC_ST_CTRL (*((volatile INT32U *)0xE000E010uL)) /* SysTick Ctrl & Status Reg. */
43 #define OS_CPU_CM3_NVIC_ST_RELOAD (*((volatile INT32U *)0xE000E014uL)) /* SysTick Reload Value Reg. */
44 #define OS_CPU_CM3_NVIC_ST_CURRENT (*((volatile INT32U *)0xE000E018uL)) /* SysTick Current Value Reg. */
45 #define OS_CPU_CM3_NVIC_ST_CAL (*((volatile INT32U *)0xE000E01CuL)) /* SysTick Cal Value Reg. */
46 #define OS_CPU_CM3_NVIC_PRIO_ST (*((volatile INT8U *)0xE000ED23uL)) /* SysTick Handler Prio Reg. */
47 
48 #define OS_CPU_CM3_NVIC_ST_CTRL_COUNT 0x00010000uL /* Count flag. */
49 #define OS_CPU_CM3_NVIC_ST_CTRL_CLK_SRC 0x00000004uL /* Clock Source. */
50 #define OS_CPU_CM3_NVIC_ST_CTRL_INTEN 0x00000002uL /* Interrupt enable. */
51 #define OS_CPU_CM3_NVIC_ST_CTRL_ENABLE 0x00000001uL /* Counter mode. */
52 #define OS_CPU_CM3_NVIC_PRIO_MIN 0xFFu /* Min handler prio. */
53 
54 /*
55 *********************************************************************************************************
56 * OS INITIALIZATION HOOK
57 * (BEGINNING)
58 *
59 * Description: This function is called by OSInit() at the beginning of OSInit().
60 *
61 * Arguments : none
62 *
63 * Note(s) : 1) Interrupts should be disabled during this call.
64 *********************************************************************************************************
65 */
66 #if OS_CPU_HOOKS_EN > 0u
67 void OSInitHookBegin (void)
68 {
69 #if OS_TMR_EN > 0u
70  OSTmrCtr = 0u;
71 #endif
72 }
73 #endif
74 
75 /*
76 *********************************************************************************************************
77 * OS INITIALIZATION HOOK
78 * (END)
79 *
80 * Description: This function is called by OSInit() at the end of OSInit().
81 *
82 * Arguments : none
83 *
84 * Note(s) : 1) Interrupts should be disabled during this call.
85 *********************************************************************************************************
86 */
87 #if OS_CPU_HOOKS_EN > 0u
88 void OSInitHookEnd (void)
89 {
90 }
91 #endif
92 
93 /*
94 *********************************************************************************************************
95 * TASK CREATION HOOK
96 *
97 * Description: This function is called when a task is created.
98 *
99 * Arguments : ptcb is a pointer to the task control block of the task being created.
100 *
101 * Note(s) : 1) Interrupts are disabled during this call.
102 *********************************************************************************************************
103 */
104 #if OS_CPU_HOOKS_EN > 0u
106 {
107 #if OS_APP_HOOKS_EN > 0u
108  App_TaskCreateHook(ptcb);
109 #else
110  (void)ptcb; /* Prevent compiler warning */
111 #endif
112 }
113 #endif
114 
115 
116 /*
117 *********************************************************************************************************
118 * TASK DELETION HOOK
119 *
120 * Description: This function is called when a task is deleted.
121 *
122 * Arguments : ptcb is a pointer to the task control block of the task being deleted.
123 *
124 * Note(s) : 1) Interrupts are disabled during this call.
125 *********************************************************************************************************
126 */
127 #if OS_CPU_HOOKS_EN > 0u
128 void OSTaskDelHook (OS_TCB *ptcb)
129 {
130 #if OS_APP_HOOKS_EN > 0u
131  App_TaskDelHook(ptcb);
132 #else
133  (void)ptcb; /* Prevent compiler warning */
134 #endif
135 }
136 #endif
137 
138 /*
139 *********************************************************************************************************
140 * IDLE TASK HOOK
141 *
142 * Description: This function is called by the idle task. This hook has been added to allow you to do
143 * such things as STOP the CPU to conserve power.
144 *
145 * Arguments : none
146 *
147 * Note(s) : 1) Interrupts are enabled during this call.
148 *********************************************************************************************************
149 */
150 #if OS_CPU_HOOKS_EN > 0u
151 void OSTaskIdleHook (void)
152 {
153 #if OS_APP_HOOKS_EN > 0u
155 #endif
156 }
157 #endif
158 
159 /*
160 *********************************************************************************************************
161 * TASK RETURN HOOK
162 *
163 * Description: This function is called if a task accidentally returns. In other words, a task should
164 * either be an infinite loop or delete itself when done.
165 *
166 * Arguments : ptcb is a pointer to the task control block of the task that is returning.
167 *
168 * Note(s) : none
169 *********************************************************************************************************
170 */
171 
172 #if OS_CPU_HOOKS_EN > 0u
174 {
175 #if OS_APP_HOOKS_EN > 0u
176  App_TaskReturnHook(ptcb);
177 #else
178  (void)ptcb;
179 #endif
180 }
181 #endif
182 
183 /*
184 *********************************************************************************************************
185 * STATISTIC TASK HOOK
186 *
187 * Description: This function is called every second by uC/OS-II's statistics task. This allows your
188 * application to add functionality to the statistics task.
189 *
190 * Arguments : none
191 *********************************************************************************************************
192 */
193 
194 #if OS_CPU_HOOKS_EN > 0u
195 void OSTaskStatHook (void)
196 {
197 #if OS_APP_HOOKS_EN > 0u
199 #endif
200 }
201 #endif
202 
203 /*
204 *********************************************************************************************************
205 * INITIALIZE A TASK'S STACK
206 *
207 * Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
208 * stack frame of the task being created. This function is highly processor specific.
209 *
210 * Arguments : task is a pointer to the task code
211 *
212 * p_arg is a pointer to a user supplied data area that will be passed to the task
213 * when the task first executes.
214 *
215 * ptos is a pointer to the top of stack. It is assumed that 'ptos' points to
216 * a 'free' entry on the task stack. If OS_STK_GROWTH is set to 1 then
217 * 'ptos' will contain the HIGHEST valid address of the stack. Similarly, if
218 * OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
219 * of the stack.
220 *
221 * opt specifies options that can be used to alter the behavior of OSTaskStkInit().
222 * (see uCOS_II.H for OS_TASK_OPT_xxx).
223 *
224 * Returns : Always returns the location of the new top-of-stack once the processor registers have
225 * been placed on the stack in the proper order.
226 *
227 * Note(s) : 1) Interrupts are enabled when your task starts executing.
228 * 2) All tasks run in Thread mode, using process stack.
229 *********************************************************************************************************
230 */
231 
232 OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
233 {
234  OS_STK *stk;
235 
236 
237  (void)opt; /* 'opt' is not used, prevent warning */
238  stk = ptos; /* Load stack pointer */
239 
240  /* Registers stacked as if auto-saved on exception */
241  *(stk) = (INT32U)0x01000000uL; /* xPSR */
242  *(--stk) = (INT32U)task; /* Entry Point */
243  *(--stk) = (INT32U)OS_TaskReturn; /* R14 (LR) */
244  *(--stk) = (INT32U)0x12121212uL; /* R12 */
245  *(--stk) = (INT32U)0x03030303uL; /* R3 */
246  *(--stk) = (INT32U)0x02020202uL; /* R2 */
247  *(--stk) = (INT32U)0x01010101uL; /* R1 */
248  *(--stk) = (INT32U)p_arg; /* R0 : argument */
249 
250  /* Remaining registers saved on process stack */
251  *(--stk) = (INT32U)0x11111111uL; /* R11 */
252  *(--stk) = (INT32U)0x10101010uL; /* R10 */
253  *(--stk) = (INT32U)0x09090909uL; /* R9 */
254  *(--stk) = (INT32U)0x08080808uL; /* R8 */
255  *(--stk) = (INT32U)0x07070707uL; /* R7 */
256  *(--stk) = (INT32U)0x06060606uL; /* R6 */
257  *(--stk) = (INT32U)0x05050505uL; /* R5 */
258  *(--stk) = (INT32U)0x04040404uL; /* R4 */
259 
260  return (stk);
261 }
262 
263 /*
264 *********************************************************************************************************
265 * TASK SWITCH HOOK
266 *
267 * Description: This function is called when a task switch is performed. This allows you to perform other
268 * operations during a context switch.
269 *
270 * Arguments : none
271 *
272 * Note(s) : 1) Interrupts are disabled during this call.
273 * 2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
274 * will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
275 * task being switched out (i.e. the preempted task).
276 *********************************************************************************************************
277 */
278 #if (OS_CPU_HOOKS_EN > 0u) && (OS_TASK_SW_HOOK_EN > 0u)
279 void OSTaskSwHook (void)
280 {
281 #if OS_APP_HOOKS_EN > 0u
282  App_TaskSwHook();
283 #endif
284 }
285 #endif
286 
287 /*
288 *********************************************************************************************************
289 * OS_TCBInit() HOOK
290 *
291 * Description: This function is called by OS_TCBInit() after setting up most of the TCB.
292 *
293 * Arguments : ptcb is a pointer to the TCB of the task being created.
294 *
295 * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
296 *********************************************************************************************************
297 */
298 #if OS_CPU_HOOKS_EN > 0u
299 void OSTCBInitHook (OS_TCB *ptcb)
300 {
301 #if OS_APP_HOOKS_EN > 0u
302  App_TCBInitHook(ptcb);
303 #else
304  (void)ptcb; /* Prevent compiler warning */
305 #endif
306 }
307 #endif
308 
309 /*
310 *********************************************************************************************************
311 * TICK HOOK
312 *
313 * Description: This function is called every tick.
314 *
315 * Arguments : none
316 *
317 * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
318 *********************************************************************************************************
319 */
320 #if (OS_CPU_HOOKS_EN > 0u) && (OS_TIME_TICK_HOOK_EN > 0u)
321 void OSTimeTickHook (void)
322 {
323 #if OS_APP_HOOKS_EN > 0u
325 #endif
326 
327 #if OS_TMR_EN > 0u
328  OSTmrCtr++;
330  OSTmrCtr = 0;
331  OSTmrSignal();
332  }
333 #endif
334 }
335 #endif
336 
337 /*
338 *********************************************************************************************************
339 * SYS TICK HANDLER
340 *
341 * Description: Handle the system tick (SysTick) interrupt, which is used to generate the uC/OS-II tick
342 * interrupt.
343 *
344 * Arguments : none.
345 *
346 * Note(s) : 1) This function MUST be placed on entry 15 of the Cortex-M3 vector table.
347 *********************************************************************************************************
348 */
349 
351 {
352  OS_CPU_SR cpu_sr;
353 
354 
355  OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
356  OSIntNesting++;
358 
359  OSTimeTick(); /* Call uC/OS-II's OSTimeTick() */
360 
361  OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
362 }
363 
364 /*
365 *********************************************************************************************************
366 * INITIALIZE SYS TICK
367 *
368 * Description: Initialize the SysTick.
369 *
370 * Arguments : cnts is the number of SysTick counts between two OS tick interrupts.
371 *
372 * Note(s) : 1) This function MUST be called after OSStart() & after processor initialization.
373 *********************************************************************************************************
374 */
375 
377 {
378  OS_CPU_CM3_NVIC_ST_RELOAD = cnts - 1u;
379  /* Set prio of SysTick handler to min prio. */
381  /* Enable timer. */
383  /* Enable timer interrupt. */
385 }