port.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /* Standard includes. */
  29. #include <stdlib.h>
  30. /* Scheduler includes. */
  31. #include "FreeRTOS.h"
  32. #include "task.h"
  33. /* The critical nesting value is initialised to a non zero value to ensure
  34. interrupts don't accidentally become enabled before the scheduler is started. */
  35. #define portINITIAL_CRITICAL_NESTING (( uint16_t ) 10)
  36. /* Initial PSW value allocated to a newly created task.
  37. * 1100011000000000
  38. * ||||||||-------------- Fill byte
  39. * |||||||--------------- Carry Flag cleared
  40. * |||||----------------- In-service priority Flags set to low level
  41. * ||||------------------ Register bank Select 0 Flag cleared
  42. * |||------------------- Auxiliary Carry Flag cleared
  43. * ||-------------------- Register bank Select 1 Flag cleared
  44. * |--------------------- Zero Flag set
  45. * ---------------------- Global Interrupt Flag set (enabled)
  46. */
  47. #define portPSW (0xc6UL)
  48. /* We require the address of the pxCurrentTCB variable, but don't want to know
  49. any details of its type. */
  50. typedef void TCB_t;
  51. extern volatile TCB_t * volatile pxCurrentTCB;
  52. /* Most ports implement critical sections by placing the interrupt flags on
  53. the stack before disabling interrupts. Exiting the critical section is then
  54. simply a case of popping the flags from the stack. As 78K0 IAR does not use
  55. a frame pointer this cannot be done as modifying the stack will clobber all
  56. the stack variables. Instead each task maintains a count of the critical
  57. section nesting depth. Each time a critical section is entered the count is
  58. incremented. Each time a critical section is left the count is decremented -
  59. with interrupts only being re-enabled if the count is zero.
  60. usCriticalNesting will get set to zero when the scheduler starts, but must
  61. not be initialised to zero as this will cause problems during the startup
  62. sequence. */
  63. volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  64. /*-----------------------------------------------------------*/
  65. /*
  66. * Sets up the periodic ISR used for the RTOS tick.
  67. */
  68. static void prvSetupTimerInterrupt( void );
  69. /*-----------------------------------------------------------*/
  70. /*
  71. * Initialise the stack of a task to look exactly as if a call to
  72. * portSAVE_CONTEXT had been called.
  73. *
  74. * See the header file portable.h.
  75. */
  76. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  77. {
  78. uint32_t *pulLocal;
  79. #if configMEMORY_MODE == 1
  80. {
  81. /* Parameters are passed in on the stack, and written using a 32bit value
  82. hence a space is left for the second two bytes. */
  83. pxTopOfStack--;
  84. /* Write in the parameter value. */
  85. pulLocal = ( uint32_t * ) pxTopOfStack;
  86. *pulLocal = ( uint32_t ) pvParameters;
  87. pxTopOfStack--;
  88. /* These values are just spacers. The return address of the function
  89. would normally be written here. */
  90. *pxTopOfStack = ( StackType_t ) 0xcdcd;
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0xcdcd;
  93. pxTopOfStack--;
  94. /* The start address / PSW value is also written in as a 32bit value,
  95. so leave a space for the second two bytes. */
  96. pxTopOfStack--;
  97. /* Task function start address combined with the PSW. */
  98. pulLocal = ( uint32_t * ) pxTopOfStack;
  99. *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
  100. pxTopOfStack--;
  101. /* An initial value for the AX register. */
  102. *pxTopOfStack = ( StackType_t ) 0x1111;
  103. pxTopOfStack--;
  104. }
  105. #else
  106. {
  107. /* Task function address is written to the stack first. As it is
  108. written as a 32bit value a space is left on the stack for the second
  109. two bytes. */
  110. pxTopOfStack--;
  111. /* Task function start address combined with the PSW. */
  112. pulLocal = ( uint32_t * ) pxTopOfStack;
  113. *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
  114. pxTopOfStack--;
  115. /* The parameter is passed in AX. */
  116. *pxTopOfStack = ( StackType_t ) pvParameters;
  117. pxTopOfStack--;
  118. }
  119. #endif
  120. /* An initial value for the HL register. */
  121. *pxTopOfStack = ( StackType_t ) 0x2222;
  122. pxTopOfStack--;
  123. /* CS and ES registers. */
  124. *pxTopOfStack = ( StackType_t ) 0x0F00;
  125. pxTopOfStack--;
  126. /* Finally the remaining general purpose registers DE and BC */
  127. *pxTopOfStack = ( StackType_t ) 0xDEDE;
  128. pxTopOfStack--;
  129. *pxTopOfStack = ( StackType_t ) 0xBCBC;
  130. pxTopOfStack--;
  131. /* Finally the critical section nesting count is set to zero when the task
  132. first starts. */
  133. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  134. /* Return a pointer to the top of the stack we have generated so this can
  135. be stored in the task control block for the task. */
  136. return pxTopOfStack;
  137. }
  138. /*-----------------------------------------------------------*/
  139. BaseType_t xPortStartScheduler( void )
  140. {
  141. /* Setup the hardware to generate the tick. Interrupts are disabled when
  142. this function is called. */
  143. prvSetupTimerInterrupt();
  144. /* Restore the context of the first task that is going to run. */
  145. vPortStart();
  146. /* Should not get here as the tasks are now running! */
  147. return pdTRUE;
  148. }
  149. /*-----------------------------------------------------------*/
  150. void vPortEndScheduler( void )
  151. {
  152. /* It is unlikely that the 78K0R port will get stopped. If required simply
  153. disable the tick interrupt here. */
  154. }
  155. /*-----------------------------------------------------------*/
  156. static void prvSetupTimerInterrupt( void )
  157. {
  158. /* Setup channel 5 of the TAU to generate the tick interrupt. */
  159. /* First the Timer Array Unit has to be enabled. */
  160. TAU0EN = 1;
  161. /* To configure the Timer Array Unit all Channels have to first be stopped. */
  162. TT0 = 0xff;
  163. /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt
  164. priority. */
  165. TMMK05 = 1;
  166. /* Clear Timer Array Unit Channel 5 interrupt flag. */
  167. TMIF05 = 0;
  168. /* Set Timer Array Unit Channel 5 interrupt priority */
  169. TMPR005 = 0;
  170. TMPR105 = 0;
  171. /* Set Timer Array Unit Channel 5 Mode as interval timer. */
  172. TMR05 = 0x0000;
  173. /* Set the compare match value according to the tick rate we want. */
  174. TDR05 = ( TickType_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
  175. /* Set Timer Array Unit Channel 5 output mode */
  176. TOM0 &= ~0x0020;
  177. /* Set Timer Array Unit Channel 5 output level */
  178. TOL0 &= ~0x0020;
  179. /* Set Timer Array Unit Channel 5 output enable */
  180. TOE0 &= ~0x0020;
  181. /* Interrupt of Timer Array Unit Channel 5 enabled */
  182. TMMK05 = 0;
  183. /* Start Timer Array Unit Channel 5.*/
  184. TS0 |= 0x0020;
  185. }
  186. /*-----------------------------------------------------------*/