]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main_blinky.c
9b8e3831a387a542a375594101065126476dcd28
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / 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  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
67  * project, and a more comprehensive test and demo application.  The\r
68  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
69  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
70  * in main.c.  This file implements the simply blinky style version.\r
71  *\r
72  * NOTE 2:  This file only contains the source code that is specific to the\r
73  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
74  * required to configure the hardware, are defined in main.c.\r
75  ******************************************************************************\r
76  *\r
77  * main_blinky() creates one queue, two tasks, and one software timer.  It then \r
78  * starts the scheduler.\r
79  *\r
80  * The Blinky Software Timer:\r
81  * This demonstrates an auto-reload software timer.  The timer callback function\r
82  * does nothing but toggle an LED.\r
83  *\r
84  * The Queue Send Task:\r
85  * The queue send task is implemented by the prvQueueSendTask() function in\r
86  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
87  * block for 200 milliseconds, before sending the value 100 to the queue that\r
88  * was created within main_blinky().  Once the value is sent, the task loops\r
89  * back around to block for another 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.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
94  * blocks on attempts to read data from the queue that was created within\r
95  * main_blinky().  When data is received, the task checks the value of the\r
96  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
97  * time' parameter passed to the queue receive function specifies that the\r
98  * task should be held in the Blocked state indefinitely to wait for data to\r
99  * be available on the queue.  The queue receive task will only leave the\r
100  * Blocked state when the queue send task writes to the queue.  As the queue\r
101  * send task writes to the queue every 200 milliseconds, the queue receive\r
102  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
103  * the LED every 200 milliseconds.\r
104  */\r
105 \r
106 /* Standard includes. */\r
107 #include <stdio.h>\r
108 \r
109 /* Kernel includes. */\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "queue.h"\r
113 #include "timers.h"\r
114 \r
115 /* Standard demo includes. */\r
116 #include "partest.h"\r
117 \r
118 /* Priorities at which the tasks are created. */\r
119 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
120 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
121 \r
122 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
123 to ticks using the portTICK_RATE_MS constant. */\r
124 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
125 \r
126 /* The number of items the queue can hold.  This is 1 as the receive task\r
127 will remove items as they are added, meaning the send task should always find\r
128 the queue empty. */\r
129 #define mainQUEUE_LENGTH                                        ( 1 )\r
130 \r
131 /* Values passed to the two tasks just to check the task parameter\r
132 functionality. */\r
133 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
134 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
135 \r
136 /* The period of the blinky software timer.  The period is specified in ms and\r
137 converted to ticks using the portTICK_RATE_MS constant. */\r
138 #define mainBLINKY_TIMER_PERIOD                         ( 50 / portTICK_RATE_MS )\r
139 \r
140 /* The LED used by the communicating tasks and the blinky timer respectively. */\r
141 #define mainTASKS_LED                                           ( 0 )\r
142 #define mainTIMER_LED                                           ( 1 )\r
143 \r
144 /* Misc. */\r
145 #define mainDONT_BLOCK                                          ( 0 )\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /*\r
150  * The tasks as described in the comments at the top of this file.\r
151  */\r
152 static void prvQueueReceiveTask( void *pvParameters );\r
153 static void prvQueueSendTask( void *pvParameters );\r
154 \r
155 /*\r
156  * The callback function for the blinky software timer, as described at the top\r
157  * of this file.\r
158  */\r
159 static void prvBlinkyTimerCallback( xTimerHandle xTimer );\r
160  \r
161 /*\r
162  * Called by main() to create the simply blinky style application if\r
163  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
164  */\r
165 void main_blinky( void );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* The queue used by both tasks. */\r
170 static xQueueHandle xQueue = NULL;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void main_blinky( void )\r
175 {\r
176 xTimerHandle xTimer;\r
177 \r
178         /* Create the queue. */\r
179         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
180 \r
181         if( xQueue != NULL )\r
182         {\r
183                 /* Create the two tasks as described in the comments at the top of this\r
184                 file. */\r
185                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
186                                         ( signed char * ) "Rx",                                 /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
187                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
188                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
189                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
190                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
191 \r
192                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
193 \r
194                 /* Create the blinky software timer as described at the top of this\r
195                 file. */\r
196                 xTimer = xTimerCreate(  ( const signed char * ) "Blinky",       /* A text name, purely to help debugging. */\r
197                                                                 ( mainBLINKY_TIMER_PERIOD ),            /* The timer period. */\r
198                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
199                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
200                                                                 prvBlinkyTimerCallback                          /* The callback function that inspects the status of all the other tasks. */\r
201                                                         );      \r
202                 \r
203                 if( xTimer != NULL )\r
204                 {\r
205                         xTimerStart( xTimer, mainDONT_BLOCK );\r
206                 }\r
207                                 \r
208                 /* Start the tasks and timer running. */\r
209                 vTaskStartScheduler();\r
210         }\r
211 \r
212         /* If all is well, the scheduler will now be running, and the following\r
213         line will never be reached.  If the following line does execute, then\r
214         there was insufficient FreeRTOS heap memory available for the idle and/or\r
215         timer tasks     to be created.  See the memory management section on the\r
216         FreeRTOS web site for more details. */\r
217         for( ;; );\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvQueueSendTask( void *pvParameters )\r
222 {\r
223 portTickType xNextWakeTime;\r
224 const unsigned long ulValueToSend = 100UL;\r
225 \r
226         /* Check the task parameter is as expected. */\r
227         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
228 \r
229         /* Initialise xNextWakeTime - this only needs to be done once. */\r
230         xNextWakeTime = xTaskGetTickCount();\r
231 \r
232         for( ;; )\r
233         {\r
234                 /* Place this task in the blocked state until it is time to run again.\r
235                 The block time is specified in ticks, the constant used converts ticks\r
236                 to ms.  While in the Blocked state this task will not consume any CPU\r
237                 time. */\r
238                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
239 \r
240                 /* Send to the queue - causing the queue receive task to unblock and\r
241                 toggle the LED.  0 is used as the block time so the sending operation\r
242                 will not block - it shouldn't need to block as the queue should always\r
243                 be empty at this point in the code. */\r
244                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvQueueReceiveTask( void *pvParameters )\r
250 {\r
251 unsigned long ulReceivedValue;\r
252 \r
253         /* Check the task parameter is as expected. */\r
254         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
255 \r
256         for( ;; )\r
257         {\r
258                 /* Wait until something arrives in the queue - this task will block\r
259                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
260                 FreeRTOSConfig.h. */\r
261                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
262 \r
263                 /*  To get here something must have been received from the queue, but\r
264                 is it the expected value?  If it is, toggle the LED. */\r
265                 if( ulReceivedValue == 100UL )\r
266                 {\r
267                         vParTestToggleLED( mainTASKS_LED );\r
268                         ulReceivedValue = 0U;\r
269                 }\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvBlinkyTimerCallback( xTimerHandle xTimer )\r
275 {\r
276         /* This function is called when the blinky software time expires.  All the\r
277         function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
278         with the period set by mainBLINKY_TIMER_PERIOD. */\r
279         vParTestToggleLED( mainTIMER_LED );\r
280 }\r
281 \r