]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crflash.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / Common / Minimal / crflash.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * This demo application file demonstrates the use of queues to pass data\r
68  * between co-routines.\r
69  *\r
70  * N represents the number of 'fixed delay' co-routines that are created and\r
71  * is set during initialisation.\r
72  *\r
73  * N 'fixed delay' co-routines are created that just block for a fixed\r
74  * period then post the number of an LED onto a queue.  Each such co-routine\r
75  * uses a different block period.  A single 'flash' co-routine is also created\r
76  * that blocks on the same queue, waiting for the number of the next LED it\r
77  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
78  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
79  * LED N-1 is caused to flash at a different rate.\r
80  *\r
81  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
82  * flash co-routine is created with co-routine priority 1.  This means that\r
83  * the queue should never contain more than a single item.  This is because\r
84  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
85  * a priority greater than the tasks posting to the queue it is guaranteed to\r
86  * have emptied the queue and blocked once again before the queue can contain\r
87  * any more date.  An error is indicated if an attempt to post data to the\r
88  * queue fails - indicating that the queue is already full.\r
89  *\r
90  */\r
91 \r
92 /* Scheduler includes. */\r
93 #include "FreeRTOS.h"\r
94 #include "croutine.h"\r
95 #include "queue.h"\r
96 \r
97 /* Demo application includes. */\r
98 #include "partest.h"\r
99 #include "crflash.h"\r
100 \r
101 /* The queue should only need to be of length 1.  See the description at the\r
102 top of the file. */\r
103 #define crfQUEUE_LENGTH         1\r
104 \r
105 #define crfFIXED_DELAY_PRIORITY         0\r
106 #define crfFLASH_PRIORITY                       1\r
107 \r
108 /* Only one flash co-routine is created so the index is not significant. */\r
109 #define crfFLASH_INDEX                          0\r
110 \r
111 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
112 created. */\r
113 #define crfMAX_FLASH_TASKS                      8\r
114 \r
115 /* We don't want to block when posting to the queue. */\r
116 #define crfPOSTING_BLOCK_TIME           0\r
117 \r
118 /*\r
119  * The 'fixed delay' co-routine as described at the top of the file.\r
120  */\r
121 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
122 \r
123 /*\r
124  * The 'flash' co-routine as described at the top of the file.\r
125  */\r
126 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
127 \r
128 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
129 'flash' co-routine. */\r
130 static QueueHandle_t xFlashQueue;\r
131 \r
132 /* This will be set to pdFALSE if we detect an error. */\r
133 static BaseType_t xCoRoutineFlashStatus = pdPASS;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /*\r
138  * See the header file for details.\r
139  */\r
140 void vStartFlashCoRoutines( UBaseType_t uxNumberToCreate )\r
141 {\r
142 UBaseType_t uxIndex;\r
143 \r
144         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
145         {\r
146                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
147         }\r
148 \r
149         /* Create the queue used to pass data between the co-routines. */\r
150         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( UBaseType_t ) );\r
151 \r
152         if( xFlashQueue )\r
153         {\r
154                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
155                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
156                 {\r
157                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
158                 }\r
159 \r
160                 /* Create the 'flash' co-routine. */\r
161                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
167 {\r
168 /* Even though this is a co-routine the xResult variable does not need to be\r
169 static as we do not need it to maintain its state between blocks. */\r
170 BaseType_t xResult;\r
171 /* The uxIndex parameter of the co-routine function is used as an index into\r
172 the xFlashRates array to obtain the delay period to use. */\r
173 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,\r
174                                                                                                                                 200 / portTICK_PERIOD_MS,\r
175                                                                                                                                 250 / portTICK_PERIOD_MS,\r
176                                                                                                                                 300 / portTICK_PERIOD_MS,\r
177                                                                                                                                 350 / portTICK_PERIOD_MS,\r
178                                                                                                                                 400 / portTICK_PERIOD_MS,\r
179                                                                                                                                 450 / portTICK_PERIOD_MS,\r
180                                                                                                                                 500  / portTICK_PERIOD_MS };\r
181 \r
182         /* Co-routines MUST start with a call to crSTART. */\r
183         crSTART( xHandle );\r
184 \r
185         for( ;; )\r
186         {\r
187                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
188                 flash. */\r
189                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
190 \r
191                 if( xResult != pdPASS )\r
192                 {\r
193                         /* For the reasons stated at the top of the file we should always\r
194                         find that we can post to the queue.  If we could not then an error\r
195                         has occurred. */\r
196                         xCoRoutineFlashStatus = pdFAIL;\r
197                 }\r
198 \r
199                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
200         }\r
201 \r
202         /* Co-routines MUST end with a call to crEND. */\r
203         crEND();\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
208 {\r
209 /* Even though this is a co-routine the variable do not need to be\r
210 static as we do not need it to maintain their state between blocks. */\r
211 BaseType_t xResult;\r
212 UBaseType_t uxLEDToFlash;\r
213 \r
214         /* Co-routines MUST start with a call to crSTART. */\r
215         crSTART( xHandle );\r
216         ( void ) uxIndex;\r
217         \r
218         for( ;; )\r
219         {\r
220                 /* Block to wait for the number of the LED to flash. */\r
221                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
222 \r
223                 if( xResult != pdPASS )\r
224                 {\r
225                         /* We would not expect to wake unless we received something. */\r
226                         xCoRoutineFlashStatus = pdFAIL;\r
227                 }\r
228                 else\r
229                 {\r
230                         /* We received the number of an LED to flash - flash it! */\r
231                         vParTestToggleLED( uxLEDToFlash );\r
232                 }\r
233         }\r
234 \r
235         /* Co-routines MUST end with a call to crEND. */\r
236         crEND();\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 BaseType_t xAreFlashCoRoutinesStillRunning( void )\r
241 {\r
242         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
243         or not. */\r
244         return xCoRoutineFlashStatus;\r
245 }\r
246 \r