2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /*-----------------------------------------------------------
\r
29 * Implementation of functions defined in portable.h for the RISC-V RV32 port.
\r
30 *----------------------------------------------------------*/
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
35 #include "portmacro.h"
\r
37 /* Standard includes. */
\r
40 #ifndef configCLINT_BASE_ADDRESS
\r
41 #warning configCLINT_BASE_ADDRESS must be defined in FreeRTOSConfig.h. If the target chip includes a Core Local Interrupter (CLINT) then set configCLINT_BASE_ADDRESS to the CLINT base address. Otherwise set configCLINT_BASE_ADDRESS to 0.
\r
44 /* Let the user override the pre-loading of the initial LR with the address of
\r
45 prvTaskExitError() in case it messes up unwinding of the stack in the
\r
47 #ifdef configTASK_RETURN_ADDRESS
\r
48 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
50 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
53 /* The stack used by interrupt service routines. Set configISR_STACK_SIZE_WORDS
\r
54 to use a statically allocated array as the interrupt stack. Alternative leave
\r
55 configISR_STACK_SIZE_WORDS undefined and update the linker script so that a
\r
56 linker variable names __freertos_irq_stack_top has the same value as the top
\r
57 of the stack used by main. Using the linker script method will repurpose the
\r
58 stack that was used by main before the scheduler was started for use as the
\r
59 interrupt stack after the scheduler has started. */
\r
60 #ifdef configISR_STACK_SIZE_WORDS
\r
61 static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };
\r
62 const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ] );
\r
64 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
\r
65 the task stacks, and so will legitimately appear in many positions within
\r
67 #define portISR_STACK_FILL_BYTE 0xee
\r
69 extern const uint32_t __freertos_irq_stack_top[];
\r
70 const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;
\r
74 * Setup the timer to generate the tick interrupts. The implementation in this
\r
75 * file is weak to allow application writers to change the timer used to
\r
76 * generate the tick interrupt.
\r
78 void vPortSetupTimerInterrupt( void ) __attribute__(( weak ));
\r
80 /*-----------------------------------------------------------*/
\r
82 /* Used to program the machine timer compare register. */
\r
83 uint64_t ullNextTime = 0ULL;
\r
84 const uint64_t *pullNextTime = &ullNextTime;
\r
85 const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / ( configTICK_RATE_HZ ) ); /* Assumes increment won't go over 32-bits. */
\r
86 uint32_t const ullMachineTimerCompareRegisterBase = ( uint64_t const ) ( ( configCLINT_BASE_ADDRESS ) + 0x4000 );
\r
87 volatile uint64_t * pullMachineTimerCompareRegister = NULL;
\r
89 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
\r
90 stack checking. A problem in the ISR stack will trigger an assert, not call the
\r
91 stack overflow hook function (because the stack overflow hook is specific to a
\r
92 task stack, not the ISR stack). */
\r
93 #if defined( configISR_STACK_SIZE_WORDS ) && ( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
94 #warning This path not tested, or even compiled yet.
\r
96 static const uint8_t ucExpectedStackBytes[] = {
\r
97 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
98 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
99 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
100 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
101 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
\r
103 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
\r
105 /* Define the function away. */
\r
106 #define portCHECK_ISR_STACK()
\r
107 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
109 /*-----------------------------------------------------------*/
\r
111 #if( configCLINT_BASE_ADDRESS != 0 )
\r
113 void vPortSetupTimerInterrupt( void )
\r
115 uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;
\r
116 volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( configCLINT_BASE_ADDRESS + 0xBFFC );
\r
117 volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configCLINT_BASE_ADDRESS + 0xBFF8 );
\r
118 volatile uint32_t ulHartId;
\r
120 __asm volatile( "csrr %0, mhartid" : "=r"( ulHartId ) );
\r
121 pullMachineTimerCompareRegister = ( volatile uint64_t * ) ( ullMachineTimerCompareRegisterBase + ( ulHartId * sizeof( uint64_t ) ) );
\r
125 ulCurrentTimeHigh = *pulTimeHigh;
\r
126 ulCurrentTimeLow = *pulTimeLow;
\r
127 } while( ulCurrentTimeHigh != *pulTimeHigh );
\r
129 ullNextTime = ( uint64_t ) ulCurrentTimeHigh;
\r
130 ullNextTime <<= 32ULL;
\r
131 ullNextTime |= ( uint64_t ) ulCurrentTimeLow;
\r
132 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
\r
133 *pullMachineTimerCompareRegister = ullNextTime;
\r
135 /* Prepare the time to use after the next tick interrupt. */
\r
136 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;
\r
139 #endif /* ( configCLINT_BASE_ADDRESS != 0 ) */
\r
140 /*-----------------------------------------------------------*/
\r
142 BaseType_t xPortStartScheduler( void )
\r
144 extern void xPortStartFirstTask( void );
\r
146 #if( configASSERT_DEFINED == 1 )
\r
148 volatile uint32_t mtvec = 0;
\r
150 /* Check the least significant two bits of mtvec are 00 - indicating
\r
151 single vector mode. */
\r
152 __asm volatile( "csrr %0, mtvec" : "=r"( mtvec ) );
\r
153 configASSERT( ( mtvec & 0x03UL ) == 0 );
\r
155 /* Check alignment of the interrupt stack - which is the same as the
\r
156 stack that was being used by main() prior to the scheduler being
\r
158 configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );
\r
160 #ifdef configISR_STACK_SIZE_WORDS
\r
162 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
\r
164 #endif /* configISR_STACK_SIZE_WORDS */
\r
166 #endif /* configASSERT_DEFINED */
\r
168 /* If there is a CLINT then it is ok to use the default implementation
\r
169 in this file, otherwise vPortSetupTimerInterrupt() must be implemented to
\r
170 configure whichever clock is to be used to generate the tick interrupt. */
\r
171 vPortSetupTimerInterrupt();
\r
173 #if( configCLINT_BASE_ADDRESS != 0 )
\r
175 /* Enable mtime and external interrupts. 1<<7 for timer interrupt, 1<<11
\r
176 for external interrupt. _RB_ What happens here when mtime is not present as
\r
178 __asm volatile( "csrs mie, %0" :: "r"(0x880) );
\r
182 /* Enable external interrupts. */
\r
183 __asm volatile( "csrs mie, %0" :: "r"(0x800) );
\r
185 #endif /* configCLINT_BASE_ADDRESS */
\r
187 xPortStartFirstTask();
\r
189 /* Should not get here as after calling xPortStartFirstTask() only tasks
\r
190 should be executing. */
\r
193 /*-----------------------------------------------------------*/
\r
195 void vPortEndScheduler( void )
\r
197 /* Not implemented. */
\r