]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main_blinky.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / main_blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /******************************************************************************\r
29  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
33  * in main.c.  This file implements the simply blinky style version.\r
34  *\r
35  * NOTE 2:  This file only contains the source code that is specific to the\r
36  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
37  * required to configure the hardware, are defined in main.c.\r
38  ******************************************************************************\r
39  *\r
40  * main_blinky() creates one queue, two tasks, and one software timer.  It then\r
41  * starts the scheduler.\r
42  *\r
43  * The Blinky Software Timer:\r
44  * This demonstrates an auto-reload software timer.  The timer callback function\r
45  * does nothing but toggle an LED.\r
46  *\r
47  * The Queue Send Task:\r
48  * The queue send task is implemented by the prvQueueSendTask() function in\r
49  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
50  * block for 200 milliseconds, before sending the value 100 to the queue that\r
51  * was created within main_blinky().  Once the value is sent, the task loops\r
52  * back around to block for another 200 milliseconds.\r
53  *\r
54  * The Queue Receive Task:\r
55  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
56  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
57  * blocks on attempts to read data from the queue that was created within\r
58  * main_blinky().  When data is received, the task checks the value of the\r
59  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
60  * time' parameter passed to the queue receive function specifies that the\r
61  * task should be held in the Blocked state indefinitely to wait for data to\r
62  * be available on the queue.  The queue receive task will only leave the\r
63  * Blocked state when the queue send task writes to the queue.  As the queue\r
64  * send task writes to the queue every 200 milliseconds, the queue receive\r
65  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
66  * the LED every 200 milliseconds.\r
67  */\r
68 \r
69 /* Standard includes. */\r
70 #include <stdio.h>\r
71 \r
72 /* Kernel includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 #include "queue.h"\r
76 #include "timers.h"\r
77 \r
78 /* Standard demo includes. */\r
79 #include "partest.h"\r
80 \r
81 /* Priorities at which the tasks are created. */\r
82 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
83 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
84 \r
85 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
86 to ticks using the portTICK_PERIOD_MS constant. */\r
87 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
88 \r
89 /* The number of items the queue can hold.  This is 1 as the receive task\r
90 will remove items as they are added, meaning the send task should always find\r
91 the queue empty. */\r
92 #define mainQUEUE_LENGTH                                        ( 1 )\r
93 \r
94 /* Values passed to the two tasks just to check the task parameter\r
95 functionality. */\r
96 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
97 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
98 \r
99 /* The period of the blinky software timer.  The period is specified in ms and\r
100 converted to ticks using the portTICK_PERIOD_MS constant. */\r
101 #define mainBLINKY_TIMER_PERIOD                         ( 50 / portTICK_PERIOD_MS )\r
102 \r
103 /* The LED used by the communicating tasks and the blinky timer respectively. */\r
104 #define mainTASKS_LED                                           ( 0 )\r
105 #define mainTIMER_LED                                           ( 1 )\r
106 \r
107 /* Misc. */\r
108 #define mainDONT_BLOCK                                          ( 0 )\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * The tasks as described in the comments at the top of this file.\r
114  */\r
115 static void prvQueueReceiveTask( void *pvParameters );\r
116 static void prvQueueSendTask( void *pvParameters );\r
117 \r
118 /*\r
119  * The callback function for the blinky software timer, as described at the top\r
120  * of this file.\r
121  */\r
122 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
123 \r
124 /*\r
125  * Called by main() to create the simply blinky style application if\r
126  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
127  */\r
128 void main_blinky( void );\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* The queue used by both tasks. */\r
133 static QueueHandle_t xQueue = NULL;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 void main_blinky( void )\r
138 {\r
139 TimerHandle_t xTimer;\r
140 \r
141         /* Create the queue. */\r
142         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
143 \r
144         if( xQueue != NULL )\r
145         {\r
146                 /* Create the two tasks as described in the comments at the top of this\r
147                 file. */\r
148                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
149                                         "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
150                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
151                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
152                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
153                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
154 \r
155                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
156 \r
157                 /* Create the blinky software timer as described at the top of this\r
158                 file. */\r
159                 xTimer = xTimerCreate(  "Blinky",                                       /* A text name, purely to help debugging. */\r
160                                                                 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */\r
161                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
162                                                                 ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
163                                                                 prvBlinkyTimerCallback          /* The callback function that inspects the status of all the other tasks. */\r
164                                                         );\r
165 \r
166                 if( xTimer != NULL )\r
167                 {\r
168                         xTimerStart( xTimer, mainDONT_BLOCK );\r
169                 }\r
170 \r
171                 /* Start the tasks and timer running. */\r
172                 vTaskStartScheduler();\r
173         }\r
174 \r
175         /* If all is well, the scheduler will now be running, and the following\r
176         line will never be reached.  If the following line does execute, then\r
177         there was insufficient FreeRTOS heap memory available for the idle and/or\r
178         timer tasks     to be created.  See the memory management section on the\r
179         FreeRTOS web site for more details. */\r
180         for( ;; );\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static void prvQueueSendTask( void *pvParameters )\r
185 {\r
186 TickType_t xNextWakeTime;\r
187 const unsigned long ulValueToSend = 100UL;\r
188 \r
189         /* Check the task parameter is as expected. */\r
190         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
191 \r
192         /* Initialise xNextWakeTime - this only needs to be done once. */\r
193         xNextWakeTime = xTaskGetTickCount();\r
194 \r
195         for( ;; )\r
196         {\r
197                 /* Place this task in the blocked state until it is time to run again.\r
198                 The block time is specified in ticks, the constant used converts ticks\r
199                 to ms.  While in the Blocked state this task will not consume any CPU\r
200                 time. */\r
201                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
202 \r
203                 /* Send to the queue - causing the queue receive task to unblock and\r
204                 toggle the LED.  0 is used as the block time so the sending operation\r
205                 will not block - it shouldn't need to block as the queue should always\r
206                 be empty at this point in the code. */\r
207                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void prvQueueReceiveTask( void *pvParameters )\r
213 {\r
214 unsigned long ulReceivedValue;\r
215 \r
216         /* Check the task parameter is as expected. */\r
217         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
218 \r
219         for( ;; )\r
220         {\r
221                 /* Wait until something arrives in the queue - this task will block\r
222                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
223                 FreeRTOSConfig.h. */\r
224                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
225 \r
226                 /*  To get here something must have been received from the queue, but\r
227                 is it the expected value?  If it is, toggle the LED. */\r
228                 if( ulReceivedValue == 100UL )\r
229                 {\r
230                         vParTestToggleLED( mainTASKS_LED );\r
231                         ulReceivedValue = 0U;\r
232                 }\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
238 {\r
239         /* This function is called when the blinky software time expires.  All the\r
240         function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
241         with the period set by mainBLINKY_TIMER_PERIOD. */\r
242         vParTestToggleLED( mainTIMER_LED );\r
243 }\r
244 \r