port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 V2.6.0
  30. + AVR port - Replaced the inb() and outb() functions with direct memory
  31. access. This allows the port to be built with the 20050414 build of
  32. WinAVR.
  33. */
  34. #include <stdlib.h>
  35. #include <avr/interrupt.h>
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. /*-----------------------------------------------------------
  39. * Implementation of functions defined in portable.h for the AVR port.
  40. *----------------------------------------------------------*/
  41. /* Start tasks with interrupts enables. */
  42. #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
  43. /* Hardware constants for timer 1. */
  44. #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
  45. #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
  46. #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
  47. #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
  48. /*-----------------------------------------------------------*/
  49. /* We require the address of the pxCurrentTCB variable, but don't want to know
  50. any details of its type. */
  51. typedef void TCB_t;
  52. extern volatile TCB_t * volatile pxCurrentTCB;
  53. /*-----------------------------------------------------------*/
  54. /*
  55. * Macro to save all the general purpose registers, the save the stack pointer
  56. * into the TCB.
  57. *
  58. * The first thing we do is save the flags then disable interrupts. This is to
  59. * guard our stack against having a context switch interrupt after we have already
  60. * pushed the registers onto the stack - causing the 32 registers to be on the
  61. * stack twice.
  62. *
  63. * r1 is set to zero as the compiler expects it to be thus, however some
  64. * of the math routines make use of R1.
  65. *
  66. * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
  67. * so we need not worry about reading/writing to the stack pointer.
  68. */
  69. #define portSAVE_CONTEXT() \
  70. asm volatile ( "push r0 \n\t" \
  71. "in r0, __SREG__ \n\t" \
  72. "cli \n\t" \
  73. "push r0 \n\t" \
  74. "push r1 \n\t" \
  75. "clr r1 \n\t" \
  76. "push r2 \n\t" \
  77. "push r3 \n\t" \
  78. "push r4 \n\t" \
  79. "push r5 \n\t" \
  80. "push r6 \n\t" \
  81. "push r7 \n\t" \
  82. "push r8 \n\t" \
  83. "push r9 \n\t" \
  84. "push r10 \n\t" \
  85. "push r11 \n\t" \
  86. "push r12 \n\t" \
  87. "push r13 \n\t" \
  88. "push r14 \n\t" \
  89. "push r15 \n\t" \
  90. "push r16 \n\t" \
  91. "push r17 \n\t" \
  92. "push r18 \n\t" \
  93. "push r19 \n\t" \
  94. "push r20 \n\t" \
  95. "push r21 \n\t" \
  96. "push r22 \n\t" \
  97. "push r23 \n\t" \
  98. "push r24 \n\t" \
  99. "push r25 \n\t" \
  100. "push r26 \n\t" \
  101. "push r27 \n\t" \
  102. "push r28 \n\t" \
  103. "push r29 \n\t" \
  104. "push r30 \n\t" \
  105. "push r31 \n\t" \
  106. "lds r26, pxCurrentTCB \n\t" \
  107. "lds r27, pxCurrentTCB + 1 \n\t" \
  108. "in r0, 0x3d \n\t" \
  109. "st x+, r0 \n\t" \
  110. "in r0, 0x3e \n\t" \
  111. "st x+, r0 \n\t" \
  112. );
  113. /*
  114. * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
  115. * the context save so we can write to the stack pointer.
  116. */
  117. #define portRESTORE_CONTEXT() \
  118. asm volatile ( "lds r26, pxCurrentTCB \n\t" \
  119. "lds r27, pxCurrentTCB + 1 \n\t" \
  120. "ld r28, x+ \n\t" \
  121. "out __SP_L__, r28 \n\t" \
  122. "ld r29, x+ \n\t" \
  123. "out __SP_H__, r29 \n\t" \
  124. "pop r31 \n\t" \
  125. "pop r30 \n\t" \
  126. "pop r29 \n\t" \
  127. "pop r28 \n\t" \
  128. "pop r27 \n\t" \
  129. "pop r26 \n\t" \
  130. "pop r25 \n\t" \
  131. "pop r24 \n\t" \
  132. "pop r23 \n\t" \
  133. "pop r22 \n\t" \
  134. "pop r21 \n\t" \
  135. "pop r20 \n\t" \
  136. "pop r19 \n\t" \
  137. "pop r18 \n\t" \
  138. "pop r17 \n\t" \
  139. "pop r16 \n\t" \
  140. "pop r15 \n\t" \
  141. "pop r14 \n\t" \
  142. "pop r13 \n\t" \
  143. "pop r12 \n\t" \
  144. "pop r11 \n\t" \
  145. "pop r10 \n\t" \
  146. "pop r9 \n\t" \
  147. "pop r8 \n\t" \
  148. "pop r7 \n\t" \
  149. "pop r6 \n\t" \
  150. "pop r5 \n\t" \
  151. "pop r4 \n\t" \
  152. "pop r3 \n\t" \
  153. "pop r2 \n\t" \
  154. "pop r1 \n\t" \
  155. "pop r0 \n\t" \
  156. "out __SREG__, r0 \n\t" \
  157. "pop r0 \n\t" \
  158. );
  159. /*-----------------------------------------------------------*/
  160. /*
  161. * Perform hardware setup to enable ticks from timer 1, compare match A.
  162. */
  163. static void prvSetupTimerInterrupt( void );
  164. /*-----------------------------------------------------------*/
  165. /*
  166. * See header file for description.
  167. */
  168. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  169. {
  170. uint16_t usAddress;
  171. /* Place a few bytes of known values on the bottom of the stack.
  172. This is just useful for debugging. */
  173. *pxTopOfStack = 0x11;
  174. pxTopOfStack--;
  175. *pxTopOfStack = 0x22;
  176. pxTopOfStack--;
  177. *pxTopOfStack = 0x33;
  178. pxTopOfStack--;
  179. /* Simulate how the stack would look after a call to vPortYield() generated by
  180. the compiler. */
  181. /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
  182. /* The start of the task code will be popped off the stack last, so place
  183. it on first. */
  184. usAddress = ( uint16_t ) pxCode;
  185. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  186. pxTopOfStack--;
  187. usAddress >>= 8;
  188. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  189. pxTopOfStack--;
  190. /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
  191. portSAVE_CONTEXT places the flags on the stack immediately after r0
  192. to ensure the interrupts get disabled as soon as possible, and so ensuring
  193. the stack use is minimal should a context switch interrupt occur. */
  194. *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
  195. pxTopOfStack--;
  196. *pxTopOfStack = portFLAGS_INT_ENABLED;
  197. pxTopOfStack--;
  198. /* Now the remaining registers. The compiler expects R1 to be 0. */
  199. *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
  200. pxTopOfStack--;
  201. *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
  202. pxTopOfStack--;
  203. *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
  204. pxTopOfStack--;
  205. *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
  206. pxTopOfStack--;
  207. *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
  208. pxTopOfStack--;
  209. *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
  210. pxTopOfStack--;
  211. *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
  212. pxTopOfStack--;
  213. *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
  214. pxTopOfStack--;
  215. *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
  216. pxTopOfStack--;
  217. *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
  218. pxTopOfStack--;
  219. *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
  220. pxTopOfStack--;
  221. *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
  222. pxTopOfStack--;
  223. *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
  224. pxTopOfStack--;
  225. *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
  226. pxTopOfStack--;
  227. *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
  228. pxTopOfStack--;
  229. *pxTopOfStack = ( StackType_t ) 0x16; /* R16 */
  230. pxTopOfStack--;
  231. *pxTopOfStack = ( StackType_t ) 0x17; /* R17 */
  232. pxTopOfStack--;
  233. *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
  234. pxTopOfStack--;
  235. *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
  236. pxTopOfStack--;
  237. *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
  238. pxTopOfStack--;
  239. *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
  240. pxTopOfStack--;
  241. *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
  242. pxTopOfStack--;
  243. *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
  244. pxTopOfStack--;
  245. /* Place the parameter on the stack in the expected location. */
  246. usAddress = ( uint16_t ) pvParameters;
  247. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  248. pxTopOfStack--;
  249. usAddress >>= 8;
  250. *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
  251. pxTopOfStack--;
  252. *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
  253. pxTopOfStack--;
  254. *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
  255. pxTopOfStack--;
  256. *pxTopOfStack = ( StackType_t ) 0x28; /* R28 Y */
  257. pxTopOfStack--;
  258. *pxTopOfStack = ( StackType_t ) 0x29; /* R29 */
  259. pxTopOfStack--;
  260. *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
  261. pxTopOfStack--;
  262. *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
  263. pxTopOfStack--;
  264. /*lint +e950 +e611 +e923 */
  265. return pxTopOfStack;
  266. }
  267. /*-----------------------------------------------------------*/
  268. BaseType_t xPortStartScheduler( void )
  269. {
  270. /* Setup the hardware to generate the tick. */
  271. prvSetupTimerInterrupt();
  272. /* Restore the context of the first task that is going to run. */
  273. portRESTORE_CONTEXT();
  274. /* Simulate a function call end as generated by the compiler. We will now
  275. jump to the start of the task the context of which we have just restored. */
  276. asm volatile ( "ret" );
  277. /* Should not get here. */
  278. return pdTRUE;
  279. }
  280. /*-----------------------------------------------------------*/
  281. void vPortEndScheduler( void )
  282. {
  283. /* It is unlikely that the AVR port will get stopped. If required simply
  284. disable the tick interrupt here. */
  285. }
  286. /*-----------------------------------------------------------*/
  287. /*
  288. * Manual context switch. The first thing we do is save the registers so we
  289. * can use a naked attribute.
  290. */
  291. void vPortYield( void ) __attribute__ ( ( naked ) );
  292. void vPortYield( void )
  293. {
  294. portSAVE_CONTEXT();
  295. vTaskSwitchContext();
  296. portRESTORE_CONTEXT();
  297. asm volatile ( "ret" );
  298. }
  299. /*-----------------------------------------------------------*/
  300. /*
  301. * Context switch function used by the tick. This must be identical to
  302. * vPortYield() from the call to vTaskSwitchContext() onwards. The only
  303. * difference from vPortYield() is the tick count is incremented as the
  304. * call comes from the tick ISR.
  305. */
  306. void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
  307. void vPortYieldFromTick( void )
  308. {
  309. portSAVE_CONTEXT();
  310. if( xTaskIncrementTick() != pdFALSE )
  311. {
  312. vTaskSwitchContext();
  313. }
  314. portRESTORE_CONTEXT();
  315. asm volatile ( "ret" );
  316. }
  317. /*-----------------------------------------------------------*/
  318. /*
  319. * Setup timer 1 compare match A to generate a tick interrupt.
  320. */
  321. static void prvSetupTimerInterrupt( void )
  322. {
  323. uint32_t ulCompareMatch;
  324. uint8_t ucHighByte, ucLowByte;
  325. /* Using 16bit timer 1 to generate the tick. Correct fuses must be
  326. selected for the configCPU_CLOCK_HZ clock. */
  327. ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
  328. /* We only have 16 bits so have to scale to get our required tick rate. */
  329. ulCompareMatch /= portCLOCK_PRESCALER;
  330. /* Adjust for correct value. */
  331. ulCompareMatch -= ( uint32_t ) 1;
  332. /* Setup compare match value for compare match A. Interrupts are disabled
  333. before this is called so we need not worry here. */
  334. ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
  335. ulCompareMatch >>= 8;
  336. ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
  337. OCR1AH = ucHighByte;
  338. OCR1AL = ucLowByte;
  339. /* Setup clock source and compare match behaviour. */
  340. ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
  341. TCCR1B = ucLowByte;
  342. /* Enable the interrupt - this is okay as interrupt are currently globally
  343. disabled. */
  344. ucLowByte = TIMSK;
  345. ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
  346. TIMSK = ucLowByte;
  347. }
  348. /*-----------------------------------------------------------*/
  349. #if configUSE_PREEMPTION == 1
  350. /*
  351. * Tick ISR for preemptive scheduler. We can use a naked attribute as
  352. * the context is saved at the start of vPortYieldFromTick(). The tick
  353. * count is incremented after the context is saved.
  354. */
  355. void TIMER1_COMPA_vect( void ) __attribute__ ( ( signal, naked ) );
  356. void TIMER1_COMPA_vect( void )
  357. {
  358. vPortYieldFromTick();
  359. asm volatile ( "reti" );
  360. }
  361. #else
  362. /*
  363. * Tick ISR for the cooperative scheduler. All this does is increment the
  364. * tick count. We don't need to switch context, this can only be done by
  365. * manual calls to taskYIELD();
  366. */
  367. void TIMER1_COMPA_vect( void ) __attribute__ ( ( signal ) );
  368. void TIMER1_COMPA_vect( void )
  369. {
  370. xTaskIncrementTick();
  371. }
  372. #endif