port.c 4.4 KB

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