portcomn.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*
  29. Changes from V1.00:
  30. + pxPortInitialiseStack() now initialises the stack of new tasks to the
  31. same format used by the compiler. This allows the compiler generated
  32. interrupt mechanism to be used for context switches.
  33. Changes from V2.4.2:
  34. + pvPortMalloc and vPortFree have been removed. The projects now use
  35. the definitions from the source/portable/MemMang directory.
  36. Changes from V2.6.1:
  37. + usPortCheckFreeStackSpace() has been moved to tasks.c.
  38. */
  39. #include <stdlib.h>
  40. #include "FreeRTOS.h"
  41. /*-----------------------------------------------------------*/
  42. /* See header file for description. */
  43. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  44. {
  45. StackType_t DS_Reg = 0, *pxOriginalSP;
  46. /* Place a few bytes of known values on the bottom of the stack.
  47. This is just useful for debugging. */
  48. *pxTopOfStack = 0x1111;
  49. pxTopOfStack--;
  50. *pxTopOfStack = 0x2222;
  51. pxTopOfStack--;
  52. *pxTopOfStack = 0x3333;
  53. pxTopOfStack--;
  54. *pxTopOfStack = 0x4444;
  55. pxTopOfStack--;
  56. *pxTopOfStack = 0x5555;
  57. pxTopOfStack--;
  58. /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
  59. /* We are going to start the scheduler using a return from interrupt
  60. instruction to load the program counter, so first there would be the
  61. status register and interrupt return address. We make this the start
  62. of the task. */
  63. *pxTopOfStack = portINITIAL_SW;
  64. pxTopOfStack--;
  65. *pxTopOfStack = FP_SEG( pxCode );
  66. pxTopOfStack--;
  67. *pxTopOfStack = FP_OFF( pxCode );
  68. pxTopOfStack--;
  69. /* We are going to setup the stack for the new task to look like
  70. the stack frame was setup by a compiler generated ISR. We need to know
  71. the address of the existing stack top to place in the SP register within
  72. the stack frame. pxOriginalSP holds SP before (simulated) pusha was
  73. called. */
  74. pxOriginalSP = pxTopOfStack;
  75. /* The remaining registers would be pushed on the stack by our context
  76. switch function. These are loaded with values simply to make debugging
  77. easier. */
  78. *pxTopOfStack = FP_OFF( pvParameters ); /* AX */
  79. pxTopOfStack--;
  80. *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
  81. pxTopOfStack--;
  82. *pxTopOfStack = FP_SEG( pvParameters ); /* DX */
  83. pxTopOfStack--;
  84. *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
  85. pxTopOfStack--;
  86. *pxTopOfStack = FP_OFF( pxOriginalSP ); /* SP */
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
  89. pxTopOfStack--;
  90. *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
  93. /* We need the true data segment. */
  94. __asm{ MOV DS_Reg, DS };
  95. pxTopOfStack--;
  96. *pxTopOfStack = DS_Reg; /* DS */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
  99. /* The AX register is pushed again twice - don't know why. */
  100. pxTopOfStack--;
  101. *pxTopOfStack = FP_OFF( pvParameters ); /* AX */
  102. pxTopOfStack--;
  103. *pxTopOfStack = FP_OFF( pvParameters ); /* AX */
  104. #ifdef DEBUG_BUILD
  105. /* The compiler adds space to each ISR stack if building to
  106. include debug information. Presumably this is used by the
  107. debugger - we don't need to initialise it to anything just
  108. make sure it is there. */
  109. pxTopOfStack--;
  110. #endif
  111. /*lint +e950 +e611 +e923 */
  112. return pxTopOfStack;
  113. }
  114. /*-----------------------------------------------------------*/