]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A2F200_SoftConsole/main-blinky.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / CORTEX_A2F200_SoftConsole / main-blinky.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * main-blinky.c is included when the "Blinky" build configuration is used.\r
67  * main-full.c is included when the "Full" build configuration is used.\r
68  *\r
69  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
70  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can\r
71  * interact with FreeRTOS tasks/timers.\r
72  *\r
73  * This simple demo project runs on the SmartFusion A2F-EVAL-KIT evaluation\r
74  * board, which is populated with an A2F200M3F SmartFusion mixed signal FPGA.\r
75  * The A2F200M3F incorporates a Cortex-M3 microcontroller.\r
76  *\r
77  * The idle hook function:\r
78  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
79  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
80  *\r
81  * The main() Function:\r
82  * main() creates one software timer, one queue, and two tasks.  It then starts\r
83  * the scheduler.\r
84  *\r
85  * The Queue Send Task:\r
86  * The queue send task is implemented by the prvQueueSendTask() function in\r
87  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
88  * block for 200 milliseconds, before sending the value 100 to the queue that\r
89  * was created within main().  Once the value is sent, the task loops back\r
90  * around to block for another 200 milliseconds.\r
91  *\r
92  * The Queue Receive Task:\r
93  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
94  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
95  * repeatedly attempt to read data from the queue that was created within\r
96  * main().  When data is received, the task checks the value of the data, and\r
97  * if the value equals the expected 100, toggles the green LED.  The 'block\r
98  * time' parameter passed to the queue receive function specifies that the task\r
99  * should be held in the Blocked state indefinitely to wait for data to be\r
100  * available on the queue.  The queue receive task will only leave the Blocked\r
101  * state when the queue send task writes to the queue.  As the queue send task\r
102  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
103  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
104  * every 200 milliseconds.\r
105  *\r
106  * The LED Software Timer and the Button Interrupt:\r
107  * The user button SW1 is configured to generate an interrupt each time it is\r
108  * pressed.  The interrupt service routine switches an LED on, and resets the\r
109  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
110  * and uses a callback function that is defined to just turn the LED off again.\r
111  * Therefore, pressing the user button will turn the LED on, and the LED will\r
112  * remain on until a full five seconds pass without the button being pressed.\r
113  */\r
114 \r
115 /* Kernel includes. */\r
116 #include "FreeRTOS.h"\r
117 #include "task.h"\r
118 #include "queue.h"\r
119 #include "timers.h"\r
120 \r
121 /* Microsemi drivers/libraries. */\r
122 #include "mss_gpio.h"\r
123 #include "mss_watchdog.h"\r
124 \r
125 \r
126 /* Priorities at which the tasks are created. */\r
127 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
128 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
129 \r
130 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
131 converted to ticks using the portTICK_RATE_MS constant. */\r
132 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
133 \r
134 /* The number of items the queue can hold.  This is 1 as the receive task\r
135 will remove items as they are added, meaning the send task should always find\r
136 the queue empty. */\r
137 #define mainQUEUE_LENGTH                                        ( 1 )\r
138 \r
139 /* The LED toggle by the queue receive task. */\r
140 #define mainTASK_CONTROLLED_LED                         0x01UL\r
141 \r
142 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
143 #define mainTIMER_CONTROLLED_LED                        0x02UL\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /*\r
148  * Setup the NVIC, LED outputs, and button inputs.\r
149  */\r
150 static void prvSetupHardware( void );\r
151 \r
152 /*\r
153  * The tasks as described in the comments at the top of this file.\r
154  */\r
155 static void prvQueueReceiveTask( void *pvParameters );\r
156 static void prvQueueSendTask( void *pvParameters );\r
157 \r
158 /*\r
159  * The LED timer callback function.  This does nothing but switch off the\r
160  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
161  */\r
162 static void vLEDTimerCallback( xTimerHandle xTimer );\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* The queue used by both tasks. */\r
167 static xQueueHandle xQueue = NULL;\r
168 \r
169 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
170 function. */\r
171 static xTimerHandle xLEDTimer = NULL;\r
172 \r
173 /* Maintains the current LED output state. */\r
174 static volatile unsigned long ulGPIOState = 0UL;\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 int main(void)\r
179 {\r
180         /* Configure the NVIC, LED outputs and button inputs. */\r
181         prvSetupHardware();\r
182 \r
183         /* Create the queue. */\r
184         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
185 \r
186         if( xQueue != NULL )\r
187         {\r
188                 /* Start the two tasks as described in the comments at the top of this\r
189                 file. */\r
190                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
191                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
192 \r
193                 /* Create the software timer that is responsible for turning off the LED\r
194                 if the button is not pushed within 5000ms, as described at the top of\r
195                 this file. */\r
196                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
197                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
198                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
199                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
200                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
201                                                                 );\r
202 \r
203                 /* Start the tasks and timer running. */\r
204                 vTaskStartScheduler();\r
205         }\r
206 \r
207         /* If all is well, the scheduler will now be running, and the following line\r
208         will never be reached.  If the following line does execute, then there was\r
209         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
210         to be created.  See the memory management section on the FreeRTOS web site\r
211         for more details. */\r
212         for( ;; );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void vLEDTimerCallback( xTimerHandle xTimer )\r
217 {\r
218         /* The timer has expired - so no button pushes have occurred in the last\r
219         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
220         a critical section because it is accessed from multiple tasks, and the\r
221         button interrupt - in this trivial case, for simplicity, the critical\r
222         section is omitted. */\r
223         ulGPIOState |= mainTIMER_CONTROLLED_LED;\r
224         MSS_GPIO_set_outputs( ulGPIOState );\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /* The ISR executed when the user button is pushed. */\r
229 void GPIO8_IRQHandler( void )\r
230 {\r
231 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
232 \r
233         /* The button was pushed, so ensure the LED is on before resetting the\r
234         LED timer.  The LED timer will turn the LED off if the button is not\r
235         pushed within 5000ms. */\r
236         ulGPIOState &= ~mainTIMER_CONTROLLED_LED;\r
237         MSS_GPIO_set_outputs( ulGPIOState );\r
238 \r
239         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
240         because the interrupt priority is below the\r
241         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
242         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
243 \r
244         /* Clear the interrupt before leaving. */\r
245     MSS_GPIO_clear_irq( MSS_GPIO_8 );\r
246 \r
247         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
248         service/daemon task) to unblock, and the unblocked task has a priority\r
249         higher than or equal to the task that was interrupted, then\r
250         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
251         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
252         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvQueueSendTask( void *pvParameters )\r
257 {\r
258 portTickType xNextWakeTime;\r
259 const unsigned long ulValueToSend = 100UL;\r
260 \r
261         /* Initialise xNextWakeTime - this only needs to be done once. */\r
262         xNextWakeTime = xTaskGetTickCount();\r
263 \r
264         for( ;; )\r
265         {\r
266                 /* Place this task in the blocked state until it is time to run again.\r
267                 The block time is specified in ticks, the constant used converts ticks\r
268                 to ms.  While in the Blocked state this task will not consume any CPU\r
269                 time. */\r
270                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
271 \r
272                 /* Send to the queue - causing the queue receive task to unblock and\r
273                 toggle an LED.  0 is used as the block time so the sending operation\r
274                 will not block - it shouldn't need to block as the queue should always\r
275                 be empty at this point in the code. */\r
276                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void prvQueueReceiveTask( void *pvParameters )\r
282 {\r
283 unsigned long ulReceivedValue;\r
284 \r
285         for( ;; )\r
286         {\r
287                 /* Wait until something arrives in the queue - this task will block\r
288                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
289                 FreeRTOSConfig.h. */\r
290                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
291 \r
292                 /*  To get here something must have been received from the queue, but\r
293                 is it the expected value?  If it is, toggle the green LED. */\r
294                 if( ulReceivedValue == 100UL )\r
295                 {\r
296                         /* NOTE - accessing the LED port should use a critical section\r
297                         because it is accessed from multiple tasks, and the button interrupt\r
298                         - in this trivial case, for simplicity, the critical section is\r
299                         omitted. */\r
300                         if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )\r
301                         {\r
302                                 ulGPIOState &= ~mainTASK_CONTROLLED_LED;\r
303                         }\r
304                         else\r
305                         {\r
306                                 ulGPIOState |= mainTASK_CONTROLLED_LED;\r
307                         }\r
308                         MSS_GPIO_set_outputs( ulGPIOState );\r
309                 }\r
310         }\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static void prvSetupHardware( void )\r
315 {\r
316         SystemCoreClockUpdate();\r
317 \r
318         /* Disable the Watch Dog Timer */\r
319         MSS_WD_disable( );\r
320 \r
321         /* Initialise the GPIO */\r
322         MSS_GPIO_init();\r
323 \r
324         /* Set up GPIO for the LEDs. */\r
325     MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );\r
326     MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );\r
327     MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );\r
328     MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );\r
329     MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );\r
330     MSS_GPIO_config( MSS_GPIO_5 , MSS_GPIO_OUTPUT_MODE );\r
331     MSS_GPIO_config( MSS_GPIO_6 , MSS_GPIO_OUTPUT_MODE );\r
332     MSS_GPIO_config( MSS_GPIO_7 , MSS_GPIO_OUTPUT_MODE );\r
333 \r
334     /* All LEDs start off. */\r
335     ulGPIOState = 0xffffffffUL;\r
336     MSS_GPIO_set_outputs( ulGPIOState );\r
337 \r
338         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
339         NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
340     NVIC_EnableIRQ( GPIO8_IRQn );\r
341     MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );\r
342     MSS_GPIO_enable_irq( MSS_GPIO_8 );\r
343 }\r
344 /*-----------------------------------------------------------*/\r
345 \r
346 void vApplicationMallocFailedHook( void )\r
347 {\r
348         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
349         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
350         internally by FreeRTOS API functions that create tasks, queues, software\r
351         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
352         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
353         for( ;; );\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
358 {\r
359         ( void ) pcTaskName;\r
360         ( void ) pxTask;\r
361 \r
362         /* Run time stack overflow checking is performed if\r
363         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
364         function is called if a stack overflow is detected. */\r
365         for( ;; );\r
366 }\r
367 /*-----------------------------------------------------------*/\r
368 \r
369 void vApplicationIdleHook( void )\r
370 {\r
371 volatile size_t xFreeHeapSpace;\r
372 \r
373         /* This function is called on each cycle of the idle task.  In this case it\r
374         does nothing useful, other than report the amout of FreeRTOS heap that\r
375         remains unallocated. */\r
376         xFreeHeapSpace = xPortGetFreeHeapSize();\r
377 \r
378         if( xFreeHeapSpace > 100 )\r
379         {\r
380                 /* By now, the kernel has allocated everything it is going to, so\r
381                 if there is a lot of heap remaining unallocated then\r
382                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
383                 reduced accordingly. */\r
384         }\r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r
388 void vMainConfigureTimerForRunTimeStats( void )\r
389 {\r
390         /* This function is not used by the Blinky build configuration, but needs\r
391         to be defined as the Blinky and Full build configurations share a\r
392         FreeRTOSConfig.h header file. */\r
393 }\r
394 /*-----------------------------------------------------------*/\r
395 \r
396 unsigned long ulGetRunTimeCounterValue( void )\r
397 {\r
398         /* This function is not used by the Blinky build configuration, but needs\r
399         to be defined as the Blinky and Full build configurations share a\r
400         FreeRTOSConfig.h header file. */\r
401         return 0UL;\r
402 }\r
403 \r