]> begriffs open source - cmsis-freertos/blob - Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*
2  * FreeRTOS V202111.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  * Defines the tasks and co-routines used to toggle the segments of the two
30  * seven segment displays, as described at the top of main.c
31  */
32
33
34 #include <stdlib.h>
35
36 /* Scheduler include files. */
37 #include "FreeRTOS.h"
38 #include "task.h"
39 #include "croutine.h"
40
41 /* Demo program include files. */
42 #include "partest.h"
43
44 /*-----------------------------------------------------------*/
45
46 /* One task per segment of the left side display. */
47 #define ledNUM_OF_LED_TASKS     ( 7 )
48
49 /* Each task toggles at a frequency that is a multiple of 333ms. */
50 #define ledFLASH_RATE_BASE      ( ( TickType_t ) 333 )
51
52 /* One co-routine per segment of the right hand display. */
53 #define ledNUM_OF_LED_CO_ROUTINES       7
54
55 /* All co-routines run at the same priority. */
56 #define ledCO_ROUTINE_PRIORITY          0
57
58 /*-----------------------------------------------------------*/
59
60 /* The task that is created 7 times. */
61 static void vLEDFlashTask( void *pvParameters );
62
63 /* The co-routine that is created 7 times. */
64 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );
65
66 /* This task is created once, but itself creates 7 co-routines. */
67 static void vLEDCoRoutineControlTask( void *pvParameters );
68
69 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended
70 and resumed. */
71 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
72
73 /* Handle to the task in which the co-routines run.  Used so the
74 co-routines can be suspended and resumed. */
75 static TaskHandle_t xCoroutineTask;
76
77 /*-----------------------------------------------------------*/
78
79 /**
80  * Creates the tasks and co-routines used to toggle the segments of the two
81  * seven segment displays, as described at the top of main.c
82  */
83 void vCreateFlashTasksAndCoRoutines( void )
84 {
85 signed short sLEDTask;
86
87         /* Create the tasks that flash segments on the first LED. */
88         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
89         {
90                 /* Spawn the task. */
91                 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
92         }
93
94         /* Create the task in which the co-routines run.  The co-routines themselves
95         are created within the task. */
96         xTaskCreate( vLEDCoRoutineControlTask, "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
97 }
98 /*-----------------------------------------------------------*/
99
100 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
101 {
102 short sLEDTask;
103
104         if( ucIndex == configLEFT_DISPLAY )
105         {
106                 /* Suspend or resume the tasks that are toggling the segments of the
107                 left side display. */
108                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
109                 {
110                         if( xFlashTaskHandles[ sLEDTask ] != NULL )
111                         {
112                                 if( sSuspendTasks == pdTRUE )
113                                 {
114                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
115                                 }
116                                 else
117                                 {
118                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );
119                                 }
120                         }
121                 }
122         }
123         else
124         {
125                 /* Suspend or resume the task in which the co-routines are running.  The
126                 co-routines toggle the segments of the right side display. */
127                 if( sSuspendTasks == pdTRUE )
128                 {
129                         vTaskSuspend( xCoroutineTask );
130                 }
131                 else
132                 {
133                         vTaskResume( xCoroutineTask );
134                 }
135         }
136 }
137 /*-----------------------------------------------------------*/
138
139 static void vLEDFlashTask( void * pvParameters )
140 {
141 TickType_t xFlashRate, xLastFlashTime;
142 unsigned short usLED;
143
144         /* The LED to flash is passed in as the task parameter. */
145         usLED = ( unsigned short ) pvParameters;
146
147         /* Calculate the rate at which this task is going to toggle its LED. */
148         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( TickType_t ) usLED );
149         xFlashRate /= portTICK_PERIOD_MS;
150
151         /* We will turn the LED on and off again in the delay period, so each
152         delay is only half the total period. */
153         xFlashRate /= ( TickType_t ) 2;
154
155         /* We need to initialise xLastFlashTime prior to the first call to
156         vTaskDelayUntil(). */
157         xLastFlashTime = xTaskGetTickCount();
158
159         for(;;)
160         {
161                 /* Delay for half the flash period then turn the LED on. */
162                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
163                 vParTestToggleLED( usLED );
164
165                 /* Delay for half the flash period then turn the LED off. */
166                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
167                 vParTestToggleLED( usLED );
168         }
169 }
170 /*-----------------------------------------------------------*/
171
172 static void vLEDCoRoutineControlTask( void *pvParameters )
173 {
174 unsigned short usCoroutine;
175
176         ( void ) pvParameters;
177
178         /* Create the co-routines - one of each segment of the right side display. */
179         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
180         {
181                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
182         }
183
184         /* This task has nothing else to do except scheduler the co-routines it just
185         created. */
186         for( ;; )
187         {
188                 vCoRoutineSchedule();
189         }
190 }
191 /*-----------------------------------------------------------*/
192
193 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )
194 {
195 /* The usIndex parameter of the co-routine function is used as an index into
196 the xFlashRates array to obtain the delay period to use. */
197 static const TickType_t xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_PERIOD_MS,
198                                                                                                                                 300 / portTICK_PERIOD_MS,
199                                                                                                                                 450 / portTICK_PERIOD_MS,
200                                                                                                                                 600 / portTICK_PERIOD_MS,
201                                                                                                                                 750 / portTICK_PERIOD_MS,
202                                                                                                                                 900 / portTICK_PERIOD_MS,
203                                                                                                                                 1050 / portTICK_PERIOD_MS };
204
205         /* Co-routines MUST start with a call to crSTART. */
206         crSTART( xHandle );
207
208         for( ;; )
209         {
210                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of
211                 the left side display which use the low numbers. */
212                 vParTestToggleLED( usIndex + 8 );
213
214                 /* Delay until it is time to toggle the segment that this co-routine is
215                 controlling again. */
216                 crDELAY( xHandle, xFlashRates[ usIndex ] );
217         }
218
219         /* Co-routines MUST end with a call to crEND. */
220         crEND();
221 }
222 /*-----------------------------------------------------------*/
223
224