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