]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/main.c
ab7ac6e8611d86447a58fce2cc36838a5fa525a6
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \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
35  *\r
36  * This file implements the code that is not demo specific, including the\r
37  * hardware setup, standard FreeRTOS hook functions, and the ISR hander called\r
38  * by the RTOS after interrupt entry (including nesting) has been taken care of.\r
39  *\r
40  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
41  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
42  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
43  *\r
44  */\r
45 \r
46 /* Scheduler include files. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 #include "semphr.h"\r
50 \r
51 /* Standard demo includes. */\r
52 #include "partest.h"\r
53 #include "TimerDemo.h"\r
54 #include "QueueOverwrite.h"\r
55 #include "EventGroupsDemo.h"\r
56 \r
57 /* Library includes. */\r
58 #include "chip.h"\r
59 \r
60 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
61 or 0 to run the more comprehensive test and demo application. */\r
62 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /*\r
67  * Configure the hardware as necessary to run this demo.\r
68  */\r
69 static void prvSetupHardware( void );\r
70 \r
71 /*\r
72  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
73  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
74  */\r
75 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
76         extern void main_blinky( void );\r
77 #else\r
78         extern void main_full( void );\r
79 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
80 \r
81 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
82 within this file. */\r
83 void vApplicationMallocFailedHook( void );\r
84 void vApplicationIdleHook( void );\r
85 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
86 void vApplicationTickHook( void );\r
87 \r
88 /* Prototype for the IRQ handler called by the generic Cortex-A5 RTOS port\r
89 layer. */\r
90 void vApplicationIRQHandler( void );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 int main( void )\r
95 {\r
96         /* Configure the hardware ready to run the demo. */\r
97         prvSetupHardware();\r
98 \r
99         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
100         of this file. */\r
101         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
102         {\r
103                 main_blinky();\r
104         }\r
105         #else\r
106         {\r
107                 main_full();\r
108         }\r
109         #endif\r
110 \r
111         return 0;\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvSetupHardware( void )\r
116 {\r
117     /* Disable watchdog */\r
118     WDT_Disable( WDT );\r
119 \r
120         /* Set protect mode in the AIC for easier debugging. */\r
121         AIC->AIC_DCR |= AIC_DCR_PROT;\r
122 \r
123         /* Configure ports used by LEDs. */\r
124         vParTestInitialise();\r
125 \r
126         #if defined (ddram)\r
127                 MMU_Initialize( ( uint32_t * ) 0x30C000 );\r
128                 CP15_EnableMMU();\r
129                 CP15_EnableDcache();\r
130                 CP15_EnableIcache();\r
131         #endif\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vApplicationMallocFailedHook( void )\r
136 {\r
137         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
138         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
139         internally by FreeRTOS API functions that create tasks, queues, software\r
140         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
141         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
142 \r
143         /* Force an assert. */\r
144         configASSERT( ( volatile void * ) NULL );\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
149 {\r
150         ( void ) pcTaskName;\r
151         ( void ) pxTask;\r
152 \r
153         /* Run time stack overflow checking is performed if\r
154         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
155         function is called if a stack overflow is detected. */\r
156 \r
157         /* Force an assert. */\r
158         configASSERT( ( volatile void * ) NULL );\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vApplicationIdleHook( void )\r
163 {\r
164 volatile size_t xFreeHeapSpace;\r
165 \r
166         /* This is just a trivial example of an idle hook.  It is called on each\r
167         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
168         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
169         memory management section on the http://www.FreeRTOS.org web site for memory\r
170         management options.  If there is a lot of heap memory free then the\r
171         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
172         RAM. */\r
173         xFreeHeapSpace = xPortGetFreeHeapSize();\r
174 \r
175         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
176         ( void ) xFreeHeapSpace;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
181 {\r
182 volatile unsigned long ul = 0;\r
183 \r
184         ( void ) pcFile;\r
185         ( void ) ulLine;\r
186 \r
187         taskENTER_CRITICAL();\r
188         {\r
189                 /* Set ul to a non-zero value using the debugger to step out of this\r
190                 function. */\r
191                 while( ul == 0 )\r
192                 {\r
193                         portNOP();\r
194                 }\r
195         }\r
196         taskEXIT_CRITICAL();\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vApplicationTickHook( void )\r
201 {\r
202         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0\r
203         {\r
204                 /* The full demo includes a software timer demo/test that requires\r
205                 prodding periodically from the tick interrupt. */\r
206                 vTimerPeriodicISRTests();\r
207 \r
208                 /* Call the periodic queue overwrite from ISR demo. */\r
209                 vQueueOverwritePeriodicISRDemo();\r
210 \r
211                 /* Call the periodic event group from ISR demo. */\r
212                 vPeriodicEventGroupsProcessing();\r
213         }\r
214         #endif\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /* The function called by the RTOS port layer after it has managed interrupt\r
219 entry. */\r
220 void vApplicationIRQHandler( void )\r
221 {\r
222 typedef void (*ISRFunction_t)( void );\r
223 ISRFunction_t pxISRFunction;\r
224 volatile uint32_t * pulAIC_IVR = ( uint32_t * ) configINTERRUPT_VECTOR_ADDRESS;\r
225 \r
226         /* Obtain the address of the interrupt handler from the AIR. */\r
227         pxISRFunction = ( ISRFunction_t ) *pulAIC_IVR;\r
228 \r
229         /* Write back to the SAMA5's interrupt controller's IVR register in case the\r
230         CPU is in protect mode.  If the interrupt controller is not in protect mode\r
231         then this write is not necessary. */\r
232         *pulAIC_IVR = ( uint32_t ) pxISRFunction;\r
233 \r
234         /* Ensure the write takes before re-enabling interrupts. */\r
235         __DSB();\r
236         __ISB();\r
237     __enable_irq();\r
238 \r
239         /* Call the installed ISR. */\r
240         pxISRFunction();\r
241 }\r
242 \r
243 \r