Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HAL_CM.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: HAL_CM.C
  5. * Purpose: Hardware Abstraction Layer for Cortex-M
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 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_Conf.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. /* Write to the top of stack. */
  53. stk = &p_TCB->stack[size];
  54. /* Auto correct to 8-byte ARM stack alignment. */
  55. if ((U32)stk & 0x04) {
  56. stk--;
  57. }
  58. stk -= 16;
  59. /* Default xPSR and initial PC */
  60. stk[15] = INITIAL_xPSR;
  61. stk[14] = (U32)task_body;
  62. /* Clear R4-R11,R0-R3,R12,LR registers. */
  63. for (i = 0; i < 14; i++) {
  64. stk[i] = 0;
  65. }
  66. /* Assign a void pointer to R0. */
  67. stk[8] = (U32)p_TCB->msg;
  68. /* Initial Task stack pointer. */
  69. p_TCB->tsk_stack = (U32)stk;
  70. /* Task entry point. */
  71. p_TCB->ptask = task_body;
  72. /* Set a magic word for checking of stack overflow.
  73. For the main thread (ID: 0x01) the stack is in a memory area shared with the
  74. heap, therefore the last word of the stack is a moving target.
  75. We want to do stack/heap collision detection instead.
  76. */
  77. if (p_TCB->task_id != 0x01)
  78. p_TCB->stack[0] = MAGIC_WORD;
  79. }
  80. /*--------------------------- rt_ret_val ----------------------------------*/
  81. static __inline U32 *rt_ret_regs (P_TCB p_TCB) {
  82. /* Get pointer to task return value registers (R0..R3) in Stack */
  83. #if (__TARGET_FPU_VFP)
  84. if (p_TCB->stack_frame) {
  85. /* Extended Stack Frame: R4-R11,S16-S31,R0-R3,R12,LR,PC,xPSR,S0-S15,FPSCR */
  86. return (U32 *)(p_TCB->tsk_stack + 8*4 + 16*4);
  87. } else {
  88. /* Basic Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  89. return (U32 *)(p_TCB->tsk_stack + 8*4);
  90. }
  91. #else
  92. /* Stack Frame: R4-R11,R0-R3,R12,LR,PC,xPSR */
  93. return (U32 *)(p_TCB->tsk_stack + 8*4);
  94. #endif
  95. }
  96. void rt_ret_val (P_TCB p_TCB, U32 v0) {
  97. U32 *ret;
  98. ret = rt_ret_regs(p_TCB);
  99. ret[0] = v0;
  100. }
  101. void rt_ret_val2(P_TCB p_TCB, U32 v0, U32 v1) {
  102. U32 *ret;
  103. ret = rt_ret_regs(p_TCB);
  104. ret[0] = v0;
  105. ret[1] = v1;
  106. }
  107. /*--------------------------- dbg_init --------------------------------------*/
  108. #ifdef DBG_MSG
  109. void dbg_init (void) {
  110. if ((DEMCR & DEMCR_TRCENA) &&
  111. (ITM_CONTROL & ITM_ITMENA) &&
  112. (ITM_ENABLE & (1UL << 31))) {
  113. dbg_msg = __TRUE;
  114. }
  115. }
  116. #endif
  117. /*--------------------------- dbg_task_notify -------------------------------*/
  118. #ifdef DBG_MSG
  119. void dbg_task_notify (P_TCB p_tcb, BOOL create) {
  120. while (ITM_PORT31_U32 == 0);
  121. ITM_PORT31_U32 = (U32)p_tcb->ptask;
  122. while (ITM_PORT31_U32 == 0);
  123. ITM_PORT31_U16 = (create << 8) | p_tcb->task_id;
  124. }
  125. #endif
  126. /*--------------------------- dbg_task_switch -------------------------------*/
  127. #ifdef DBG_MSG
  128. void dbg_task_switch (U32 task_id) {
  129. while (ITM_PORT31_U32 == 0);
  130. ITM_PORT31_U8 = task_id;
  131. }
  132. #endif
  133. /*----------------------------------------------------------------------------
  134. * end of file
  135. *---------------------------------------------------------------------------*/