]> git.sur5r.net Git - freertos/blob - Demo/PIC32MX_MPLAB/main_blinky.c
Renamed the CORTEX_M4_ATSAM4S_AVR_Studio directory to the correct CORTEX_M4_ATSAM4S_A...
[freertos] / Demo / PIC32MX_MPLAB / main_blinky.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /******************************************************************************\r
68  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
69  * project, and a more comprehensive test and demo application.  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 Blinky Software Timer:\r
83  * This demonstrates an auto-reload software timer.  The timer callback function\r
84  * does nothing but toggle an LED.\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.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
89  * block for 200 milliseconds, before sending the value 100 to the queue that\r
90  * was created within main_blinky().  Once the value is sent, the task loops\r
91  * back around to block for another 200 milliseconds.\r
92  *\r
93  * The Queue Receive Task:\r
94  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
95  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
96  * blocks on attempts to read data from the queue that was created within\r
97  * main_blinky().  When data is received, the task checks the value of the\r
98  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
99  * time' parameter passed to the queue receive function specifies that the\r
100  * task should be held in the Blocked state indefinitely to wait for data to\r
101  * be available on the queue.  The queue receive task will only leave the\r
102  * Blocked state when the queue send task writes to the queue.  As the queue\r
103  * send task writes to the queue every 200 milliseconds, the queue receive\r
104  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
105  * the LED every 200 milliseconds.\r
106  */\r
107 \r
108 /* Standard includes. */\r
109 #include <stdio.h>\r
110 \r
111 /* Kernel includes. */\r
112 #include "FreeRTOS.h"\r
113 #include "task.h"\r
114 #include "queue.h"\r
115 #include "timers.h"\r
116 \r
117 /* Standard demo includes. */\r
118 #include "partest.h"\r
119 \r
120 /* Priorities at which the tasks are created. */\r
121 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
122 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
123 \r
124 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
125 to ticks using the portTICK_RATE_MS constant. */\r
126 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
127 \r
128 /* The number of items the queue can hold.  This is 1 as the receive task\r
129 will remove items as they are added, meaning the send task should always find\r
130 the queue empty. */\r
131 #define mainQUEUE_LENGTH                                        ( 1 )\r
132 \r
133 /* Values passed to the two tasks just to check the task parameter\r
134 functionality. */\r
135 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
136 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
137 \r
138 /* The period of the blinky software timer.  The period is specified in ms and\r
139 converted to ticks using the portTICK_RATE_MS constant. */\r
140 #define mainBLINKY_TIMER_PERIOD                         ( 50 / portTICK_RATE_MS )\r
141 \r
142 /* The LED used by the communicating tasks and the blinky timer respectively. */\r
143 #define mainTASKS_LED                                           ( 0 )\r
144 #define mainTIMER_LED                                           ( 1 )\r
145 \r
146 /* Misc. */\r
147 #define mainDONT_BLOCK                                          ( 0 )\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * The tasks as described in the comments at the top of this file.\r
153  */\r
154 static void prvQueueReceiveTask( void *pvParameters );\r
155 static void prvQueueSendTask( void *pvParameters );\r
156 \r
157 /*\r
158  * The callback function for the blinky software timer, as described at the top\r
159  * of this file.\r
160  */\r
161 static void prvBlinkyTimerCallback( xTimerHandle xTimer );\r
162  \r
163 /*\r
164  * Called by main() to create the simply blinky style application if\r
165  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
166  */\r
167 void main_blinky( void );\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /* The queue used by both tasks. */\r
172 static xQueueHandle xQueue = NULL;\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void main_blinky( void )\r
177 {\r
178 xTimerHandle xTimer;\r
179 \r
180         /* Create the queue. */\r
181         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
182 \r
183         if( xQueue != NULL )\r
184         {\r
185                 /* Create the two tasks as described in the comments at the top of this\r
186                 file. */\r
187                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
188                                         ( signed char * ) "Rx",                                 /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
189                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
190                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
191                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
192                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
193 \r
194                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
195 \r
196                 /* Create the blinky software timer as described at the top of this\r
197                 file. */\r
198                 xTimer = xTimerCreate(  ( const signed char * ) "Blinky",       /* A text name, purely to help debugging. */\r
199                                                                 ( mainBLINKY_TIMER_PERIOD ),            /* The timer period. */\r
200                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
201                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
202                                                                 prvBlinkyTimerCallback                          /* The callback function that inspects the status of all the other tasks. */\r
203                                                         );      \r
204                 \r
205                 if( xTimer != NULL )\r
206                 {\r
207                         xTimerStart( xTimer, mainDONT_BLOCK );\r
208                 }\r
209                                 \r
210                 /* Start the tasks and timer running. */\r
211                 vTaskStartScheduler();\r
212         }\r
213 \r
214         /* If all is well, the scheduler will now be running, and the following\r
215         line will never be reached.  If the following line does execute, then\r
216         there was insufficient FreeRTOS heap memory available for the idle and/or\r
217         timer tasks     to be created.  See the memory management section on the\r
218         FreeRTOS web site for more details. */\r
219         for( ;; );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 static void prvQueueSendTask( void *pvParameters )\r
224 {\r
225 portTickType xNextWakeTime;\r
226 const unsigned long ulValueToSend = 100UL;\r
227 \r
228         /* Check the task parameter is as expected. */\r
229         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
230 \r
231         /* Initialise xNextWakeTime - this only needs to be done once. */\r
232         xNextWakeTime = xTaskGetTickCount();\r
233 \r
234         for( ;; )\r
235         {\r
236                 /* Place this task in the blocked state until it is time to run again.\r
237                 The block time is specified in ticks, the constant used converts ticks\r
238                 to ms.  While in the Blocked state this task will not consume any CPU\r
239                 time. */\r
240                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
241 \r
242                 /* Send to the queue - causing the queue receive task to unblock and\r
243                 toggle the LED.  0 is used as the block time so the sending operation\r
244                 will not block - it shouldn't need to block as the queue should always\r
245                 be empty at this point in the code. */\r
246                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvQueueReceiveTask( void *pvParameters )\r
252 {\r
253 unsigned long ulReceivedValue;\r
254 \r
255         /* Check the task parameter is as expected. */\r
256         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
257 \r
258         for( ;; )\r
259         {\r
260                 /* Wait until something arrives in the queue - this task will block\r
261                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
262                 FreeRTOSConfig.h. */\r
263                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
264 \r
265                 /*  To get here something must have been received from the queue, but\r
266                 is it the expected value?  If it is, toggle the LED. */\r
267                 if( ulReceivedValue == 100UL )\r
268                 {\r
269                         vParTestToggleLED( mainTASKS_LED );\r
270                         ulReceivedValue = 0U;\r
271                 }\r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 static void prvBlinkyTimerCallback( xTimerHandle xTimer )\r
277 {\r
278         /* This function is called when the blinky software time expires.  All the\r
279         function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
280         with the period set by mainBLINKY_TIMER_PERIOD. */\r
281         vParTestToggleLED( mainTIMER_LED );\r
282 }\r
283 \r