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