port.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. void vPortStartFirstTask( void ) __attribute__ (( naked ));
  38. /*
  39. * Exception handlers.
  40. */
  41. void vPortSVCHandler( void );
  42. void xPortPendSVHandler( void ) __attribute__ (( naked ));
  43. /*-----------------------------------------------------------*/
  44. void vPortStartFirstTask( void )
  45. {
  46. __asm volatile(
  47. " .syntax unified \n"
  48. " \n"
  49. #if defined(__SES_ARM)
  50. " ldr r0, =_vectors \n" /* Locate the stack using _vectors table. */
  51. #else
  52. " ldr r0, =__isr_vector \n" /* Locate the stack using __isr_vector table. */
  53. #endif
  54. " ldr r0, [r0] \n"
  55. " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
  56. " \n"
  57. " ldr r3, =pxCurrentTCB \n" /* Obtain location of pxCurrentTCB. */
  58. " ldr r1, [r3] \n"
  59. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  60. " adds r0, #32 \n" /* Discard everything up to r0 */
  61. " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
  62. " movs r0, #2 \n" /* Switch to the psp stack. */
  63. " msr CONTROL, r0 \n"
  64. " pop {r0-r5} \n" /* Pop the registers that are saved automatically. */
  65. " mov lr, r5 \n" /* lr is now in r5. */
  66. " cpsie i \n" /* The first task has its context and interrupts can be enabled. */
  67. " pop {pc} \n" /* Finally, pop the PC to jump to the user defined task code. */
  68. " \n"
  69. " .align 2 \n"
  70. );
  71. }
  72. /*-----------------------------------------------------------*/
  73. void vPortSVCHandler( void )
  74. {
  75. /* This function is no longer used, but retained for backward
  76. compatibility. */
  77. }
  78. /*-----------------------------------------------------------*/
  79. void xPortPendSVHandler( void )
  80. {
  81. /* This is a naked function. */
  82. __asm volatile
  83. (
  84. " .syntax unified \n"
  85. " \n"
  86. " mrs r0, psp \n"
  87. " \n"
  88. " ldr r3, =pxCurrentTCB \n" /* Get the location of the current TCB. */
  89. " ldr r2, [r3] \n"
  90. " \n"
  91. " subs r0, #32 \n" /* Make space for the remaining low registers. */
  92. " str r0, [r2] \n" /* Save the new top of stack. */
  93. " stmia r0!, {r4-r7} \n" /* Store the low registers that are not saved automatically. */
  94. " mov r4, r8 \n" /* Store the high registers. */
  95. " mov r5, r9 \n"
  96. " mov r6, r10 \n"
  97. " mov r7, r11 \n"
  98. " stmia r0!, {r4-r7} \n"
  99. " \n"
  100. " push {r3, r14} \n"
  101. " bl vPortSafeTaskSwitchContext \n"
  102. " pop {r2, r3} \n" /* lr goes in r3. r2 now holds tcb pointer. */
  103. " \n"
  104. " ldr r1, [r2] \n"
  105. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  106. " adds r0, #16 \n" /* Move to the high registers. */
  107. " ldmia r0!, {r4-r7} \n" /* Pop the high registers. */
  108. " mov r8, r4 \n"
  109. " mov r9, r5 \n"
  110. " mov r10, r6 \n"
  111. " mov r11, r7 \n"
  112. " \n"
  113. " msr psp, r0 \n" /* Remember the new top of stack for the task. */
  114. " \n"
  115. " subs r0, #32 \n" /* Go back for the low registers that are not automatically restored. */
  116. " ldmia r0!, {r4-r7} \n" /* Pop low registers. */
  117. " \n"
  118. " bx r3 \n"
  119. " \n"
  120. " .align 2 \n"
  121. );
  122. }