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