port.c 6.4 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 CM0 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. void vPortSVCHandler( void );
  42. __stackless void xPortPendSVHandler( void );
  43. /*
  44. * The real object type has no meaning here - we would just use its addresses inside assembly
  45. */
  46. extern uint32_t __Vectors;
  47. extern uint32_t pxCurrentTCB;
  48. /*-----------------------------------------------------------*/
  49. __stackless void vPortStartFirstTask( void )
  50. {
  51. __asm volatile(
  52. " ldr r0, 1f \n" /* Use the NVIC offset register to locate the stack. */
  53. " ldr r0, [r0] \n"
  54. " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
  55. " \n"
  56. " ldr r3, 2f \n" /* Obtain location of pxCurrentTCB. */
  57. " ldr r1, [r3] \n"
  58. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  59. " adds r0, #32 \n" /* Discard everything up to r0 */
  60. " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
  61. " movs r0, #2 \n" /* Switch to the psp stack. */
  62. " msr CONTROL, r0 \n"
  63. " pop {r0-r5} \n" /* Pop the registers that are saved automatically. */
  64. " mov lr, r5 \n" /* lr is now in r5. */
  65. " cpsie i \n" /* The first task has its context and interrupts can be enabled. */
  66. " pop {pc} \n" /* Finally, pop the PC to jump to the user defined task code. */
  67. " \n"
  68. " nop \n" /* Manual alignment of the label below */
  69. "DATA \n"
  70. "1: \n"
  71. " DC32 %c0 \n"
  72. "2: \n"
  73. " DC32 %c1 \n"
  74. : /* Outputs */
  75. : /* Inputs */
  76. "i"(&__Vectors),
  77. "i"(&pxCurrentTCB)
  78. );
  79. }
  80. /*-----------------------------------------------------------*/
  81. void vPortSVCHandler( void )
  82. {
  83. /* This function is no longer used, but retained for backward
  84. compatibility. */
  85. }
  86. /*-----------------------------------------------------------*/
  87. __stackless void xPortPendSVHandler( void )
  88. {
  89. /* This is a naked function. */
  90. __asm volatile
  91. (
  92. " mrs r0, psp \n"
  93. " \n"
  94. " ldr r3, 1f \n" /* Get the location of the current TCB. */
  95. " ldr r2, [r3] \n"
  96. " \n"
  97. " subs r0, #32 \n" /* Make space for the remaining low registers. */
  98. " str r0, [r2] \n" /* Save the new top of stack. */
  99. " stmia r0!, {r4-r7} \n" /* Store the low registers that are not saved automatically. */
  100. " mov r4, r8 \n" /* Store the high registers. */
  101. " mov r5, r9 \n"
  102. " mov r6, r10 \n"
  103. " mov r7, r11 \n"
  104. " stmia r0!, {r4-r7} \n"
  105. " \n"
  106. " push {r3, r14} \n"
  107. " bl %c1 \n"
  108. " pop {r2, r3} \n" /* lr goes in r3. r2 now holds tcb pointer. */
  109. " \n"
  110. " ldr r1, [r2] \n"
  111. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  112. " adds r0, #16 \n" /* Move to the high registers. */
  113. " ldmia r0!, {r4-r7} \n" /* Pop the high registers. */
  114. " mov r8, r4 \n"
  115. " mov r9, r5 \n"
  116. " mov r10, r6 \n"
  117. " mov r11, r7 \n"
  118. " \n"
  119. " msr psp, r0 \n" /* Remember the new top of stack for the task. */
  120. " \n"
  121. " subs r0, #32 \n" /* Go back for the low registers that are not automatically restored. */
  122. " ldmia r0!, {r4-r7} \n" /* Pop low registers. */
  123. " \n"
  124. " bx r3 \n"
  125. " \n"
  126. " nop \n" /* Manual alignment of the label below */
  127. "DATA \n"
  128. "1: \n"
  129. " DC32 %c0 \n"
  130. : /* Outputs */
  131. : /* Inputs */
  132. "i"(&pxCurrentTCB),
  133. "i"(&vTaskSwitchContext)
  134. );
  135. }