]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crflash.c
1e59da8ab87afb1508150a2d9d922e8573d8e23c
[freertos] / FreeRTOS / Demo / Common / Minimal / crflash.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68  * This demo application file demonstrates the use of queues to pass data\r
69  * between co-routines.\r
70  *\r
71  * N represents the number of 'fixed delay' co-routines that are created and\r
72  * is set during initialisation.\r
73  *\r
74  * N 'fixed delay' co-routines are created that just block for a fixed\r
75  * period then post the number of an LED onto a queue.  Each such co-routine\r
76  * uses a different block period.  A single 'flash' co-routine is also created\r
77  * that blocks on the same queue, waiting for the number of the next LED it\r
78  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
79  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
80  * LED N-1 is caused to flash at a different rate.\r
81  *\r
82  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
83  * flash co-routine is created with co-routine priority 1.  This means that\r
84  * the queue should never contain more than a single item.  This is because\r
85  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
86  * a priority greater than the tasks posting to the queue it is guaranteed to\r
87  * have emptied the queue and blocked once again before the queue can contain\r
88  * any more date.  An error is indicated if an attempt to post data to the\r
89  * queue fails - indicating that the queue is already full.\r
90  *\r
91  */\r
92 \r
93 /* Scheduler includes. */\r
94 #include "FreeRTOS.h"\r
95 #include "croutine.h"\r
96 #include "queue.h"\r
97 \r
98 /* Demo application includes. */\r
99 #include "partest.h"\r
100 #include "crflash.h"\r
101 \r
102 /* The queue should only need to be of length 1.  See the description at the\r
103 top of the file. */\r
104 #define crfQUEUE_LENGTH         1\r
105 \r
106 #define crfFIXED_DELAY_PRIORITY         0\r
107 #define crfFLASH_PRIORITY                       1\r
108 \r
109 /* Only one flash co-routine is created so the index is not significant. */\r
110 #define crfFLASH_INDEX                          0\r
111 \r
112 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
113 created. */\r
114 #define crfMAX_FLASH_TASKS                      8\r
115 \r
116 /* We don't want to block when posting to the queue. */\r
117 #define crfPOSTING_BLOCK_TIME           0\r
118 \r
119 /*\r
120  * The 'fixed delay' co-routine as described at the top of the file.\r
121  */\r
122 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
123 \r
124 /*\r
125  * The 'flash' co-routine as described at the top of the file.\r
126  */\r
127 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
128 \r
129 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
130 'flash' co-routine. */\r
131 static xQueueHandle xFlashQueue;\r
132 \r
133 /* This will be set to pdFALSE if we detect an error. */\r
134 static portBASE_TYPE xCoRoutineFlashStatus = pdPASS;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /*\r
139  * See the header file for details.\r
140  */\r
141 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )\r
142 {\r
143 unsigned portBASE_TYPE uxIndex;\r
144 \r
145         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
146         {\r
147                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
148         }\r
149 \r
150         /* Create the queue used to pass data between the co-routines. */\r
151         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
152 \r
153         if( xFlashQueue )\r
154         {\r
155                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
156                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
157                 {\r
158                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
159                 }\r
160 \r
161                 /* Create the 'flash' co-routine. */\r
162                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
163         }\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
168 {\r
169 /* Even though this is a co-routine the xResult variable does not need to be\r
170 static as we do not need it to maintain its state between blocks. */\r
171 signed portBASE_TYPE xResult;\r
172 /* The uxIndex parameter of the co-routine function is used as an index into\r
173 the xFlashRates array to obtain the delay period to use. */\r
174 static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,\r
175                                                                                                                                 200 / portTICK_RATE_MS,\r
176                                                                                                                                 250 / portTICK_RATE_MS,\r
177                                                                                                                                 300 / portTICK_RATE_MS,\r
178                                                                                                                                 350 / portTICK_RATE_MS,\r
179                                                                                                                                 400 / portTICK_RATE_MS,\r
180                                                                                                                                 450 / portTICK_RATE_MS,\r
181                                                                                                                                 500  / portTICK_RATE_MS };\r
182 \r
183         /* Co-routines MUST start with a call to crSTART. */\r
184         crSTART( xHandle );\r
185 \r
186         for( ;; )\r
187         {\r
188                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
189                 flash. */\r
190                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
191 \r
192                 if( xResult != pdPASS )\r
193                 {\r
194                         /* For the reasons stated at the top of the file we should always\r
195                         find that we can post to the queue.  If we could not then an error\r
196                         has occurred. */\r
197                         xCoRoutineFlashStatus = pdFAIL;\r
198                 }\r
199 \r
200                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
201         }\r
202 \r
203         /* Co-routines MUST end with a call to crEND. */\r
204         crEND();\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
209 {\r
210 /* Even though this is a co-routine the variable do not need to be\r
211 static as we do not need it to maintain their state between blocks. */\r
212 signed portBASE_TYPE xResult;\r
213 unsigned portBASE_TYPE uxLEDToFlash;\r
214 \r
215         /* Co-routines MUST start with a call to crSTART. */\r
216         crSTART( xHandle );\r
217         ( void ) uxIndex;\r
218         \r
219         for( ;; )\r
220         {\r
221                 /* Block to wait for the number of the LED to flash. */\r
222                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
223 \r
224                 if( xResult != pdPASS )\r
225                 {\r
226                         /* We would not expect to wake unless we received something. */\r
227                         xCoRoutineFlashStatus = pdFAIL;\r
228                 }\r
229                 else\r
230                 {\r
231                         /* We received the number of an LED to flash - flash it! */\r
232                         vParTestToggleLED( uxLEDToFlash );\r
233                 }\r
234         }\r
235 \r
236         /* Co-routines MUST end with a call to crEND. */\r
237         crEND();\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )\r
242 {\r
243         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
244         or not. */\r
245         return xCoRoutineFlashStatus;\r
246 }\r
247 \r