port.c 11 KB

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