]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4E_Atmel_Studio/src/main_blinky.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4E_Atmel_Studio / src / main_blinky.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /******************************************************************************\r
67  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
68  * project, and a more comprehensive demo application that makes use of\r
69  * FreeRTOS_CLI, FreeRTOS+UDP and FreeRTOS+FAT SL.  The\r
70  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
71  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
72  * in main.c.  This file implements the simply blinky style version.\r
73  *\r
74  * NOTE 2:  This file only contains the source code that is specific to the\r
75  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
76  * required to configure the hardware, are defined in main.c.\r
77  ******************************************************************************\r
78  *\r
79  * main_blinky() creates one queue, two tasks and one software timer.  It then\r
80  * starts the scheduler.\r
81  *\r
82  * The Queue Send Task:\r
83  * The queue send task is implemented by the prvQueueSendTask() function in\r
84  * this file.  The task sits in a loop that sends a value to the queue every\r
85  * 200 milliseconds.\r
86  *\r
87  * The Queue Receive Task:\r
88  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
89  * in this file.  The task sits in a loop that blocks on the queue to wait for\r
90  * data to arrive (it does not use any CPU time while it is in the Blocked\r
91  * state), toggling an LED each time it receives the value sent by the queue\r
92  * send task.  As the queue send task writes to the queue every 200 milliseconds\r
93  * the LED will toggle every 200 milliseconds.\r
94  */\r
95 \r
96 \r
97 /* Kernel includes. */\r
98 #include "FreeRTOS.h"\r
99 #include "task.h"\r
100 #include "queue.h"\r
101 #include "timers.h"\r
102 \r
103 /* Demo application includes. */\r
104 #include "partest.h"\r
105 \r
106 /* Priorities at which the tasks are created. */\r
107 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
108 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
109 \r
110 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
111 to ticks using the portTICK_PERIOD_MS constant. */\r
112 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
113 \r
114 /* The number of items the queue can hold.  This is 1 as the receive task\r
115 will remove items as they are added, meaning the send task should always find\r
116 the queue empty. */\r
117 #define mainQUEUE_LENGTH                                        ( 1 )\r
118 \r
119 /* The period of the blinky software timer.  The period is specified in ms and\r
120 converted to ticks using the portTICK_PERIOD_MS constant. */\r
121 #define mainBLINKY_TIMER_PERIOD                         ( 50 / portTICK_PERIOD_MS )\r
122 \r
123 /* A block time of zero simply means "don't block". */\r
124 #define mainDONT_BLOCK                                          ( 0 )\r
125 \r
126 /* The LEDs toggled by the timer callback and queue receive task respectively. */\r
127 #define mainTIMER_LED                                           0\r
128 #define mainTASK_LED                                            1\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /*\r
133  * The tasks as described in the comments at the top of this file.\r
134  */\r
135 static void prvQueueReceiveTask( void *pvParameters );\r
136 static void prvQueueSendTask( void *pvParameters );\r
137 \r
138 /*\r
139  * The callback function for the blinky software timer, as described at the top\r
140  * of this file.\r
141  */\r
142 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
143 \r
144 /*\r
145  * Called by main() to create the simply blinky style application if\r
146  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
147  */\r
148 void main_blinky( void );\r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void main_blinky( void )\r
153 {\r
154 TimerHandle_t xTimer;\r
155 QueueHandle_t xQueue;\r
156 \r
157         /* Create the queue. */\r
158         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
159 \r
160         if( xQueue != NULL )\r
161         {\r
162                 /* Start the two tasks as described in the comments at the top of this\r
163                 file. */\r
164                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
165                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
166                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */\r
167                                         ( void * ) xQueue,                                      /* Pass the queue into the task using the task parameter. */\r
168                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
169                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
170 \r
171                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) xQueue, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
172 \r
173                 /* Create the blinky software timer as described at the top of this\r
174                 file. */\r
175                 xTimer = xTimerCreate(  "Blinky",                                       /* A text name, purely to help debugging. */\r
176                                                                 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */\r
177                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
178                                                                 ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
179                                                                 prvBlinkyTimerCallback );       /* The callback function that inspects the status of all the other tasks. */\r
180 \r
181                 configASSERT( xTimer );\r
182 \r
183                 if( xTimer != NULL )\r
184                 {\r
185                         xTimerStart( xTimer, mainDONT_BLOCK );\r
186                 }\r
187 \r
188                 /* Start the tasks and timer running. */\r
189                 vTaskStartScheduler();\r
190         }\r
191 \r
192         /* If all is well, the scheduler will now be running, and the following\r
193         line will never be reached.  If the following line does execute, then\r
194         there was insufficient FreeRTOS heap memory available for the idle and/or\r
195         timer tasks     to be created.  See the memory management section on the\r
196         FreeRTOS web site for more details. */\r
197         for( ;; );\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvQueueSendTask( void *pvParameters )\r
202 {\r
203 TickType_t xNextWakeTime;\r
204 const unsigned long ulValueToSend = 100UL;\r
205 QueueHandle_t xQueue;\r
206 \r
207         /* The handle of the queue is passed in using the task's parameter. */\r
208         xQueue = ( QueueHandle_t ) pvParameters;\r
209 \r
210         /* Initialise xNextWakeTime - this only needs to be done once. */\r
211         xNextWakeTime = xTaskGetTickCount();\r
212 \r
213         for( ;; )\r
214         {\r
215                 /* Place this task in the blocked state until it is time to run again.\r
216                 The block time is specified in ticks, the constant used converts ticks\r
217                 to ms.  While in the Blocked state this task will not consume any CPU\r
218                 time. */\r
219                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
220 \r
221                 /* Send to the queue - causing the queue receive task to unblock and\r
222                 toggle the LED.  0 is used as the block time so the sending operation\r
223                 will not block - it shouldn't need to block as the queue should always\r
224                 be empty at this point in the code. */\r
225                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
226         }\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 static void prvQueueReceiveTask( void *pvParameters )\r
231 {\r
232 unsigned long ulReceivedValue;\r
233 QueueHandle_t xQueue;\r
234 \r
235         /* The queue is passed in as the task's parameter. */\r
236         xQueue = ( QueueHandle_t ) pvParameters;\r
237 \r
238         for( ;; )\r
239         {\r
240                 /* Wait until something arrives in the queue - this task will block\r
241                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
242                 FreeRTOSConfig.h. */\r
243                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
244 \r
245                 /*  To get here something must have been received from the queue, but\r
246                 is it the expected value?  If it is, toggle the LED. */\r
247                 if( ulReceivedValue == 100UL )\r
248                 {\r
249                         vParTestToggleLED( mainTASK_LED );\r
250                         ulReceivedValue = 0U;\r
251                 }\r
252         }\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
257 {\r
258         /* Avoid compiler warnings. */\r
259         ( void ) xTimer;\r
260 \r
261         /* This function is called when the blinky software time expires.  All the\r
262         function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
263         with the period set by mainBLINKY_TIMER_PERIOD. */\r
264         vParTestToggleLED( mainTIMER_LED );\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r