]> begriffs open source - cmsis-freertos/blob - Demo/MSP430X_MSP430FR5969_LaunchPad_IAR_CCS/Blinky_Demo/main_blinky.c
Updated pack to FreeRTOS 10.3.1
[cmsis-freertos] / Demo / MSP430X_MSP430FR5969_LaunchPad_IAR_CCS / Blinky_Demo / main_blinky.c
1 /*
2  * FreeRTOS Kernel V10.3.1
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
30  * style 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, 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...and so on.
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 an 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 "semphr.h"
69
70 /* Standard demo includes. */
71 #include "partest.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                     ( pdMS_TO_TICKS( 200 ) )
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 /* The LED toggled by the Rx task. */
87 #define mainTASK_LED                                            ( 0 )
88
89 /*-----------------------------------------------------------*/
90
91 /*
92  * Called by main when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 in
93  * main.c.
94  */
95 void main_blinky( void );
96
97 /*
98  * The tasks as described in the comments at the top of this file.
99  */
100 static void prvQueueReceiveTask( void *pvParameters );
101 static void prvQueueSendTask( void *pvParameters );
102
103 /*-----------------------------------------------------------*/
104
105 /* The queue used by both tasks. */
106 static QueueHandle_t xQueue = NULL;
107
108 /*-----------------------------------------------------------*/
109
110 void main_blinky( void )
111 {
112         /* Create the queue. */
113         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
114
115         if( xQueue != NULL )
116         {
117                 /* Start the two tasks as described in the comments at the top of this
118                 file. */
119                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */
120                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */
121                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */
122                                         NULL,                                                           /* The parameter passed to the task - not used in this case. */
123                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */
124                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */
125
126                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
127
128                 /* Start the tasks and timer running. */
129                 vTaskStartScheduler();
130         }
131
132         /* If all is well, the scheduler will now be running, and the following
133         line will never be reached.  If the following line does execute, then
134         there was insufficient FreeRTOS heap memory available for the Idle and/or
135         timer tasks to be created.  See the memory management section on the
136         FreeRTOS web site for more details on the FreeRTOS heap
137         http://www.freertos.org/a00111.html. */
138         for( ;; );
139 }
140 /*-----------------------------------------------------------*/
141
142 static void prvQueueSendTask( void *pvParameters )
143 {
144 TickType_t xNextWakeTime;
145 const unsigned long ulValueToSend = 100UL;
146
147         /* Remove compiler warning about unused parameter. */
148         ( void ) pvParameters;
149
150         /* Initialise xNextWakeTime - this only needs to be done once. */
151         xNextWakeTime = xTaskGetTickCount();
152
153         for( ;; )
154         {
155                 /* Place this task in the blocked state until it is time to run again. */
156                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
157
158                 /* Send to the queue - causing the queue receive task to unblock and
159                 toggle the LED.  0 is used as the block time so the sending operation
160                 will not block - it shouldn't need to block as the queue should always
161                 be empty at this point in the code. */
162                 xQueueSend( xQueue, &ulValueToSend, 0U );
163         }
164 }
165 /*-----------------------------------------------------------*/
166
167 static void prvQueueReceiveTask( void *pvParameters )
168 {
169 unsigned long ulReceivedValue;
170 const unsigned long ulExpectedValue = 100UL;
171
172         /* Remove compiler warning about unused parameter. */
173         ( void ) pvParameters;
174
175         for( ;; )
176         {
177                 /* Wait until something arrives in the queue - this task will block
178                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
179                 FreeRTOSConfig.h. */
180                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
181
182                 /*  To get here something must have been received from the queue, but
183                 is it the expected value?  If it is, toggle the LED. */
184                 if( ulReceivedValue == ulExpectedValue )
185                 {
186                         vParTestToggleLED( mainTASK_LED );
187                         ulReceivedValue = 0U;
188                 }
189         }
190 }
191 /*-----------------------------------------------------------*/
192