]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_LPC51U68_LPCXpresso/app/main.c
Remove build files accidentally checked in.
[freertos] / FreeRTOS / Demo / CORTEX_M0+_LPC51U68_LPCXpresso / 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 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to\r
52  * 0 -- to run the more comprehensive test and demo application,\r
53  * 1 -- to run the simple blinky demo.\r
54  */\r
55 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 typedef enum LED_STATE {\r
59         LED_RED_BLINK_ON = 1,\r
60         LED_RED_BLINK_OFF,\r
61         LED_GREEN_BLINK_ON,\r
62         LED_GREEN_BLINK_OFF,\r
63         LED_BLUE_BLINK_ON,\r
64         LED_BLUE_BLINK_OFF,\r
65 } E_LED_STATE;\r
66 \r
67 /* Static variable to keep track of LED color.\r
68  * red -> green -> blue -> red -> ...\r
69  * This variable is not intended for multi-threaded application.\r
70  */\r
71 static E_LED_STATE eLedState = LED_RED_BLINK_ON;\r
72 \r
73 /* Show iteration number in UART.\r
74  * This variable is not intended for multi-threaded application.\r
75  */\r
76 static int i = 0;\r
77 \r
78 /* Track how many times tick interrupt has occurred. */\r
79 static unsigned int uTickInterruptCounter = 0;\r
80 \r
81 /*\r
82  * Perform any application specific hardware configuration.  The clocks,\r
83  * memory, etc. are configured before main() is called.\r
84  */\r
85 static void prvSetupHardware( void );\r
86 \r
87 /**\r
88  * Heap_5 is being used because the RAM is not contiguous, therefore the heap\r
89  * needs to be initialized.  See http://www.freertos.org/a00111.html\r
90  */\r
91 static void prvInitializeHeap( void );\r
92 \r
93 /*\r
94  * The hardware only has a single LED.  Simply toggle it.\r
95  */\r
96 void vMainToggleLED( void );\r
97 \r
98 /* main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
99 main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0. */\r
100 void main_blinky( void );\r
101 void main_full( void );\r
102 \r
103 /*\r
104  * @brief   Application entry point.\r
105  */\r
106 int main(void)\r
107 {\r
108 \r
109         /* Prepare the hardware to run this demo. */\r
110         prvSetupHardware();\r
111 \r
112         /* Initialize heap regions. */\r
113         prvInitializeHeap();\r
114 \r
115         /* Show something on UART.\r
116         Serial port setup as baudrate: 115200, data: 8-bit, parity: none, stop bits: 1, flow control: none.\r
117         sTerminal setup as receive: auto, transmit: CR+LF.*/\r
118         PRINTF("FreeRTOS demo.\r\n");\r
119 \r
120         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
121         of this file. */\r
122         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
123         {\r
124                 main_blinky();\r
125         }\r
126         #else\r
127         {\r
128                 main_full();\r
129         }\r
130         #endif\r
131 \r
132         return 0;\r
133 }\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 void vMainToggleLED( void )\r
137 {\r
138         switch (eLedState)\r
139         {\r
140                 case LED_RED_BLINK_ON:\r
141                         PRINTF("Iteration %d -- tick interrupt count %d.\r\n", i, uTickInterruptCounter);\r
142                         i++;\r
143 \r
144                         LED_RED_ON();\r
145                         eLedState = LED_RED_BLINK_OFF;\r
146                         break;\r
147                 case LED_RED_BLINK_OFF:\r
148                         LED_RED_OFF();\r
149                         eLedState = LED_GREEN_BLINK_ON;\r
150                         break;\r
151                 case LED_GREEN_BLINK_ON:\r
152                         LED_GREEN_ON();\r
153                         eLedState = LED_GREEN_BLINK_OFF;\r
154                         break;\r
155                 case LED_GREEN_BLINK_OFF:\r
156                         LED_GREEN_OFF();\r
157                         eLedState = LED_BLUE_BLINK_ON;\r
158                         break;\r
159                 case LED_BLUE_BLINK_ON:\r
160                         LED_BLUE_ON();\r
161                         eLedState = LED_BLUE_BLINK_OFF;\r
162                         break;\r
163                 case LED_BLUE_BLINK_OFF:\r
164                         LED_BLUE_OFF();\r
165                         eLedState = LED_RED_BLINK_ON;\r
166                         break;\r
167                 default:\r
168                         /* Unexpected state. Let's reset to default color. */\r
169                         eLedState = LED_RED_BLINK_ON;\r
170         }\r
171 \r
172         return;\r
173 }\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void prvSetupHardware( void )\r
178 {\r
179         /* Initialize board hardware. */\r
180         BOARD_InitBootPins();\r
181         BOARD_InitBootClocks();\r
182         BOARD_InitBootPeripherals();\r
183 \r
184         /* Enable clock for GPIO. */\r
185         CLOCK_EnableClock(kCLOCK_Gpio0);\r
186         CLOCK_EnableClock(kCLOCK_Gpio1);\r
187 \r
188         /* Initialize FSL debug console. */\r
189         BOARD_InitDebugConsole();\r
190 \r
191         /* Initialize tri-color LED. */\r
192         LED_RED_INIT(LOGIC_LED_OFF);\r
193         LED_GREEN_INIT(LOGIC_LED_OFF);\r
194         LED_BLUE_INIT(LOGIC_LED_OFF);\r
195 \r
196         return;\r
197 }\r
198 \r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvInitializeHeap( void )\r
202 {\r
203         /* Place the first block of the heap memory in the first bank of RAM. */\r
204         static uint8_t ucHeap1[ configTOTAL_HEAP_SIZE ];\r
205 \r
206         /* Place the second block of the heap memory in the second bank of RAM. */\r
207         static uint8_t ucHeap2[ 16 * 1024 ] COMPILER_ATTRIBUTE_PLACE_IN_2ND_MEMORY_BANK;\r
208         \r
209         /* Memory regions are defined in address order, and terminate with NULL. */\r
210         static HeapRegion_t xHeapRegions[] =\r
211         {\r
212                 { ( unsigned char * ) ucHeap1, sizeof( ucHeap1 ) },\r
213                 { ( unsigned char * ) ucHeap2, sizeof( ucHeap2 ) },\r
214                 { NULL,                        0                 }\r
215         };\r
216 \r
217         vPortDefineHeapRegions( xHeapRegions );\r
218 \r
219         return;\r
220 }\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vApplicationMallocFailedHook( void )\r
225 {\r
226         /* vApplicationMallocFailedHook() will only be called if\r
227         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
228         function that will get called if a call to pvPortMalloc() fails.\r
229         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
230         timer or semaphore is created.  It is also called by various parts of the\r
231         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
232         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
233         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
234         to query the size of free heap space that remains (although it does not\r
235         provide information on how the remaining heap might be fragmented). */\r
236         taskDISABLE_INTERRUPTS();\r
237         for( ;; );\r
238 }\r
239 \r
240 /*-----------------------------------------------------------*/\r
241 \r
242 void vApplicationIdleHook( void )\r
243 {\r
244         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
245         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
246         task.  It is essential that code added to this hook function never attempts\r
247         to block in any way (for example, call xQueueReceive() with a block time\r
248         specified, or call vTaskDelay()).  If the application makes use of the\r
249         vTaskDelete() API function (as this demo application does) then it is also\r
250         important that vApplicationIdleHook() is permitted to return to its calling\r
251         function, because it is the responsibility of the idle task to clean up\r
252         memory allocated by the kernel to any task that has since been deleted. */\r
253 }\r
254 \r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
258 {\r
259         ( void ) pcTaskName;\r
260         ( void ) pxTask;\r
261 \r
262         /* Run time stack overflow checking is performed if\r
263         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
264         function is called if a stack overflow is detected. */\r
265         taskDISABLE_INTERRUPTS();\r
266         for( ;; );\r
267 }\r
268 \r
269 /*-----------------------------------------------------------*/\r
270 \r
271 void vApplicationTickHook( void )\r
272 {\r
273 #if mainCHECK_INTERRUPT_STACK == 1\r
274 extern unsigned long _pvHeapStart[];\r
275 \r
276         /* This function will be called by each tick interrupt if\r
277         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
278         added here, but the tick hook is called from an interrupt context, so\r
279         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
280         functions can be used (those that end in FromISR()). */\r
281 \r
282         /* Manually check the last few bytes of the interrupt stack to check they\r
283         have not been overwritten.  Note - the task stacks are automatically\r
284         checked for overflow if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2\r
285         in FreeRTOSConifg.h, but the interrupt stack is not. */\r
286         configASSERT( memcmp( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) ) == 0U );\r
287 #endif /* mainCHECK_INTERRUPT_STACK */\r
288 \r
289         uTickInterruptCounter++;\r
290 }\r
291 \r
292 \r