]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/RISC-V-RV32/port.c
Move the RISC-V pxPortInitialiseStack() implementation to the assembly port file...
[freertos] / FreeRTOS / Source / portable / GCC / RISC-V-RV32 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.1\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the RISC-V RV32 port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 #include "portmacro.h"\r
36 \r
37 /* Let the user override the pre-loading of the initial LR with the address of\r
38 prvTaskExitError() in case it messes up unwinding of the stack in the\r
39 debugger. */\r
40 #ifdef configTASK_RETURN_ADDRESS\r
41         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
42 #else\r
43         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
44 #endif\r
45 \r
46 /* The stack used by interrupt service routines.  Set configISR_STACK_SIZE_WORDS\r
47 to use a statically allocated array as the interrupt stack.  Alternative leave\r
48 configISR_STACK_SIZE_WORDS undefined and update the linker script so that a\r
49 linker variable names __freertos_irq_stack_top has the same value as the top\r
50 of the stack used by main.  Using the linker script method will repurpose the\r
51 stack that was used by main before the scheduler was started for use as the\r
52 interrupt stack after the scheduler has started. */\r
53 #ifdef configISR_STACK_SIZE_WORDS\r
54         static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };\r
55         const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ ( configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ) - 1 ] );\r
56 #else\r
57         extern const uint32_t __freertos_irq_stack_top[];\r
58         const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;\r
59 #endif\r
60 \r
61 /*\r
62  * Setup the timer to generate the tick interrupts.  The implementation in this\r
63  * file is weak to allow application writers to change the timer used to\r
64  * generate the tick interrupt.\r
65  */\r
66 void vPortSetupTimerInterrupt( void ) __attribute__(( weak ));\r
67 \r
68 /*\r
69  * Used to catch tasks that attempt to return from their implementing function.\r
70  */\r
71 static void prvTaskExitError( void );\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Used to program the machine timer compare register. */\r
76 uint64_t ullNextTime = 0ULL;\r
77 const uint64_t *pullNextTime = &ullNextTime;\r
78 const uint32_t ulTimerIncrementsForOneTick = ( uint32_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ); /* Assumes increment won't go over 32-bits. */\r
79 volatile uint64_t * const pullMachineTimerCompareRegister = ( volatile uint64_t * const ) ( configCLINT_BASE_ADDRESS + 0x4000 );\r
80 \r
81 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task\r
82 stack checking.  A problem in the ISR stack will trigger an assert, not call the\r
83 stack overflow hook function (because the stack overflow hook is specific to a\r
84 task stack, not the ISR stack). */\r
85 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
86         #warning This path not tested, or even compiled yet.\r
87         /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for\r
88         the task stacks, and so will legitimately appear in many positions within\r
89         the ISR stack. */\r
90         #define portISR_STACK_FILL_BYTE 0xee\r
91 \r
92         static const uint8_t ucExpectedStackBytes[] = {\r
93                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
94                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
95                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
96                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
97                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };   \\r
98 \r
99         #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )\r
100 #else\r
101         /* Define the function away. */\r
102         #define portCHECK_ISR_STACK()\r
103 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 static void prvTaskExitError( void )\r
108 {\r
109 volatile uint32_t ulx = 0;\r
110 #warning Not currently used\r
111         /* A function that implements a task must not exit or attempt to return to\r
112         its caller as there is nothing to return to.  If a task wants to exit it\r
113         should instead call vTaskDelete( NULL ).\r
114 \r
115         Artificially force an assert() to be triggered if configASSERT() is\r
116         defined, then stop here so application writers can catch the error. */\r
117         configASSERT( ulx == ~0UL );\r
118         portDISABLE_INTERRUPTS();\r
119         for( ;; );\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 #if( configCLINT_BASE_ADDRESS != 0 )\r
124 \r
125         void vPortSetupTimerInterrupt( void )\r
126         {\r
127         uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;\r
128         volatile uint32_t * const pulTimeHigh = ( volatile uint32_t * const ) ( configCLINT_BASE_ADDRESS + 0xBFFC );\r
129         volatile uint32_t * const pulTimeLow = ( volatile uint32_t * const ) ( configCLINT_BASE_ADDRESS + 0xBFF8 );\r
130 \r
131                 do\r
132                 {\r
133                         ulCurrentTimeHigh = *pulTimeHigh;\r
134                         ulCurrentTimeLow = *pulTimeLow;\r
135                 } while( ulCurrentTimeHigh != *pulTimeHigh );\r
136 \r
137                 ullNextTime = ( uint64_t ) ulCurrentTimeHigh;\r
138                 ullNextTime <<= 32ULL;\r
139                 ullNextTime |= ( uint64_t ) ulCurrentTimeLow;\r
140                 ullNextTime += ( uint64_t ) ulTimerIncrementsForOneTick;\r
141                 *pullMachineTimerCompareRegister = ullNextTime;\r
142 \r
143                 /* Prepare the time to use after the next tick interrupt. */\r
144                 ullNextTime += ( uint64_t ) ulTimerIncrementsForOneTick;\r
145         }\r
146 \r
147 #endif /* ( configCLINT_BASE_ADDRESS != 0 ) */\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 BaseType_t xPortStartScheduler( void )\r
151 {\r
152 extern void xPortStartFirstTask( void );\r
153 \r
154         #if( configASSERT_DEFINED == 1 )\r
155         {\r
156                 volatile uint32_t mtvec = 0;\r
157 \r
158                 /* Check the least significant two bits of mtvec are 00 - indicating\r
159                 single vector mode. */\r
160                 __asm volatile( "csrr %0, mtvec" : "=r"( mtvec ) );\r
161                 configASSERT( ( mtvec & 0x03UL ) == 0 );\r
162 \r
163                 /* Check alignment of the interrupt stack - which is the same as the\r
164                 stack that was being used by main() prior to the scheduler being\r
165                 started. */\r
166                 configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );\r
167         }\r
168         #endif /* configASSERT_DEFINED */\r
169 \r
170         /* If there is a CLINT then it is ok to use the default implementation\r
171         in this file, otherwise vPortSetupTimerInterrupt() must be implemented to\r
172         configure whichever clock is to be used to generate the tick interrupt. */\r
173         vPortSetupTimerInterrupt();\r
174 \r
175         #if( configCLINT_BASE_ADDRESS != 0 )\r
176         {\r
177                 /* Enable mtime and external interrupts.  1<<7 for timer interrupt, 1<<11\r
178                 for external interrupt.  _RB_ What happens here when mtime is not present as\r
179                 with pulpino? */\r
180                 __asm volatile( "csrs mie, %0" :: "r"(0x880) );\r
181         }\r
182         #else\r
183         {\r
184                 /* Enable external interrupts. */\r
185                 __asm volatile( "csrs mie, %0" :: "r"(0x800) );\r
186         }\r
187         #endif /* configCLINT_BASE_ADDRESS */\r
188 \r
189         xPortStartFirstTask();\r
190 \r
191         /* Should not get here as after calling xPortStartFirstTask() only tasks\r
192         should be executing. */\r
193         return pdFAIL;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vPortEndScheduler( void )\r
198 {\r
199         /* Not implemented. */\r
200         for( ;; );\r
201 }\r
202 \r
203 \r
204 \r
205 \r
206 \r