]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/main.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4L_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 low power project that\r
31  * demonstrates the FreeRTOS tickless mode, and a more comprehensive test and\r
32  * demo application.  The configCREATE_LOW_POWER_DEMO setting (defined at the\r
33  * top of FreeRTOSConfig.h) is used to select between the two.  The low power\r
34  * demo is implemented and described in main_low_power.c.  The more\r
35  * comprehensive test and demo application is implemented and described in\r
36  * main_full.c.\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 /* Standard includes. */\r
43 #include <stdio.h>\r
44 \r
45 /* Kernel includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Standard demo includes - just needed for the LED (ParTest) initialisation\r
50 function. */\r
51 #include "partest.h"\r
52 \r
53 /* Atmel library includes. */\r
54 #include <asf.h>\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_low_power() is used when configCREATE_LOW_POWER_DEMO is set to 1.\r
65  * main_full() is used when configCREATE_LOW_POWER_DEMO is set to 0.\r
66  * configCREATE_LOW_POWER_DEMO is defined at the top of main.c.\r
67  */\r
68 extern void main_low_power( void );\r
69 extern void main_full( void );\r
70 \r
71 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
72 within this file. */\r
73 void vApplicationMallocFailedHook( void );\r
74 void vApplicationIdleHook( void );\r
75 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
76 void vApplicationTickHook( void );\r
77 \r
78 /* A handler for a button interrupt.  The button's only purpose is to bring the\r
79 CPU out of sleep mode early. */\r
80 static void prvButtonISR( void );\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* See the documentation page for this demo on the FreeRTOS.org web site for\r
85 full information - including hardware setup requirements. */\r
86 int main( void )\r
87 {\r
88         /* Prepare the hardware to run this demo. */\r
89         prvSetupHardware();\r
90 \r
91         /* The configCREATE_LOW_POWER_DEMO setting is described at the top of\r
92         this file. */\r
93         #if( configCREATE_LOW_POWER_DEMO == 1 )\r
94         {\r
95                 main_low_power();\r
96         }\r
97         #else\r
98         {\r
99                 main_full();\r
100         }\r
101         #endif\r
102 \r
103         return 0;\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 static void prvButtonISR( void )\r
108 {\r
109         /* The button doesn't do anything other than providing a means for brining\r
110         the MCU out of sleep mode early. */\r
111         if( eic_line_interrupt_is_pending( EIC, GPIO_PUSH_BUTTON_EIC_LINE ) ) \r
112         {\r
113                 eic_line_clear_interrupt( EIC, GPIO_PUSH_BUTTON_EIC_LINE );\r
114         }               \r
115 }\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 static void prvSetupHardware( void )\r
119 {\r
120 extern void SystemCoreClockUpdate( void );\r
121 struct eic_line_config xEICLineConfiguration;\r
122 \r
123         /* Configure the external interrupt controller so button pushes can\r
124         generate interrupts. */\r
125         xEICLineConfiguration.eic_mode = EIC_MODE_EDGE_TRIGGERED;\r
126         xEICLineConfiguration.eic_edge = EIC_EDGE_FALLING_EDGE;\r
127         xEICLineConfiguration.eic_level = EIC_LEVEL_LOW_LEVEL;\r
128         xEICLineConfiguration.eic_filter = EIC_FILTER_DISABLED;\r
129         xEICLineConfiguration.eic_async = EIC_ASYNCH_MODE;\r
130         eic_enable( EIC );\r
131         eic_line_set_config( EIC, GPIO_PUSH_BUTTON_EIC_LINE, &xEICLineConfiguration );\r
132         eic_line_set_callback( EIC, GPIO_PUSH_BUTTON_EIC_LINE, prvButtonISR, EIC_5_IRQn, 0 );\r
133         eic_line_enable( EIC, GPIO_PUSH_BUTTON_EIC_LINE );\r
134 \r
135         /* ASF function to setup clocking. */\r
136         sysclk_init();\r
137 \r
138         /* Ensure all priority bits are assigned as preemption priority bits. */\r
139         NVIC_SetPriorityGrouping( 0 );\r
140 \r
141         /* Atmel library function to setup for the evaluation kit being used. */\r
142         board_init();\r
143 \r
144         /* Initialise the sleep manager in case the low power demo is being used. */\r
145         sleepmgr_init();\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vApplicationMallocFailedHook( void )\r
150 {\r
151         /* vApplicationMallocFailedHook() will only be called if\r
152         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
153         function that will get called if a call to pvPortMalloc() fails.\r
154         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
155         timer or semaphore is created.  It is also called by various parts of the\r
156         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
157         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
158         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
159         to query the size of free heap space that remains (although it does not\r
160         provide information on how the remaining heap might be fragmented). */\r
161         taskDISABLE_INTERRUPTS();\r
162         for( ;; );\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vApplicationIdleHook( void )\r
167 {\r
168         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
169         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
170         task.  It is essential that code added to this hook function never attempts\r
171         to block in any way (for example, call xQueueReceive() with a block time\r
172         specified, or call vTaskDelay()).  If the application makes use of the\r
173         vTaskDelete() API function (as this demo application does) then it is also\r
174         important that vApplicationIdleHook() is permitted to return to its calling\r
175         function, because it is the responsibility of the idle task to clean up\r
176         memory allocated by the kernel to any task that has since been deleted. */\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
181 {\r
182         ( void ) pcTaskName;\r
183         ( void ) pxTask;\r
184 \r
185         /* Run time stack overflow checking is performed if\r
186         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
187         function is called if a stack overflow is detected. */\r
188         taskDISABLE_INTERRUPTS();\r
189         for( ;; );\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 void vApplicationTickHook( void )\r
194 {\r
195         /* This function will be called by each tick interrupt if\r
196         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
197         added here, but the tick hook is called from an interrupt context, so\r
198         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
199         functions can be used (those that end in FromISR()). */\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 void vAssertCalled( void )\r
204 {\r
205 volatile unsigned long ul = 0;\r
206 \r
207         taskENTER_CRITICAL();\r
208         {\r
209                 /* Set ul to a non-zero value using the debugger to step out of this\r
210                 function. */\r
211                 while( ul == 0 )\r
212                 {\r
213                         __asm volatile( "NOP" );\r
214                 }\r
215         }\r
216         taskEXIT_CRITICAL();\r
217 }\r