]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_M0_LPC1114_LPCXpresso/RTOSDemo/Source/main.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / CORTEX_M0_LPC1114_LPCXpresso / RTOSDemo / Source / main.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * This project provides two demo applications.  A simple blinky style project,
30  * and a more comprehensive test and demo application.  The
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
32  * select between the two.  The simply blinky demo is implemented and described
33  * in main_blinky.c.  The more comprehensive test and demo application is
34  * implemented and described in main_full.c.
35  *
36  * This file implements the code that is not demo specific, including the
37  * hardware setup and FreeRTOS hook functions.  It also contains a dummy
38  * interrupt service routine called Dummy_IRQHandler() that is provided as an
39  * example of how to use interrupt safe FreeRTOS API functions (those that end
40  * in "FromISR").
41  *
42  *****************************************************************************/
43
44 /* Standard includes. */
45 #include "string.h"
46
47 /* FreeRTOS includes. */
48 #include "FreeRTOS.h"
49 #include "task.h"
50
51 /* Hardware specific includes. */
52 #include "lpc11xx.h"
53
54 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
55 or 0 to run the more comprehensive test and demo application. */
56 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0
57
58 /* The bit on port 0 to which the LED is wired. */
59 #define mainLED_BIT             ( 1UL << 7UL )
60
61 /* The configCHECK_FOR_STACK_OVERFLOW setting in FreeRTOSConifg can be used to
62 check task stacks for overflows.  It does not however check the stack used by
63 interrupts.  This demo has a simple addition that will also check the stack used
64 by interrupts if mainCHECK_INTERRUPT_STACK is set to 1.  Note that this check is
65 only performed from the tick hook function (which runs in an interrupt context).
66 It is a good debugging aid - but won't catch interrupt stack problems until the
67 tick interrupt next executes. */
68 #define mainCHECK_INTERRUPT_STACK                       1
69 #if mainCHECK_INTERRUPT_STACK == 1
70         const unsigned char ucExpectedInterruptStackValues[] = { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
71 #endif
72
73 /*-----------------------------------------------------------*/
74
75 /*
76  * Perform any application specific hardware configuration.  The clocks,
77  * memory, etc. are configured before main() is called.
78  */
79 static void prvSetupHardware( void );
80
81 /*
82  * The hardware only has a single LED.  Simply toggle it.
83  */
84 void vMainToggleLED( void );
85
86 /* main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
87 main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0. */
88 void main_blinky( void );
89 void main_full( void );
90
91 /*-----------------------------------------------------------*/
92
93 /* The GPIO port to which the LED is attached. */
94 static LPC_GPIO_TypeDef * const xGPIO0 = LPC_GPIO0;
95
96 /*-----------------------------------------------------------*/
97 int main( void )
98 {
99         /* Prepare the hardware to run this demo. */
100         prvSetupHardware();
101
102         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
103         of this file. */
104         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1
105         {
106                 main_blinky();
107         }
108         #else
109         {
110                 main_full();
111         }
112         #endif
113
114         return 0;
115 }
116 /*-----------------------------------------------------------*/
117
118 void vMainToggleLED( void )
119 {
120 static unsigned long ulLEDState = 0UL;
121
122         if( ulLEDState == 0UL )
123         {
124                 xGPIO0->MASKED_ACCESS[ mainLED_BIT ] = 0UL;
125         }
126         else
127         {
128                 xGPIO0->MASKED_ACCESS[ mainLED_BIT ] = mainLED_BIT;
129         }
130
131         ulLEDState = !ulLEDState;
132 }
133 /*-----------------------------------------------------------*/
134
135 static void prvSetupHardware( void )
136 {
137 extern unsigned long _vStackTop[], _pvHeapStart[];
138 unsigned long ulInterruptStackSize;
139
140         /* Enable AHB clock for GPIO. */
141         LPC_SYSCON->SYSAHBCLKCTRL |= ( 1 << 6 );
142
143         /* Configure GPIO for LED output. */
144         xGPIO0->DIR |= mainLED_BIT;
145
146         /* The size of the stack used by main and interrupts is not defined in
147         the linker, but just uses whatever RAM is left.  Calculate the amount of
148         RAM available for the main/interrupt/system stack, and check it against
149         a reasonable number.  If this assert is hit then it is likely you don't
150         have enough stack to start the kernel, or to allow interrupts to nest.
151         Note - this is separate to the stacks that are used by tasks.  The stacks
152         that are used by tasks are automatically checked if
153         configCHECK_FOR_STACK_OVERFLOW is not 0 in FreeRTOSConfig.h - but the stack
154         used by interrupts is not.  Reducing the conifgTOTAL_HEAP_SIZE setting will
155         increase the stack available to main() and interrupts. */
156         ulInterruptStackSize = ( ( unsigned long ) _vStackTop ) - ( ( unsigned long ) _pvHeapStart );
157         configASSERT( ulInterruptStackSize > 350UL );
158
159         /* Fill the stack used by main() and interrupts to a known value, so its
160         use can be manually checked. */
161         memcpy( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) );
162 }
163 /*-----------------------------------------------------------*/
164
165 void vApplicationMallocFailedHook( void )
166 {
167         /* vApplicationMallocFailedHook() will only be called if
168         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
169         function that will get called if a call to pvPortMalloc() fails.
170         pvPortMalloc() is called internally by the kernel whenever a task, queue,
171         timer or semaphore is created.  It is also called by various parts of the
172         demo application.  If heap_1.c or heap_2.c are used, then the size of the
173         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
174         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
175         to query the size of free heap space that remains (although it does not
176         provide information on how the remaining heap might be fragmented). */
177         taskDISABLE_INTERRUPTS();
178         for( ;; );
179 }
180 /*-----------------------------------------------------------*/
181
182 void vApplicationIdleHook( void )
183 {
184         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
185         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
186         task.  It is essential that code added to this hook function never attempts
187         to block in any way (for example, call xQueueReceive() with a block time
188         specified, or call vTaskDelay()).  If the application makes use of the
189         vTaskDelete() API function (as this demo application does) then it is also
190         important that vApplicationIdleHook() is permitted to return to its calling
191         function, because it is the responsibility of the idle task to clean up
192         memory allocated by the kernel to any task that has since been deleted. */
193 }
194 /*-----------------------------------------------------------*/
195
196 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
197 {
198         ( void ) pcTaskName;
199         ( void ) pxTask;
200
201         /* Run time stack overflow checking is performed if
202         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
203         function is called if a stack overflow is detected. */
204         taskDISABLE_INTERRUPTS();
205         for( ;; );
206 }
207 /*-----------------------------------------------------------*/
208
209 void vApplicationTickHook( void )
210 {
211 #if mainCHECK_INTERRUPT_STACK == 1
212 extern unsigned long _pvHeapStart[];
213
214         /* This function will be called by each tick interrupt if
215         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
216         added here, but the tick hook is called from an interrupt context, so
217         code must not attempt to block, and only the interrupt safe FreeRTOS API
218         functions can be used (those that end in FromISR()). */
219
220         /* Manually check the last few bytes of the interrupt stack to check they
221         have not been overwritten.  Note - the task stacks are automatically
222         checked for overflow if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2
223         in FreeRTOSConifg.h, but the interrupt stack is not. */
224         configASSERT( memcmp( ( void * ) _pvHeapStart, ucExpectedInterruptStackValues, sizeof( ucExpectedInterruptStackValues ) ) == 0U );
225 #endif /* mainCHECK_INTERRUPT_STACK */
226 }
227 /*-----------------------------------------------------------*/
228
229 #ifdef JUST_AN_EXAMPLE_ISR
230
231 void Dummy_IRQHandler(void)
232 {
233 long lHigherPriorityTaskWoken = pdFALSE;
234
235         /* Clear the interrupt if necessary. */
236         Dummy_ClearITPendingBit();
237
238         /* This interrupt does nothing more than demonstrate how to synchronise a
239         task with an interrupt.  A semaphore is used for this purpose.  Note
240         lHigherPriorityTaskWoken is initialised to zero.  Only FreeRTOS API functions
241         that end in "FromISR" can be called from an ISR. */
242         xSemaphoreGiveFromISR( xTestSemaphore, &lHigherPriorityTaskWoken );
243
244         /* If there was a task that was blocked on the semaphore, and giving the
245         semaphore caused the task to unblock, and the unblocked task has a priority
246         higher than the current Running state task (the task that this interrupt
247         interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE
248         internally within xSemaphoreGiveFromISR().  Passing pdTRUE into the
249         portEND_SWITCHING_ISR() macro will result in a context switch being pended to
250         ensure this interrupt returns directly to the unblocked, higher priority,
251         task.  Passing pdFALSE into portEND_SWITCHING_ISR() has no effect. */
252         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
253 }
254
255 #endif /* JUST_AN_EXAMPLE_ISR */
256
257
258
259