]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_GCC/main.c
Added the "full" demo to the RISC-V_RV32_SiFive_HiFive1_GCC demo - backup check in...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_GCC / main.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 /* FreeRTOS kernel includes. */\r
29 #include <FreeRTOS.h>\r
30 #include <task.h>\r
31 \r
32 /* Freedom metal driver includes. */\r
33 #include <metal/cpu.h>\r
34 #include <metal/led.h>\r
35 #include <metal/button.h>\r
36 \r
37 /******************************************************************************\r
38  * This project provides two demo applications.  A simple blinky style project,\r
39  * and a more comprehensive test and demo application.  The\r
40  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
41  * select between the two.  The simply blinky demo is implemented and described\r
42  * in main_blinky.c.  The more comprehensive test and demo application is\r
43  * implemented and described in main_full.c.\r
44  *\r
45  * This file implements the code that is not demo specific, including the\r
46  * hardware setup and standard FreeRTOS hook functions.\r
47  *\r
48  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
49  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
50  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
51  *\r
52  *\r
53  */\r
54 \r
55 #warning Also test in QEMU and add instructions above.\r
56 \r
57 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
58 or 0 to run the more comprehensive test and demo application. */\r
59 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
60 \r
61 /* Index to first HART (there is only one). */\r
62 #define mainHART_0              0\r
63 \r
64 /* Register addresses within the PLIC. */\r
65 #define mainPLIC_PENDING_0 ( * ( ( volatile uint32_t * ) 0x0C001000UL ) )\r
66 #define mainPLIC_PENDING_1 ( * ( ( volatile uint32_t * ) 0x0C001004UL ) )\r
67 #define mainPLIC_ENABLE_0  ( * ( ( volatile uint32_t * ) 0x0C002000UL ) )\r
68 #define mainPLIC_ENABLE_1  ( * ( ( volatile uint32_t * ) 0x0C002004UL ) )\r
69 \r
70 /*\r
71  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
72  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
73  */\r
74 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
75         extern void main_blinky( void );\r
76 #else\r
77         extern void main_full( void );\r
78 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
79 \r
80 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
81 within this file.  See https://www.freertos.org/a00016.html */\r
82 void vApplicationMallocFailedHook( void );\r
83 void vApplicationIdleHook( void );\r
84 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
85 void vApplicationTickHook( void );\r
86 \r
87 /* Setup the hardware to run this demo. */\r
88 static void prvSetupHardware( void );\r
89 \r
90 /* Used by the Freedom Metal drivers. */\r
91 static struct metal_led *pxLED = NULL;\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 int main( void )\r
96 {\r
97         prvSetupHardware();\r
98 \r
99         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
100         of this file. */\r
101         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
102         {\r
103                 main_blinky();\r
104         }\r
105         #else\r
106         {\r
107                 main_full();\r
108         }\r
109         #endif\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 static void prvSetupHardware( void )\r
114 {\r
115 struct metal_cpu *pxCPU;\r
116 struct metal_interrupt *pxInterruptController;\r
117 \r
118         /* Initialise the blue LED. */\r
119         pxLED = metal_led_get_rgb( "LD0", "blue" );\r
120         configASSERT( pxLED );\r
121         metal_led_enable( pxLED );\r
122         metal_led_off( pxLED );\r
123 \r
124         /* Initialise the interrupt controller. */\r
125         pxCPU = metal_cpu_get( mainHART_0 );\r
126         configASSERT( pxCPU );\r
127         pxInterruptController = metal_cpu_interrupt_controller( pxCPU );\r
128         configASSERT( pxInterruptController );\r
129         metal_interrupt_init( pxInterruptController );\r
130 \r
131         /* Set all interrupt enable bits to 0. */\r
132         mainPLIC_ENABLE_0 = 0UL;\r
133         mainPLIC_ENABLE_1 = 0UL;\r
134 \r
135         /* Clear all pending interrupts. */\r
136         mainPLIC_PENDING_0 = 0UL;\r
137         mainPLIC_PENDING_1 = 0UL;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vApplicationMallocFailedHook( void )\r
142 {\r
143         /* vApplicationMallocFailedHook() will only be called if\r
144         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
145         function that will get called if a call to pvPortMalloc() fails.\r
146         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
147         timer or semaphore is created.  It is also called by various parts of the\r
148         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
149         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
150         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
151         to query the size of free heap space that remains (although it does not\r
152         provide information on how the remaining heap might be fragmented). */\r
153         taskDISABLE_INTERRUPTS();\r
154         for( ;; );\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vApplicationIdleHook( void )\r
159 {\r
160         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
161         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
162         task.  It is essential that code added to this hook function never attempts\r
163         to block in any way (for example, call xQueueReceive() with a block time\r
164         specified, or call vTaskDelay()).  If the application makes use of the\r
165         vTaskDelete() API function (as this demo application does) then it is also\r
166         important that vApplicationIdleHook() is permitted to return to its calling\r
167         function, because it is the responsibility of the idle task to clean up\r
168         memory allocated by the kernel to any task that has since been deleted. */\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
173 {\r
174         ( void ) pcTaskName;\r
175         ( void ) pxTask;\r
176 \r
177         /* Run time stack overflow checking is performed if\r
178         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
179         function is called if a stack overflow is detected. */\r
180         taskDISABLE_INTERRUPTS();\r
181         for( ;; );\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vApplicationTickHook( void )\r
186 {\r
187         /* The tests in the full demo expect some interaction with interrupts. */\r
188         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
189         {\r
190                 extern void vFullDemoTickHook( void );\r
191                 vFullDemoTickHook();\r
192         }\r
193         #endif\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vAssertCalled( void )\r
198 {\r
199 static struct metal_led *pxRedLED = NULL;\r
200 volatile uint32_t ul;\r
201 \r
202         taskDISABLE_INTERRUPTS();\r
203 \r
204         /* Initialise the red LED. */\r
205         pxRedLED = metal_led_get_rgb( "LD0", "red" );\r
206         configASSERT( pxRedLED );\r
207         metal_led_enable( pxRedLED );\r
208         metal_led_off( pxRedLED );\r
209 \r
210         /* Flash the red LED to indicate that assert was hit - interrupts are off\r
211         here to prevent any further tick interrupts or context switches, so the\r
212         delay is implemented as a crude loop. */\r
213         for( ;; )\r
214         {\r
215                 for( ul = 0; ul < 0x1ffff; ul++ )\r
216                 {\r
217                         __asm volatile( "nop" );\r
218                 }\r
219                 metal_led_toggle( pxRedLED );\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 volatile uint32_t ulMEPC = 0UL, ulMCAUSE = 0UL, ulPending0Register = 0UL, ulPending1Register = 0UL;\r
225 \r
226 void handle_trap( void )\r
227 {\r
228 #warning Not implemented.\r
229 \r
230         __asm volatile( "csrr %0, mepc" : "=r"( ulMEPC ) );\r
231         __asm volatile( "csrr %0, mcause" : "=r"( ulMCAUSE ) );\r
232         ulPending0Register = mainPLIC_PENDING_0;\r
233         ulPending1Register = mainPLIC_PENDING_1;\r
234         configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 void vToggleLED( void )\r
239 {\r
240         metal_led_toggle( pxLED );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void *malloc( size_t xSize )\r
245 {\r
246         configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );\r
247         return NULL;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 \r