]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_M0_LPC1114_LPCXpresso/RTOSDemo/Source/main-blinky.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Demo / CORTEX_M0_LPC1114_LPCXpresso / RTOSDemo / Source / main-blinky.c
1 /*
2  * FreeRTOS V202107.00
3  * Copyright (C) 2020 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  * full 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, and two tasks.  It then starts the
41  * scheduler.
42  *
43  * The Queue Send Task:
44  * The queue send task is implemented by the prvQueueSendTask() function in
45  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
46  * block for 200 milliseconds, before sending the value 100 to the queue that
47  * was created within main_blinky().  Once the value is sent, the task loops
48  * back around to block for another 200 milliseconds.
49  *
50  * The Queue Receive Task:
51  * The queue receive task is implemented by the prvQueueReceiveTask() function
52  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly
53  * blocks on attempts to read data from the queue that was created within
54  * main_blinky().  When data is received, the task checks the value of the
55  * data, and if the value equals the expected 100, toggles the LED.  The 'block
56  * time' parameter passed to the queue receive function specifies that the
57  * task should be held in the Blocked state indefinitely to wait for data to
58  * be available on the queue.  The queue receive task will only leave the
59  * Blocked state when the queue send task writes to the queue.  As the queue
60  * send task writes to the queue every 200 milliseconds, the queue receive
61  * task leaves the Blocked state every 200 milliseconds, and therefore toggles
62  * the LED every 200 milliseconds.
63  */
64
65 /* Kernel includes. */
66 #include "FreeRTOS.h"
67 #include "task.h"
68 #include "queue.h"
69
70 /* Hardware specific includes. */
71 #include "lpc11xx.h"
72
73 /* Priorities at which the tasks are created. */
74 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
75 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
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
92 /*
93  * The tasks as described in the comments at the top of this file.
94  */
95 static void prvQueueReceiveTask( void *pvParameters );
96 static void prvQueueSendTask( void *pvParameters );
97
98 /*
99  * Called by main() to create the simply blinky style application if
100  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
101  */
102 void main_blinky( void );
103
104 /*
105  * The hardware only has a single LED.  Simply toggle it.
106  */
107 extern void vMainToggleLED( void );
108
109 /*-----------------------------------------------------------*/
110
111 /* The queue used by both tasks. */
112 static QueueHandle_t xQueue = NULL;
113
114 /*-----------------------------------------------------------*/
115
116 void main_blinky( void )
117 {
118         /* Create the queue. */
119         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
120
121         if( xQueue != NULL )
122         {
123                 /* Start the two tasks as described in the comments at the top of this
124                 file. */
125                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */
126                                         "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */
127                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */
128                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
129                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */
130                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */
131
132                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
133
134                 /* Start the tasks and timer running. */
135                 vTaskStartScheduler();
136         }
137
138         /* If all is well, the scheduler will now be running, and the following
139         line will never be reached.  If the following line does execute, then
140         there was insufficient FreeRTOS heap memory available for the idle and/or
141         timer tasks     to be created.  See the memory management section on the
142         FreeRTOS web site for more details. */
143         for( ;; );
144 }
145 /*-----------------------------------------------------------*/
146
147 static void prvQueueSendTask( void *pvParameters )
148 {
149 TickType_t xNextWakeTime;
150 const unsigned long ulValueToSend = 100UL;
151
152         /* Check the task parameter is as expected. */
153         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
154
155         /* Initialise xNextWakeTime - this only needs to be done once. */
156         xNextWakeTime = xTaskGetTickCount();
157
158         for( ;; )
159         {
160                 /* Place this task in the blocked state until it is time to run again.
161                 The block time is specified in ticks, the constant used converts ticks
162                 to ms.  While in the Blocked state this task will not consume any CPU
163                 time. */
164                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
165
166                 /* Send to the queue - causing the queue receive task to unblock and
167                 toggle the LED.  0 is used as the block time so the sending operation
168                 will not block - it shouldn't need to block as the queue should always
169                 be empty at this point in the code. */
170                 xQueueSend( xQueue, &ulValueToSend, 0U );
171         }
172 }
173 /*-----------------------------------------------------------*/
174
175 static void prvQueueReceiveTask( void *pvParameters )
176 {
177 unsigned long ulReceivedValue;
178
179         /* Check the task parameter is as expected. */
180         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
181
182         for( ;; )
183         {
184                 /* Wait until something arrives in the queue - this task will block
185                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
186                 FreeRTOSConfig.h. */
187                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
188
189                 /*  To get here something must have been received from the queue, but
190                 is it the expected value?  If it is, toggle the LED. */
191                 if( ulReceivedValue == 100UL )
192                 {
193                         vMainToggleLED();
194                         ulReceivedValue = 0U;
195                 }
196         }
197 }
198 /*-----------------------------------------------------------*/
199