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