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 /* FreeRTOS kernel includes. */
\r
29 #include <FreeRTOS.h>
\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
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
45 * This file implements the code that is not demo specific, including the
\r
46 * hardware setup and standard FreeRTOS hook functions.
\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
55 #warning Also test in QEMU and add instructions above.
\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
61 /* Index to first HART (there is only one). */
\r
62 #define mainHART_0 0
\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
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
74 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
\r
75 extern void main_blinky( void );
\r
77 extern void main_full( void );
\r
78 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\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
87 /* Setup the hardware to run this demo. */
\r
88 static void prvSetupHardware( void );
\r
90 /* Used by the Freedom Metal drivers. */
\r
91 static struct metal_led *pxLED = NULL;
\r
93 /*-----------------------------------------------------------*/
\r
99 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
101 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
111 /*-----------------------------------------------------------*/
\r
113 static void prvSetupHardware( void )
\r
115 struct metal_cpu *pxCPU;
\r
116 struct metal_interrupt *pxInterruptController;
\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
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
131 /* Set all interrupt enable bits to 0. */
\r
132 mainPLIC_ENABLE_0 = 0UL;
\r
133 mainPLIC_ENABLE_1 = 0UL;
\r
135 /* Clear all pending interrupts. */
\r
136 mainPLIC_PENDING_0 = 0UL;
\r
137 mainPLIC_PENDING_1 = 0UL;
\r
139 /*-----------------------------------------------------------*/
\r
141 void vApplicationMallocFailedHook( void )
\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
156 /*-----------------------------------------------------------*/
\r
158 void vApplicationIdleHook( void )
\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
170 /*-----------------------------------------------------------*/
\r
172 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
174 ( void ) pcTaskName;
\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
183 /*-----------------------------------------------------------*/
\r
185 void vApplicationTickHook( void )
\r
187 /* The tests in the full demo expect some interaction with interrupts. */
\r
188 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
190 extern void vFullDemoTickHook( void );
\r
191 vFullDemoTickHook();
\r
195 /*-----------------------------------------------------------*/
\r
197 void vAssertCalled( void )
\r
199 static struct metal_led *pxRedLED = NULL;
\r
200 volatile uint32_t ul;
\r
202 taskDISABLE_INTERRUPTS();
\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
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
215 for( ul = 0; ul < 0x1ffff; ul++ )
\r
217 __asm volatile( "nop" );
\r
219 metal_led_toggle( pxRedLED );
\r
222 /*-----------------------------------------------------------*/
\r
224 volatile uint32_t ulMEPC = 0UL, ulMCAUSE = 0UL, ulPending0Register = 0UL, ulPending1Register = 0UL;
\r
226 void handle_trap( void )
\r
228 #warning Not implemented.
\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
236 /*-----------------------------------------------------------*/
\r
238 void vToggleLED( void )
\r
240 metal_led_toggle( pxLED );
\r
242 /*-----------------------------------------------------------*/
\r
244 void *malloc( size_t xSize )
\r
246 configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );
\r
249 /*-----------------------------------------------------------*/
\r