Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

728 righe
35 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. *
  6. * Copyright 1992-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * ARMv7-M Port
  20. *
  21. * File : os_cpu_c.c
  22. * Version : V2.93.01
  23. *********************************************************************************************************
  24. * For : ARMv7-M Cortex-M
  25. * Mode : Thumb-2 ISA
  26. *********************************************************************************************************
  27. * Note(s) : (1) This port supports the ARM Cortex-M3, Cortex-M4 and Cortex-M7 architectures.
  28. * (2) It has been tested with the following Hardware Floating Point Unit.
  29. * (a) Single-precision: FPv4-SP-D16-M and FPv5-SP-D16-M
  30. * (b) Double-precision: FPv5-D16-M
  31. *********************************************************************************************************
  32. */
  33. #define OS_CPU_GLOBALS
  34. /*
  35. *********************************************************************************************************
  36. * INCLUDE FILES
  37. *********************************************************************************************************
  38. */
  39. #include <ucos_ii.h>
  40. /*
  41. *********************************************************************************************************
  42. * LOCAL VARIABLES
  43. *********************************************************************************************************
  44. */
  45. #if OS_TMR_EN > 0u
  46. static INT16U OSTmrCtr;
  47. #endif
  48. /*
  49. *********************************************************************************************************
  50. * LOCAL GLOBAL VARIABLES
  51. *********************************************************************************************************
  52. */
  53. INT32U OS_KA_BASEPRI_Boundary; /* Base Priority boundary. */
  54. /*
  55. *********************************************************************************************************
  56. * SYS TICK DEFINES
  57. *********************************************************************************************************
  58. */
  59. #define OS_CPU_CM_SYST_CSR (*((volatile INT32U *)0xE000E010uL)) /* SysTick Ctrl & Status Reg. */
  60. #define OS_CPU_CM_SYST_RVR (*((volatile INT32U *)0xE000E014uL)) /* SysTick Reload Value Reg. */
  61. #define OS_CPU_CM_SYST_CVR (*((volatile INT32U *)0xE000E018uL)) /* SysTick Current Value Reg. */
  62. #define OS_CPU_CM_SYST_CALIB (*((volatile INT32U *)0xE000E01CuL)) /* SysTick Cal Value Reg. */
  63. #define OS_CPU_CM_SCB_SHPRI1 (*((volatile INT32U *)0xE000ED18uL)) /* System Handlers 4 to 7 Prio. */
  64. #define OS_CPU_CM_SCB_SHPRI2 (*((volatile INT32U *)0xE000ED1CuL)) /* System Handlers 8 to 11 Prio. */
  65. #define OS_CPU_CM_SCB_SHPRI3 (*((volatile INT32U *)0xE000ED20uL)) /* System Handlers 12 to 15 Prio. */
  66. #define OS_CPU_CM_SYST_CSR_COUNTFLAG 0x00010000uL /* Count flag. */
  67. #define OS_CPU_CM_SYST_CSR_CLKSOURCE 0x00000004uL /* Clock Source. */
  68. #define OS_CPU_CM_SYST_CSR_TICKINT 0x00000002uL /* Interrupt enable. */
  69. #define OS_CPU_CM_SYST_CSR_ENABLE 0x00000001uL /* Counter mode. */
  70. /*
  71. *********************************************************************************************************
  72. * FLOATING POINT DEFINES
  73. *
  74. * Note(s) : 1) Enabled FP lazy stacking and enable automatic state saving.
  75. *********************************************************************************************************
  76. */
  77. #define OS_CPU_CM_FP_FPCCR (*((volatile INT32U *)0xE000EF34uL)) /* Floating-Point Context Control Reg. */
  78. #define OS_CPU_CM_FPCCR_LAZY_STK 0xC0000000uL /* See Note 1. */
  79. /*
  80. *********************************************************************************************************
  81. * OS INITIALIZATION HOOK
  82. * (BEGINNING)
  83. *
  84. * Description: This function is called by OSInit() at the beginning of OSInit().
  85. *
  86. * Arguments : none
  87. *
  88. * Note(s) : 1) Interrupts should be disabled during this call.
  89. * 2) When using hardware floating point please do the following during the reset handler:
  90. * a) Set full access for CP10 & CP11 bits in CPACR register.
  91. * b) Set bits ASPEN and LSPEN in FPCCR register.
  92. *********************************************************************************************************
  93. */
  94. #if OS_CPU_HOOKS_EN > 0u
  95. void OSInitHookBegin (void)
  96. {
  97. INT32U size;
  98. OS_STK *pstk;
  99. #if (OS_CPU_ARM_FP_EN > 0u)
  100. INT32U reg_val;
  101. #endif
  102. /* Clear exception stack for stack checking. */
  103. pstk = &OS_CPU_ExceptStk[0];
  104. size = OS_CPU_EXCEPT_STK_SIZE;
  105. while (size > 0u) {
  106. size--;
  107. *pstk++ = (OS_STK)0;
  108. }
  109. /* Align the ISR stack to 8-bytes */
  110. OS_CPU_ExceptStkBase = (OS_STK *)&OS_CPU_ExceptStk[OS_CPU_EXCEPT_STK_SIZE];
  111. OS_CPU_ExceptStkBase = (OS_STK *)((OS_STK)(OS_CPU_ExceptStkBase) & 0xFFFFFFF8);
  112. #if (OS_CPU_ARM_FP_EN > 0u)
  113. reg_val = OS_CPU_CM_FP_FPCCR; /* Check the floating point mode. */
  114. if ((reg_val & OS_CPU_CM_FPCCR_LAZY_STK) != OS_CPU_CM_FPCCR_LAZY_STK) {
  115. while (1u) { /* See Note (2). */
  116. ;
  117. }
  118. }
  119. #endif
  120. #if OS_TMR_EN > 0u
  121. OSTmrCtr = 0u;
  122. #endif
  123. /* Set BASEPRI boundary from the configuration. */
  124. OS_KA_BASEPRI_Boundary = (INT32U)(CPU_CFG_KA_IPL_BOUNDARY << (8u - CPU_CFG_NVIC_PRIO_BITS));
  125. }
  126. #endif
  127. /*
  128. *********************************************************************************************************
  129. * OS INITIALIZATION HOOK
  130. * (END)
  131. *
  132. * Description: This function is called by OSInit() at the end of OSInit().
  133. *
  134. * Arguments : none
  135. *
  136. * Note(s) : 1) Interrupts should be disabled during this call.
  137. *********************************************************************************************************
  138. */
  139. #if OS_CPU_HOOKS_EN > 0u
  140. void OSInitHookEnd (void)
  141. {
  142. }
  143. #endif
  144. /*
  145. *********************************************************************************************************
  146. * TASK CREATION HOOK
  147. *
  148. * Description: This function is called when a task is created.
  149. *
  150. * Arguments : ptcb is a pointer to the task control block of the task being created.
  151. *
  152. * Note(s) : 1) Interrupts are disabled during this call.
  153. *********************************************************************************************************
  154. */
  155. #if OS_CPU_HOOKS_EN > 0u
  156. void OSTaskCreateHook (OS_TCB *ptcb)
  157. {
  158. #if OS_APP_HOOKS_EN > 0u
  159. App_TaskCreateHook(ptcb);
  160. #else
  161. (void)ptcb; /* Prevent compiler warning */
  162. #endif
  163. }
  164. #endif
  165. /*
  166. *********************************************************************************************************
  167. * TASK DELETION HOOK
  168. *
  169. * Description: This function is called when a task is deleted.
  170. *
  171. * Arguments : ptcb is a pointer to the task control block of the task being deleted.
  172. *
  173. * Note(s) : 1) Interrupts are disabled during this call.
  174. *********************************************************************************************************
  175. */
  176. #if OS_CPU_HOOKS_EN > 0u
  177. void OSTaskDelHook (OS_TCB *ptcb)
  178. {
  179. #if OS_APP_HOOKS_EN > 0u
  180. App_TaskDelHook(ptcb);
  181. #else
  182. (void)ptcb; /* Prevent compiler warning */
  183. #endif
  184. }
  185. #endif
  186. /*
  187. *********************************************************************************************************
  188. * IDLE TASK HOOK
  189. *
  190. * Description: This function is called by the idle task. This hook has been added to allow you to do
  191. * such things as STOP the CPU to conserve power.
  192. *
  193. * Arguments : none
  194. *
  195. * Note(s) : 1) Interrupts are enabled during this call.
  196. *********************************************************************************************************
  197. */
  198. #if OS_CPU_HOOKS_EN > 0u
  199. void OSTaskIdleHook (void)
  200. {
  201. #if OS_APP_HOOKS_EN > 0u
  202. App_TaskIdleHook();
  203. #endif
  204. }
  205. #endif
  206. /*
  207. *********************************************************************************************************
  208. * TASK RETURN HOOK
  209. *
  210. * Description: This function is called if a task accidentally returns. In other words, a task should
  211. * either be an infinite loop or delete itself when done.
  212. *
  213. * Arguments : ptcb is a pointer to the task control block of the task that is returning.
  214. *
  215. * Note(s) : none
  216. *********************************************************************************************************
  217. */
  218. #if OS_CPU_HOOKS_EN > 0u
  219. void OSTaskReturnHook (OS_TCB *ptcb)
  220. {
  221. #if OS_APP_HOOKS_EN > 0u
  222. App_TaskReturnHook(ptcb);
  223. #else
  224. (void)ptcb;
  225. #endif
  226. }
  227. #endif
  228. /*
  229. *********************************************************************************************************
  230. * STATISTIC TASK HOOK
  231. *
  232. * Description: This function is called every second by uC/OS-II's statistics task. This allows your
  233. * application to add functionality to the statistics task.
  234. *
  235. * Arguments : none
  236. *********************************************************************************************************
  237. */
  238. #if OS_CPU_HOOKS_EN > 0u
  239. void OSTaskStatHook (void)
  240. {
  241. #if OS_APP_HOOKS_EN > 0u
  242. App_TaskStatHook();
  243. #endif
  244. }
  245. #endif
  246. /*
  247. *********************************************************************************************************
  248. * INITIALIZE A TASK'S STACK
  249. *
  250. * Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
  251. * stack frame of the task being created. This function is highly processor specific.
  252. *
  253. * Arguments : task is a pointer to the task code
  254. *
  255. * p_arg is a pointer to a user supplied data area that will be passed to the task
  256. * when the task first executes.
  257. *
  258. * ptos is a pointer to the top of stack. It is assumed that 'ptos' points to
  259. * a 'free' entry on the task stack. If OS_STK_GROWTH is set to 1 then
  260. * 'ptos' will contain the HIGHEST valid address of the stack. Similarly, if
  261. * OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
  262. * of the stack.
  263. *
  264. * opt specifies options that can be used to alter the behavior of OSTaskStkInit().
  265. * (see uCOS_II.H for OS_TASK_OPT_xxx).
  266. *
  267. * Returns : Always returns the location of the new top-of-stack once the processor registers have
  268. * been placed on the stack in the proper order.
  269. *
  270. * Note(s) : (1) Interrupts are enabled when task starts executing.
  271. *
  272. * (2) All tasks run in Thread mode, using process stack.
  273. *
  274. * (3) There are two different stack frames depending on whether the Floating-Point(FP)
  275. * co-processor is enabled or not.
  276. *
  277. * (a) The stack frame shown in the diagram is used when the Co-processor Access Control
  278. * Register(CPACR) is disabling the Floating Point Unit. In this case, the FP
  279. * registers(S0- S31) & FP Status Control(FPSCR) register are not saved in the stack frame.
  280. *
  281. * (b) The stack frame shown in the diagram is used when the Floating Point Unit is enabled,
  282. * that is, CP10 and CP11 field in CPACR are ones and FPCCR sets bits ASPEN and LSPEN to 1.
  283. *
  284. * (1) When enabling the FPU through CPACR, make sure to set bits ASPEN and LSPEN in the
  285. * Floating-Point Context Control Register (FPCCR).
  286. *
  287. * +-------------+
  288. * | |
  289. * +-------------+
  290. * | |
  291. * +-------------+
  292. * | FPSCR |
  293. * +-------------+
  294. * | S15 |
  295. * +-------------+
  296. * | S14 |
  297. * +-------------+
  298. * | S13 |
  299. * +-------------+
  300. * .
  301. * .
  302. * .
  303. * +-------------+
  304. * | S2 |
  305. * +-------------+
  306. * | S1 |
  307. * +-------------+ +-------------+
  308. * | | | S0 |
  309. * +-------------+ +-------------+
  310. * | xPSR | | xPSR |
  311. * +-------------+ +-------------+
  312. * | Return Addr | | Return Addr |
  313. * +-------------+ +-------------+
  314. * | LR(R14) | | LR(R14) |
  315. * +-------------+ +-------------+
  316. * | R12 | | R12 |
  317. * +-------------+ +-------------+
  318. * | R3 | | R3 |
  319. * +-------------+ +-------------+
  320. * | R2 | | R0 |
  321. * +-------------+ +-------------+
  322. * | R1 | | R1 |
  323. * +-------------+ +-------------+
  324. * | R0 | | R0 |
  325. * +-------------+ +-------------+
  326. * | EXEC_RETURN | | EXEC_RETURN |
  327. * +-------------+ +-------------+
  328. * | R11 | | R11 |
  329. * +-------------+ +-------------+
  330. * | R10 | | R10 |
  331. * +-------------+ +-------------+
  332. * | R9 | | R9 |
  333. * +-------------+ +-------------+
  334. * | R8 | | R8 |
  335. * +-------------+ +-------------+
  336. * | R7 | | R7 |
  337. * +-------------+ +-------------+
  338. * | R6 | | R6 |
  339. * +-------------+ +-------------+
  340. * | R5 | | R5 |
  341. * +-------------+ +-------------+
  342. * | R4 | | R4 |
  343. * +-------------+ +-------------+
  344. * (a) | S31 |
  345. * +-------------+
  346. * | S30 |
  347. * +-------------+
  348. * | S29 |
  349. +-------------+
  350. * .
  351. * .
  352. * .
  353. * +-------------+
  354. * | S17 |
  355. +-------------+
  356. * | S16 |
  357. * +-------------+
  358. * (b)
  359. *
  360. * (4) The SP must be 8-byte aligned in conforming to the Procedure Call Standard for the ARM architecture
  361. *
  362. * (a) Section 2.1 of the ABI for the ARM Architecture Advisory Note. SP must be 8-byte aligned
  363. * on entry to AAPCS-Conforming functions states :
  364. *
  365. * The Procedure Call Standard for the ARM Architecture [AAPCS] requires primitive
  366. * data types to be naturally aligned according to their sizes (for size = 1, 2, 4, 8 bytes).
  367. * Doing otherwise creates more problems than it solves.
  368. *
  369. * In return for preserving the natural alignment of data, conforming code is permitted
  370. * to rely on that alignment. To support aligning data allocated on the stack, the stack
  371. * pointer (SP) is required to be 8-byte aligned on entry to a conforming function. In
  372. * practice this requirement is met if:
  373. *
  374. * (1) At each call site, the current size of the calling function's stack frame is a multiple of 8 bytes.
  375. * This places an obligation on compilers and assembly language programmers.
  376. *
  377. * (2) SP is a multiple of 8 when control first enters a program.
  378. * This places an obligation on authors of low level OS, RTOS, and runtime library
  379. * code to align SP at all points at which control first enters
  380. * a body of (AAPCS-conforming) code.
  381. *
  382. * In turn, this requires the value of SP to be aligned to 0 modulo 8:
  383. *
  384. * (3) By exception handlers, before calling AAPCS-conforming code.
  385. *
  386. * (4) By OS/RTOS/run-time system code, before giving control to an application.
  387. *
  388. * (b) Section 2.3.1 corrective steps from the the SP must be 8-byte aligned on entry
  389. * to AAPCS-conforming functions advisory note also states.
  390. *
  391. * " This requirement extends to operating systems and run-time code for all architecture versions
  392. * prior to ARMV7 and to the A, R and M architecture profiles thereafter. Special considerations
  393. * associated with ARMV7M are discussed in section 2.3.3"
  394. *
  395. * (1) Even if the SP 8-byte aligment is not a requirement for the ARMv7M profile, the stack is aligned
  396. * to 8-byte boundaries to support legacy execution enviroments.
  397. *
  398. * (c) Section 5.2.1.2 from the Procedure Call Standard for the ARM
  399. * architecture states : "The stack must also conform to the following
  400. * constraint at a public interface:
  401. *
  402. * (1) SP mod 8 = 0. The stack must be double-word aligned"
  403. *
  404. * (d) From the ARM Technical Support Knowledge Base. 8 Byte stack aligment.
  405. *
  406. * "8 byte stack alignment is a requirement of the ARM Architecture Procedure
  407. * Call Standard [AAPCS]. This specifies that functions must maintain an 8 byte
  408. * aligned stack address (e.g. 0x00, 0x08, 0x10, 0x18, 0x20) on all external
  409. * interfaces. In practice this requirement is met if:
  410. *
  411. * (1) At each external interface, the current stack pointer
  412. * is a multiple of 8 bytes.
  413. *
  414. * (2) Your OS maintains 8 byte stack alignment on its external interfaces
  415. * e.g. on task switches"
  416. *
  417. * (5) Exception Return Behavior(EXEC_RETURN)
  418. * 0xFFFFFFFD Return to Thread mode, exception return uses non-floating point state
  419. * from the PSP and execution uses PSP after return.
  420. *
  421. * 0xFFFFFFED Return to Thread mode, exception return uses floating point state
  422. * from the PSP and execution uses PSP after return.
  423. **********************************************************************************************************
  424. */
  425. OS_STK *OSTaskStkInit (void (*task)(void *p_arg),
  426. void *p_arg,
  427. OS_STK *ptos,
  428. INT16U opt)
  429. {
  430. OS_STK *p_stk;
  431. (void)opt; /* 'opt' is not used, prevent warning */
  432. p_stk = ptos + 1u; /* Load stack pointer */
  433. /* Align the stack to 8-bytes. */
  434. p_stk = (OS_STK *)((OS_STK)(p_stk) & 0xFFFFFFF8u);
  435. /* Registers stacked as if auto-saved on exception */
  436. #if (OS_CPU_ARM_FP_EN > 0u) /* FPU auto-saved registers. */
  437. --p_stk;
  438. *(--p_stk) = (OS_STK)0x02000000u; /* FPSCR */
  439. /* Initialize S0-S15 floating point registers */
  440. *(--p_stk) = (OS_STK)0x41700000u; /* S15 */
  441. *(--p_stk) = (OS_STK)0x41600000u; /* S14 */
  442. *(--p_stk) = (OS_STK)0x41500000u; /* S13 */
  443. *(--p_stk) = (OS_STK)0x41400000u; /* S12 */
  444. *(--p_stk) = (OS_STK)0x41300000u; /* S11 */
  445. *(--p_stk) = (OS_STK)0x41200000u; /* S10 */
  446. *(--p_stk) = (OS_STK)0x41100000u; /* S9 */
  447. *(--p_stk) = (OS_STK)0x41000000u; /* S8 */
  448. *(--p_stk) = (OS_STK)0x40E00000u; /* S7 */
  449. *(--p_stk) = (OS_STK)0x40C00000u; /* S6 */
  450. *(--p_stk) = (OS_STK)0x40A00000u; /* S5 */
  451. *(--p_stk) = (OS_STK)0x40800000u; /* S4 */
  452. *(--p_stk) = (OS_STK)0x40400000u; /* S3 */
  453. *(--p_stk) = (OS_STK)0x40000000u; /* S2 */
  454. *(--p_stk) = (OS_STK)0x3F800000u; /* S1 */
  455. *(--p_stk) = (OS_STK)0x00000000u; /* S0 */
  456. #endif
  457. *(--p_stk) = (OS_STK)0x01000000uL; /* xPSR */
  458. *(--p_stk) = (OS_STK)task; /* Entry Point */
  459. *(--p_stk) = (OS_STK)OS_TaskReturn; /* R14 (LR) */
  460. *(--p_stk) = (OS_STK)0x12121212uL; /* R12 */
  461. *(--p_stk) = (OS_STK)0x03030303uL; /* R3 */
  462. *(--p_stk) = (OS_STK)0x02020202uL; /* R2 */
  463. *(--p_stk) = (OS_STK)0x01010101uL; /* R1 */
  464. *(--p_stk) = (OS_STK)p_arg; /* R0 : argument */
  465. #if (OS_CPU_ARM_FP_EN > 0u)
  466. *(--p_stk) = (OS_STK)0xFFFFFFEDuL; /* R14: EXEC_RETURN; See Note 5 */
  467. #else
  468. *(--p_stk) = (OS_STK)0xFFFFFFFDuL; /* R14: EXEC_RETURN; See Note 5 */
  469. #endif
  470. /* Remaining registers saved on process stack */
  471. *(--p_stk) = (OS_STK)0x11111111uL; /* R11 */
  472. *(--p_stk) = (OS_STK)0x10101010uL; /* R10 */
  473. *(--p_stk) = (OS_STK)0x09090909uL; /* R9 */
  474. *(--p_stk) = (OS_STK)0x08080808uL; /* R8 */
  475. *(--p_stk) = (OS_STK)0x07070707uL; /* R7 */
  476. *(--p_stk) = (OS_STK)0x06060606uL; /* R6 */
  477. *(--p_stk) = (OS_STK)0x05050505uL; /* R5 */
  478. *(--p_stk) = (OS_STK)0x04040404uL; /* R4 */
  479. #if (OS_CPU_ARM_FP_EN > 0u)
  480. /* Initialize S16-S31 floating point registers */
  481. *(--p_stk) = (OS_STK)0x41F80000u; /* S31 */
  482. *(--p_stk) = (OS_STK)0x41F00000u; /* S30 */
  483. *(--p_stk) = (OS_STK)0x41E80000u; /* S29 */
  484. *(--p_stk) = (OS_STK)0x41E00000u; /* S28 */
  485. *(--p_stk) = (OS_STK)0x41D80000u; /* S27 */
  486. *(--p_stk) = (OS_STK)0x41D00000u; /* S26 */
  487. *(--p_stk) = (OS_STK)0x41C80000u; /* S25 */
  488. *(--p_stk) = (OS_STK)0x41C00000u; /* S24 */
  489. *(--p_stk) = (OS_STK)0x41B80000u; /* S23 */
  490. *(--p_stk) = (OS_STK)0x41B00000u; /* S22 */
  491. *(--p_stk) = (OS_STK)0x41A80000u; /* S21 */
  492. *(--p_stk) = (OS_STK)0x41A00000u; /* S20 */
  493. *(--p_stk) = (OS_STK)0x41980000u; /* S19 */
  494. *(--p_stk) = (OS_STK)0x41900000u; /* S18 */
  495. *(--p_stk) = (OS_STK)0x41880000u; /* S17 */
  496. *(--p_stk) = (OS_STK)0x41800000u; /* S16 */
  497. #endif
  498. return (p_stk);
  499. }
  500. /*
  501. *********************************************************************************************************
  502. * TASK SWITCH HOOK
  503. *
  504. * Description: This function is called when a task switch is performed. This allows you to perform other
  505. * operations during a context switch.
  506. *
  507. * Arguments : none
  508. *
  509. * Note(s) : 1) Interrupts are disabled during this call.
  510. * 2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
  511. * will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
  512. * task being switched out (i.e. the preempted task).
  513. *********************************************************************************************************
  514. */
  515. #if (OS_CPU_HOOKS_EN > 0u) && (OS_TASK_SW_HOOK_EN > 0u)
  516. void OSTaskSwHook (void)
  517. {
  518. #if (OS_CPU_ARM_FP_EN > 0u)
  519. OS_CPU_FP_Reg_Push(OSTCBCur->OSTCBStkPtr); /* Push the FP registers of the current task. */
  520. #endif
  521. #if OS_APP_HOOKS_EN > 0u
  522. App_TaskSwHook();
  523. #endif
  524. OS_TRACE_TASK_SWITCHED_IN(OSTCBHighRdy);
  525. #if (OS_CPU_ARM_FP_EN > 0u)
  526. OS_CPU_FP_Reg_Pop(OSTCBHighRdy->OSTCBStkPtr); /* Pop the FP registers of the highest ready task. */
  527. #endif
  528. }
  529. #endif
  530. /*
  531. *********************************************************************************************************
  532. * OS_TCBInit() HOOK
  533. *
  534. * Description: This function is called by OS_TCBInit() after setting up most of the TCB.
  535. *
  536. * Arguments : ptcb is a pointer to the TCB of the task being created.
  537. *
  538. * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
  539. *********************************************************************************************************
  540. */
  541. #if OS_CPU_HOOKS_EN > 0u
  542. void OSTCBInitHook (OS_TCB *ptcb)
  543. {
  544. #if OS_APP_HOOKS_EN > 0u
  545. App_TCBInitHook(ptcb);
  546. #else
  547. (void)ptcb; /* Prevent compiler warning */
  548. #endif
  549. }
  550. #endif
  551. /*
  552. *********************************************************************************************************
  553. * TICK HOOK
  554. *
  555. * Description: This function is called every tick.
  556. *
  557. * Arguments : none
  558. *
  559. * Note(s) : 1) Interrupts may or may not be ENABLED during this call.
  560. *********************************************************************************************************
  561. */
  562. #if (OS_CPU_HOOKS_EN > 0u) && (OS_TIME_TICK_HOOK_EN > 0u)
  563. void OSTimeTickHook (void)
  564. {
  565. #if OS_APP_HOOKS_EN > 0u
  566. App_TimeTickHook();
  567. #endif
  568. #if OS_TMR_EN > 0u
  569. OSTmrCtr++;
  570. if (OSTmrCtr >= (OS_TICKS_PER_SEC / OS_TMR_CFG_TICKS_PER_SEC)) {
  571. OSTmrCtr = 0u;
  572. OSTmrSignal();
  573. }
  574. #endif
  575. }
  576. #endif
  577. /*
  578. *********************************************************************************************************
  579. * SYS TICK HANDLER
  580. *
  581. * Description: Handle the system tick (SysTick) interrupt, which is used to generate the uC/OS-II tick
  582. * interrupt.
  583. *
  584. * Arguments : None.
  585. *
  586. * Note(s) : 1) This function MUST be placed on entry 15 of the Cortex-M vector table.
  587. *********************************************************************************************************
  588. */
  589. //void OS_CPU_SysTickHandler (void)
  590. //{
  591. //#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  592. // OS_CPU_SR cpu_sr;
  593. //#endif
  594. // OS_ENTER_CRITICAL();
  595. // OSIntEnter(); /* Tell uC/OS-II that we are starting an ISR */
  596. // OS_EXIT_CRITICAL();
  597. // OSTimeTick(); /* Call uC/OS-II's OSTimeTick() */
  598. // OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
  599. //}
  600. /*
  601. *********************************************************************************************************
  602. * INITIALIZE SYS TICK
  603. *
  604. * Description: Initialize the SysTick using the CPU clock frequency.
  605. *
  606. * Arguments : cpu_freq CPU clock frequency.
  607. *
  608. * Note(s) : 1) This function MUST be called after OSStart() & after processor initialization.
  609. *
  610. * 2) Either OS_CPU_SysTickInitFreq or OS_CPU_SysTickInit() can be called.
  611. *********************************************************************************************************
  612. */
  613. void OS_CPU_SysTickInitFreq (INT32U cpu_freq)
  614. {
  615. INT32U cnts;
  616. cnts = (cpu_freq / (INT32U)OS_TICKS_PER_SEC); /* Determine nbr SysTick cnts between two OS tick intr. */
  617. OS_CPU_SysTickInit(cnts);
  618. }
  619. /*
  620. *********************************************************************************************************
  621. * INITIALIZE SYS TICK
  622. *
  623. * Description: Initialize the SysTick using the number of counts between two ticks.
  624. *
  625. * Arguments : cnts Number of SysTick counts between two OS tick interrupts.
  626. *
  627. * Note(s) : 1) This function MUST be called after OSStart() & after processor initialization.
  628. *
  629. * 2) Either OS_CPU_SysTickInitFreq or OS_CPU_SysTickInit() can be called.
  630. *********************************************************************************************************
  631. */
  632. void OS_CPU_SysTickInit (INT32U cnts)
  633. {
  634. INT32U prio;
  635. INT32U basepri;
  636. /* Set BASEPRI boundary from the configuration. */
  637. basepri = (INT32U)(CPU_CFG_KA_IPL_BOUNDARY << (8u - CPU_CFG_NVIC_PRIO_BITS));
  638. OS_CPU_CM_SYST_RVR = cnts - 1u; /* Set Reload register. */
  639. /* Set SysTick handler prio. */
  640. prio = OS_CPU_CM_SCB_SHPRI3;
  641. prio &= 0x00FFFFFFu;
  642. prio |= (basepri << 24u);
  643. OS_CPU_CM_SCB_SHPRI3 = prio;
  644. /* Enable timer. */
  645. OS_CPU_CM_SYST_CSR |= OS_CPU_CM_SYST_CSR_CLKSOURCE |
  646. OS_CPU_CM_SYST_CSR_ENABLE;
  647. /* Enable timer interrupt. */
  648. OS_CPU_CM_SYST_CSR |= OS_CPU_CM_SYST_CSR_TICKINT;
  649. }