Repository navigation
Expand file tree
/
Copy pathos_cpu_c.c
More file actions
431 lines (367 loc) · 16 KB
/
Copy pathos_cpu_c.c
File metadata and controls
431 lines (367 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
*********************************************************************************************************
* uC/OS-III
* The Real-Time Kernel
*
* Copyright 2009-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* ARMv7-R Port
*
* File : os_cpu_c.c
* Version : V3.08.00
*********************************************************************************************************
* For : ARMv7-R Cortex-R
* Mode : ARM or Thumb
*********************************************************************************************************
*/
#define OS_CPU_GLOBALS
#ifdef VSC_INCLUDE_SOURCE_FILE_NAMES
const CPU_CHAR *os_cpu_c__c = "$Id: $";
#endif
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "../../../Source/os.h"
#include "os_cpu.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*
* Note(s) : 1) ARM_MODE_ARM is the CPSR bit mask for ARM Mode
* 2) ARM_MODE_THUMB is the CPSR bit mask for THUMB Mode
* 3) ARM_SVC_MODE_THUMB is the CPSR bit mask for SVC MODE + THUMB Mode
* 4) ARM_SVC_MODE_ARM is the CPSR bit mask for SVC MODE + ARM Mode
*********************************************************************************************************
*/
#if (CPU_CFG_ENDIAN_TYPE == CPU_ENDIAN_TYPE_LITTLE)
#define ARM_MODE_ARM 0x00000000u
#define ARM_MODE_THUMB 0x00000020u
#else /* Set bit 9 in big-endian mode. */
#define ARM_MODE_ARM 0x00000200u
#define ARM_MODE_THUMB 0x00000220u
#endif
#define ARM_SVC_MODE_THUMB (0x00000013u + ARM_MODE_THUMB)
#define ARM_SVC_MODE_ARM (0x00000013u + ARM_MODE_ARM)
/*
*********************************************************************************************************
* IDLE TASK HOOK
*
* Description: This function is called by the idle task. This hook has been added to allow you to do
* such things as STOP the CPU to conserve power.
*
* Arguments : None.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSIdleTaskHook (void)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppIdleTaskHookPtr != (OS_APP_HOOK_VOID)0) {
(*OS_AppIdleTaskHookPtr)();
}
#endif
}
/*
*********************************************************************************************************
* OS INITIALIZATION HOOK
*
* Description: This function is called by OSInit() at the beginning of OSInit().
*
* Arguments : None.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSInitHook (void)
{
CPU_STK_SIZE i;
CPU_STK *p_stk;
p_stk = OSCfg_ISRStkBasePtr; /* Clear the ISR stack */
for (i = 0u; i < OSCfg_ISRStkSize; i++) {
*p_stk++ = (CPU_STK)0u;
}
OS_CPU_ExceptStkBase = (CPU_STK *)(OSCfg_ISRStkBasePtr + OSCfg_ISRStkSize - 1u);
OS_CPU_ARM_DRegCnt = OS_CPU_ARM_DRegCntGet();
}
/*
*********************************************************************************************************
* REDZONE HIT HOOK
*
* Description: This function is called when a task's stack overflowed.
*
* Arguments : p_tcb Pointer to the task control block of the offending task. NULL if ISR.
*
* Note(s) : None.
*********************************************************************************************************
*/
#if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
void OSRedzoneHitHook (OS_TCB *p_tcb)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppRedzoneHitHookPtr != (OS_APP_HOOK_TCB)0) {
(*OS_AppRedzoneHitHookPtr)(p_tcb);
} else {
CPU_SW_EXCEPTION(;);
}
#else
(void)p_tcb; /* Prevent compiler warning */
CPU_SW_EXCEPTION(;);
#endif
}
#endif
/*
*********************************************************************************************************
* STATISTIC TASK HOOK
*
* Description: This function is called every second by uC/OS-III's statistics task. This allows your
* application to add functionality to the statistics task.
*
* Arguments : None.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSStatTaskHook (void)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppStatTaskHookPtr != (OS_APP_HOOK_VOID)0) {
(*OS_AppStatTaskHookPtr)();
}
#endif
}
/*
*********************************************************************************************************
* TASK CREATION HOOK
*
* Description: This function is called when a task is created.
*
* Arguments : p_tcb Pointer to the task control block of the task being created.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSTaskCreateHook (OS_TCB *p_tcb)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppTaskCreateHookPtr != (OS_APP_HOOK_TCB)0) {
(*OS_AppTaskCreateHookPtr)(p_tcb);
}
#else
(void)p_tcb; /* Prevent compiler warning */
#endif
}
/*
*********************************************************************************************************
* TASK DELETION HOOK
*
* Description: This function is called when a task is deleted.
*
* Arguments : p_tcb Pointer to the task control block of the task being deleted.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSTaskDelHook (OS_TCB *p_tcb)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppTaskDelHookPtr != (OS_APP_HOOK_TCB)0) {
(*OS_AppTaskDelHookPtr)(p_tcb);
}
#else
(void)p_tcb; /* Prevent compiler warning */
#endif
}
/*
*********************************************************************************************************
* TASK RETURN HOOK
*
* Description: This function is called if a task accidentally returns. In other words, a task should
* either be an infinite loop or delete itself when done.
*
* Arguments : p_tcb Pointer to the task control block of the task that is returning.
*
* Note(s) : None.
*********************************************************************************************************
*/
void OSTaskReturnHook (OS_TCB *p_tcb)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppTaskReturnHookPtr != (OS_APP_HOOK_TCB)0) {
(*OS_AppTaskReturnHookPtr)(p_tcb);
}
#else
(void)p_tcb; /* Prevent compiler warning */
#endif
}
/*
**********************************************************************************************************
* INITIALIZE A TASK'S STACK
*
* Description: This function is called by OS_Task_Create() or OSTaskCreateExt() to initialize the stack
* frame of the task being created. This function is highly processor specific.
*
* Arguments : p_task Pointer to the task entry point address.
*
* p_arg Pointer to a user supplied data area that will be passed to the task
* when the task first executes.
*
* p_stk_base Pointer to the base address of the stack.
*
* stk_size Size of the stack, in number of CPU_STK elements.
*
* opt Options used to alter the behavior of OS_Task_StkInit().
* (see OS.H for OS_TASK_OPT_xxx).
*
* Returns : Always returns the location of the new top-of-stack' once the processor registers have
* been placed on the stack in the proper order.
*
* Note(s) : 1) Interrupts are enabled when task starts executing.
*
* 2) All tasks run in SVC mode.
**********************************************************************************************************
*/
CPU_STK *OSTaskStkInit (OS_TASK_PTR p_task,
void *p_arg,
CPU_STK *p_stk_base,
CPU_STK *p_stk_limit,
CPU_STK_SIZE stk_size,
OS_OPT opt)
{
CPU_STK *p_stk;
CPU_STK task_addr;
CPU_INT32U i;
CPU_INT32U fpu_reg_cnt;
(void)opt; /* Prevent compiler warning */
(void)p_stk_limit;
p_stk = &p_stk_base[stk_size]; /* Load stack pointer */
p_stk = (CPU_STK *)((CPU_STK)p_stk & ~(CPU_CFG_STK_ALIGN_BYTES - 1u)); /* Align stack pointer. */
task_addr = (CPU_STK)p_task & ~1u; /* Mask off lower bit in case task is thumb mode */
*--p_stk = (CPU_STK)task_addr; /* Entry Point */
*--p_stk = (CPU_STK)OS_TaskReturn; /* Reg R14 (LR) */
*--p_stk = (CPU_STK)0x12121212u; /* Reg R12 */
*--p_stk = (CPU_STK)0x11111111u; /* Reg R11 */
*--p_stk = (CPU_STK)0x10101010u; /* Reg R10 */
*--p_stk = (CPU_STK)0x09090909u; /* Reg R9 */
*--p_stk = (CPU_STK)0x08080808u; /* Reg R8 */
*--p_stk = (CPU_STK)0x07070707u; /* Reg R7 */
*--p_stk = (CPU_STK)0x06060606u; /* Reg R6 */
*--p_stk = (CPU_STK)0x05050505u; /* Reg R5 */
*--p_stk = (CPU_STK)0x04040404u; /* Reg R4 */
*--p_stk = (CPU_STK)0x03030303u; /* Reg R3 */
*--p_stk = (CPU_STK)0x02020202u; /* Reg R2 */
*--p_stk = (CPU_STK)0x01010101u; /* Reg R1 */
*--p_stk = (CPU_STK)p_arg; /* Reg R0 : argument */
if (((CPU_STK)p_task & 0x01u) == 0x01u) { /* See if task runs in Thumb or ARM mode */
*--p_stk = (CPU_STK)ARM_SVC_MODE_THUMB; /* CPSR (Enable IRQ and FIQ interrupts, THUMB-mode) */
} else {
*--p_stk = (CPU_STK)ARM_SVC_MODE_ARM; /* CPSR (Enable IRQ and FIQ interrupts, ARM-mode) */
}
fpu_reg_cnt = OS_CPU_ARM_DRegCntGet();
if (fpu_reg_cnt != 0u) {
*--p_stk = (CPU_STK)0; /* Initialize Floating point status & control register */
/* Initialize general-purpose Floating point registers */
for (i = 0u; i < fpu_reg_cnt * 2u; i++) {
*--p_stk = (CPU_STK)0;
}
*--p_stk = (CPU_STK)(0x40000000); /* Initialize Floating-Point Exception Register (Enable)*/
}
return (p_stk);
}
/*
*********************************************************************************************************
* TASK SWITCH HOOK
*
* Description: This function is called when a task switch is performed. This allows you to perform other
* operations during a context switch.
*
* Arguments : None.
*
* Note(s) : 1) Interrupts are disabled during this call.
* 2) It is assumed that the global pointer 'OSTCBHighRdyPtr' points to the TCB of the task
* that will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCurPtr' points
* to the task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/
void OSTaskSwHook (void)
{
#if OS_CFG_TASK_PROFILE_EN > 0u
CPU_TS ts;
#endif
#ifdef CPU_CFG_INT_DIS_MEAS_EN
CPU_TS int_dis_time;
#endif
#if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
CPU_BOOLEAN stk_status;
#endif
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppTaskSwHookPtr != (OS_APP_HOOK_VOID)0) {
(*OS_AppTaskSwHookPtr)();
}
#endif
OS_TRACE_TASK_SWITCHED_IN(OSTCBHighRdyPtr);
#if OS_CFG_TASK_PROFILE_EN > 0u
ts = OS_TS_GET();
if (OSTCBCurPtr != OSTCBHighRdyPtr) {
OSTCBCurPtr->CyclesDelta = ts - OSTCBCurPtr->CyclesStart;
OSTCBCurPtr->CyclesTotal += (OS_CYCLES)OSTCBCurPtr->CyclesDelta;
}
OSTCBHighRdyPtr->CyclesStart = ts;
#endif
#ifdef CPU_CFG_INT_DIS_MEAS_EN
int_dis_time = CPU_IntDisMeasMaxCurReset(); /* Keep track of per-task interrupt disable time */
if (OSTCBCurPtr->IntDisTimeMax < int_dis_time) {
OSTCBCurPtr->IntDisTimeMax = int_dis_time;
}
#endif
#if OS_CFG_SCHED_LOCK_TIME_MEAS_EN > 0u
if (OSTCBCurPtr->SchedLockTimeMax < OSSchedLockTimeMaxCur) { /* Keep track of per-task scheduler lock time */
OSTCBCurPtr->SchedLockTimeMax = OSSchedLockTimeMaxCur;
}
OSSchedLockTimeMaxCur = (CPU_TS)0; /* Reset the per-task value */
#endif
#if (OS_CFG_TASK_STK_REDZONE_EN > 0u)
/* Check if stack overflowed. */
stk_status = OSTaskStkRedzoneChk((OS_TCB *)0u);
if (stk_status != OS_TRUE) {
OSRedzoneHitHook(OSTCBCurPtr);
}
#endif
}
/*
*********************************************************************************************************
* TICK HOOK
*
* Description: This function is called every tick.
*
* Arguments : None.
*
* Note(s) : 1) This function is assumed to be called from the Tick ISR.
*********************************************************************************************************
*/
void OSTimeTickHook (void)
{
#if OS_CFG_APP_HOOKS_EN > 0u
if (OS_AppTimeTickHookPtr != (OS_APP_HOOK_VOID)0) {
(*OS_AppTimeTickHookPtr)();
}
#endif
}
#ifdef __cplusplus
}
#endif