]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_GCC/main.c
RIS-V_RV32_SiFive_HiFive1_GCC project now running the blinky demo - still a work...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_GCC / main.c
1 #if 1\r
2 /*\r
3  * FreeRTOS Kernel V10.2.1\r
4  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
5  *\r
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
7  * this software and associated documentation files (the "Software"), to deal in\r
8  * the Software without restriction, including without limitation the rights to\r
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
10  * the Software, and to permit persons to whom the Software is furnished to do so,\r
11  * subject to the following conditions:\r
12  *\r
13  * The above copyright notice and this permission notice shall be included in all\r
14  * copies or substantial portions of the Software.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* FreeRTOS kernel includes. */\r
30 #include <FreeRTOS.h>\r
31 #include <task.h>\r
32 \r
33 /* Freedom metal driver includes. */\r
34 #include <metal/cpu.h>\r
35 #include <metal/led.h>\r
36 #include <metal/button.h>\r
37 \r
38 \r
39 /******************************************************************************\r
40  * This project provides two demo applications.  A simple blinky style project,\r
41  * and a more comprehensive test and demo application.  The\r
42  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
43  * select between the two.  The simply blinky demo is implemented and described\r
44  * in main_blinky.c.  The more comprehensive test and demo application is\r
45  * implemented and described in main_full.c.\r
46  *\r
47  * This file implements the code that is not demo specific, including the\r
48  * hardware setup and standard FreeRTOS hook functions.\r
49  *\r
50  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
51  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
52  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
53  *\r
54  *\r
55  */\r
56 \r
57 #warning Also test in QEMU and add instructions above.\r
58 \r
59 /* Index to first HART (there is only one). */\r
60 #define mainHART_0              0\r
61 \r
62 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
63 or 0 to run the more comprehensive test and demo application. */\r
64 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
65 \r
66 /*\r
67  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
68  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
69  */\r
70 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
71         extern void main_blinky( void );\r
72 #else\r
73         extern void main_full( void );\r
74 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
75 \r
76 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
77 within this file.  See https://www.freertos.org/a00016.html */\r
78 void vApplicationMallocFailedHook( void );\r
79 void vApplicationIdleHook( void );\r
80 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
81 void vApplicationTickHook( void );\r
82 \r
83 /* Setup the hardware to run this demo. */\r
84 static void prvSetupHardware( void );\r
85 \r
86 /* Used by the Freedom Metal drivers. */\r
87 static struct metal_led *pxLED = NULL;\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 int main( void )\r
92 {\r
93         prvSetupHardware();\r
94 \r
95         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
96         of this file. */\r
97         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
98         {\r
99                 main_blinky();\r
100         }\r
101         #else\r
102         {\r
103                 main_full();\r
104         }\r
105         #endif\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 static void prvSetupHardware( void )\r
110 {\r
111 struct metal_cpu *pxCPU;\r
112 struct metal_interrupt *pxInterruptController;\r
113 \r
114         /* Initialise the red LED. */\r
115         pxLED = metal_led_get_rgb( "LD0", "red" );\r
116         configASSERT( pxLED );\r
117         metal_led_enable( pxLED );\r
118         metal_led_off( pxLED );\r
119 \r
120         /* Initialise the interrupt controller. */\r
121         pxCPU = metal_cpu_get( mainHART_0 );\r
122         configASSERT( pxCPU );\r
123         pxInterruptController = metal_cpu_interrupt_controller( pxCPU );\r
124         configASSERT( pxInterruptController );\r
125         metal_interrupt_init( pxInterruptController );\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 void vApplicationMallocFailedHook( void )\r
130 {\r
131         /* vApplicationMallocFailedHook() will only be called if\r
132         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
133         function that will get called if a call to pvPortMalloc() fails.\r
134         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
135         timer or semaphore is created.  It is also called by various parts of the\r
136         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
137         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
138         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
139         to query the size of free heap space that remains (although it does not\r
140         provide information on how the remaining heap might be fragmented). */\r
141         taskDISABLE_INTERRUPTS();\r
142         for( ;; );\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 void vApplicationIdleHook( void )\r
147 {\r
148         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
149         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
150         task.  It is essential that code added to this hook function never attempts\r
151         to block in any way (for example, call xQueueReceive() with a block time\r
152         specified, or call vTaskDelay()).  If the application makes use of the\r
153         vTaskDelete() API function (as this demo application does) then it is also\r
154         important that vApplicationIdleHook() is permitted to return to its calling\r
155         function, because it is the responsibility of the idle task to clean up\r
156         memory allocated by the kernel to any task that has since been deleted. */\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
161 {\r
162         ( void ) pcTaskName;\r
163         ( void ) pxTask;\r
164 \r
165         /* Run time stack overflow checking is performed if\r
166         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
167         function is called if a stack overflow is detected. */\r
168         taskDISABLE_INTERRUPTS();\r
169         for( ;; );\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vApplicationTickHook( void )\r
174 {\r
175         /* The tests in the full demo expect some interaction with interrupts. */\r
176         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
177         {\r
178                 extern void vFullDemoTickHook( void );\r
179                 vFullDemoTickHook();\r
180         }\r
181         #endif\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vAssertCalled( void )\r
186 {\r
187         taskDISABLE_INTERRUPTS();\r
188         for( ;; );\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void handle_trap( void )\r
193 {\r
194 #warning Not implemented.\r
195 \r
196         configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vToggleLED( void )\r
201 {\r
202         metal_led_toggle( pxLED );\r
203 }\r
204 \r
205 \r
206 \r
207 \r
208 #else\r
209 \r
210 static void prvSetupTimerInterrupt( void )\r
211 {\r
212         int rc, up_cnt, dn_cnt;\r
213 \r
214     // Setup Timer and its interrupt so we can toggle LEDs on 1s cadence\r
215     tmr_intr = metal_cpu_timer_interrupt_controller(pxCPU);\r
216     if (tmr_intr == NULL) {\r
217         printf("TIMER interrupt controller is  null.\n");\r
218         return 4;\r
219     }\r
220     metal_interrupt_init(tmr_intr);\r
221     tmr_id = metal_cpu_timer_get_interrupt_id(pxCPU);\r
222     rc = metal_interrupt_register_handler(tmr_intr, tmr_id, timer_isr, pxCPU);\r
223     if (rc < 0) {\r
224         printf("TIMER interrupt handler registration failed\n");\r
225         return (rc * -1);\r
226     }\r
227 \r
228     // Lastly CPU interrupt\r
229     if (metal_interrupt_enable(pxInterruptController, 0) == -1) {\r
230         printf("CPU interrupt enable failed\n");\r
231         return 6;\r
232     }\r
233 \r
234     // Red -> Green -> Blue, repeat\r
235     while (1) {\r
236 \r
237         // Turn on RED\r
238         wait_for_timer(pxLED);\r
239 \r
240         // Turn on Green\r
241         wait_for_timer(led0_green);\r
242 \r
243         // Turn on Blue\r
244         wait_for_timer(led0_blue);\r
245     }\r
246 \r
247     // return\r
248     return 0;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 \r
253 #include <stdio.h>\r
254 #include <metal/cpu.h>\r
255 #include <metal/led.h>\r
256 #include <metal/button.h>\r
257 #include <metal/switch.h>\r
258 \r
259 #define RTC_FREQ    32768\r
260 \r
261 struct metal_cpu *pxCPU;\r
262 struct metal_interrupt *pxInterruptController, *tmr_intr;\r
263 int tmr_id;\r
264 volatile uint32_t timer_isr_flag;\r
265 \r
266 \r
267 void timer_isr (int id, void *data) {\r
268 \r
269     // Disable Timer interrupt\r
270     metal_interrupt_disable(tmr_intr, tmr_id);\r
271 \r
272     // Flag showing we hit timer isr\r
273     timer_isr_flag = 1;\r
274 }\r
275 \r
276 void wait_for_timer(struct metal_led *which_led) {\r
277 \r
278     // clear global timer isr flag\r
279     timer_isr_flag = 0;\r
280 \r
281     // Turn on desired LED\r
282     metal_led_on(which_led);\r
283 \r
284     // Set timer\r
285     metal_cpu_set_mtimecmp(pxCPU, metal_cpu_get_mtime(pxCPU) + RTC_FREQ);\r
286 \r
287     // Enable Timer interrupt\r
288     metal_interrupt_enable(tmr_intr, tmr_id);\r
289 \r
290     // wait till timer triggers and isr is hit\r
291     while (timer_isr_flag == 0){};\r
292 \r
293     timer_isr_flag = 0;\r
294 \r
295     // Turn off this LED\r
296     metal_led_off(which_led);\r
297 }\r
298 \r
299 \r
300 \r
301 #endif\r
302 \r
303 \r