2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
29 * This project provides two demo applications. A simple blinky style project,
\r
30 * and a more comprehensive test and demo application. The
\r
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
\r
32 * select between the two. The simply blinky demo is implemented and described
\r
33 * in main_blinky.c. The more comprehensive test and demo application is
\r
34 * implemented and described in main_full.c.
\r
36 * This file implements the code that is not demo specific, including the
\r
37 * hardware setup and standard FreeRTOS hook functions.
\r
39 * When running on the HiFive Rev B hardware:
\r
40 * When executing correctly the blue LED will toggle every three seconds. If
\r
41 * the blue LED toggles every 500ms then one of the self-monitoring test tasks
\r
42 * discovered a potential issue. If the red led toggles rapidly then a hardware
\r
43 * exception occurred.
\r
45 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
46 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
47 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
51 /* FreeRTOS kernel includes. */
\r
52 #include <FreeRTOS.h>
\r
55 /* Freedom metal driver includes. */
\r
56 #include <metal/cpu.h>
\r
57 #include <metal/led.h>
\r
59 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
\r
60 or 0 to run the more comprehensive test and demo application. */
\r
61 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
\r
63 /* Index to first HART (there is only one). */
\r
64 #define mainHART_0 0
\r
66 /* Registers used to initialise the PLIC. */
\r
67 #define mainPLIC_PENDING_0 ( * ( ( volatile uint32_t * ) 0x0C001000UL ) )
\r
68 #define mainPLIC_PENDING_1 ( * ( ( volatile uint32_t * ) 0x0C001004UL ) )
\r
69 #define mainPLIC_ENABLE_0 ( * ( ( volatile uint32_t * ) 0x0C002000UL ) )
\r
70 #define mainPLIC_ENABLE_1 ( * ( ( volatile uint32_t * ) 0x0C002004UL ) )
\r
72 /*-----------------------------------------------------------*/
\r
75 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
76 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
78 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
\r
79 extern void main_blinky( void );
\r
81 extern void main_full( void );
\r
82 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\r
85 * Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
86 * within this file. See https://www.freertos.org/a00016.html
\r
88 void vApplicationMallocFailedHook( void );
\r
89 void vApplicationIdleHook( void );
\r
90 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
91 void vApplicationTickHook( void );
\r
94 * Setup the hardware to run this demo.
\r
96 static void prvSetupHardware( void );
\r
99 * Used by the Freedom Metal drivers.
\r
101 static struct metal_led *pxBlueLED = NULL;
\r
103 /*-----------------------------------------------------------*/
\r
107 prvSetupHardware();
\r
109 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
111 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
121 /*-----------------------------------------------------------*/
\r
123 static void prvSetupHardware( void )
\r
125 struct metal_cpu *pxCPU;
\r
126 struct metal_interrupt *pxInterruptController;
\r
128 /* Initialise the blue LED. */
\r
129 pxBlueLED = metal_led_get_rgb( "LD0", "blue" );
\r
130 configASSERT( pxBlueLED );
\r
131 metal_led_enable( pxBlueLED );
\r
132 metal_led_off( pxBlueLED );
\r
134 /* Initialise the interrupt controller. */
\r
135 pxCPU = metal_cpu_get( mainHART_0 );
\r
136 configASSERT( pxCPU );
\r
137 pxInterruptController = metal_cpu_interrupt_controller( pxCPU );
\r
138 configASSERT( pxInterruptController );
\r
139 metal_interrupt_init( pxInterruptController );
\r
141 /* Set all interrupt enable bits to 0. */
\r
142 mainPLIC_ENABLE_0 = 0UL;
\r
143 mainPLIC_ENABLE_1 = 0UL;
\r
145 /* Clear all pending interrupts. */
\r
146 mainPLIC_PENDING_0 = 0UL;
\r
147 mainPLIC_PENDING_1 = 0UL;
\r
149 /*-----------------------------------------------------------*/
\r
151 void vApplicationMallocFailedHook( void )
\r
153 /* vApplicationMallocFailedHook() will only be called if
\r
154 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
155 function that will get called if a call to pvPortMalloc() fails.
\r
156 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
157 timer or semaphore is created. It is also called by various parts of the
\r
158 demo application. If heap_1.c or heap_2.c are used, then the size of the
\r
159 heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
160 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
161 to query the size of free heap space that remains (although it does not
\r
162 provide information on how the remaining heap might be fragmented). */
\r
163 taskDISABLE_INTERRUPTS();
\r
166 /*-----------------------------------------------------------*/
\r
168 void vApplicationIdleHook( void )
\r
170 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
171 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
172 task. It is essential that code added to this hook function never attempts
\r
173 to block in any way (for example, call xQueueReceive() with a block time
\r
174 specified, or call vTaskDelay()). If the application makes use of the
\r
175 vTaskDelete() API function (as this demo application does) then it is also
\r
176 important that vApplicationIdleHook() is permitted to return to its calling
\r
177 function, because it is the responsibility of the idle task to clean up
\r
178 memory allocated by the kernel to any task that has since been deleted. */
\r
180 /*-----------------------------------------------------------*/
\r
182 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
184 ( void ) pcTaskName;
\r
187 /* Run time stack overflow checking is performed if
\r
188 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
189 function is called if a stack overflow is detected. */
\r
190 taskDISABLE_INTERRUPTS();
\r
193 /*-----------------------------------------------------------*/
\r
195 void vApplicationTickHook( void )
\r
197 /* The tests in the full demo expect some interaction with interrupts. */
\r
198 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
200 extern void vFullDemoTickHook( void );
\r
201 vFullDemoTickHook();
\r
205 /*-----------------------------------------------------------*/
\r
207 void vAssertCalled( void )
\r
209 static struct metal_led *pxRedLED = NULL;
\r
210 volatile uint32_t ul;
\r
211 const uint32_t ulNullLoopDelay = 0x1ffffUL;
\r
213 taskDISABLE_INTERRUPTS();
\r
215 /* Initialise the red LED. */
\r
216 pxRedLED = metal_led_get_rgb( "LD0", "red" );
\r
217 configASSERT( pxRedLED );
\r
218 metal_led_enable( pxRedLED );
\r
219 metal_led_off( pxRedLED );
\r
221 /* Flash the red LED to indicate that assert was hit - interrupts are off
\r
222 here to prevent any further tick interrupts or context switches, so the
\r
223 delay is implemented as a crude loop instead of a peripheral timer. */
\r
226 for( ul = 0; ul < ulNullLoopDelay; ul++ )
\r
228 __asm volatile( "nop" );
\r
230 metal_led_toggle( pxRedLED );
\r
233 /*-----------------------------------------------------------*/
\r
235 void handle_trap( void )
\r
237 volatile uint32_t ulMEPC = 0UL, ulMCAUSE = 0UL, ulPLICPending0Register = 0UL, ulPLICPending1Register = 0UL;
\r
239 /* Store a few register values that might be useful when determining why this
\r
240 function was called. */
\r
241 __asm volatile( "csrr %0, mepc" : "=r"( ulMEPC ) );
\r
242 __asm volatile( "csrr %0, mcause" : "=r"( ulMCAUSE ) );
\r
243 ulPLICPending0Register = mainPLIC_PENDING_0;
\r
244 ulPLICPending1Register = mainPLIC_PENDING_1;
\r
246 /* Prevent compiler warnings about unused variables. */
\r
247 ( void ) ulPLICPending0Register;
\r
248 ( void ) ulPLICPending1Register;
\r
250 /* Force an assert as this function has not been implemented as the demo
\r
251 does not use external interrupts. */
\r
252 configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );
\r
254 /*-----------------------------------------------------------*/
\r
256 void vToggleLED( void )
\r
258 metal_led_toggle( pxBlueLED );
\r
260 /*-----------------------------------------------------------*/
\r
262 void *malloc( size_t xSize )
\r
264 /* The linker script does not define a heap so artificially force an assert()
\r
265 if something unexpectedly uses the C library heap. See
\r
266 https://www.freertos.org/a00111.html for more information. */
\r
267 configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );
\r
270 /*-----------------------------------------------------------*/
\r