2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 FreeRTOS hook functions.
\r
39 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
40 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
41 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
45 /* Standard includes. */
\r
49 /* Scheduler include files. */
\r
50 #include "FreeRTOS.h"
\r
54 /* Standard demo includes. */
\r
55 #include "partest.h"
\r
56 #include "TimerDemo.h"
\r
57 #include "QueueOverwrite.h"
\r
58 #include "EventGroupsDemo.h"
\r
59 #include "IntSemTest.h"
\r
61 /* Altera library includes. */
\r
62 #include "alt_timers.h"
\r
63 #include "alt_clock_manager.h"
\r
64 #include "alt_interrupt.h"
\r
65 #include "alt_globaltmr.h"
\r
66 #include "alt_address_space.h"
\r
67 #include "mmu_support.h"
\r
68 #include "cache_support.h"
\r
70 /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is used to select between two demo
\r
71 * applications, as described at the top of this file.
\r
73 * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 the simple blinky example
\r
76 * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0 the comprehensive test
\r
77 * and demo application will be run.
\r
79 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
\r
81 /*-----------------------------------------------------------*/
\r
84 * Configure the hardware as necessary to run this demo.
\r
86 static void prvSetupHardware( void );
\r
89 * See the comments at the top of this file and above the
\r
90 * mainSELECTED_APPLICATION definition.
\r
92 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
93 extern void main_blinky( void );
\r
95 extern void main_full( void );
\r
96 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\r
98 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
99 within this file. */
\r
100 void vApplicationMallocFailedHook( void );
\r
101 void vApplicationIdleHook( void );
\r
102 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
103 void vApplicationTickHook( void );
\r
105 /*-----------------------------------------------------------*/
\r
107 /* configAPPLICATION_ALLOCATED_HEAP is set to 1 in FreeRTOSConfig.h so the
\r
108 application can define the array used as the FreeRTOS heap. This is done so the
\r
109 heap can be forced into fast internal RAM - useful because the stacks used by
\r
110 the tasks come from this space. */
\r
111 uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ( ( section( ".oc_ram" ) ) );
\r
113 /* FreeRTOS uses its own interrupt handler code. This code cannot use the array
\r
114 of handlers defined by the Altera drivers because the array is declared static,
\r
115 and so not accessible outside of the dirver's source file. Instead declare an
\r
116 array for use by the FreeRTOS handler. See:
\r
117 http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html. */
\r
118 static INT_DISPATCH_t xISRHandlers[ ALT_INT_PROVISION_INT_COUNT ];
\r
120 /*-----------------------------------------------------------*/
\r
124 /* Configure the hardware ready to run the demo. */
\r
125 prvSetupHardware();
\r
127 /* The mainSELECTED_APPLICATION setting is described at the top
\r
129 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
139 /* Don't expect to reach here. */
\r
142 /*-----------------------------------------------------------*/
\r
144 static void prvSetupHardware( void )
\r
146 extern uint8_t __cs3_interrupt_vector;
\r
147 uint32_t ulSCTLR, ulVectorTable = ( uint32_t ) &__cs3_interrupt_vector;
\r
148 const uint32_t ulVBit = 13U;
\r
150 alt_int_global_init();
\r
151 alt_int_cpu_binary_point_set( 0 );
\r
153 /* Clear SCTLR.V for low vectors and map the vector table to the beginning
\r
155 __asm( "MRC p15, 0, %0, c1, c0, 0" : "=r" ( ulSCTLR ) );
\r
156 ulSCTLR &= ~( 1 << ulVBit );
\r
157 __asm( "MCR p15, 0, %0, c1, c0, 0" : : "r" ( ulSCTLR ) );
\r
158 __asm( "MCR p15, 0, %0, c12, c0, 0" : : "r" ( ulVectorTable ) );
\r
163 /* GPIO for LEDs. ParTest is a historic name which used to stand for
\r
164 parallel port test. */
\r
165 vParTestInitialise();
\r
167 /*-----------------------------------------------------------*/
\r
169 void vApplicationMallocFailedHook( void )
\r
171 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
172 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
173 internally by FreeRTOS API functions that create tasks, queues, software
\r
174 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
175 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
176 taskDISABLE_INTERRUPTS();
\r
179 /*-----------------------------------------------------------*/
\r
181 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
183 ( void ) pcTaskName;
\r
186 /* Run time stack overflow checking is performed if
\r
187 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
188 function is called if a stack overflow is detected. */
\r
189 taskDISABLE_INTERRUPTS();
\r
192 /*-----------------------------------------------------------*/
\r
194 void vApplicationIdleHook( void )
\r
196 volatile size_t xFreeHeapSpace;
\r
198 /* This is just a trivial example of an idle hook. It is called on each
\r
199 cycle of the idle task. It must *NOT* attempt to block. In this case the
\r
200 idle task just queries the amount of FreeRTOS heap that remains. See the
\r
201 memory management section on the http://www.FreeRTOS.org web site for memory
\r
202 management options. If there is a lot of heap memory free then the
\r
203 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
\r
205 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
207 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
208 ( void ) xFreeHeapSpace;
\r
210 /*-----------------------------------------------------------*/
\r
212 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
214 volatile unsigned long ul = 0;
\r
219 taskENTER_CRITICAL();
\r
221 /* Set ul to a non-zero value using the debugger to step out of this
\r
228 taskEXIT_CRITICAL();
\r
230 /*-----------------------------------------------------------*/
\r
232 void vApplicationTickHook( void )
\r
234 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
\r
236 /* The full demo includes a software timer demo/test that requires
\r
237 prodding periodically from the tick interrupt. */
\r
238 vTimerPeriodicISRTests();
\r
240 /* Call the periodic queue overwrite from ISR demo. */
\r
241 vQueueOverwritePeriodicISRDemo();
\r
243 /* Call the periodic event group from ISR demo. */
\r
244 vPeriodicEventGroupsProcessing();
\r
246 /* Call the periodic test that uses mutexes form an interrupt. */
\r
247 vInterruptSemaphorePeriodicTest();
\r
251 /*-----------------------------------------------------------*/
\r
253 void vConfigureTickInterrupt( void )
\r
255 alt_freq_t ulTempFrequency;
\r
256 const alt_freq_t ulMicroSecondsPerSecond = 1000000UL;
\r
257 void FreeRTOS_Tick_Handler( void );
\r
259 /* Interrupts are disabled when this function is called. */
\r
261 /* Initialise the general purpose timer modules. */
\r
262 alt_gpt_all_tmr_init();
\r
264 /* ALT_CLK_MPU_PERIPH = mpu_periph_clk */
\r
265 alt_clk_freq_get( ALT_CLK_MPU_PERIPH, &ulTempFrequency );
\r
267 /* Use the local private timer. */
\r
268 alt_gpt_counter_set( ALT_GPT_CPU_PRIVATE_TMR, ulTempFrequency / configTICK_RATE_HZ );
\r
270 /* Sanity check. */
\r
271 configASSERT( alt_gpt_time_microsecs_get( ALT_GPT_CPU_PRIVATE_TMR ) == ( ulMicroSecondsPerSecond / configTICK_RATE_HZ ) );
\r
273 /* Set to periodic mode. */
\r
274 alt_gpt_mode_set( ALT_GPT_CPU_PRIVATE_TMR, ALT_GPT_RESTART_MODE_PERIODIC );
\r
276 /* The timer can be started here as interrupts are disabled. */
\r
277 alt_gpt_tmr_start( ALT_GPT_CPU_PRIVATE_TMR );
\r
279 /* Register the standard FreeRTOS Cortex-A tick handler as the timer's
\r
280 interrupt handler. The handler clears the interrupt using the
\r
281 configCLEAR_TICK_INTERRUPT() macro, which is defined in FreeRTOSConfig.h. */
\r
282 vRegisterIRQHandler( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, ( alt_int_callback_t ) FreeRTOS_Tick_Handler, NULL );
\r
284 /* This tick interrupt must run at the lowest priority. */
\r
285 alt_int_dist_priority_set( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );
\r
287 /* Ensure the interrupt is forwarded to the CPU. */
\r
288 alt_int_dist_enable( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE );
\r
290 /* Finally, enable the interrupt. */
\r
291 alt_gpt_int_clear_pending( ALT_GPT_CPU_PRIVATE_TMR );
\r
292 alt_gpt_int_enable( ALT_GPT_CPU_PRIVATE_TMR );
\r
295 /*-----------------------------------------------------------*/
\r
297 void vRegisterIRQHandler( uint32_t ulID, alt_int_callback_t pxHandlerFunction, void *pvContext )
\r
299 if( ulID < ALT_INT_PROVISION_INT_COUNT )
\r
301 xISRHandlers[ ulID ].pxISR = pxHandlerFunction;
\r
302 xISRHandlers[ ulID ].pvContext = pvContext;
\r
305 /*-----------------------------------------------------------*/
\r
307 void vApplicationIRQHandler( uint32_t ulICCIAR )
\r
309 uint32_t ulInterruptID;
\r
311 alt_int_callback_t pxISR;
\r
313 /* Re-enable interrupts. */
\r
314 __asm ( "cpsie i" );
\r
316 /* The ID of the interrupt is obtained by bitwise anding the ICCIAR value
\r
318 ulInterruptID = ulICCIAR & 0x3FFUL;
\r
320 if( ulInterruptID < ALT_INT_PROVISION_INT_COUNT )
\r
322 /* Call the function installed in the array of installed handler
\r
324 pxISR = xISRHandlers[ ulInterruptID ].pxISR;
\r
325 pvContext = xISRHandlers[ ulInterruptID ].pvContext;
\r
326 pxISR( ulICCIAR, pvContext );
\r