Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdint.h>
  2.  
  3. uint32_t pxCurrentTCB = 0;
  4. #ifdef __cplusplus
  5. extern "C"
  6. #endif
  7. void vTaskSwitchContext() { }
  8.  
  9.  
  10. void vPortStartFirstTask( void )
  11. {
  12. /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector
  13. table offset register that can be used to locate the initial stack value.
  14. Not all M0 parts have the application vector table at address 0. */
  15. #if 1
  16. __asm volatile(
  17. " .syntax unified \n"
  18. " ldr r2, pxCurrentTCBConst2 \n" /* Obtain location of pxCurrentTCB. */
  19. " ldr r3, [r2] \n"
  20. " ldr r0, [r3] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  21. " adds r0, #32 \n" /* Discard everything up to r0. */
  22. " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
  23. " movs r0, #2 \n" /* Switch to the psp stack. */
  24. " msr CONTROL, r0 \n"
  25. " isb \n"
  26. " pop {r0-r5} \n" /* Pop the registers that are saved automatically. */
  27. " mov lr, r5 \n" /* lr is now in r5. */
  28. " pop {r3} \n" /* Return address is now in r3. */
  29. " pop {r2} \n" /* Pop and discard XPSR. */
  30. " cpsie i \n" /* The first task has its context and interrupts can be enabled. */
  31. " bx r3 \n" /* Finally, jump to the user defined task code. */
  32. " \n"
  33. " .align 4 \n"
  34. "pxCurrentTCBConst2: .word pxCurrentTCB "
  35. );
  36. #endif
  37. }
  38.  
  39.  
  40. void xPortPendSVHandler( void )
  41. {
  42. /* This is a naked function. */
  43.  
  44. __asm volatile
  45. (
  46. " .syntax unified \n"
  47. " mrs r0, psp \n"
  48. " \n"
  49. " ldr r3, pxCurrentTCBConst \n" /* Get the location of the current TCB. */
  50. " ldr r2, [r3] \n"
  51. " \n"
  52. " subs r0, r0, #32 \n" /* Make space for the remaining low registers. */
  53. " str r0, [r2] \n" /* Save the new top of stack. */
  54. " stmia r0!, {r4-r7} \n" /* Store the low registers that are not saved automatically. */
  55. " mov r4, r8 \n" /* Store the high registers. */
  56. " mov r5, r9 \n"
  57. " mov r6, r10 \n"
  58. " mov r7, r11 \n"
  59. " stmia r0!, {r4-r7} \n"
  60. " \n"
  61. " push {r3, r14} \n"
  62. " cpsid i \n"
  63. " bl vTaskSwitchContext \n"
  64. " cpsie i \n"
  65. " pop {r2, r3} \n" /* lr goes in r3. r2 now holds tcb pointer. */
  66. " \n"
  67. " ldr r1, [r2] \n"
  68. " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
  69. " adds r0, r0, #16 \n" /* Move to the high registers. */
  70. " ldmia r0!, {r4-r7} \n" /* Pop the high registers. */
  71. " mov r8, r4 \n"
  72. " mov r9, r5 \n"
  73. " mov r10, r6 \n"
  74. " mov r11, r7 \n"
  75. " \n"
  76. " msr psp, r0 \n" /* Remember the new top of stack for the task. */
  77. " \n"
  78. " subs r0, r0, #32 \n" /* Go back for the low registers that are not automatically restored. */
  79. " ldmia r0!, {r4-r7} \n" /* Pop low registers. */
  80. " \n"
  81. " bx r3 \n"
  82. " \n"
  83. " .align 4 \n"
  84. "pxCurrentTCBConst: .word pxCurrentTCB "
  85. );
  86. }
  87.  
  88. int main() {
  89.  
  90. //test call
  91. xPortPendSVHandler();
  92. vPortStartFirstTask();
  93.  
  94. return 0;
  95. }
  96.  
  97. void _start() {
  98. main();
  99. }
Advertisement
RAW Paste Data Copied
Advertisement