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