3 * FreeRTOS Kernel V10.2.1
\r
4 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
13 * The above copyright notice and this permission notice shall be included in all
\r
14 * copies or substantial portions of the Software.
\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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* FreeRTOS kernel includes. */
\r
30 #include <FreeRTOS.h>
\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
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
47 * This file implements the code that is not demo specific, including the
\r
48 * hardware setup and standard FreeRTOS hook functions.
\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
57 #warning Also test in QEMU and add instructions above.
\r
59 /* Index to first HART (there is only one). */
\r
60 #define mainHART_0 0
\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
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
70 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
\r
71 extern void main_blinky( void );
\r
73 extern void main_full( void );
\r
74 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\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
83 /* Setup the hardware to run this demo. */
\r
84 static void prvSetupHardware( void );
\r
86 /* Used by the Freedom Metal drivers. */
\r
87 static struct metal_led *pxLED = NULL;
\r
89 /*-----------------------------------------------------------*/
\r
95 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
97 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
107 /*-----------------------------------------------------------*/
\r
109 static void prvSetupHardware( void )
\r
111 struct metal_cpu *pxCPU;
\r
112 struct metal_interrupt *pxInterruptController;
\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
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
127 /*-----------------------------------------------------------*/
\r
129 void vApplicationMallocFailedHook( void )
\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
144 /*-----------------------------------------------------------*/
\r
146 void vApplicationIdleHook( void )
\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
158 /*-----------------------------------------------------------*/
\r
160 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
162 ( void ) pcTaskName;
\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
171 /*-----------------------------------------------------------*/
\r
173 void vApplicationTickHook( void )
\r
175 /* The tests in the full demo expect some interaction with interrupts. */
\r
176 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )
\r
178 extern void vFullDemoTickHook( void );
\r
179 vFullDemoTickHook();
\r
183 /*-----------------------------------------------------------*/
\r
185 void vAssertCalled( void )
\r
187 taskDISABLE_INTERRUPTS();
\r
190 /*-----------------------------------------------------------*/
\r
192 void handle_trap( void )
\r
194 #warning Not implemented.
\r
196 configASSERT( metal_cpu_get( mainHART_0 ) == 0x00 );
\r
198 /*-----------------------------------------------------------*/
\r
200 void vToggleLED( void )
\r
202 metal_led_toggle( pxLED );
\r
210 static void prvSetupTimerInterrupt( void )
\r
212 int rc, up_cnt, dn_cnt;
\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
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
224 printf("TIMER interrupt handler registration failed\n");
\r
228 // Lastly CPU interrupt
\r
229 if (metal_interrupt_enable(pxInterruptController, 0) == -1) {
\r
230 printf("CPU interrupt enable failed\n");
\r
234 // Red -> Green -> Blue, repeat
\r
238 wait_for_timer(pxLED);
\r
241 wait_for_timer(led0_green);
\r
244 wait_for_timer(led0_blue);
\r
250 /*-----------------------------------------------------------*/
\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
259 #define RTC_FREQ 32768
\r
261 struct metal_cpu *pxCPU;
\r
262 struct metal_interrupt *pxInterruptController, *tmr_intr;
\r
264 volatile uint32_t timer_isr_flag;
\r
267 void timer_isr (int id, void *data) {
\r
269 // Disable Timer interrupt
\r
270 metal_interrupt_disable(tmr_intr, tmr_id);
\r
272 // Flag showing we hit timer isr
\r
273 timer_isr_flag = 1;
\r
276 void wait_for_timer(struct metal_led *which_led) {
\r
278 // clear global timer isr flag
\r
279 timer_isr_flag = 0;
\r
281 // Turn on desired LED
\r
282 metal_led_on(which_led);
\r
285 metal_cpu_set_mtimecmp(pxCPU, metal_cpu_get_mtime(pxCPU) + RTC_FREQ);
\r
287 // Enable Timer interrupt
\r
288 metal_interrupt_enable(tmr_intr, tmr_id);
\r
290 // wait till timer triggers and isr is hit
\r
291 while (timer_isr_flag == 0){};
\r
293 timer_isr_flag = 0;
\r
295 // Turn off this LED
\r
296 metal_led_off(which_led);
\r