]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * This project provides two demo applications.  A simple blinky style project,\r
31  * and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
33  * select between the two.  The simply blinky demo is implemented and described\r
34  * in main_blinky.c.  The more comprehensive test and demo application is\r
35  * implemented and described in main_full.c.\r
36  *\r
37  * This file implements the code that is not demo specific, including the\r
38  * hardware setup, standard FreeRTOS hook functions, and the ISR hander called\r
39  * by the RTOS after interrupt entry (including nesting) has been taken care of.\r
40  *\r
41  * NOTE on LEDS:\r
42  *\r
43  *     This demo is NOT configured to use the LED built onto the SAMA6D2\r
44  *     XPLained board!\r
45  *\r
46  *     The LED driver PIN_LED definitions have been altered in\r
47  *     board_sama5d2-xplained.h to remap them to GPIOs terminating on pins 30,\r
48  *     32 and 34 of J17. (This change is conditional on the preprocessor\r
49  *     #define "LEDS_ON_J17".) These GPIOs are configured to be "high drive"\r
50  *     push-pull outputs; they can source up to 18mA at 1.8v. Low\r
51  *     forward-voltage LEDs may be connected via 100 ohm resistors to pins\r
52  *     30, 32 and 34 with their cathodes to pin 35/36 (GND).\r
53  *\r
54  */\r
55 \r
56 /*\r
57  * Procedure to download and execute code from RAM\r
58  * -----------------------------------------------\r
59  *\r
60  * 1. Close jumper JP9(BOOT_DIS) and JP2(DEBUG_DIS).\r
61  * 2. Open jumper JP1(EDBG_DIS).\r
62  * 3. Power on the board by USB connection on J14(EDBG_JTAG).\r
63  * 4. Open \93EDBG Virtual COM Port\94 with setting \9357600,8,N,1\94.\r
64  * 5. Type \93#\94 on HyperTerminal and get \93>\94 as a reply.\r
65  * 6. Don\92t reset the board during debugging. For IAR, set the Debugger to CMSIS\r
66  *    DAP, with the "Disabled(no reset)" option.\r
67  */\r
68 \r
69 /* Scheduler include files. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "semphr.h"\r
73 \r
74 /* Standard demo includes. */\r
75 #include "partest.h"\r
76 #include "TimerDemo.h"\r
77 #include "QueueOverwrite.h"\r
78 #include "EventGroupsDemo.h"\r
79 \r
80 /* Library includes. */\r
81 #include "board_sama5d2-xplained.h"\r
82 #include "peripherals/wdt.h"\r
83 #include "peripherals/pio.h"\r
84 #include "chip.h"\r
85 \r
86 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
87 or 0 to run the more comprehensive test and demo application. */\r
88 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /*\r
93  * Configure the hardware as necessary to run this demo.\r
94  */\r
95 static void prvSetupHardware( void );\r
96 \r
97 /*\r
98  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
99  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
100  */\r
101 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
102         extern void main_blinky( void );\r
103 #else\r
104         extern void main_full( void );\r
105 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
106 \r
107 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
108 within this file. */\r
109 void vApplicationMallocFailedHook( void );\r
110 void vApplicationIdleHook( void );\r
111 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
112 void vApplicationTickHook( void );\r
113 \r
114 /* Prototype for the IRQ handler called by the generic Cortex-A5 RTOS port\r
115 layer. */\r
116 void vApplicationIRQHandler( void );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 int main( void )\r
121 {\r
122         /* Configure the hardware ready to run the demo. */\r
123         prvSetupHardware();\r
124 \r
125         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
126         of this file. */\r
127         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
128         {\r
129                 main_blinky();\r
130         }\r
131         #else\r
132         {\r
133                 main_full();\r
134         }\r
135         #endif\r
136 \r
137         return 0;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void prvSetupHardware( void )\r
142 {\r
143         /* Disable watchdog */\r
144         wdt_disable( );\r
145 \r
146         /* Set protect mode in the AIC for easier debugging. */\r
147         AIC->AIC_DCR |= AIC_DCR_PROT;\r
148 \r
149         /* Configure ports used by LEDs. */\r
150         vParTestInitialise();\r
151 \r
152         #if defined (ddram)\r
153                 MMU_Initialize( ( uint32_t * ) 0x30C000 );\r
154                 CP15_EnableMMU();\r
155                 CP15_EnableDcache();\r
156                 CP15_EnableIcache();\r
157         #endif\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 void vApplicationMallocFailedHook( void )\r
162 {\r
163         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
164         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
165         internally by FreeRTOS API functions that create tasks, queues, software\r
166         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
167         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
168 \r
169         /* Force an assert. */\r
170         configASSERT( ( volatile void * ) NULL );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
175 {\r
176         ( void ) pcTaskName;\r
177         ( void ) pxTask;\r
178 \r
179         /* Run time stack overflow checking is performed if\r
180         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
181         function is called if a stack overflow is detected. */\r
182 \r
183         /* Force an assert. */\r
184         configASSERT( ( volatile void * ) NULL );\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vApplicationIdleHook( void )\r
189 {\r
190 volatile size_t xFreeHeapSpace;\r
191 \r
192         /* This is just a trivial example of an idle hook.  It is called on each\r
193         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
194         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
195         memory management section on the http://www.FreeRTOS.org web site for memory\r
196         management options.  If there is a lot of heap memory free then the\r
197         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
198         RAM. */\r
199         xFreeHeapSpace = xPortGetFreeHeapSize();\r
200 \r
201         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
202         ( void ) xFreeHeapSpace;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
207 {\r
208 volatile unsigned long ul = 0;\r
209 \r
210         ( void ) pcFile;\r
211         ( void ) ulLine;\r
212 \r
213         taskENTER_CRITICAL();\r
214         {\r
215                 /* Set ul to a non-zero value using the debugger to step out of this\r
216                 function. */\r
217                 while( ul == 0 )\r
218                 {\r
219                         portNOP();\r
220                 }\r
221         }\r
222         taskEXIT_CRITICAL();\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void vApplicationTickHook( void )\r
227 {\r
228         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0\r
229         {\r
230                 /* The full demo includes a software timer demo/test that requires\r
231                 prodding periodically from the tick interrupt. */\r
232                 vTimerPeriodicISRTests();\r
233 \r
234                 /* Call the periodic queue overwrite from ISR demo. */\r
235                 vQueueOverwritePeriodicISRDemo();\r
236 \r
237                 /* Call the periodic event group from ISR demo. */\r
238                 vPeriodicEventGroupsProcessing();\r
239         }\r
240         #endif\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 /* The function called by the RTOS port layer after it has managed interrupt\r
245 entry. */\r
246 void vApplicationIRQHandler( void )\r
247 {\r
248 typedef void (*ISRFunction_t)( void );\r
249 ISRFunction_t pxISRFunction;\r
250 volatile uint32_t * pulAIC_IVR = ( uint32_t * ) configINTERRUPT_VECTOR_ADDRESS;\r
251 \r
252         /* Obtain the address of the interrupt handler from the AIR. */\r
253         pxISRFunction = ( ISRFunction_t ) *pulAIC_IVR;\r
254 \r
255         /* Write back to the SAMA5's interrupt controller's IVR register in case the\r
256         CPU is in protect mode.  If the interrupt controller is not in protect mode\r
257         then this write is not necessary. */\r
258         *pulAIC_IVR = ( uint32_t ) pxISRFunction;\r
259 \r
260         /* Ensure the write takes before re-enabling interrupts. */\r
261         __DSB();\r
262         __ISB();\r
263         __enable_irq();\r
264 \r
265         /* Call the installed ISR. */\r
266         pxISRFunction();\r
267 }\r
268 \r
269 /* Keep the linker quiet. */\r
270 size_t __write(int, const unsigned char *, size_t);\r
271 size_t __write(int f, const unsigned char *p, size_t s)\r
272 {\r
273   (void) f;\r
274   (void) p;\r
275   (void) s;\r
276   return 0;\r
277 }\r
278 \r
279 \r
280 \r