port.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 );
  38. /*
  39. * Exception handlers.
  40. */
  41. void xPortPendSVHandler( void );
  42. void vPortSVCHandler( void );
  43. /*-----------------------------------------------------------*/
  44. __asm void vPortSVCHandler( void )
  45. {
  46. PRESERVE8
  47. /* Get the location of the current TCB. */
  48. ldr r3, =pxCurrentTCB
  49. ldr r1, [r3]
  50. ldr r0, [r1]
  51. /* Pop the core registers. */
  52. ldmia r0!, {r4-r11, r14}
  53. msr psp, r0
  54. isb
  55. mov r0, #0
  56. msr basepri, r0
  57. bx r14
  58. }
  59. /*-----------------------------------------------------------*/
  60. __asm void vPortStartFirstTask( void )
  61. {
  62. PRESERVE8
  63. EXTERN __Vectors
  64. /* Use the NVIC offset register to locate the stack. */
  65. ldr r0, =__Vectors
  66. ldr r0, [r0]
  67. /* Set the msp back to the start of the stack. */
  68. msr msp, r0
  69. /* Globally enable interrupts. */
  70. cpsie i
  71. cpsie f
  72. dsb
  73. isb
  74. #ifdef SOFTDEVICE_PRESENT
  75. /* Block kernel interrupts only (PendSV) before calling SVC */
  76. mov r0, #(configKERNEL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  77. msr basepri, r0
  78. #endif
  79. /* Call SVC to start the first task. */
  80. svc 0
  81. ALIGN
  82. }
  83. /*-----------------------------------------------------------*/
  84. __asm void xPortPendSVHandler( void )
  85. {
  86. extern uxCriticalNesting;
  87. extern pxCurrentTCB;
  88. extern vTaskSwitchContext;
  89. PRESERVE8
  90. mrs r0, psp
  91. isb
  92. /* Get the location of the current TCB. */
  93. ldr r3, =pxCurrentTCB
  94. ldr r2, [r3]
  95. /* Is the task using the FPU context? If so, push high vfp registers. */
  96. tst r14, #0x10
  97. it eq
  98. vstmdbeq r0!, {s16-s31}
  99. /* Save the core registers. */
  100. stmdb r0!, {r4-r11, r14}
  101. /* Save the new top of stack into the first member of the TCB. */
  102. str r0, [r2]
  103. stmdb sp!, {r3}
  104. mov r0, #(configMAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
  105. msr basepri, r0
  106. dsb
  107. isb
  108. bl vTaskSwitchContext
  109. mov r0, #0
  110. msr basepri, r0
  111. ldmia sp!, {r3}
  112. /* The first item in pxCurrentTCB is the task top of stack. */
  113. ldr r1, [r3]
  114. ldr r0, [r1]
  115. /* Pop the core registers. */
  116. ldmia r0!, {r4-r11, r14}
  117. /* Is the task using the FPU context? If so, pop the high vfp registers
  118. too. */
  119. tst r14, #0x10
  120. it eq
  121. vldmiaeq r0!, {s16-s31}
  122. msr psp, r0
  123. isb
  124. bx r14
  125. ALIGN
  126. }
  127. /*-----------------------------------------------------------*/