port.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* Scheduler includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. /* The critical nesting value is initialised to a non zero value to ensure
  32. interrupts don't accidentally become enabled before the scheduler is started. */
  33. #define portINITIAL_CRITICAL_NESTING ( ( uint16_t ) 10 )
  34. /* Initial PSW value allocated to a newly created task.
  35. * 1100011000000000
  36. * ||||||||-------------- Fill byte
  37. * |||||||--------------- Carry Flag cleared
  38. * |||||----------------- In-service priority Flags set to low level
  39. * ||||------------------ Register bank Select 0 Flag cleared
  40. * |||------------------- Auxiliary Carry Flag cleared
  41. * ||-------------------- Register bank Select 1 Flag cleared
  42. * |--------------------- Zero Flag set
  43. * ---------------------- Global Interrupt Flag set (enabled)
  44. */
  45. #define portPSW ( 0xc6UL )
  46. /* The address of the pxCurrentTCB variable, but don't know or need to know its
  47. type. */
  48. typedef void TCB_t;
  49. extern volatile TCB_t * volatile pxCurrentTCB;
  50. /* Each task maintains a count of the critical section nesting depth. Each time
  51. a critical section is entered the count is incremented. Each time a critical
  52. section is exited the count is decremented - with interrupts only being
  53. re-enabled if the count is zero.
  54. usCriticalNesting will get set to zero when the scheduler starts, but must
  55. not be initialised to zero as that could cause problems during the startup
  56. sequence. */
  57. volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  58. /*-----------------------------------------------------------*/
  59. /*
  60. * Sets up the periodic ISR used for the RTOS tick using the interval timer.
  61. * The application writer can define configSETUP_TICK_INTERRUPT() (in
  62. * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
  63. * in place of prvSetupTimerInterrupt().
  64. */
  65. static void prvSetupTimerInterrupt( void );
  66. #ifndef configSETUP_TICK_INTERRUPT
  67. /* The user has not provided their own tick interrupt configuration so use
  68. the definition in this file (which uses the interval timer). */
  69. #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
  70. #endif /* configSETUP_TICK_INTERRUPT */
  71. /*
  72. * Defined in portasm.s87, this function starts the scheduler by loading the
  73. * context of the first task to run.
  74. */
  75. extern void vPortStartFirstTask( void );
  76. /*
  77. * Used to catch tasks that attempt to return from their implementing function.
  78. */
  79. static void prvTaskExitError( void );
  80. /*-----------------------------------------------------------*/
  81. /*
  82. * Initialise the stack of a task to look exactly as if a call to
  83. * portSAVE_CONTEXT had been called.
  84. *
  85. * See the header file portable.h.
  86. */
  87. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  88. {
  89. uint32_t *pulLocal;
  90. /* With large code and large data sizeof( StackType_t ) == 2, and
  91. sizeof( StackType_t * ) == 4. With small code and small data
  92. sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */
  93. #if __DATA_MODEL__ == __DATA_MODEL_FAR__
  94. {
  95. /* Parameters are passed in on the stack, and written using a 32-bit value
  96. hence a space is left for the second two bytes. */
  97. pxTopOfStack--;
  98. /* Write in the parameter value. */
  99. pulLocal = ( uint32_t * ) pxTopOfStack;
  100. *pulLocal = ( uint32_t ) pvParameters;
  101. pxTopOfStack--;
  102. /* The return address, leaving space for the first two bytes of the
  103. 32-bit value. See the comments above the prvTaskExitError() prototype
  104. at the top of this file. */
  105. pxTopOfStack--;
  106. pulLocal = ( uint32_t * ) pxTopOfStack;
  107. *pulLocal = ( uint32_t ) prvTaskExitError;
  108. pxTopOfStack--;
  109. /* The start address / PSW value is also written in as a 32-bit value,
  110. so leave a space for the second two bytes. */
  111. pxTopOfStack--;
  112. /* Task function start address combined with the PSW. */
  113. pulLocal = ( uint32_t * ) pxTopOfStack;
  114. *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
  115. pxTopOfStack--;
  116. /* An initial value for the AX register. */
  117. *pxTopOfStack = ( StackType_t ) 0x1111;
  118. pxTopOfStack--;
  119. }
  120. #else
  121. {
  122. /* The return address, leaving space for the first two bytes of the
  123. 32-bit value. See the comments above the prvTaskExitError() prototype
  124. at the top of this file. */
  125. pxTopOfStack--;
  126. pulLocal = ( uint32_t * ) pxTopOfStack;
  127. *pulLocal = ( uint32_t ) prvTaskExitError;
  128. pxTopOfStack--;
  129. /* Task function. Again as it is written as a 32-bit value a space is
  130. left on the stack for the second two bytes. */
  131. pxTopOfStack--;
  132. /* Task function start address combined with the PSW. */
  133. pulLocal = ( uint32_t * ) pxTopOfStack;
  134. *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
  135. pxTopOfStack--;
  136. /* The parameter is passed in AX. */
  137. *pxTopOfStack = ( StackType_t ) pvParameters;
  138. pxTopOfStack--;
  139. }
  140. #endif
  141. /* An initial value for the HL register. */
  142. *pxTopOfStack = ( StackType_t ) 0x2222;
  143. pxTopOfStack--;
  144. /* CS and ES registers. */
  145. *pxTopOfStack = ( StackType_t ) 0x0F00;
  146. pxTopOfStack--;
  147. /* The remaining general purpose registers DE and BC */
  148. *pxTopOfStack = ( StackType_t ) 0xDEDE;
  149. pxTopOfStack--;
  150. *pxTopOfStack = ( StackType_t ) 0xBCBC;
  151. pxTopOfStack--;
  152. /* Finally the critical section nesting count is set to zero when the task
  153. first starts. */
  154. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  155. /* Return a pointer to the top of the stack that has been generated so it
  156. can be stored in the task control block for the task. */
  157. return pxTopOfStack;
  158. }
  159. /*-----------------------------------------------------------*/
  160. static void prvTaskExitError( void )
  161. {
  162. /* A function that implements a task must not exit or attempt to return to
  163. its caller as there is nothing to return to. If a task wants to exit it
  164. should instead call vTaskDelete( NULL ).
  165. Artificially force an assert() to be triggered if configASSERT() is
  166. defined, then stop here so application writers can catch the error. */
  167. configASSERT( usCriticalNesting == ~0U );
  168. portDISABLE_INTERRUPTS();
  169. for( ;; );
  170. }
  171. /*-----------------------------------------------------------*/
  172. BaseType_t xPortStartScheduler( void )
  173. {
  174. /* Setup the hardware to generate the tick. Interrupts are disabled when
  175. this function is called. */
  176. configSETUP_TICK_INTERRUPT();
  177. /* Restore the context of the first task that is going to run. */
  178. vPortStartFirstTask();
  179. /* Execution should not reach here as the tasks are now running!
  180. prvSetupTimerInterrupt() is called here to prevent the compiler outputting
  181. a warning about a statically declared function not being referenced in the
  182. case that the application writer has provided their own tick interrupt
  183. configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
  184. their own routine will be called in place of prvSetupTimerInterrupt()). */
  185. prvSetupTimerInterrupt();
  186. return pdTRUE;
  187. }
  188. /*-----------------------------------------------------------*/
  189. void vPortEndScheduler( void )
  190. {
  191. /* It is unlikely that the RL78 port will get stopped. */
  192. }
  193. /*-----------------------------------------------------------*/
  194. static void prvSetupTimerInterrupt( void )
  195. {
  196. const uint16_t usClockHz = 15000UL; /* Internal clock. */
  197. const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
  198. /* Use the internal 15K clock. */
  199. OSMC = ( uint8_t ) 0x16;
  200. #ifdef RTCEN
  201. {
  202. /* Supply the interval timer clock. */
  203. RTCEN = ( uint8_t ) 1U;
  204. /* Disable INTIT interrupt. */
  205. ITMK = ( uint8_t ) 1;
  206. /* Disable ITMC operation. */
  207. ITMC = ( uint8_t ) 0x0000;
  208. /* Clear INIT interrupt. */
  209. ITIF = ( uint8_t ) 0;
  210. /* Set interval and enable interrupt operation. */
  211. ITMC = usCompareMatch | 0x8000U;
  212. /* Enable INTIT interrupt. */
  213. ITMK = ( uint8_t ) 0;
  214. }
  215. #endif
  216. #ifdef TMKAEN
  217. {
  218. /* Supply the interval timer clock. */
  219. TMKAEN = ( uint8_t ) 1U;
  220. /* Disable INTIT interrupt. */
  221. TMKAMK = ( uint8_t ) 1;
  222. /* Disable ITMC operation. */
  223. ITMC = ( uint8_t ) 0x0000;
  224. /* Clear INIT interrupt. */
  225. TMKAIF = ( uint8_t ) 0;
  226. /* Set interval and enable interrupt operation. */
  227. ITMC = usCompareMatch | 0x8000U;
  228. /* Enable INTIT interrupt. */
  229. TMKAMK = ( uint8_t ) 0;
  230. }
  231. #endif
  232. }
  233. /*-----------------------------------------------------------*/