port.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.5.2
  30. + usCriticalNesting now has a volatile qualifier.
  31. */
  32. /* Standard includes. */
  33. #include <stdlib.h>
  34. #include <signal.h>
  35. /* Scheduler includes. */
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. /*-----------------------------------------------------------
  39. * Implementation of functions defined in portable.h for the MSP430 port.
  40. *----------------------------------------------------------*/
  41. /* Constants required for hardware setup. The tick ISR runs off the ACLK,
  42. not the MCLK. */
  43. #define portACLK_FREQUENCY_HZ ( ( TickType_t ) 32768 )
  44. #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
  45. #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x08 )
  46. /* We require the address of the pxCurrentTCB variable, but don't want to know
  47. any details of its type. */
  48. typedef void TCB_t;
  49. extern volatile TCB_t * volatile pxCurrentTCB;
  50. /* Most ports implement critical sections by placing the interrupt flags on
  51. the stack before disabling interrupts. Exiting the critical section is then
  52. simply a case of popping the flags from the stack. As mspgcc does not use
  53. a frame pointer this cannot be done as modifying the stack will clobber all
  54. the stack variables. Instead each task maintains a count of the critical
  55. section nesting depth. Each time a critical section is entered the count is
  56. incremented. Each time a critical section is left the count is decremented -
  57. with interrupts only being re-enabled if the count is zero.
  58. usCriticalNesting will get set to zero when the scheduler starts, but must
  59. not be initialised to zero as this will cause problems during the startup
  60. sequence. */
  61. volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  62. /*-----------------------------------------------------------*/
  63. /*
  64. * Macro to save a task context to the task stack. This simply pushes all the
  65. * general purpose msp430 registers onto the stack, followed by the
  66. * usCriticalNesting value used by the task. Finally the resultant stack
  67. * pointer value is saved into the task control block so it can be retrieved
  68. * the next time the task executes.
  69. */
  70. #define portSAVE_CONTEXT() \
  71. asm volatile ( "push r4 \n\t" \
  72. "push r5 \n\t" \
  73. "push r6 \n\t" \
  74. "push r7 \n\t" \
  75. "push r8 \n\t" \
  76. "push r9 \n\t" \
  77. "push r10 \n\t" \
  78. "push r11 \n\t" \
  79. "push r12 \n\t" \
  80. "push r13 \n\t" \
  81. "push r14 \n\t" \
  82. "push r15 \n\t" \
  83. "mov.w usCriticalNesting, r14 \n\t" \
  84. "push r14 \n\t" \
  85. "mov.w pxCurrentTCB, r12 \n\t" \
  86. "mov.w r1, @r12 \n\t" \
  87. );
  88. /*
  89. * Macro to restore a task context from the task stack. This is effectively
  90. * the reverse of portSAVE_CONTEXT(). First the stack pointer value is
  91. * loaded from the task control block. Next the value for usCriticalNesting
  92. * used by the task is retrieved from the stack - followed by the value of all
  93. * the general purpose msp430 registers.
  94. *
  95. * The bic instruction ensures there are no low power bits set in the status
  96. * register that is about to be popped from the stack.
  97. */
  98. #define portRESTORE_CONTEXT() \
  99. asm volatile ( "mov.w pxCurrentTCB, r12 \n\t" \
  100. "mov.w @r12, r1 \n\t" \
  101. "pop r15 \n\t" \
  102. "mov.w r15, usCriticalNesting \n\t" \
  103. "pop r15 \n\t" \
  104. "pop r14 \n\t" \
  105. "pop r13 \n\t" \
  106. "pop r12 \n\t" \
  107. "pop r11 \n\t" \
  108. "pop r10 \n\t" \
  109. "pop r9 \n\t" \
  110. "pop r8 \n\t" \
  111. "pop r7 \n\t" \
  112. "pop r6 \n\t" \
  113. "pop r5 \n\t" \
  114. "pop r4 \n\t" \
  115. "bic #(0xf0),0(r1) \n\t" \
  116. "reti \n\t" \
  117. );
  118. /*-----------------------------------------------------------*/
  119. /*
  120. * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
  121. * could have alternatively used the watchdog timer or timer 1.
  122. */
  123. static void prvSetupTimerInterrupt( void );
  124. /*-----------------------------------------------------------*/
  125. /*
  126. * Initialise the stack of a task to look exactly as if a call to
  127. * portSAVE_CONTEXT had been called.
  128. *
  129. * See the header file portable.h.
  130. */
  131. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  132. {
  133. /*
  134. Place a few bytes of known values on the bottom of the stack.
  135. This is just useful for debugging and can be included if required.
  136. *pxTopOfStack = ( StackType_t ) 0x1111;
  137. pxTopOfStack--;
  138. *pxTopOfStack = ( StackType_t ) 0x2222;
  139. pxTopOfStack--;
  140. *pxTopOfStack = ( StackType_t ) 0x3333;
  141. pxTopOfStack--;
  142. */
  143. /* The msp430 automatically pushes the PC then SR onto the stack before
  144. executing an ISR. We want the stack to look just as if this has happened
  145. so place a pointer to the start of the task on the stack first - followed
  146. by the flags we want the task to use when it starts up. */
  147. *pxTopOfStack = ( StackType_t ) pxCode;
  148. pxTopOfStack--;
  149. *pxTopOfStack = portFLAGS_INT_ENABLED;
  150. pxTopOfStack--;
  151. /* Next the general purpose registers. */
  152. *pxTopOfStack = ( StackType_t ) 0x4444;
  153. pxTopOfStack--;
  154. *pxTopOfStack = ( StackType_t ) 0x5555;
  155. pxTopOfStack--;
  156. *pxTopOfStack = ( StackType_t ) 0x6666;
  157. pxTopOfStack--;
  158. *pxTopOfStack = ( StackType_t ) 0x7777;
  159. pxTopOfStack--;
  160. *pxTopOfStack = ( StackType_t ) 0x8888;
  161. pxTopOfStack--;
  162. *pxTopOfStack = ( StackType_t ) 0x9999;
  163. pxTopOfStack--;
  164. *pxTopOfStack = ( StackType_t ) 0xaaaa;
  165. pxTopOfStack--;
  166. *pxTopOfStack = ( StackType_t ) 0xbbbb;
  167. pxTopOfStack--;
  168. *pxTopOfStack = ( StackType_t ) 0xcccc;
  169. pxTopOfStack--;
  170. *pxTopOfStack = ( StackType_t ) 0xdddd;
  171. pxTopOfStack--;
  172. *pxTopOfStack = ( StackType_t ) 0xeeee;
  173. pxTopOfStack--;
  174. /* When the task starts is will expect to find the function parameter in
  175. R15. */
  176. *pxTopOfStack = ( StackType_t ) pvParameters;
  177. pxTopOfStack--;
  178. /* The code generated by the mspgcc compiler does not maintain separate
  179. stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
  180. use the stack as per other ports. Instead a variable is used to keep
  181. track of the critical section nesting. This variable has to be stored
  182. as part of the task context and is initially set to zero. */
  183. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  184. /* Return a pointer to the top of the stack we have generated so this can
  185. be stored in the task control block for the task. */
  186. return pxTopOfStack;
  187. }
  188. /*-----------------------------------------------------------*/
  189. BaseType_t xPortStartScheduler( void )
  190. {
  191. /* Setup the hardware to generate the tick. Interrupts are disabled when
  192. this function is called. */
  193. prvSetupTimerInterrupt();
  194. /* Restore the context of the first task that is going to run. */
  195. portRESTORE_CONTEXT();
  196. /* Should not get here as the tasks are now running! */
  197. return pdTRUE;
  198. }
  199. /*-----------------------------------------------------------*/
  200. void vPortEndScheduler( void )
  201. {
  202. /* It is unlikely that the MSP430 port will get stopped. If required simply
  203. disable the tick interrupt here. */
  204. }
  205. /*-----------------------------------------------------------*/
  206. /*
  207. * Manual context switch called by portYIELD or taskYIELD.
  208. *
  209. * The first thing we do is save the registers so we can use a naked attribute.
  210. */
  211. void vPortYield( void ) __attribute__ ( ( naked ) );
  212. void vPortYield( void )
  213. {
  214. /* We want the stack of the task being saved to look exactly as if the task
  215. was saved during a pre-emptive RTOS tick ISR. Before calling an ISR the
  216. msp430 places the status register onto the stack. As this is a function
  217. call and not an ISR we have to do this manually. */
  218. asm volatile ( "push r2" );
  219. _DINT();
  220. /* Save the context of the current task. */
  221. portSAVE_CONTEXT();
  222. /* Switch to the highest priority task that is ready to run. */
  223. vTaskSwitchContext();
  224. /* Restore the context of the new task. */
  225. portRESTORE_CONTEXT();
  226. }
  227. /*-----------------------------------------------------------*/
  228. /*
  229. * Hardware initialisation to generate the RTOS tick. This uses timer 0
  230. * but could alternatively use the watchdog timer or timer 1.
  231. */
  232. static void prvSetupTimerInterrupt( void )
  233. {
  234. /* Ensure the timer is stopped. */
  235. TACTL = 0;
  236. /* Run the timer of the ACLK. */
  237. TACTL = TASSEL_1;
  238. /* Clear everything to start with. */
  239. TACTL |= TACLR;
  240. /* Set the compare match value according to the tick rate we want. */
  241. TACCR0 = portACLK_FREQUENCY_HZ / configTICK_RATE_HZ;
  242. /* Enable the interrupts. */
  243. TACCTL0 = CCIE;
  244. /* Start up clean. */
  245. TACTL |= TACLR;
  246. /* Up mode. */
  247. TACTL |= MC_1;
  248. }
  249. /*-----------------------------------------------------------*/
  250. /*
  251. * The interrupt service routine used depends on whether the pre-emptive
  252. * scheduler is being used or not.
  253. */
  254. #if configUSE_PREEMPTION == 1
  255. /*
  256. * Tick ISR for preemptive scheduler. We can use a naked attribute as
  257. * the context is saved at the start of vPortYieldFromTick(). The tick
  258. * count is incremented after the context is saved.
  259. */
  260. interrupt (TIMERA0_VECTOR) prvTickISR( void ) __attribute__ ( ( naked ) );
  261. interrupt (TIMERA0_VECTOR) prvTickISR( void )
  262. {
  263. /* Save the context of the interrupted task. */
  264. portSAVE_CONTEXT();
  265. /* Increment the tick count then switch to the highest priority task
  266. that is ready to run. */
  267. if( xTaskIncrementTick() != pdFALSE )
  268. {
  269. vTaskSwitchContext();
  270. }
  271. /* Restore the context of the new task. */
  272. portRESTORE_CONTEXT();
  273. }
  274. #else
  275. /*
  276. * Tick ISR for the cooperative scheduler. All this does is increment the
  277. * tick count. We don't need to switch context, this can only be done by
  278. * manual calls to taskYIELD();
  279. */
  280. interrupt (TIMERA0_VECTOR) prvTickISR( void );
  281. interrupt (TIMERA0_VECTOR) prvTickISR( void )
  282. {
  283. xTaskIncrementTick();
  284. }
  285. #endif