]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/crflash.c
Correct task names in BlockQ.c.
[freertos] / Demo / Common / Minimal / crflash.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * This demo application file demonstrates the use of queues to pass data\r
56  * between co-routines.\r
57  *\r
58  * N represents the number of 'fixed delay' co-routines that are created and\r
59  * is set during initialisation.\r
60  *\r
61  * N 'fixed delay' co-routines are created that just block for a fixed\r
62  * period then post the number of an LED onto a queue.  Each such co-routine\r
63  * uses a different block period.  A single 'flash' co-routine is also created\r
64  * that blocks on the same queue, waiting for the number of the next LED it\r
65  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
66  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
67  * LED N-1 is caused to flash at a different rate.\r
68  *\r
69  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
70  * flash co-routine is created with co-routine priority 1.  This means that\r
71  * the queue should never contain more than a single item.  This is because\r
72  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
73  * a priority greater than the tasks posting to the queue it is guaranteed to\r
74  * have emptied the queue and blocked once again before the queue can contain\r
75  * any more date.  An error is indicated if an attempt to post data to the\r
76  * queue fails - indicating that the queue is already full.\r
77  *\r
78  */\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "croutine.h"\r
83 #include "queue.h"\r
84 \r
85 /* Demo application includes. */\r
86 #include "partest.h"\r
87 #include "crflash.h"\r
88 \r
89 /* The queue should only need to be of length 1.  See the description at the\r
90 top of the file. */\r
91 #define crfQUEUE_LENGTH         1\r
92 \r
93 #define crfFIXED_DELAY_PRIORITY         0\r
94 #define crfFLASH_PRIORITY                       1\r
95 \r
96 /* Only one flash co-routine is created so the index is not significant. */\r
97 #define crfFLASH_INDEX                          0\r
98 \r
99 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
100 created. */\r
101 #define crfMAX_FLASH_TASKS                      8\r
102 \r
103 /* We don't want to block when posting to the queue. */\r
104 #define crfPOSTING_BLOCK_TIME           0\r
105 \r
106 /*\r
107  * The 'fixed delay' co-routine as described at the top of the file.\r
108  */\r
109 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
110 \r
111 /*\r
112  * The 'flash' co-routine as described at the top of the file.\r
113  */\r
114 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
115 \r
116 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
117 'flash' co-routine. */\r
118 static xQueueHandle xFlashQueue;\r
119 \r
120 /* This will be set to pdFALSE if we detect an error. */\r
121 static portBASE_TYPE xCoRoutineFlashStatus = pdPASS;\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * See the header file for details.\r
127  */\r
128 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )\r
129 {\r
130 unsigned portBASE_TYPE uxIndex;\r
131 \r
132         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
133         {\r
134                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
135         }\r
136 \r
137         /* Create the queue used to pass data between the co-routines. */\r
138         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
139 \r
140         if( xFlashQueue )\r
141         {\r
142                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
143                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
144                 {\r
145                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
146                 }\r
147 \r
148                 /* Create the 'flash' co-routine. */\r
149                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
150         }\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
155 {\r
156 /* Even though this is a co-routine the xResult variable does not need to be\r
157 static as we do not need it to maintain its state between blocks. */\r
158 signed portBASE_TYPE xResult;\r
159 /* The uxIndex parameter of the co-routine function is used as an index into\r
160 the xFlashRates array to obtain the delay period to use. */\r
161 static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,\r
162                                                                                                                                 200 / portTICK_RATE_MS,\r
163                                                                                                                                 250 / portTICK_RATE_MS,\r
164                                                                                                                                 300 / portTICK_RATE_MS,\r
165                                                                                                                                 350 / portTICK_RATE_MS,\r
166                                                                                                                                 400 / portTICK_RATE_MS,\r
167                                                                                                                                 450 / portTICK_RATE_MS,\r
168                                                                                                                                 500  / portTICK_RATE_MS };\r
169 \r
170         /* Co-routines MUST start with a call to crSTART. */\r
171         crSTART( xHandle );\r
172 \r
173         for( ;; )\r
174         {\r
175                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
176                 flash. */\r
177                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
178 \r
179                 if( xResult != pdPASS )\r
180                 {\r
181                         /* For the reasons stated at the top of the file we should always\r
182                         find that we can post to the queue.  If we could not then an error\r
183                         has occurred. */\r
184                         xCoRoutineFlashStatus = pdFAIL;\r
185                 }\r
186 \r
187                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
188         }\r
189 \r
190         /* Co-routines MUST end with a call to crEND. */\r
191         crEND();\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
196 {\r
197 /* Even though this is a co-routine the variable do not need to be\r
198 static as we do not need it to maintain their state between blocks. */\r
199 signed portBASE_TYPE xResult;\r
200 unsigned portBASE_TYPE uxLEDToFlash;\r
201 \r
202         /* Co-routines MUST start with a call to crSTART. */\r
203         crSTART( xHandle );\r
204         ( void ) uxIndex;\r
205         \r
206         for( ;; )\r
207         {\r
208                 /* Block to wait for the number of the LED to flash. */\r
209                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
210 \r
211                 if( xResult != pdPASS )\r
212                 {\r
213                         /* We would not expect to wake unless we received something. */\r
214                         xCoRoutineFlashStatus = pdFAIL;\r
215                 }\r
216                 else\r
217                 {\r
218                         /* We received the number of an LED to flash - flash it! */\r
219                         vParTestToggleLED( uxLEDToFlash );\r
220                 }\r
221         }\r
222 \r
223         /* Co-routines MUST end with a call to crEND. */\r
224         crEND();\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )\r
229 {\r
230         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
231         or not. */\r
232         return xCoRoutineFlashStatus;\r
233 }\r
234 \r