]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RISC-V/port.c
8c59aef2ff29f9d46d7960dedf5f8937479f9ba0
[freertos] / FreeRTOS / Source / portable / IAR / RISC-V / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 #ifndef configCLINT_BASE_ADDRESS\r
38         #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
39 #endif\r
40 \r
41 /* Let the user override the pre-loading of the initial LR with the address of\r
42 prvTaskExitError() in case it messes up unwinding of the stack in the\r
43 debugger. */\r
44 #ifdef configTASK_RETURN_ADDRESS\r
45         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
46 #else\r
47         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
48 #endif\r
49 \r
50 /* The stack used by interrupt service routines.  Set configISR_STACK_SIZE_WORDS\r
51 to use a statically allocated array as the interrupt stack.  Alternative leave\r
52 configISR_STACK_SIZE_WORDS undefined and update the linker script so that a\r
53 linker variable names __freertos_irq_stack_top has the same value as the top\r
54 of the stack used by main.  Using the linker script method will repurpose the\r
55 stack that was used by main before the scheduler was started for use as the\r
56 interrupt stack after the scheduler has started. */\r
57 #ifdef configISR_STACK_SIZE_WORDS\r
58         static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };\r
59         const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ] );\r
60 #else\r
61         extern const uint32_t __freertos_irq_stack_top[];\r
62         const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;\r
63 #endif\r
64 \r
65 /*\r
66  * Setup the timer to generate the tick interrupts.  The implementation in this\r
67  * file is weak to allow application writers to change the timer used to\r
68  * generate the tick interrupt.\r
69  */\r
70 void vPortSetupTimerInterrupt( void ) __attribute__(( weak ));\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* Used to program the machine timer compare register. */\r
75 uint64_t ullNextTime = 0ULL;\r
76 const uint64_t *pullNextTime = &ullNextTime;\r
77 const size_t uxTimerIncrementsForOneTick = ( size_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ); /* Assumes increment won't go over 32-bits. */\r
78 volatile uint64_t * const pullMachineTimerCompareRegisterBase = ( uint64_t * ) ( configCLINT_BASE_ADDRESS + 0x4000 );\r
79 volatile uint64_t * pullMachineTimerCompareRegister = 0;\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 #if( configCLINT_BASE_ADDRESS != 0 )\r
108 \r
109         void vPortSetupTimerInterrupt( void )\r
110         {\r
111         uint32_t ulCurrentTimeHigh, ulCurrentTimeLow;\r
112         volatile uint32_t * const pulTimeHigh = ( uint32_t * ) ( configCLINT_BASE_ADDRESS + 0xBFFC );\r
113         volatile uint32_t * const pulTimeLow = ( uint32_t * ) ( configCLINT_BASE_ADDRESS + 0xBFF8 );\r
114         volatile uint32_t ulHartId = 0;\r
115 \r
116                 __asm volatile( "csrr %0, 0xf14" : "=r"( ulHartId ) ); /* 0xf14 is hartid. */\r
117                 pullMachineTimerCompareRegister  = &( pullMachineTimerCompareRegisterBase[ ulHartId ] );\r
118 \r
119                 do\r
120                 {\r
121                         ulCurrentTimeHigh = *pulTimeHigh;\r
122                         ulCurrentTimeLow = *pulTimeLow;\r
123                 } while( ulCurrentTimeHigh != *pulTimeHigh );\r
124 \r
125                 ullNextTime = ( uint64_t ) ulCurrentTimeHigh;\r
126                 ullNextTime <<= 32ULL;\r
127                 ullNextTime |= ( uint64_t ) ulCurrentTimeLow;\r
128                 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;\r
129                 *pullMachineTimerCompareRegister = ullNextTime;\r
130 \r
131                 /* Prepare the time to use after the next tick interrupt. */\r
132                 ullNextTime += ( uint64_t ) uxTimerIncrementsForOneTick;\r
133         }\r
134 \r
135 #endif /* ( configCLINT_BASE_ADDRESS != 0 ) */\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 BaseType_t xPortStartScheduler( void )\r
139 {\r
140 extern void xPortStartFirstTask( void );\r
141 \r
142         #if( configASSERT_DEFINED == 1 )\r
143         {\r
144                 volatile uint32_t mtvec = 0;\r
145 \r
146                 /* Check the least significant two bits of mtvec are 00 - indicating\r
147                 single vector mode. */\r
148                 __asm volatile( "csrr %0, 0x305" : "=r"( mtvec ) ); /* 0x305 is mtvec. */\r
149                 configASSERT( ( mtvec & 0x03UL ) == 0 );\r
150 \r
151                 /* Check alignment of the interrupt stack - which is the same as the\r
152                 stack that was being used by main() prior to the scheduler being\r
153                 started. */\r
154                 configASSERT( ( xISRStackTop & portBYTE_ALIGNMENT_MASK ) == 0 );\r
155         }\r
156         #endif /* configASSERT_DEFINED */\r
157 \r
158         /* If there is a CLINT then it is ok to use the default implementation\r
159         in this file, otherwise vPortSetupTimerInterrupt() must be implemented to\r
160         configure whichever clock is to be used to generate the tick interrupt. */\r
161         vPortSetupTimerInterrupt();\r
162 \r
163         #if( configCLINT_BASE_ADDRESS != 0 )\r
164         {\r
165                 /* Enable mtime and external interrupts.  1<<7 for timer interrupt, 1<<11\r
166                 for external interrupt.  _RB_ What happens here when mtime is not present as\r
167                 with pulpino? */\r
168                 __asm volatile( "csrs 0x304, %0" :: "r"(0x880) ); /* 0x304 is mie. */\r
169         }\r
170         #else\r
171         {\r
172                 /* Enable external interrupts. */\r
173                 __asm volatile( "csrs 0x304, %0" :: "r"(0x800) ); /* 304 is mie. */\r
174         }\r
175         #endif /* configCLINT_BASE_ADDRESS */\r
176 \r
177         xPortStartFirstTask();\r
178 \r
179         /* Should not get here as after calling xPortStartFirstTask() only tasks\r
180         should be executing. */\r
181         return pdFAIL;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vPortEndScheduler( void )\r
186 {\r
187         /* Not implemented. */\r
188         for( ;; );\r
189 }\r
190 \r
191 \r
192 \r
193 \r
194 \r