port.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* Critical nesting should be initialised to a non zero value so interrupts don't
  34. accidentally get enabled before the scheduler is started. */
  35. #define portINITIAL_CRITICAL_NESTING (( StackType_t ) 10)
  36. /* The PSW value assigned to tasks when they start to run for the first time. */
  37. #define portPSW (( StackType_t ) 0x00000000)
  38. /* We require the address of the pxCurrentTCB variable, but don't want to know
  39. any details of its type. */
  40. typedef void TCB_t;
  41. extern volatile TCB_t * volatile pxCurrentTCB;
  42. /* Keeps track of the nesting level of critical sections. */
  43. volatile StackType_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
  44. /*-----------------------------------------------------------*/
  45. /* Sets up the timer to generate the tick interrupt. */
  46. static void prvSetupTimerInterrupt( void );
  47. /*-----------------------------------------------------------*/
  48. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  49. {
  50. *pxTopOfStack = ( StackType_t ) pxCode; /* Task function start address */
  51. pxTopOfStack--;
  52. *pxTopOfStack = ( StackType_t ) pxCode; /* Task function start address */
  53. pxTopOfStack--;
  54. *pxTopOfStack = portPSW; /* Initial PSW value */
  55. pxTopOfStack--;
  56. *pxTopOfStack = ( StackType_t ) 0x20202020; /* Initial Value of R20 */
  57. pxTopOfStack--;
  58. *pxTopOfStack = ( StackType_t ) 0x21212121; /* Initial Value of R21 */
  59. pxTopOfStack--;
  60. *pxTopOfStack = ( StackType_t ) 0x22222222; /* Initial Value of R22 */
  61. pxTopOfStack--;
  62. *pxTopOfStack = ( StackType_t ) 0x23232323; /* Initial Value of R23 */
  63. pxTopOfStack--;
  64. *pxTopOfStack = ( StackType_t ) 0x24242424; /* Initial Value of R24 */
  65. pxTopOfStack--;
  66. #if (__DATA_MODEL__ == 0) || (__DATA_MODEL__ == 1)
  67. *pxTopOfStack = ( StackType_t ) 0x25252525; /* Initial Value of R25 */
  68. pxTopOfStack--;
  69. #endif /* configDATA_MODE */
  70. *pxTopOfStack = ( StackType_t ) 0x26262626; /* Initial Value of R26 */
  71. pxTopOfStack--;
  72. *pxTopOfStack = ( StackType_t ) 0x27272727; /* Initial Value of R27 */
  73. pxTopOfStack--;
  74. *pxTopOfStack = ( StackType_t ) 0x28282828; /* Initial Value of R28 */
  75. pxTopOfStack--;
  76. *pxTopOfStack = ( StackType_t ) 0x29292929; /* Initial Value of R29 */
  77. pxTopOfStack--;
  78. *pxTopOfStack = ( StackType_t ) 0x30303030; /* Initial Value of R30 */
  79. pxTopOfStack--;
  80. *pxTopOfStack = ( StackType_t ) 0x19191919; /* Initial Value of R19 */
  81. pxTopOfStack--;
  82. *pxTopOfStack = ( StackType_t ) 0x18181818; /* Initial Value of R18 */
  83. pxTopOfStack--;
  84. *pxTopOfStack = ( StackType_t ) 0x17171717; /* Initial Value of R17 */
  85. pxTopOfStack--;
  86. *pxTopOfStack = ( StackType_t ) 0x16161616; /* Initial Value of R16 */
  87. pxTopOfStack--;
  88. *pxTopOfStack = ( StackType_t ) 0x15151515; /* Initial Value of R15 */
  89. pxTopOfStack--;
  90. *pxTopOfStack = ( StackType_t ) 0x14141414; /* Initial Value of R14 */
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0x13131313; /* Initial Value of R13 */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) 0x12121212; /* Initial Value of R12 */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x11111111; /* Initial Value of R11 */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x10101010; /* Initial Value of R10 */
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) 0x99999999; /* Initial Value of R09 */
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x88888888; /* Initial Value of R08 */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) 0x77777777; /* Initial Value of R07 */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) 0x66666666; /* Initial Value of R06 */
  107. pxTopOfStack--;
  108. *pxTopOfStack = ( StackType_t ) 0x55555555; /* Initial Value of R05 */
  109. pxTopOfStack--;
  110. #if __DATA_MODEL__ == 0 || __DATA_MODEL__ == 1
  111. *pxTopOfStack = ( StackType_t ) 0x44444444; /* Initial Value of R04 */
  112. pxTopOfStack--;
  113. #endif /* configDATA_MODE */
  114. *pxTopOfStack = ( StackType_t ) 0x22222222; /* Initial Value of R02 */
  115. pxTopOfStack--;
  116. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 is expected to hold the function parameter*/
  117. pxTopOfStack--;
  118. *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  119. /*
  120. * Return a pointer to the top of the stack we have generated so this can
  121. * be stored in the task control block for the task.
  122. */
  123. return pxTopOfStack;
  124. }
  125. /*-----------------------------------------------------------*/
  126. BaseType_t xPortStartScheduler( void )
  127. {
  128. /* Setup the hardware to generate the tick. Interrupts are disabled when
  129. this function is called. */
  130. prvSetupTimerInterrupt();
  131. /* Restore the context of the first task that is going to run. */
  132. vPortStart();
  133. /* Should not get here as the tasks are now running! */
  134. return pdTRUE;
  135. }
  136. /*-----------------------------------------------------------*/
  137. void vPortEndScheduler( void )
  138. {
  139. /* It is unlikely that the V850ES/Fx3 port will get stopped. If required simply
  140. disable the tick interrupt here. */
  141. }
  142. /*-----------------------------------------------------------*/
  143. /*
  144. * Hardware initialisation to generate the RTOS tick. This uses
  145. */
  146. static void prvSetupTimerInterrupt( void )
  147. {
  148. TM0CE = 0; /* TMM0 operation disable */
  149. TM0EQMK0 = 1; /* INTTM0EQ0 interrupt disable */
  150. TM0EQIF0 = 0; /* clear INTTM0EQ0 interrupt flag */
  151. #ifdef __IAR_V850ES_Fx3__
  152. {
  153. TM0CMP0 = (((configCPU_CLOCK_HZ / configTICK_RATE_HZ) / 2)-1); /* divided by 2 because peripherals only run at CPU_CLOCK/2 */
  154. }
  155. #else
  156. {
  157. TM0CMP0 = (configCPU_CLOCK_HZ / configTICK_RATE_HZ);
  158. }
  159. #endif
  160. TM0EQIC0 &= 0xF8;
  161. TM0CTL0 = 0x00;
  162. TM0EQIF0 = 0; /* clear INTTM0EQ0 interrupt flag */
  163. TM0EQMK0 = 0; /* INTTM0EQ0 interrupt enable */
  164. TM0CE = 1; /* TMM0 operation enable */
  165. }
  166. /*-----------------------------------------------------------*/