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