port.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. * Implementation of functions defined in portable.h for the SH2A port.
  30. *----------------------------------------------------------*/
  31. /* Standard C includes. */
  32. #include "limits.h"
  33. /* Scheduler includes. */
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. /* Library includes. */
  37. #include "string.h"
  38. /* Hardware specifics. */
  39. #include "machine.h"
  40. /*-----------------------------------------------------------*/
  41. /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
  42. PSW is set with U and I set, and PM and IPL clear. */
  43. #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
  44. /* The peripheral clock is divided by this value before being supplying the
  45. CMT. */
  46. #if ( configUSE_TICKLESS_IDLE == 0 )
  47. /* If tickless idle is not used then the divisor can be fixed. */
  48. #define portCLOCK_DIVISOR 8UL
  49. #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
  50. #define portCLOCK_DIVISOR 512UL
  51. #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
  52. #define portCLOCK_DIVISOR 128UL
  53. #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
  54. #define portCLOCK_DIVISOR 32UL
  55. #else
  56. #define portCLOCK_DIVISOR 8UL
  57. #endif
  58. /* Keys required to lock and unlock access to certain system registers
  59. respectively. */
  60. #define portUNLOCK_KEY 0xA50B
  61. #define portLOCK_KEY 0xA500
  62. /*-----------------------------------------------------------*/
  63. /*
  64. * Function to start the first task executing - written in asm code as direct
  65. * access to registers is required.
  66. */
  67. extern void prvStartFirstTask( void );
  68. /*
  69. * The tick ISR handler. The peripheral used is configured by the application
  70. * via a hook/callback function.
  71. */
  72. __interrupt static void prvTickISR( void );
  73. /*
  74. * Sets up the periodic ISR used for the RTOS tick using the CMT.
  75. * The application writer can define configSETUP_TICK_INTERRUPT() (in
  76. * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
  77. * in place of prvSetupTimerInterrupt().
  78. */
  79. static void prvSetupTimerInterrupt( void );
  80. #ifndef configSETUP_TICK_INTERRUPT
  81. /* The user has not provided their own tick interrupt configuration so use
  82. the definition in this file (which uses the interval timer). */
  83. #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
  84. #endif /* configSETUP_TICK_INTERRUPT */
  85. /*
  86. * Called after the sleep mode registers have been configured, prvSleep()
  87. * executes the pre and post sleep macros, and actually calls the wait
  88. * instruction.
  89. */
  90. #if configUSE_TICKLESS_IDLE == 1
  91. static void prvSleep( TickType_t xExpectedIdleTime );
  92. #endif /* configUSE_TICKLESS_IDLE */
  93. /*-----------------------------------------------------------*/
  94. extern void *pxCurrentTCB;
  95. /*-----------------------------------------------------------*/
  96. /* Calculate how many clock increments make up a single tick period. */
  97. static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
  98. #if configUSE_TICKLESS_IDLE == 1
  99. /* Holds the maximum number of ticks that can be suppressed - which is
  100. basically how far into the future an interrupt can be generated. Set
  101. during initialisation. This is the maximum possible value that the
  102. compare match register can hold divided by ulMatchValueForOneTick. */
  103. static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
  104. /* Flag set from the tick interrupt to allow the sleep processing to know if
  105. sleep mode was exited because of a tick interrupt, or an interrupt
  106. generated by something else. */
  107. static volatile uint32_t ulTickFlag = pdFALSE;
  108. /* The CMT counter is stopped temporarily each time it is re-programmed.
  109. The following constant offsets the CMT counter match value by the number of
  110. CMT counts that would typically be missed while the counter was stopped to
  111. compensate for the lost time. The large difference between the divided CMT
  112. clock and the CPU clock means it is likely ulStoppedTimerCompensation will
  113. equal zero - and be optimised away. */
  114. static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
  115. #endif
  116. /*-----------------------------------------------------------*/
  117. /*
  118. * See header file for description.
  119. */
  120. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  121. {
  122. /* Offset to end up on 8 byte boundary. */
  123. pxTopOfStack--;
  124. /* R0 is not included as it is the stack pointer. */
  125. *pxTopOfStack = 0x00;
  126. pxTopOfStack--;
  127. *pxTopOfStack = 0x00;
  128. pxTopOfStack--;
  129. *pxTopOfStack = portINITIAL_PSW;
  130. pxTopOfStack--;
  131. *pxTopOfStack = ( StackType_t ) pxCode;
  132. /* When debugging it can be useful if every register is set to a known
  133. value. Otherwise code space can be saved by just setting the registers
  134. that need to be set. */
  135. #ifdef USE_FULL_REGISTER_INITIALISATION
  136. {
  137. pxTopOfStack--;
  138. *pxTopOfStack = 0x12345678; /* r15. */
  139. pxTopOfStack--;
  140. *pxTopOfStack = 0xaaaabbbb;
  141. pxTopOfStack--;
  142. *pxTopOfStack = 0xdddddddd;
  143. pxTopOfStack--;
  144. *pxTopOfStack = 0xcccccccc;
  145. pxTopOfStack--;
  146. *pxTopOfStack = 0xbbbbbbbb;
  147. pxTopOfStack--;
  148. *pxTopOfStack = 0xaaaaaaaa;
  149. pxTopOfStack--;
  150. *pxTopOfStack = 0x99999999;
  151. pxTopOfStack--;
  152. *pxTopOfStack = 0x88888888;
  153. pxTopOfStack--;
  154. *pxTopOfStack = 0x77777777;
  155. pxTopOfStack--;
  156. *pxTopOfStack = 0x66666666;
  157. pxTopOfStack--;
  158. *pxTopOfStack = 0x55555555;
  159. pxTopOfStack--;
  160. *pxTopOfStack = 0x44444444;
  161. pxTopOfStack--;
  162. *pxTopOfStack = 0x33333333;
  163. pxTopOfStack--;
  164. *pxTopOfStack = 0x22222222;
  165. pxTopOfStack--;
  166. }
  167. #else
  168. {
  169. /* Leave space for the registers that will get popped from the stack
  170. when the task first starts executing. */
  171. pxTopOfStack -= 15;
  172. }
  173. #endif
  174. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
  175. pxTopOfStack--;
  176. *pxTopOfStack = 0x12345678; /* Accumulator. */
  177. pxTopOfStack--;
  178. *pxTopOfStack = 0x87654321; /* Accumulator. */
  179. return pxTopOfStack;
  180. }
  181. /*-----------------------------------------------------------*/
  182. BaseType_t xPortStartScheduler( void )
  183. {
  184. /* Use pxCurrentTCB just so it does not get optimised away. */
  185. if( pxCurrentTCB != NULL )
  186. {
  187. /* Call an application function to set up the timer that will generate
  188. the tick interrupt. This way the application can decide which
  189. peripheral to use. If tickless mode is used then the default
  190. implementation defined in this file (which uses CMT0) should not be
  191. overridden. */
  192. configSETUP_TICK_INTERRUPT();
  193. /* Enable the software interrupt. */
  194. _IEN( _ICU_SWINT ) = 1;
  195. /* Ensure the software interrupt is clear. */
  196. _IR( _ICU_SWINT ) = 0;
  197. /* Ensure the software interrupt is set to the kernel priority. */
  198. _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
  199. /* Start the first task. */
  200. prvStartFirstTask();
  201. }
  202. /* Execution should not reach here as the tasks are now running!
  203. prvSetupTimerInterrupt() is called here to prevent the compiler outputting
  204. a warning about a statically declared function not being referenced in the
  205. case that the application writer has provided their own tick interrupt
  206. configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
  207. their own routine will be called in place of prvSetupTimerInterrupt()). */
  208. prvSetupTimerInterrupt();
  209. /* Should not get here. */
  210. return pdFAIL;
  211. }
  212. /*-----------------------------------------------------------*/
  213. #pragma vector = configTICK_VECTOR
  214. __interrupt static void prvTickISR( void )
  215. {
  216. /* Re-enable interrupts. */
  217. __enable_interrupt();
  218. /* Increment the tick, and perform any processing the new tick value
  219. necessitates. */
  220. __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
  221. {
  222. if( xTaskIncrementTick() != pdFALSE )
  223. {
  224. taskYIELD();
  225. }
  226. }
  227. __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
  228. #if configUSE_TICKLESS_IDLE == 1
  229. {
  230. /* The CPU woke because of a tick. */
  231. ulTickFlag = pdTRUE;
  232. /* If this is the first tick since exiting tickless mode then the CMT
  233. compare match value needs resetting. */
  234. CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
  235. }
  236. #endif
  237. }
  238. /*-----------------------------------------------------------*/
  239. void vPortEndScheduler( void )
  240. {
  241. /* Not implemented in ports where there is nothing to return to.
  242. Artificially force an assert. */
  243. configASSERT( pxCurrentTCB == NULL );
  244. }
  245. /*-----------------------------------------------------------*/
  246. static void prvSetupTimerInterrupt( void )
  247. {
  248. /* Unlock. */
  249. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  250. /* Enable CMT0. */
  251. MSTP( CMT0 ) = 0;
  252. /* Lock again. */
  253. SYSTEM.PRCR.WORD = portLOCK_KEY;
  254. /* Interrupt on compare match. */
  255. CMT0.CMCR.BIT.CMIE = 1;
  256. /* Set the compare match value. */
  257. CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
  258. /* Divide the PCLK. */
  259. #if portCLOCK_DIVISOR == 512
  260. {
  261. CMT0.CMCR.BIT.CKS = 3;
  262. }
  263. #elif portCLOCK_DIVISOR == 128
  264. {
  265. CMT0.CMCR.BIT.CKS = 2;
  266. }
  267. #elif portCLOCK_DIVISOR == 32
  268. {
  269. CMT0.CMCR.BIT.CKS = 1;
  270. }
  271. #elif portCLOCK_DIVISOR == 8
  272. {
  273. CMT0.CMCR.BIT.CKS = 0;
  274. }
  275. #else
  276. {
  277. #error Invalid portCLOCK_DIVISOR setting
  278. }
  279. #endif
  280. /* Enable the interrupt... */
  281. _IEN( _CMT0_CMI0 ) = 1;
  282. /* ...and set its priority to the application defined kernel priority. */
  283. _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
  284. /* Start the timer. */
  285. CMT.CMSTR0.BIT.STR0 = 1;
  286. }
  287. /*-----------------------------------------------------------*/
  288. #if configUSE_TICKLESS_IDLE == 1
  289. static void prvSleep( TickType_t xExpectedIdleTime )
  290. {
  291. /* Allow the application to define some pre-sleep processing. */
  292. configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
  293. /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
  294. means the application defined code has already executed the WAIT
  295. instruction. */
  296. if( xExpectedIdleTime > 0 )
  297. {
  298. __wait_for_interrupt();
  299. }
  300. /* Allow the application to define some post sleep processing. */
  301. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  302. }
  303. #endif /* configUSE_TICKLESS_IDLE */
  304. /*-----------------------------------------------------------*/
  305. #if configUSE_TICKLESS_IDLE == 1
  306. void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  307. {
  308. uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
  309. eSleepModeStatus eSleepAction;
  310. /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
  311. /* Make sure the CMT reload value does not overflow the counter. */
  312. if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
  313. {
  314. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  315. }
  316. /* Calculate the reload value required to wait xExpectedIdleTime tick
  317. periods. */
  318. ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
  319. if( ulMatchValue > ulStoppedTimerCompensation )
  320. {
  321. /* Compensate for the fact that the CMT is going to be stopped
  322. momentarily. */
  323. ulMatchValue -= ulStoppedTimerCompensation;
  324. }
  325. /* Stop the CMT momentarily. The time the CMT is stopped for is
  326. accounted for as best it can be, but using the tickless mode will
  327. inevitably result in some tiny drift of the time maintained by the
  328. kernel with respect to calendar time. */
  329. CMT.CMSTR0.BIT.STR0 = 0;
  330. while( CMT.CMSTR0.BIT.STR0 == 1 )
  331. {
  332. /* Nothing to do here. */
  333. }
  334. /* Critical section using the global interrupt bit as the i bit is
  335. automatically reset by the WAIT instruction. */
  336. __disable_interrupt();
  337. /* The tick flag is set to false before sleeping. If it is true when
  338. sleep mode is exited then sleep mode was probably exited because the
  339. tick was suppressed for the entire xExpectedIdleTime period. */
  340. ulTickFlag = pdFALSE;
  341. /* If a context switch is pending then abandon the low power entry as
  342. the context switch might have been pended by an external interrupt that
  343. requires processing. */
  344. eSleepAction = eTaskConfirmSleepModeStatus();
  345. if( eSleepAction == eAbortSleep )
  346. {
  347. /* Restart tick. */
  348. CMT.CMSTR0.BIT.STR0 = 1;
  349. __enable_interrupt();
  350. }
  351. else if( eSleepAction == eNoTasksWaitingTimeout )
  352. {
  353. /* Protection off. */
  354. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  355. /* Ready for software standby with all clocks stopped. */
  356. SYSTEM.SBYCR.BIT.SSBY = 1;
  357. /* Protection on. */
  358. SYSTEM.PRCR.WORD = portLOCK_KEY;
  359. /* Sleep until something happens. Calling prvSleep() will
  360. automatically reset the i bit in the PSW. */
  361. prvSleep( xExpectedIdleTime );
  362. /* Restart the CMT. */
  363. CMT.CMSTR0.BIT.STR0 = 1;
  364. }
  365. else
  366. {
  367. /* Protection off. */
  368. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  369. /* Ready for deep sleep mode. */
  370. SYSTEM.MSTPCRC.BIT.DSLPE = 1;
  371. SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
  372. SYSTEM.SBYCR.BIT.SSBY = 0;
  373. /* Protection on. */
  374. SYSTEM.PRCR.WORD = portLOCK_KEY;
  375. /* Adjust the match value to take into account that the current
  376. time slice is already partially complete. */
  377. ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
  378. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  379. /* Restart the CMT to count up to the new match value. */
  380. CMT0.CMCNT = 0;
  381. CMT.CMSTR0.BIT.STR0 = 1;
  382. /* Sleep until something happens. Calling prvSleep() will
  383. automatically reset the i bit in the PSW. */
  384. prvSleep( xExpectedIdleTime );
  385. /* Stop CMT. Again, the time the SysTick is stopped for is
  386. accounted for as best it can be, but using the tickless mode will
  387. inevitably result in some tiny drift of the time maintained by the
  388. kernel with respect to calendar time. */
  389. CMT.CMSTR0.BIT.STR0 = 0;
  390. while( CMT.CMSTR0.BIT.STR0 == 1 )
  391. {
  392. /* Nothing to do here. */
  393. }
  394. ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
  395. if( ulTickFlag != pdFALSE )
  396. {
  397. /* The tick interrupt has already executed, although because
  398. this function is called with the scheduler suspended the actual
  399. tick processing will not occur until after this function has
  400. exited. Reset the match value with whatever remains of this
  401. tick period. */
  402. ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
  403. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  404. /* The tick interrupt handler will already have pended the tick
  405. processing in the kernel. As the pending tick will be
  406. processed as soon as this function exits, the tick value
  407. maintained by the tick is stepped forward by one less than the
  408. time spent sleeping. The actual stepping of the tick appears
  409. later in this function. */
  410. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  411. }
  412. else
  413. {
  414. /* Something other than the tick interrupt ended the sleep.
  415. How many complete tick periods passed while the processor was
  416. sleeping? */
  417. ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
  418. /* The match value is set to whatever fraction of a single tick
  419. period remains. */
  420. ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
  421. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  422. }
  423. /* Restart the CMT so it runs up to the match value. The match value
  424. will get set to the value required to generate exactly one tick period
  425. the next time the CMT interrupt executes. */
  426. CMT0.CMCNT = 0;
  427. CMT.CMSTR0.BIT.STR0 = 1;
  428. /* Wind the tick forward by the number of tick periods that the CPU
  429. remained in a low power state. */
  430. vTaskStepTick( ulCompleteTickPeriods );
  431. }
  432. }
  433. #endif /* configUSE_TICKLESS_IDLE */