]> begriffs open source - cmsis-freertos/blob - Demo/MB91460_Softune/SRC/crflash_modified.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / MB91460_Softune / SRC / crflash_modified.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  * This demo application file demonstrates the use of queues to pass data
30  * between co-routines.
31  *
32  * N represents the number of 'fixed delay' co-routines that are created and
33  * is set during initialisation.
34  *
35  * N 'fixed delay' co-routines are created that just block for a fixed
36  * period then post the number of an LED onto a queue.  Each such co-routine
37  * uses a different block period.  A single 'flash' co-routine is also created
38  * that blocks on the same queue, waiting for the number of the next LED it
39  * should flash.  Upon receiving a number it simply toggle the instructed LED
40  * then blocks on the queue once more.  In this manner each LED from LED 0 to
41  * LED N-1 is caused to flash at a different rate.
42  *
43  * The 'fixed delay' co-routines are created with co-routine priority 0.  The
44  * flash co-routine is created with co-routine priority 1.  This means that
45  * the queue should never contain more than a single item.  This is because
46  * posting to the queue will unblock the 'flash' co-routine, and as this has
47  * a priority greater than the tasks posting to the queue it is guaranteed to
48  * have emptied the queue and blocked once again before the queue can contain
49  * any more date.  An error is indicated if an attempt to post data to the
50  * queue fails - indicating that the queue is already full.
51  *
52  */
53
54 /* Scheduler includes. */
55 #include "FreeRTOS.h"
56 #include "croutine.h"
57 #include "queue.h"
58
59 /* Demo application includes. */
60 #include "partest.h"
61 #include "crflash.h"
62
63 /* The queue should only need to be of length 1.  See the description at the
64 top of the file. */
65 #define crfQUEUE_LENGTH         1
66
67 #define crfFIXED_DELAY_PRIORITY         0
68 #define crfFLASH_PRIORITY                       1
69
70 /* Only one flash co-routine is created so the index is not significant. */
71 #define crfFLASH_INDEX                          0
72
73 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be
74 created. */
75 #define crfMAX_FLASH_TASKS                      8
76
77 /* We don't want to block when posting to the queue. */
78 #define crfPOSTING_BLOCK_TIME           0
79
80 /* Added by MPi, this define is added in order to make the vParTestToggleLED()
81 work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected 
82 to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */ 
83 #define PDR00_Offset    8
84
85 /*
86  * The 'fixed delay' co-routine as described at the top of the file.
87  */
88 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
89
90 /*
91  * The 'flash' co-routine as described at the top of the file.
92  */
93 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
94
95 /* The queue used to pass data between the 'fixed delay' co-routines and the
96 'flash' co-routine. */
97 static QueueHandle_t xFlashQueue;
98
99 /* This will be set to pdFALSE if we detect an error. */
100 static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;
101
102 /*-----------------------------------------------------------*/
103
104 /*
105  * See the header file for details.
106  */
107 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )
108 {
109 unsigned portBASE_TYPE uxIndex;
110
111         if( uxNumberToCreate > crfMAX_FLASH_TASKS )
112         {
113                 uxNumberToCreate = crfMAX_FLASH_TASKS;
114         }
115
116         /* Create the queue used to pass data between the co-routines. */
117         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
118
119         if( xFlashQueue )
120         {
121                 /* Create uxNumberToCreate 'fixed delay' co-routines. */
122                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )
123                 {
124                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );
125                 }
126
127                 /* Create the 'flash' co-routine. */
128                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );
129         }
130 }
131 /*-----------------------------------------------------------*/
132
133 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
134 {
135 /* Even though this is a co-routine the xResult variable does not need to be
136 static as we do not need it to maintain its state between blocks. */
137 signed portBASE_TYPE xResult;
138 /* The uxIndex parameter of the co-routine function is used as an index into
139 the xFlashRates array to obtain the delay period to use. */
140 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,
141                                                                                                                                 200 / portTICK_PERIOD_MS,
142                                                                                                                                 250 / portTICK_PERIOD_MS,
143                                                                                                                                 300 / portTICK_PERIOD_MS,
144                                                                                                                                 350 / portTICK_PERIOD_MS,
145                                                                                                                                 400 / portTICK_PERIOD_MS,
146                                                                                                                                 450 / portTICK_PERIOD_MS,
147                                                                                                                                 500  / portTICK_PERIOD_MS };
148
149         /* Co-routines MUST start with a call to crSTART. */
150         crSTART( xHandle );
151
152         for( ;; )
153         {
154                 /* Post our uxIndex value onto the queue.  This is used as the LED to
155                 flash. */
156                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );
157
158                 if( xResult != pdPASS )
159                 {
160                         /* For the reasons stated at the top of the file we should always
161                         find that we can post to the queue.  If we could not then an error
162                         has occurred. */
163                         uxCoRoutineFlashStatus = pdFAIL;
164                 }
165
166                 crDELAY( xHandle, xFlashRates[ uxIndex ] );
167         }
168
169         /* Co-routines MUST end with a call to crEND. */
170         crEND();
171 }
172 /*-----------------------------------------------------------*/
173
174 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
175 {
176 /* Even though this is a co-routine the variable do not need to be
177 static as we do not need it to maintain their state between blocks. */
178 signed portBASE_TYPE xResult;
179 unsigned portBASE_TYPE uxLEDToFlash;
180
181         /* Co-routines MUST start with a call to crSTART. */
182         crSTART( xHandle );
183         ( void ) uxIndex;
184         
185         for( ;; )
186         {
187                 /* Block to wait for the number of the LED to flash. */
188                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                
189
190                 if( xResult != pdPASS )
191                 {
192                         /* We would not expect to wake unless we received something. */
193                         uxCoRoutineFlashStatus = pdFAIL;
194                 }
195                 else
196                 {
197                         /* We received the number of an LED to flash - flash it! */
198                         /* Added by MPi, PDR00_Offset is added in order to make the 
199                         vParTestToggleLED() work. */ 
200                         vParTestToggleLED( uxLEDToFlash +  PDR00_Offset );
201                 }
202         }
203
204         /* Co-routines MUST end with a call to crEND. */
205         crEND();
206 }
207 /*-----------------------------------------------------------*/
208
209 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )
210 {
211         /* Return pdPASS or pdFAIL depending on whether an error has been detected
212         or not. */
213         return uxCoRoutineFlashStatus;
214 }
215