port.c 17 KB

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