port.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /* Next place the address of the hardware stack. This is required so
  106. * the AVR stack pointer can be restored to point to the hardware stack. */
  107. pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;
  108. usAddress = ( uint16_t ) pxTopOfHardwareStack;
  109. /* SPL */
  110. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  111. pxTopOfStack--;
  112. /* SPH */
  113. usAddress >>= 8;
  114. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  115. pxTopOfStack--;
  116. /* Now the remaining registers. */
  117. *pxTopOfStack = ( StackType_t ) 0x01; /* R1 */
  118. pxTopOfStack--;
  119. *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
  120. pxTopOfStack--;
  121. *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
  122. pxTopOfStack--;
  123. *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
  124. pxTopOfStack--;
  125. *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
  126. pxTopOfStack--;
  127. *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
  128. pxTopOfStack--;
  129. *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
  130. pxTopOfStack--;
  131. *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
  132. pxTopOfStack--;
  133. *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
  134. pxTopOfStack--;
  135. *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
  136. pxTopOfStack--;
  137. *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
  138. pxTopOfStack--;
  139. *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
  140. pxTopOfStack--;
  141. *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
  142. pxTopOfStack--;
  143. *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
  144. pxTopOfStack--;
  145. *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
  146. pxTopOfStack--;
  147. /* Place the parameter on the stack in the expected location. */
  148. usAddress = ( uint16_t ) pvParameters;
  149. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  150. pxTopOfStack--;
  151. usAddress >>= 8;
  152. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  153. pxTopOfStack--;
  154. *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
  155. pxTopOfStack--;
  156. *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
  157. pxTopOfStack--;
  158. *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
  159. pxTopOfStack--;
  160. *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
  161. pxTopOfStack--;
  162. *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
  163. pxTopOfStack--;
  164. *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
  165. pxTopOfStack--;
  166. *pxTopOfStack = ( StackType_t ) 0x24; /* R24 */
  167. pxTopOfStack--;
  168. *pxTopOfStack = ( StackType_t ) 0x25; /* R25 */
  169. pxTopOfStack--;
  170. *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
  171. pxTopOfStack--;
  172. *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
  173. pxTopOfStack--;
  174. /* The Y register is not stored as it is used as the software stack and
  175. * gets saved into the task control block. */
  176. *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
  177. pxTopOfStack--;
  178. *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
  179. pxTopOfStack--;
  180. *pxTopOfStack = portNO_CRITICAL_NESTING; /* Critical nesting is zero when the task starts. */
  181. /*lint +e950 +e611 +e923 */
  182. return pxTopOfStack;
  183. }
  184. /*-----------------------------------------------------------*/
  185. BaseType_t xPortStartScheduler( void )
  186. {
  187. /* Setup the hardware to generate the tick. */
  188. prvSetupTimerInterrupt();
  189. /* Restore the context of the first task that is going to run.
  190. * Normally we would just call portRESTORE_CONTEXT() here, but as the IAR
  191. * compiler does not fully support inline assembler we have to make a call.*/
  192. vPortStart();
  193. /* Should not get here. */
  194. return pdTRUE;
  195. }
  196. /*-----------------------------------------------------------*/
  197. void vPortEndScheduler( void )
  198. {
  199. /* vPortEndScheduler is not implemented in this port. */
  200. }
  201. /*-----------------------------------------------------------*/
  202. /*
  203. * Setup timer to generate a tick interrupt.
  204. */
  205. static void prvSetupTimerInterrupt( void )
  206. {
  207. TICK_init();
  208. }
  209. /*-----------------------------------------------------------*/
  210. #if configUSE_PREEMPTION == 1
  211. /*
  212. * Tick ISR for preemptive scheduler. We can use a naked attribute as
  213. * the context is saved at the start of vPortYieldFromTick(). The tick
  214. * count is incremented after the context is saved.
  215. */
  216. __task void TICK_INT( void )
  217. {
  218. vPortYieldFromTick();
  219. asm ( "reti" );
  220. }
  221. #else
  222. /*
  223. * Tick ISR for the cooperative scheduler. All this does is increment the
  224. * tick count. We don't need to switch context, this can only be done by
  225. * manual calls to taskYIELD();
  226. */
  227. __interrupt void TICK_INT( void )
  228. {
  229. /* Clear tick interrupt flag. */
  230. INT_FLAGS = INT_MASK;
  231. xTaskIncrementTick();
  232. }
  233. #endif /* if configUSE_PREEMPTION == 1 */
  234. /*-----------------------------------------------------------*/
  235. void vPortEnterCritical( void )
  236. {
  237. portDISABLE_INTERRUPTS();
  238. uxCriticalNesting++;
  239. }
  240. /*-----------------------------------------------------------*/
  241. void vPortExitCritical( void )
  242. {
  243. uxCriticalNesting--;
  244. if( uxCriticalNesting == portNO_CRITICAL_NESTING )
  245. {
  246. portENABLE_INTERRUPTS();
  247. }
  248. }