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