port.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * 11000110
  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. /* Each task maintains a count of the critical section nesting depth. Each time
  47. a critical section is entered the count is incremented. Each time a critical
  48. section is exited the count is decremented - with interrupts only being
  49. re-enabled if the count is zero.
  50. usCriticalNesting will get set to zero when the scheduler starts, but must
  51. not be initialised to zero as that could cause problems during the startup
  52. sequence. */
  53. volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  54. /*-----------------------------------------------------------*/
  55. /*
  56. * Sets up the periodic ISR used for the RTOS tick.
  57. */
  58. __attribute__((weak)) void vApplicationSetupTimerInterrupt( void );
  59. /*
  60. * Starts the scheduler by loading the context of the first task to run.
  61. * (defined in portasm.S).
  62. */
  63. extern void vPortStartFirstTask( void );
  64. /*-----------------------------------------------------------*/
  65. /*
  66. * Initialise the stack of a task to look exactly as if a call to
  67. * portSAVE_CONTEXT had been called.
  68. *
  69. * See the header file portable.h.
  70. */
  71. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  72. {
  73. uint32_t *pulLocal;
  74. /* Stack type and pointers to the stack type are both 2 bytes. */
  75. /* Parameters are passed in on the stack, and written using a 32bit value
  76. hence a space is left for the second two bytes. */
  77. pxTopOfStack--;
  78. /* Write in the parameter value. */
  79. pulLocal = ( uint32_t * ) pxTopOfStack;
  80. *pulLocal = ( StackType_t ) pvParameters;
  81. pxTopOfStack--;
  82. /* The return address, leaving space for the first two bytes of the
  83. 32-bit value. */
  84. pxTopOfStack--;
  85. pulLocal = ( uint32_t * ) pxTopOfStack;
  86. *pulLocal = ( uint32_t ) 0;
  87. pxTopOfStack--;
  88. /* The start address / PSW value is also written in as a 32bit value,
  89. so leave a space for the second two bytes. */
  90. pxTopOfStack--;
  91. /* Task function start address combined with the PSW. */
  92. pulLocal = ( uint32_t * ) pxTopOfStack;
  93. *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );
  94. pxTopOfStack--;
  95. /* An initial value for the AX register. */
  96. *pxTopOfStack = ( StackType_t ) 0x1111;
  97. pxTopOfStack--;
  98. /* An initial value for the HL register. */
  99. *pxTopOfStack = ( StackType_t ) 0x2222;
  100. pxTopOfStack--;
  101. /* CS and ES registers. */
  102. *pxTopOfStack = ( StackType_t ) 0x0F00;
  103. pxTopOfStack--;
  104. /* The remaining general purpose registers bank 0 (DE and BC) and the other
  105. two register banks...register bank 3 is dedicated for use by interrupts so
  106. is not saved as part of the task context. */
  107. pxTopOfStack -= 10;
  108. /* Finally the critical section nesting count is set to zero when the task
  109. first starts. */
  110. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  111. /* Return a pointer to the top of the stack that has beene generated so it
  112. can be stored in the task control block for the task. */
  113. return pxTopOfStack;
  114. }
  115. /*-----------------------------------------------------------*/
  116. portBASE_TYPE xPortStartScheduler( void )
  117. {
  118. /* Setup the hardware to generate the tick. Interrupts are disabled when
  119. this function is called. */
  120. vApplicationSetupTimerInterrupt();
  121. /* Restore the context of the first task that is going to run. */
  122. vPortStartFirstTask();
  123. /* Execution should not reach here as the tasks are now running! */
  124. return pdTRUE;
  125. }
  126. /*-----------------------------------------------------------*/
  127. void vPortEndScheduler( void )
  128. {
  129. /* It is unlikely that the RL78 port will get stopped. */
  130. }
  131. /*-----------------------------------------------------------*/
  132. __attribute__((weak)) void vApplicationSetupTimerInterrupt( void )
  133. {
  134. const uint16_t usClockHz = 15000UL; /* Internal clock. */
  135. const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;
  136. /* Use the internal 15K clock. */
  137. OSMC = ( unsigned char ) 0x16;
  138. #ifdef RTCEN
  139. {
  140. /* Supply the interval timer clock. */
  141. RTCEN = ( unsigned char ) 1U;
  142. /* Disable INTIT interrupt. */
  143. ITMK = ( unsigned char ) 1;
  144. /* Disable ITMC operation. */
  145. ITMC = ( unsigned char ) 0x0000;
  146. /* Clear INIT interrupt. */
  147. ITIF = ( unsigned char ) 0;
  148. /* Set interval and enable interrupt operation. */
  149. ITMC = usCompareMatch | 0x8000U;
  150. /* Enable INTIT interrupt. */
  151. ITMK = ( unsigned char ) 0;
  152. }
  153. #endif
  154. #ifdef TMKAEN
  155. {
  156. /* Supply the interval timer clock. */
  157. TMKAEN = ( unsigned char ) 1U;
  158. /* Disable INTIT interrupt. */
  159. TMKAMK = ( unsigned char ) 1;
  160. /* Disable ITMC operation. */
  161. ITMC = ( unsigned char ) 0x0000;
  162. /* Clear INIT interrupt. */
  163. TMKAIF = ( unsigned char ) 0;
  164. /* Set interval and enable interrupt operation. */
  165. ITMC = usCompareMatch | 0x8000U;
  166. /* Enable INTIT interrupt. */
  167. TMKAMK = ( unsigned char ) 0;
  168. }
  169. #endif
  170. }
  171. /*-----------------------------------------------------------*/