port.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. void vPortStartFirstTask( void ) __attribute__ (( naked ));
  38. /*
  39. * Exception handlers.
  40. */
  41. void vPortSVCHandler( void ) __attribute__ (( naked ));
  42. void xPortPendSVHandler( void ) __attribute__ (( naked ));
  43. /*-----------------------------------------------------------*/
  44. void vPortStartFirstTask( void )
  45. {
  46. __asm volatile(
  47. #if defined(__SES_ARM)
  48. " ldr r0, =_vectors \n" /* Locate the stack using _vectors table. */
  49. #else
  50. " ldr r0, =__isr_vector \n" /* Locate the stack using __isr_vector table. */
  51. #endif
  52. " ldr r0, [r0] \n"
  53. " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
  54. " cpsie i \n" /* Globally enable interrupts. */
  55. " cpsie f \n"
  56. " dsb \n"
  57. " isb \n"
  58. #ifdef SOFTDEVICE_PRESENT
  59. /* Block kernel interrupts only (PendSV) before calling SVC */
  60. " mov r0, %0 \n"
  61. " msr basepri, r0 \n"
  62. #endif
  63. " svc 0 \n" /* System call to start first task. */
  64. " \n"
  65. " .align 2 \n"
  66. #ifdef SOFTDEVICE_PRESENT
  67. ::"i"(configKERNEL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  68. #endif
  69. );
  70. }
  71. /*-----------------------------------------------------------*/
  72. void vPortSVCHandler( void )
  73. {
  74. __asm volatile (
  75. " ldr r3, =pxCurrentTCB \n" /* Restore the context. */
  76. " ldr r1, [r3] \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
  77. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  78. " ldmia r0!, {r4-r11, r14} \n" /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
  79. " msr psp, r0 \n" /* Restore the task stack pointer. */
  80. " isb \n"
  81. " mov r0, #0 \n"
  82. " msr basepri, r0 \n"
  83. " bx r14 \n"
  84. " \n"
  85. " .align 2 \n"
  86. );
  87. }
  88. /*-----------------------------------------------------------*/
  89. void xPortPendSVHandler( void )
  90. {
  91. /* This is a naked function. */
  92. __asm volatile
  93. (
  94. " mrs r0, psp \n"
  95. " isb \n"
  96. " \n"
  97. " ldr r3, =pxCurrentTCB \n" /* Get the location of the current TCB. */
  98. " ldr r2, [r3] \n"
  99. " \n"
  100. " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, push high vfp registers. */
  101. " it eq \n"
  102. " vstmdbeq r0!, {s16-s31} \n"
  103. " \n"
  104. " stmdb r0!, {r4-r11, r14} \n" /* Save the core registers. */
  105. " \n"
  106. " str r0, [r2] \n" /* Save the new top of stack into the first member of the TCB. */
  107. " \n"
  108. " stmdb sp!, {r3} \n"
  109. " mov r0, %0 \n"
  110. " msr basepri, r0 \n"
  111. " dsb \n"
  112. " isb \n"
  113. " bl vTaskSwitchContext \n"
  114. " mov r0, #0 \n"
  115. " msr basepri, r0 \n"
  116. " ldmia sp!, {r3} \n"
  117. " \n"
  118. " ldr r1, [r3] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  119. " ldr r0, [r1] \n"
  120. " \n"
  121. " ldmia r0!, {r4-r11, r14} \n" /* Pop the core registers. */
  122. " \n"
  123. " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, pop the high vfp registers too. */
  124. " it eq \n"
  125. " vldmiaeq r0!, {s16-s31} \n"
  126. " \n"
  127. " msr psp, r0 \n"
  128. " isb \n"
  129. " \n"
  130. " \n"
  131. " bx r14 \n"
  132. " \n"
  133. " .align 2 \n"
  134. ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  135. );
  136. }