port.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the RISC-V RV32 port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. #include "portmacro.h"
  35. /* Standard includes. */
  36. #include "string.h"
  37. #ifdef configCLINT_BASE_ADDRESS
  38. #warning The configCLINT_BASE_ADDRESS constant has been deprecated. configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS are currently being derived from the (possibly 0) configCLINT_BASE_ADDRESS setting. Please update to define configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS dirctly in place of configCLINT_BASE_ADDRESS. See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  39. #endif
  40. #ifndef configMTIME_BASE_ADDRESS
  41. #warning configMTIME_BASE_ADDRESS must be defined in FreeRTOSConfig.h. If the target chip includes a memory-mapped mtime register then set configMTIME_BASE_ADDRESS to the mapped address. Otherwise set configMTIME_BASE_ADDRESS to 0. See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  42. #endif
  43. #ifndef configMTIMECMP_BASE_ADDRESS
  44. #warning configMTIMECMP_BASE_ADDRESS must be defined in FreeRTOSConfig.h. If the target chip includes a memory-mapped mtimecmp register then set configMTIMECMP_BASE_ADDRESS to the mapped address. Otherwise set configMTIMECMP_BASE_ADDRESS to 0. See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  45. #endif
  46. /* Let the user override the pre-loading of the initial LR with the address of
  47. prvTaskExitError() in case it messes up unwinding of the stack in the
  48. debugger. */
  49. #ifdef configTASK_RETURN_ADDRESS
  50. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  51. #else
  52. #define portTASK_RETURN_ADDRESS prvTaskExitError
  53. #endif
  54. /* The stack used by interrupt service routines. Set configISR_STACK_SIZE_WORDS
  55. to use a statically allocated array as the interrupt stack. Alternative leave
  56. configISR_STACK_SIZE_WORDS undefined and update the linker script so that a
  57. linker variable names __freertos_irq_stack_top has the same value as the top
  58. of the stack used by main. Using the linker script method will repurpose the
  59. stack that was used by main before the scheduler was started for use as the
  60. interrupt stack after the scheduler has started. */
  61. #ifdef configISR_STACK_SIZE_WORDS
  62. static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };
  63. const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ] );
  64. /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
  65. the task stacks, and so will legitimately appear in many positions within
  66. the ISR stack. */
  67. #define portISR_STACK_FILL_BYTE 0xee
  68. #else
  69. extern const uint32_t __freertos_irq_stack_top[];
  70. const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;
  71. #endif
  72. /*
  73. * Setup the timer to generate the tick interrupts. The implementation in this
  74. * file is weak to allow application writers to change the timer used to
  75. * generate the tick interrupt.
  76. */
  77. void vPortSetupTimerInterrupt( void ) __attribute__(( weak ));
  78. /*-----------------------------------------------------------*/
  79. /* Used to program the machine timer compare register. */
  80. uint64_t ullNextTime = 0ULL;
  81. const uint64_t *pullNextTime = &ullNextTime;
  82. const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / ( configTICK_RATE_HZ ) ); /* Assumes increment won't go over 32-bits. */
  83. uint32_t const ullMachineTimerCompareRegisterBase = configMTIMECMP_BASE_ADDRESS;
  84. volatile uint64_t * pullMachineTimerCompareRegister = NULL;
  85. /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
  86. stack checking. A problem in the ISR stack will trigger an assert, not call the
  87. stack overflow hook function (because the stack overflow hook is specific to a
  88. task stack, not the ISR stack). */
  89. #if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 )
  90. #warning This path not tested, or even compiled yet.
  91. static const uint8_t ucExpectedStackBytes[] = {
  92. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  93. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  94. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  95. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  96. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
  97. #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
  98. #else
  99. /* Define the function away. */
  100. #define portCHECK_ISR_STACK()
  101. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  102. /*-----------------------------------------------------------*/
  103. #if( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 )
  104. void vPortSetupTimerInterrupt( void )
  105. {
  106. uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
  107. volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( ( configMTIME_BASE_ADDRESS ) + 4UL ); /* 8-byte typer so high 32-bit word is 4 bytes up. */
  108. volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configMTIME_BASE_ADDRESS );
  109. volatile uint32_t ulHartId;
  110. __asm volatile( "csrr %0, mhartid" : "=r"( ulHartId ) );
  111. pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) );
  112. do
  113. {
  114. ulCurrentTimeHigh = *pulTimeHigh;
  115. ulCurrentTimeLow = *pulTimeLow;
  116. } while( ulCurrentTimeHigh != *pulTimeHigh );
  117. ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
  118. ullNextTime <<= 32ULL; /* High 4-byte word is 32-bits up. */
  119. ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
  120. ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
  121. *pullMachineTimerCompareRegister = ullNextTime;
  122. /* Prepare the time to use after the next tick interrupt. */
  123. ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
  124. }
  125. #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIME_BASE_ADDRESS != 0 ) */
  126. /*-----------------------------------------------------------*/
  127. BaseType_t xPortStartScheduler( void )
  128. {
  129. extern void xPortStartFirstTask( void );
  130. #if( configASSERT_DEFINED == 1 )
  131. {
  132. volatile uint32_t mtvec = 0;
  133. /* Check the least significant two bits of mtvec are 00 - indicating
  134. single vector mode. */
  135. __asm volatile( "csrr %0, mtvec" : "=r"( mtvec ) );
  136. configASSERT( ( mtvec & 0x03UL ) == 0 );
  137. /* Check alignment of the interrupt stack - which is the same as the
  138. stack that was being used by main() prior to the scheduler being
  139. started. */
  140. configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );
  141. #ifdef configISR_STACK_SIZE_WORDS
  142. {
  143. memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
  144. }
  145. #endif /* configISR_STACK_SIZE_WORDS */
  146. }
  147. #endif /* configASSERT_DEFINED */
  148. /* If there is a CLINT then it is ok to use the default implementation
  149. in this file, otherwise vPortSetupTimerInterrupt() must be implemented to
  150. configure whichever clock is to be used to generate the tick interrupt. */
  151. vPortSetupTimerInterrupt();
  152. #if( ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) )
  153. {
  154. /* Enable mtime and external interrupts. 1<<7 for timer interrupt, 1<<11
  155. for external interrupt. _RB_ What happens here when mtime is not present as
  156. with pulpino? */
  157. __asm volatile( "csrs mie, %0" :: "r"(0x880) );
  158. }
  159. #else
  160. {
  161. /* Enable external interrupts. */
  162. __asm volatile( "csrs mie, %0" :: "r"(0x800) );
  163. }
  164. #endif /* ( configMTIME_BASE_ADDRESS != 0 ) && ( configMTIMECMP_BASE_ADDRESS != 0 ) */
  165. xPortStartFirstTask();
  166. /* Should not get here as after calling xPortStartFirstTask() only tasks
  167. should be executing. */
  168. return pdFAIL;
  169. }
  170. /*-----------------------------------------------------------*/
  171. void vPortEndScheduler( void )
  172. {
  173. /* Not implemented. */
  174. for( ;; );
  175. }