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