]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_LPC51U68_GCC_IAR_KEIL/app/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_M0+_LPC51U68_GCC_IAR_KEIL / app / main.c
1 /*\r
2  * Copyright 2016-2019 NXP\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without modification,\r
6  * are permitted provided that the following conditions are met:\r
7  *\r
8  * o Redistributions of source code must retain the above copyright notice, this list\r
9  *   of conditions and the following disclaimer.\r
10  *\r
11  * o Redistributions in binary form must reproduce the above copyright notice, this\r
12  *   list of conditions and the following disclaimer in the documentation and/or\r
13  *   other materials provided with the distribution.\r
14  *\r
15  * o Neither the name of NXP Semiconductor, Inc. nor the names of its\r
16  *   contributors may be used to endorse or promote products derived from this\r
17  *   software without specific prior written permission.\r
18  *\r
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\r
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\r
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29  */\r
30  \r
31 /**\r
32  * @file    main.c\r
33  * @brief   Application entry point.\r
34  */\r
35 #include <stdio.h>\r
36 \r
37 /* Board specific includes. */\r
38 #include "board.h"\r
39 #include "peripherals.h"\r
40 #include "pin_mux.h"\r
41 #include "clock_config.h"\r
42 #include "LPC51U68.h"\r
43 #include "fsl_debug_console.h"\r
44 \r
45 /* FreeRTOS includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 #include "compiler_attributes.h"\r
50 \r
51 /*-----------------------------------------------------------*/\r
52 typedef enum LED_STATE {\r
53         LED_RED_BLINK_ON = 1,\r
54         LED_RED_BLINK_OFF,\r
55         LED_GREEN_BLINK_ON,\r
56         LED_GREEN_BLINK_OFF,\r
57         LED_BLUE_BLINK_ON,\r
58         LED_BLUE_BLINK_OFF,\r
59 } E_LED_STATE;\r
60 \r
61 /* Static variable to keep track of LED color.\r
62  * red -> green -> blue -> red -> ...\r
63  * This variable is not intended for multi-threaded application.\r
64  */\r
65 static E_LED_STATE eLedState = LED_RED_BLINK_ON;\r
66 \r
67 /* Show iteration number in UART.\r
68  * This variable is not intended for multi-threaded application.\r
69  */\r
70 static int i = 0;\r
71 \r
72 /* Track how many times tick interrupt has occurred. */\r
73 static unsigned int uTickInterruptCounter = 0;\r
74 \r
75 /*\r
76  * Perform any application specific hardware configuration.  The clocks,\r
77  * memory, etc. are configured before main() is called.\r
78  */\r
79 static void prvSetupHardware( void );\r
80 \r
81 /**\r
82  * Heap_5 is being used because the RAM is not contiguous, therefore the heap\r
83  * needs to be initialized.  See http://www.freertos.org/a00111.html\r
84  */\r
85 static void prvInitializeHeap( void );\r
86 \r
87 /*\r
88  * The hardware only has a single LED.  Simply toggle it.\r
89  */\r
90 void vMainToggleLED( void );\r
91 \r
92 /* main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
93 main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0. */\r
94 void main_blinky( void );\r
95 void main_full( void );\r
96 \r
97 /*\r
98  * @brief   Application entry point.\r
99  */\r
100 int main(void)\r
101 {\r
102 \r
103         /* Prepare the hardware to run this demo. */\r
104         prvSetupHardware();\r
105 \r
106         /* Initialize heap regions. */\r
107         prvInitializeHeap();\r
108 \r
109         /* Show something on UART.\r
110         Serial port setup as baudrate: 115200, data: 8-bit, parity: none, stop bits: 1, flow control: none.\r
111         sTerminal setup as receive: auto, transmit: CR+LF.*/\r
112         PRINTF("FreeRTOS demo.\r\n");\r
113 \r
114         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
115         of this file. */\r
116         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
117         {\r
118                 main_blinky();\r
119         }\r
120         #else\r
121         {\r
122                 main_full();\r
123         }\r
124         #endif\r
125 \r
126         return 0;\r
127 }\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 void vMainToggleLED( void )\r
131 {\r
132         switch (eLedState)\r
133         {\r
134                 case LED_RED_BLINK_ON:\r
135                         PRINTF("Iteration %d -- tick interrupt count %d.\r\n", i, uTickInterruptCounter);\r
136                         i++;\r
137 \r
138                         LED_RED_ON();\r
139                         eLedState = LED_RED_BLINK_OFF;\r
140                         break;\r
141                 case LED_RED_BLINK_OFF:\r
142                         LED_RED_OFF();\r
143                         eLedState = LED_GREEN_BLINK_ON;\r
144                         break;\r
145                 case LED_GREEN_BLINK_ON:\r
146                         LED_GREEN_ON();\r
147                         eLedState = LED_GREEN_BLINK_OFF;\r
148                         break;\r
149                 case LED_GREEN_BLINK_OFF:\r
150                         LED_GREEN_OFF();\r
151                         eLedState = LED_BLUE_BLINK_ON;\r
152                         break;\r
153                 case LED_BLUE_BLINK_ON:\r
154                         LED_BLUE_ON();\r
155                         eLedState = LED_BLUE_BLINK_OFF;\r
156                         break;\r
157                 case LED_BLUE_BLINK_OFF:\r
158                         LED_BLUE_OFF();\r
159                         eLedState = LED_RED_BLINK_ON;\r
160                         break;\r
161                 default:\r
162                         /* Unexpected state. Let's reset to default color. */\r
163                         eLedState = LED_RED_BLINK_ON;\r
164         }\r
165 \r
166         return;\r
167 }\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static void prvSetupHardware( void )\r
172 {\r
173         /* Initialize board hardware. */\r
174         BOARD_InitBootPins();\r
175         BOARD_InitBootClocks();\r
176         BOARD_InitBootPeripherals();\r
177 \r
178         /* Enable clock for GPIO. */\r
179         CLOCK_EnableClock(kCLOCK_Gpio0);\r
180         CLOCK_EnableClock(kCLOCK_Gpio1);\r
181 \r
182         /* Initialize FSL debug console. */\r
183         BOARD_InitDebugConsole();\r
184 \r
185         /* Initialize tri-color LED. */\r
186         LED_RED_INIT(LOGIC_LED_OFF);\r
187         LED_GREEN_INIT(LOGIC_LED_OFF);\r
188         LED_BLUE_INIT(LOGIC_LED_OFF);\r
189 \r
190         return;\r
191 }\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvInitializeHeap( void )\r
196 {\r
197         /* Place the first block of the heap memory in the first bank of RAM. */\r
198         static uint8_t ucHeap1[ configTOTAL_HEAP_SIZE ];\r
199 \r
200         /* Place the second block of the heap memory in the second bank of RAM. */\r
201         static uint8_t ucHeap2[ 16 * 1024 ] COMPILER_ATTRIBUTE_PLACE_IN_2ND_MEMORY_BANK;\r
202         \r
203         /* Memory regions are defined in address order, and terminate with NULL. */\r
204         static HeapRegion_t xHeapRegions[] =\r
205         {\r
206                 { ( unsigned char * ) ucHeap1, sizeof( ucHeap1 ) },\r
207                 { ( unsigned char * ) ucHeap2, sizeof( ucHeap2 ) },\r
208                 { NULL,                        0                 }\r
209         };\r
210 \r
211         vPortDefineHeapRegions( xHeapRegions );\r
212 \r
213         return;\r
214 }\r
215 \r
216 /*-----------------------------------------------------------*/\r
217 \r
218 void vApplicationMallocFailedHook( void )\r
219 {\r
220         /* vApplicationMallocFailedHook() will only be called if\r
221         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
222         function that will get called if a call to pvPortMalloc() fails.\r
223         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
224         timer or semaphore is created.  It is also called by various parts of the\r
225         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
226         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
227         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
228         to query the size of free heap space that remains (although it does not\r
229         provide information on how the remaining heap might be fragmented). */\r
230         taskDISABLE_INTERRUPTS();\r
231         for( ;; );\r
232 }\r
233 \r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vApplicationIdleHook( void )\r
237 {\r
238         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
239         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
240         task.  It is essential that code added to this hook function never attempts\r
241         to block in any way (for example, call xQueueReceive() with a block time\r
242         specified, or call vTaskDelay()).  If the application makes use of the\r
243         vTaskDelete() API function (as this demo application does) then it is also\r
244         important that vApplicationIdleHook() is permitted to return to its calling\r
245         function, because it is the responsibility of the idle task to clean up\r
246         memory allocated by the kernel to any task that has since been deleted. */\r
247 }\r
248 \r
249 /*-----------------------------------------------------------*/\r
250 \r
251 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
252 {\r
253         ( void ) pcTaskName;\r
254         ( void ) pxTask;\r
255 \r
256         /* Run time stack overflow checking is performed if\r
257         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
258         function is called if a stack overflow is detected. */\r
259         taskDISABLE_INTERRUPTS();\r
260         for( ;; );\r
261 }\r
262 \r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vApplicationTickHook( void )\r
266 {\r
267 #if mainCHECK_INTERRUPT_STACK == 1\r
268 extern unsigned long _pvHeapStart[];\r
269 \r
270         /* This function will be called by each tick interrupt if\r
271         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
272         added here, but the tick hook is called from an interrupt context, so\r
273         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
274         functions can be used (those that end in FromISR()). */\r
275 \r
276         /* Manually check the last few bytes of the interrupt stack to check they\r
277         have not been overwritten.  Note - the task stacks are automatically\r
278         checked for overflow if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2\r
279         in FreeRTOSConifg.h, but the interrupt stack is not. */\r
280         configASSERT( memcmp( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) ) == 0U );\r
281 #endif /* mainCHECK_INTERRUPT_STACK */\r
282 \r
283         uTickInterruptCounter++;\r
284 }\r
285 \r
286 \r