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