port.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
  4. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * https://www.FreeRTOS.org
  26. * https://github.com/FreeRTOS
  27. *
  28. */
  29. #include <stdlib.h>
  30. #include <xtensa/config/core.h>
  31. #include "xtensa_rtos.h"
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Defined in portasm.h */
  35. extern void _frxt_tick_timer_init(void);
  36. /* Defined in xtensa_context.S */
  37. extern void _xt_coproc_init(void);
  38. /*-----------------------------------------------------------*/
  39. /* We require the address of the pxCurrentTCB variable, but don't want to know
  40. any details of its type. */
  41. typedef void TCB_t;
  42. extern volatile TCB_t * volatile pxCurrentTCB;
  43. unsigned port_xSchedulerRunning = 0; // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting
  44. unsigned port_interruptNesting = 0; // Interrupt nesting level
  45. /*-----------------------------------------------------------*/
  46. // User exception dispatcher when exiting
  47. void _xt_user_exit(void);
  48. /*
  49. * Stack initialization
  50. */
  51. #if portUSING_MPU_WRAPPERS
  52. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )
  53. #else
  54. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  55. #endif
  56. {
  57. StackType_t * sp, * tp;
  58. XtExcFrame * frame;
  59. #if XCHAL_CP_NUM > 0
  60. uint32_t * p;
  61. #endif
  62. /* Create interrupt stack frame aligned to 16 byte boundary */
  63. sp = ( StackType_t * ) ( ( ( UBaseType_t ) pxTopOfStack - XT_CP_SIZE - XT_STK_FRMSZ ) & ~0xf );
  64. /* Clear the entire frame (do not use memset() because we don't depend on C library) */
  65. for( tp = sp; tp <= pxTopOfStack; ++tp )
  66. {
  67. *tp = 0;
  68. }
  69. frame = ( XtExcFrame * ) sp;
  70. /* Explicitly initialize certain saved registers */
  71. frame->pc = ( UBaseType_t ) pxCode; /* task entrypoint */
  72. frame->a0 = 0; /* to terminate GDB backtrace */
  73. frame->a1 = ( UBaseType_t ) sp + XT_STK_FRMSZ; /* physical top of stack frame */
  74. frame->exit = ( UBaseType_t ) _xt_user_exit; /* user exception exit dispatcher */
  75. /* Set initial PS to int level 0, EXCM disabled ('rfe' will enable), user mode. */
  76. /* Also set entry point argument parameter. */
  77. #ifdef __XTENSA_CALL0_ABI__
  78. frame->a2 = ( UBaseType_t ) pvParameters;
  79. frame->ps = PS_UM | PS_EXCM;
  80. #else
  81. /* + for windowed ABI also set WOE and CALLINC (pretend task was 'call4'd). */
  82. frame->a6 = ( UBaseType_t ) pvParameters;
  83. frame->ps = PS_UM | PS_EXCM | PS_WOE | PS_CALLINC( 1 );
  84. #endif
  85. #ifdef XT_USE_SWPRI
  86. /* Set the initial virtual priority mask value to all 1's. */
  87. frame->vpri = 0xFFFFFFFF;
  88. #endif
  89. #if XCHAL_CP_NUM > 0
  90. /* Init the coprocessor save area (see xtensa_context.h) */
  91. /* No access to TCB here, so derive indirectly. Stack growth is top to bottom.
  92. * //p = (uint32_t *) xMPUSettings->coproc_area;
  93. */
  94. p = ( uint32_t * ) ( ( ( uint32_t ) pxTopOfStack - XT_CP_SIZE ) & ~0xf );
  95. configASSERT( ( uint32_t ) p >= frame->a1 );
  96. p[ 0 ] = 0;
  97. p[ 1 ] = 0;
  98. p[ 2 ] = ( ( ( uint32_t ) p ) + 12 + XCHAL_TOTAL_SA_ALIGN - 1 ) & -XCHAL_TOTAL_SA_ALIGN;
  99. #endif
  100. return sp;
  101. }
  102. /*-----------------------------------------------------------*/
  103. void vPortEndScheduler( void )
  104. {
  105. /* It is unlikely that the Xtensa port will get stopped. If required simply
  106. disable the tick interrupt here. */
  107. }
  108. /*-----------------------------------------------------------*/
  109. BaseType_t xPortStartScheduler( void )
  110. {
  111. // Interrupts are disabled at this point and stack contains PS with enabled interrupts when task context is restored
  112. #if XCHAL_CP_NUM > 0
  113. /* Initialize co-processor management for tasks. Leave CPENABLE alone. */
  114. _xt_coproc_init();
  115. #endif
  116. /* Init the tick divisor value */
  117. _xt_tick_divisor_init();
  118. /* Setup the hardware to generate the tick. */
  119. _frxt_tick_timer_init();
  120. #if XT_USE_THREAD_SAFE_CLIB
  121. // Init C library
  122. vPortClibInit();
  123. #endif
  124. port_xSchedulerRunning = 1;
  125. // Cannot be directly called from C; never returns
  126. __asm__ volatile ("call0 _frxt_dispatch\n");
  127. /* Should not get here. */
  128. return pdTRUE;
  129. }
  130. /*-----------------------------------------------------------*/
  131. BaseType_t xPortSysTickHandler( void )
  132. {
  133. BaseType_t ret;
  134. uint32_t interruptMask;
  135. portbenchmarkIntLatency();
  136. /* Interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY must be
  137. * disabled before calling xTaskIncrementTick as it access the
  138. * kernel lists. */
  139. interruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
  140. {
  141. ret = xTaskIncrementTick();
  142. }
  143. portCLEAR_INTERRUPT_MASK_FROM_ISR( interruptMask );
  144. portYIELD_FROM_ISR( ret );
  145. return ret;
  146. }
  147. /*-----------------------------------------------------------*/
  148. /*
  149. * Used to set coprocessor area in stack. Current hack is to reuse MPU pointer for coprocessor area.
  150. */
  151. #if portUSING_MPU_WRAPPERS
  152. void vPortStoreTaskMPUSettings( xMPU_SETTINGS * xMPUSettings,
  153. const struct xMEMORY_REGION * const xRegions,
  154. StackType_t * pxBottomOfStack,
  155. uint32_t ulStackDepth )
  156. {
  157. #if XCHAL_CP_NUM > 0
  158. xMPUSettings->coproc_area = ( StackType_t * ) ( ( uint32_t ) ( pxBottomOfStack + ulStackDepth - 1 ));
  159. xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) xMPUSettings->coproc_area ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  160. xMPUSettings->coproc_area = ( StackType_t * ) ( ( ( uint32_t ) xMPUSettings->coproc_area - XT_CP_SIZE ) & ~0xf );
  161. /* NOTE: we cannot initialize the coprocessor save area here because FreeRTOS is going to
  162. * clear the stack area after we return. This is done in pxPortInitialiseStack().
  163. */
  164. #endif
  165. }
  166. #endif /* if portUSING_MPU_WRAPPERS */