HAL_CM.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: HAL_CM.C
  5. * Purpose: Hardware Abstraction Layer for Cortex-M
  6. * Rev.: V4.70
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Config.h"
  36. #include "rt_HAL_CM.h"
  37. /*----------------------------------------------------------------------------
  38. * Global Variables
  39. *---------------------------------------------------------------------------*/
  40. #ifdef DBG_MSG
  41. BIT dbg_msg;
  42. #endif
  43. /*----------------------------------------------------------------------------
  44. * Functions
  45. *---------------------------------------------------------------------------*/
  46. /*--------------------------- rt_init_stack ---------------------------------*/
  47. void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
  48. /* Prepare TCB and saved context for a first time start of a task. */
  49. U32 *stk,i,size;
  50. /* Prepare a complete interrupt frame for first task start */
  51. size = p_TCB->priv_stack >> 2;
  52. if (size == 0) {
  53. size = (U16)os_stackinfo >> 2;
  54. }
  55. /* Write to the top of stack. */
  56. stk = &p_TCB->stack[size];
  57. /* Auto correct to 8-byte ARM stack alignment. */
  58. if ((U32)stk & 0x04) {
  59. stk--;
  60. }
  61. stk -= 16;
  62. /* Default xPSR and initial PC */
  63. stk[15] = INITIAL_xPSR;
  64. stk[14] = (U32)task_body;
  65. /* Clear R4-R11,R0-R3,R12,LR registers. */
  66. for (i = 0; i < 14; i++) {
  67. stk[i] = 0;
  68. }
  69. /* Assign a void pointer to R0. */
  70. stk[8] = (U32)p_TCB->msg;
  71. /* Initial Task stack pointer. */
  72. p_TCB->tsk_stack = (U32)stk;
  73. /* Task entry point. */
  74. p_TCB->ptask = task_body;
  75. /* Set a magic word for checking of stack overflow. */
  76. p_TCB->stack[0] = MAGIC_WORD;
  77. }
  78. /*--------------------------- rt_ret_val ----------------------------------*/
  79. static __inline U32 *rt_ret_regs (P_TCB p_TCB) {
  80. /* Get pointer to task return value registers (R0..R3) in Stack */
  81. #if (__TARGET_FPU_VFP)
  82. if (p_TCB->stack_frame) {
  83. /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */
  84. return (U32 *)(p_TCB->tsk_stack + 8*4 + 16*4);
  85. } else {
  86. /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  87. return (U32 *)(p_TCB->tsk_stack + 8*4);
  88. }
  89. #else
  90. /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  91. return (U32 *)(p_TCB->tsk_stack + 8*4);
  92. #endif
  93. }
  94. void rt_ret_val (P_TCB p_TCB, U32 v0) {
  95. U32 *ret;
  96. ret = rt_ret_regs(p_TCB);
  97. ret[0] = v0;
  98. }
  99. void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) {
  100. U32 *ret;
  101. ret = rt_ret_regs(p_TCB);
  102. ret[0] = v0;
  103. ret[1] = v1;
  104. }
  105. /*--------------------------- dbg_init --------------------------------------*/
  106. #ifdef DBG_MSG
  107. void dbg_init (void) {
  108. if ((DEMCR & DEMCR_TRCENA) &&
  109. (ITM_CONTROL & ITM_ITMENA) &&
  110. (ITM_ENABLE & (1UL << 31))) {
  111. dbg_msg = __TRUE;
  112. }
  113. }
  114. #endif
  115. /*--------------------------- dbg_task_notify -------------------------------*/
  116. #ifdef DBG_MSG
  117. void dbg_task_notify (P_TCB p_tcb, BOOL create) {
  118. while (ITM_PORT31_U32 == 0);
  119. ITM_PORT31_U32 = (U32)p_tcb->ptask;
  120. while (ITM_PORT31_U32 == 0);
  121. ITM_PORT31_U16 = (create << 8) | p_tcb->task_id;
  122. }
  123. #endif
  124. /*--------------------------- dbg_task_switch -------------------------------*/
  125. #ifdef DBG_MSG
  126. void dbg_task_switch (U32 task_id) {
  127. while (ITM_PORT31_U32 == 0);
  128. ITM_PORT31_U8 = task_id;
  129. }
  130. #endif
  131. /*----------------------------------------------------------------------------
  132. * end of file
  133. *---------------------------------------------------------------------------*/