HAL_CM.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: HAL_CM.C
  5. * Purpose: Hardware Abstraction Layer for Cortex-M
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "RTX_Config.h"
  27. #include "rt_HAL_CM.h"
  28. /*----------------------------------------------------------------------------
  29. * Global Variables
  30. *---------------------------------------------------------------------------*/
  31. #ifdef DBG_MSG
  32. BIT dbg_msg;
  33. #endif
  34. /*----------------------------------------------------------------------------
  35. * Functions
  36. *---------------------------------------------------------------------------*/
  37. /*--------------------------- rt_init_stack ---------------------------------*/
  38. void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
  39. /* Prepare TCB and saved context for a first time start of a task. */
  40. U32 *stk,i,size;
  41. /* Prepare a complete interrupt frame for first task start */
  42. size = p_TCB->priv_stack >> 2;
  43. if (size == 0U) {
  44. size = (U16)os_stackinfo >> 2;
  45. }
  46. /* Write to the top of stack. */
  47. stk = &p_TCB->stack[size];
  48. /* Auto correct to 8-byte ARM stack alignment. */
  49. if ((U32)stk & 0x04U) {
  50. stk--;
  51. }
  52. stk -= 16;
  53. /* Default xPSR and initial PC */
  54. stk[15] = INITIAL_xPSR;
  55. stk[14] = (U32)task_body;
  56. /* Clear R4-R11,R0-R3,R12,LR registers. */
  57. for (i = 0U; i < 14U; i++) {
  58. stk[i] = 0U;
  59. }
  60. /* Assign a void pointer to R0. */
  61. stk[8] = (U32)p_TCB->msg;
  62. /* Initial Task stack pointer. */
  63. p_TCB->tsk_stack = (U32)stk;
  64. /* Task entry point. */
  65. p_TCB->ptask = task_body;
  66. /* Initialize stack with magic pattern. */
  67. if (os_stackinfo & 0x10000000U) {
  68. if (size > (16U+1U)) {
  69. for (i = ((size - 16U)/2U) - 1U; i; i--) {
  70. stk -= 2U;
  71. stk[1] = MAGIC_PATTERN;
  72. stk[0] = MAGIC_PATTERN;
  73. }
  74. if (--stk > p_TCB->stack) {
  75. *stk = MAGIC_PATTERN;
  76. }
  77. }
  78. }
  79. /* Set a magic word for checking of stack overflow. */
  80. p_TCB->stack[0] = MAGIC_WORD;
  81. }
  82. /*--------------------------- rt_ret_val ----------------------------------*/
  83. static __inline U32 *rt_ret_regs (P_TCB p_TCB) {
  84. /* Get pointer to task return value registers (R0..R3) in Stack */
  85. #if defined(__TARGET_FPU_VFP)
  86. if (p_TCB->stack_frame) {
  87. /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */
  88. return (U32 *)(p_TCB->tsk_stack + (8U*4U) + (16U*4U));
  89. } else {
  90. /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  91. return (U32 *)(p_TCB->tsk_stack + (8U*4U));
  92. }
  93. #else
  94. /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  95. return (U32 *)(p_TCB->tsk_stack + (8U*4U));
  96. #endif
  97. }
  98. void rt_ret_val (P_TCB p_TCB, U32 v0) {
  99. U32 *ret;
  100. ret = rt_ret_regs(p_TCB);
  101. ret[0] = v0;
  102. }
  103. void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) {
  104. U32 *ret;
  105. ret = rt_ret_regs(p_TCB);
  106. ret[0] = v0;
  107. ret[1] = v1;
  108. }
  109. /*--------------------------- dbg_init --------------------------------------*/
  110. #ifdef DBG_MSG
  111. void dbg_init (void) {
  112. if (((DEMCR & DEMCR_TRCENA) != 0U) &&
  113. ((ITM_CONTROL & ITM_ITMENA) != 0U) &&
  114. ((ITM_ENABLE & (1UL << 31)) != 0U)) {
  115. dbg_msg = __TRUE;
  116. }
  117. }
  118. #endif
  119. /*--------------------------- dbg_task_notify -------------------------------*/
  120. #ifdef DBG_MSG
  121. void dbg_task_notify (P_TCB p_tcb, BOOL create) {
  122. while (ITM_PORT31_U32 == 0U);
  123. ITM_PORT31_U32 = (U32)p_tcb->ptask;
  124. while (ITM_PORT31_U32 == 0U);
  125. ITM_PORT31_U16 = (U16)((create << 8) | p_tcb->task_id);
  126. }
  127. #endif
  128. /*--------------------------- dbg_task_switch -------------------------------*/
  129. #ifdef DBG_MSG
  130. void dbg_task_switch (U32 task_id) {
  131. while (ITM_PORT31_U32 == 0U);
  132. ITM_PORT31_U8 = (U8)task_id;
  133. }
  134. #endif
  135. /*----------------------------------------------------------------------------
  136. * end of file
  137. *---------------------------------------------------------------------------*/