]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crflash.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / Common / Minimal / crflash.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * This demo application file demonstrates the use of queues to pass data\r
30  * between co-routines.\r
31  *\r
32  * N represents the number of 'fixed delay' co-routines that are created and\r
33  * is set during initialisation.\r
34  *\r
35  * N 'fixed delay' co-routines are created that just block for a fixed\r
36  * period then post the number of an LED onto a queue.  Each such co-routine\r
37  * uses a different block period.  A single 'flash' co-routine is also created\r
38  * that blocks on the same queue, waiting for the number of the next LED it\r
39  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
40  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
41  * LED N-1 is caused to flash at a different rate.\r
42  *\r
43  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
44  * flash co-routine is created with co-routine priority 1.  This means that\r
45  * the queue should never contain more than a single item.  This is because\r
46  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
47  * a priority greater than the tasks posting to the queue it is guaranteed to\r
48  * have emptied the queue and blocked once again before the queue can contain\r
49  * any more date.  An error is indicated if an attempt to post data to the\r
50  * queue fails - indicating that the queue is already full.\r
51  *\r
52  */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "croutine.h"\r
57 #include "queue.h"\r
58 \r
59 /* Demo application includes. */\r
60 #include "partest.h"\r
61 #include "crflash.h"\r
62 \r
63 /* The queue should only need to be of length 1.  See the description at the\r
64 top of the file. */\r
65 #define crfQUEUE_LENGTH         1\r
66 \r
67 #define crfFIXED_DELAY_PRIORITY         0\r
68 #define crfFLASH_PRIORITY                       1\r
69 \r
70 /* Only one flash co-routine is created so the index is not significant. */\r
71 #define crfFLASH_INDEX                          0\r
72 \r
73 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
74 created. */\r
75 #define crfMAX_FLASH_TASKS                      8\r
76 \r
77 /* We don't want to block when posting to the queue. */\r
78 #define crfPOSTING_BLOCK_TIME           0\r
79 \r
80 /*\r
81  * The 'fixed delay' co-routine as described at the top of the file.\r
82  */\r
83 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
84 \r
85 /*\r
86  * The 'flash' co-routine as described at the top of the file.\r
87  */\r
88 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
89 \r
90 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
91 'flash' co-routine. */\r
92 static QueueHandle_t xFlashQueue;\r
93 \r
94 /* This will be set to pdFALSE if we detect an error. */\r
95 static BaseType_t xCoRoutineFlashStatus = pdPASS;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /*\r
100  * See the header file for details.\r
101  */\r
102 void vStartFlashCoRoutines( UBaseType_t uxNumberToCreate )\r
103 {\r
104 UBaseType_t uxIndex;\r
105 \r
106         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
107         {\r
108                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
109         }\r
110 \r
111         /* Create the queue used to pass data between the co-routines. */\r
112         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( UBaseType_t ) );\r
113 \r
114         if( xFlashQueue )\r
115         {\r
116                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
117                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
118                 {\r
119                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
120                 }\r
121 \r
122                 /* Create the 'flash' co-routine. */\r
123                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
124         }\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
129 {\r
130 /* Even though this is a co-routine the xResult variable does not need to be\r
131 static as we do not need it to maintain its state between blocks. */\r
132 BaseType_t xResult;\r
133 /* The uxIndex parameter of the co-routine function is used as an index into\r
134 the xFlashRates array to obtain the delay period to use. */\r
135 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,\r
136                                                                                                                                 200 / portTICK_PERIOD_MS,\r
137                                                                                                                                 250 / portTICK_PERIOD_MS,\r
138                                                                                                                                 300 / portTICK_PERIOD_MS,\r
139                                                                                                                                 350 / portTICK_PERIOD_MS,\r
140                                                                                                                                 400 / portTICK_PERIOD_MS,\r
141                                                                                                                                 450 / portTICK_PERIOD_MS,\r
142                                                                                                                                 500  / portTICK_PERIOD_MS };\r
143 \r
144         /* Co-routines MUST start with a call to crSTART. */\r
145         crSTART( xHandle );\r
146 \r
147         for( ;; )\r
148         {\r
149                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
150                 flash. */\r
151                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
152 \r
153                 if( xResult != pdPASS )\r
154                 {\r
155                         /* For the reasons stated at the top of the file we should always\r
156                         find that we can post to the queue.  If we could not then an error\r
157                         has occurred. */\r
158                         xCoRoutineFlashStatus = pdFAIL;\r
159                 }\r
160 \r
161                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
162         }\r
163 \r
164         /* Co-routines MUST end with a call to crEND. */\r
165         crEND();\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
170 {\r
171 /* Even though this is a co-routine the variable do not need to be\r
172 static as we do not need it to maintain their state between blocks. */\r
173 BaseType_t xResult;\r
174 UBaseType_t uxLEDToFlash;\r
175 \r
176         /* Co-routines MUST start with a call to crSTART. */\r
177         crSTART( xHandle );\r
178         ( void ) uxIndex;\r
179 \r
180         for( ;; )\r
181         {\r
182                 /* Block to wait for the number of the LED to flash. */\r
183                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );\r
184 \r
185                 if( xResult != pdPASS )\r
186                 {\r
187                         /* We would not expect to wake unless we received something. */\r
188                         xCoRoutineFlashStatus = pdFAIL;\r
189                 }\r
190                 else\r
191                 {\r
192                         /* We received the number of an LED to flash - flash it! */\r
193                         vParTestToggleLED( uxLEDToFlash );\r
194                 }\r
195         }\r
196 \r
197         /* Co-routines MUST end with a call to crEND. */\r
198         crEND();\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 BaseType_t xAreFlashCoRoutinesStillRunning( void )\r
203 {\r
204         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
205         or not. */\r
206         return xCoRoutineFlashStatus;\r
207 }\r
208 \r