port.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the ARM CM4F port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /*
  35. * Start first task is a separate function so it can be tested in isolation.
  36. */
  37. __stackless void vPortStartFirstTask( void );
  38. /*
  39. * Exception handlers.
  40. */
  41. __stackless void vPortSVCHandler( void );
  42. __stackless void xPortPendSVHandler( void );
  43. extern uint32_t __Vectors;
  44. extern uint32_t pxCurrentTCB;
  45. /*-----------------------------------------------------------*/
  46. __stackless void vPortStartFirstTask( void )
  47. {
  48. __asm volatile(
  49. " ldr.n r0, 1f \n" /* Locate the stack using __Vectors table. */
  50. " ldr r0, [r0] \n"
  51. " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
  52. " cpsie i \n" /* Globally enable interrupts. */
  53. " cpsie f \n"
  54. " dsb \n"
  55. " isb \n"
  56. #ifdef SOFTDEVICE_PRESENT
  57. /* Block kernel interrupts only (PendSV) before calling SVC */
  58. " mov r0, %1 \n"
  59. " msr basepri, r0 \n"
  60. #endif
  61. " svc 0 \n" /* System call to start first task. */
  62. " \n"
  63. " nop \n" /* Manual alignment of the label below */
  64. "DATA \n"
  65. "1: \n"
  66. " DC32 %c0 \n"
  67. : /* Outputs */
  68. : /* Inputs */
  69. "i"(&__Vectors)
  70. #ifdef SOFTDEVICE_PRESENT
  71. ,
  72. "i"(configKERNEL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  73. #endif
  74. );
  75. }
  76. /*-----------------------------------------------------------*/
  77. __stackless void vPortSVCHandler( void )
  78. {
  79. __asm volatile (
  80. " ldr.n r3, 1f \n" /* Restore the context. */
  81. " ldr r1, [r3] \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
  82. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  83. " ldmia r0!, {r4-r11, r14} \n" /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
  84. " msr psp, r0 \n" /* Restore the task stack pointer. */
  85. " isb \n"
  86. " mov r0, #0 \n"
  87. " msr basepri, r0 \n"
  88. " bx r14 \n"
  89. " \n"
  90. " \n" /* Manual alignment of the label below */
  91. "DATA \n"
  92. "1: \n"
  93. " DC32 %c0 \n"
  94. : /* Outputs */
  95. : /* Inputs */
  96. "i"(&pxCurrentTCB)
  97. );
  98. }
  99. /*-----------------------------------------------------------*/
  100. __stackless void xPortPendSVHandler( void )
  101. {
  102. /* This is a naked function. */
  103. __asm volatile
  104. (
  105. " mrs r0, psp \n"
  106. " isb \n"
  107. " \n"
  108. " ldr.n r3, 1f \n" /* Get the location of the current TCB. */
  109. " ldr r2, [r3] \n"
  110. " \n"
  111. " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, push high vfp registers. */
  112. " it eq \n"
  113. " vstmdbeq r0!, {s16-s31} \n"
  114. " \n"
  115. " stmdb r0!, {r4-r11, r14} \n" /* Save the core registers. */
  116. " \n"
  117. " str r0, [r2] \n" /* Save the new top of stack into the first member of the TCB. */
  118. " \n"
  119. " stmdb sp!, {r3} \n"
  120. " mov r0, %0 \n"
  121. " msr basepri, r0 \n"
  122. " dsb \n"
  123. " isb \n"
  124. " bl %c2 \n"
  125. " mov r0, #0 \n"
  126. " msr basepri, r0 \n"
  127. " ldmia sp!, {r3} \n"
  128. " \n"
  129. " ldr r1, [r3] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  130. " ldr r0, [r1] \n"
  131. " \n"
  132. " ldmia r0!, {r4-r11, r14} \n" /* Pop the core registers. */
  133. " \n"
  134. " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, pop the high vfp registers too. */
  135. " it eq \n"
  136. " vldmiaeq r0!, {s16-s31} \n"
  137. " \n"
  138. " msr psp, r0 \n"
  139. " isb \n"
  140. " \n"
  141. " \n"
  142. " bx r14 \n"
  143. " \n"
  144. " \n" /* Manual alignment of the label below */
  145. "DATA \n"
  146. "1: \n"
  147. " DC32 %c1 \n"
  148. : /* Outputs */
  149. : /* Inputs */
  150. "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)),
  151. "i"(&pxCurrentTCB),
  152. "i"(&vTaskSwitchContext)
  153. );
  154. }