]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_ATSAM4E_Atmel_Studio/src/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_ATSAM4E_Atmel_Studio / src / 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 application that includes FreeRTOS+CLI, FreeRTOS+UDP\r
32  * and FreeRTOS+FAT SL.  The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined\r
33  * in this file) is used to select between the two.  The simply blinky demo is\r
34  * implemented and described in main_blinky.c.  The more comprehensive demo\r
35  * application is implemented and described in main_full.c and full user\r
36  * instructions are provided on the following URL:\r
37  * http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html\r
38  *\r
39  * This file implements the code that is not demo specific, including the\r
40  * hardware setup and FreeRTOS hook functions.\r
41  *\r
42  */\r
43 \r
44 /* Kernel includes. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 \r
48 /* Demo application includes. */\r
49 #include "partest.h"\r
50 \r
51 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
52 or 0 to run the more comprehensive demo application that includes add-on\r
53 components. */\r
54 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /*\r
59  * Set up the hardware ready to run this demo.\r
60  */\r
61 static void prvSetupHardware( void );\r
62 \r
63 /*\r
64  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
65  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
66  */\r
67 extern void main_blinky( void );\r
68 extern void main_full( void );\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 int main( void )\r
73 {\r
74         /* Prepare the hardware to run this demo. */\r
75         prvSetupHardware();\r
76 \r
77         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
78         of this file. */\r
79         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
80         {\r
81                 main_blinky();\r
82         }\r
83         #else\r
84         {\r
85                 /* Full user instructions are provided on the following URL:\r
86                 http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html */\r
87                 main_full();\r
88         }\r
89         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
90 \r
91         return 0;\r
92 }\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 static void prvSetupHardware( void )\r
96 {\r
97         /* Call the ASF initialisation functions. */\r
98         board_init();\r
99         sysclk_init();\r
100         pmc_enable_periph_clk( ID_GMAC );\r
101         pmc_enable_periph_clk( ID_SMC );\r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void vApplicationMallocFailedHook( void )\r
106 {\r
107 static volatile uint32_t ulCount = 0;\r
108 \r
109         /* vApplicationMallocFailedHook() will only be called if\r
110         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
111         function that will get called if a call to pvPortMalloc() fails.\r
112         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
113         timer or semaphore is created.  It is also called by various parts of the\r
114         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
115         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
116         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
117         to query the size of free heap space that remains (although it does not\r
118         provide information on how the remaining heap might be fragmented). \r
119         \r
120         Just count the number of malloc fails as some failures may occur simply\r
121         because the network load is very high, resulting in the consumption of a\r
122         lot of network buffers. */\r
123         ulCount++;      \r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vApplicationIdleHook( void )\r
128 {\r
129         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
130         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
131         task.  It is essential that code added to this hook function never attempts\r
132         to block in any way (for example, call xQueueReceive() with a block time\r
133         specified, or call vTaskDelay()).  If the application makes use of the\r
134         vTaskDelete() API function (as this demo application does) then it is also\r
135         important that vApplicationIdleHook() is permitted to return to its calling\r
136         function, because it is the responsibility of the idle task to clean up\r
137         memory allocated by the kernel to any task that has since been deleted. */\r
138 \r
139         /* The simple blinky demo does not use the idle hook - the full demo does. */\r
140         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
141         {\r
142                 extern void vFullDemoIdleHook( void );\r
143 \r
144                 /* Implemented in main_full.c. */\r
145                 vFullDemoIdleHook();\r
146         }\r
147         #endif\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
152 {\r
153         ( void ) pcTaskName;\r
154         ( void ) pxTask;\r
155 \r
156         /* Run time stack overflow checking is performed if\r
157         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook function is\r
158         called if a stack overflow is detected. */\r
159         vAssertCalled( __LINE__, __FILE__ );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vApplicationTickHook( void )\r
164 {\r
165         /* This function will be called by each tick interrupt if\r
166         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
167         added here, but the tick hook is called from an interrupt context, so\r
168         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
169         functions can be used (those that end in FromISR()). */\r
170 \r
171         /* The simple blinky demo does not use the tick hook - the full demo does. */\r
172         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
173         {\r
174                 extern void vFullDemoTickHook( void );\r
175 \r
176                 /* Implemented in main_full.c. */\r
177                 vFullDemoTickHook();\r
178         }\r
179         #endif\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vAssertCalled( uint32_t ulLine, const char *pcFile )\r
184 {\r
185 /* The following two variables are just to ensure the parameters are not\r
186 optimised away and therefore unavailable when viewed in the debugger. */\r
187 volatile uint32_t ulLineNumber = ulLine, ulSetNonZeroInDebuggerToReturn = 0;\r
188 volatile const char * const pcFileName = pcFile;\r
189 \r
190         taskENTER_CRITICAL();\r
191         while( ulSetNonZeroInDebuggerToReturn == 0 )\r
192         {\r
193                 /* If you want to set out of this function in the debugger to see the\r
194                 assert() location then set ulSetNonZeroInDebuggerToReturn to a non-zero\r
195                 value. */\r
196         }\r
197         taskEXIT_CRITICAL();\r
198 \r
199         ( void ) pcFileName;\r
200         ( void ) ulLineNumber;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /* Provided to keep the linker happy. */\r
205 void _exit_( int status )\r
206 {\r
207         ( void ) status;\r
208         vAssertCalled( __LINE__, __FILE__ );\r
209         for( ;; );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* Provided to keep the linker happy. */\r
214 int _read( void )\r
215 {\r
216         return 0;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /* Provided to keep the linker happy. */\r
221 int _write( int x )\r
222 {\r
223         ( void ) x;\r
224         return 0;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 \r
229 \r