port.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include <stdlib.h>
  29. #include "porthardware.h"
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. /*-----------------------------------------------------------
  33. * Implementation of functions defined in portable.h for the AVR port.
  34. *----------------------------------------------------------*/
  35. /* Start tasks with interrupts enables. */
  36. #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
  37. /*-----------------------------------------------------------*/
  38. #define portBYTES_USED_BY_RETURN_ADDRESS 2
  39. #define portNO_CRITICAL_NESTING ( ( UBaseType_t ) 0 )
  40. /* Stores the critical section nesting. This must not be initialised to 0.
  41. * It will be initialised when a task starts. */
  42. UBaseType_t uxCriticalNesting = 0x50;
  43. /*
  44. * Setup timer to generate a tick interrupt.
  45. */
  46. static void prvSetupTimerInterrupt( void );
  47. /*
  48. * The IAR compiler does not have full support for inline assembler, so
  49. * these are defined in the portmacro assembler file.
  50. */
  51. extern void vPortYieldFromTick( void );
  52. extern void vPortStart( void );
  53. /*-----------------------------------------------------------*/
  54. /*
  55. * See header file for description.
  56. */
  57. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  58. TaskFunction_t pxCode,
  59. void * pvParameters )
  60. {
  61. uint16_t usAddress;
  62. StackType_t * pxTopOfHardwareStack;
  63. /* Simulate how the stack would look after a call to vPortYield(). */
  64. /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
  65. /* The IAR compiler requires two stacks per task. First there is the
  66. * hardware call stack which uses the AVR stack pointer. Second there is the
  67. * software stack (local variables, parameter passing, etc.) which uses the
  68. * AVR Y register.
  69. * This function places both stacks within the memory block passed in as the
  70. * first parameter. The hardware stack is placed at the bottom of the memory
  71. * block. A gap is then left for the hardware stack to grow. Next the software
  72. * stack is placed. The amount of space between the software and hardware
  73. * stacks is defined by configCALL_STACK_SIZE.
  74. * The first part of the stack is the hardware stack. Place the start
  75. * address of the task on the hardware stack. */
  76. /* Place a few bytes of known values on the bottom of the stack.
  77. * This is just useful for debugging. */
  78. /**pxTopOfStack = 0x11; */
  79. /*pxTopOfStack--; */
  80. /**pxTopOfStack = 0x22; */
  81. /*pxTopOfStack--; */
  82. /**pxTopOfStack = 0x33; */
  83. /*pxTopOfStack--; */
  84. /* Remember where the top of the hardware stack is - this is required
  85. * below. */
  86. pxTopOfHardwareStack = pxTopOfStack;
  87. usAddress = ( uint16_t ) pxCode;
  88. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  89. pxTopOfStack--;
  90. usAddress >>= 8;
  91. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  92. pxTopOfStack--;
  93. /* Leave enough space for the hardware stack before starting the software
  94. * stack. The '- 2' is because we have already used two spaces for the
  95. * address of the start of the task. */
  96. pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );
  97. /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
  98. * portSAVE_CONTEXT places the flags on the stack immediately after r0
  99. * to ensure the interrupts get disabled as soon as possible, and so ensuring
  100. * the stack use is minimal should a context switch interrupt occur. */
  101. *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
  102. pxTopOfStack--;
  103. *pxTopOfStack = portFLAGS_INT_ENABLED;
  104. pxTopOfStack--;
  105. *pxTopOfStack = ( StackType_t ) 0x00; /* RAMPZ */
  106. pxTopOfStack--;
  107. /* Next place the address of the hardware stack. This is required so
  108. * the AVR stack pointer can be restored to point to the hardware stack. */
  109. pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;
  110. usAddress = ( uint16_t ) pxTopOfHardwareStack;
  111. /* SPL */
  112. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  113. pxTopOfStack--;
  114. /* SPH */
  115. usAddress >>= 8;
  116. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  117. pxTopOfStack--;
  118. /* Now the remaining registers. */
  119. *pxTopOfStack = ( StackType_t ) 0x01; /* R1 */
  120. pxTopOfStack--;
  121. *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
  122. pxTopOfStack--;
  123. *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
  124. pxTopOfStack--;
  125. *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
  126. pxTopOfStack--;
  127. *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
  128. pxTopOfStack--;
  129. *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
  130. pxTopOfStack--;
  131. *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
  132. pxTopOfStack--;
  133. *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
  134. pxTopOfStack--;
  135. *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
  136. pxTopOfStack--;
  137. *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
  138. pxTopOfStack--;
  139. *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
  140. pxTopOfStack--;
  141. *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
  142. pxTopOfStack--;
  143. *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
  144. pxTopOfStack--;
  145. *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
  146. pxTopOfStack--;
  147. *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
  148. pxTopOfStack--;
  149. /* Place the parameter on the stack in the expected location. */
  150. usAddress = ( uint16_t ) pvParameters;
  151. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  152. pxTopOfStack--;
  153. usAddress >>= 8;
  154. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  155. pxTopOfStack--;
  156. *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
  157. pxTopOfStack--;
  158. *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
  159. pxTopOfStack--;
  160. *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
  161. pxTopOfStack--;
  162. *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
  163. pxTopOfStack--;
  164. *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
  165. pxTopOfStack--;
  166. *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
  167. pxTopOfStack--;
  168. *pxTopOfStack = ( StackType_t ) 0x24; /* R24 */
  169. pxTopOfStack--;
  170. *pxTopOfStack = ( StackType_t ) 0x25; /* R25 */
  171. pxTopOfStack--;
  172. *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
  173. pxTopOfStack--;
  174. *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
  175. pxTopOfStack--;
  176. /* The Y register is not stored as it is used as the software stack and
  177. * gets saved into the task control block. */
  178. *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
  179. pxTopOfStack--;
  180. *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
  181. pxTopOfStack--;
  182. *pxTopOfStack = portNO_CRITICAL_NESTING; /* Critical nesting is zero when the task starts. */
  183. /*lint +e950 +e611 +e923 */
  184. return pxTopOfStack;
  185. }
  186. /*-----------------------------------------------------------*/
  187. BaseType_t xPortStartScheduler( void )
  188. {
  189. /* Setup the hardware to generate the tick. */
  190. prvSetupTimerInterrupt();
  191. /* Restore the context of the first task that is going to run.
  192. * Normally we would just call portRESTORE_CONTEXT() here, but as the IAR
  193. * compiler does not fully support inline assembler we have to make a call.*/
  194. vPortStart();
  195. /* Should not get here. */
  196. return pdTRUE;
  197. }
  198. /*-----------------------------------------------------------*/
  199. void vPortEndScheduler( void )
  200. {
  201. /* vPortEndScheduler is not implemented in this port. */
  202. }
  203. /*-----------------------------------------------------------*/
  204. /*
  205. * Setup timer to generate a tick interrupt.
  206. */
  207. static void prvSetupTimerInterrupt( void )
  208. {
  209. TICK_init();
  210. }
  211. /*-----------------------------------------------------------*/
  212. #if configUSE_PREEMPTION == 1
  213. /*
  214. * Tick ISR for preemptive scheduler. We can use a naked attribute as
  215. * the context is saved at the start of vPortYieldFromTick(). The tick
  216. * count is incremented after the context is saved.
  217. */
  218. __task void TICK_INT( void )
  219. {
  220. vPortYieldFromTick();
  221. asm ( "reti" );
  222. }
  223. #else
  224. /*
  225. * Tick ISR for the cooperative scheduler. All this does is increment the
  226. * tick count. We don't need to switch context, this can only be done by
  227. * manual calls to taskYIELD();
  228. */
  229. __interrupt void TICK_INT( void )
  230. {
  231. /* Clear tick interrupt flag. */
  232. INT_FLAGS = INT_MASK;
  233. xTaskIncrementTick();
  234. }
  235. #endif /* if configUSE_PREEMPTION == 1 */
  236. /*-----------------------------------------------------------*/
  237. void vPortEnterCritical( void )
  238. {
  239. portDISABLE_INTERRUPTS();
  240. uxCriticalNesting++;
  241. }
  242. /*-----------------------------------------------------------*/
  243. void vPortExitCritical( void )
  244. {
  245. uxCriticalNesting--;
  246. if( uxCriticalNesting == portNO_CRITICAL_NESTING )
  247. {
  248. portENABLE_INTERRUPTS();
  249. }
  250. }