]> git.sur5r.net Git - freertos/blob - Demo/MB91460_Softune/SRC/crflash_modified.c
615067cd86288d6a544bbe0df20bfc9339f7f8e9
[freertos] / Demo / MB91460_Softune / SRC / crflash_modified.c
1 /*\r
2         FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 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 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /*\r
44  * This demo application file demonstrates the use of queues to pass data\r
45  * between co-routines.\r
46  *\r
47  * N represents the number of 'fixed delay' co-routines that are created and\r
48  * is set during initialisation.\r
49  *\r
50  * N 'fixed delay' co-routines are created that just block for a fixed\r
51  * period then post the number of an LED onto a queue.  Each such co-routine\r
52  * uses a different block period.  A single 'flash' co-routine is also created\r
53  * that blocks on the same queue, waiting for the number of the next LED it\r
54  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
55  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
56  * LED N-1 is caused to flash at a different rate.\r
57  *\r
58  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
59  * flash co-routine is created with co-routine priority 1.  This means that\r
60  * the queue should never contain more than a single item.  This is because\r
61  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
62  * a priority greater than the tasks posting to the queue it is guaranteed to\r
63  * have emptied the queue and blocked once again before the queue can contain\r
64  * any more date.  An error is indicated if an attempt to post data to the\r
65  * queue fails - indicating that the queue is already full.\r
66  *\r
67  */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "croutine.h"\r
72 #include "queue.h"\r
73 \r
74 /* Demo application includes. */\r
75 #include "partest.h"\r
76 #include "crflash.h"\r
77 \r
78 /* The queue should only need to be of length 1.  See the description at the\r
79 top of the file. */\r
80 #define crfQUEUE_LENGTH         1\r
81 \r
82 #define crfFIXED_DELAY_PRIORITY         0\r
83 #define crfFLASH_PRIORITY                       1\r
84 \r
85 /* Only one flash co-routine is created so the index is not significant. */\r
86 #define crfFLASH_INDEX                          0\r
87 \r
88 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
89 created. */\r
90 #define crfMAX_FLASH_TASKS                      8\r
91 \r
92 /* We don't want to block when posting to the queue. */\r
93 #define crfPOSTING_BLOCK_TIME           0\r
94 \r
95 /* Added by MPi, this define is added in order to make the vParTestToggleLED()\r
96 work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected \r
97 to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */ \r
98 #define PDR00_Offset    8\r
99 \r
100 /*\r
101  * The 'fixed delay' co-routine as described at the top of the file.\r
102  */\r
103 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
104 \r
105 /*\r
106  * The 'flash' co-routine as described at the top of the file.\r
107  */\r
108 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
109 \r
110 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
111 'flash' co-routine. */\r
112 static xQueueHandle xFlashQueue;\r
113 \r
114 /* This will be set to pdFALSE if we detect an error. */\r
115 static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * See the header file for details.\r
121  */\r
122 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )\r
123 {\r
124 unsigned portBASE_TYPE uxIndex;\r
125 \r
126         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
127         {\r
128                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
129         }\r
130 \r
131         /* Create the queue used to pass data between the co-routines. */\r
132         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
133 \r
134         if( xFlashQueue )\r
135         {\r
136                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
137                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
138                 {\r
139                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
140                 }\r
141 \r
142                 /* Create the 'flash' co-routine. */\r
143                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
144         }\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
149 {\r
150 /* Even though this is a co-routine the xResult variable does not need to be\r
151 static as we do not need it to maintain its state between blocks. */\r
152 signed portBASE_TYPE xResult;\r
153 /* The uxIndex parameter of the co-routine function is used as an index into\r
154 the xFlashRates array to obtain the delay period to use. */\r
155 static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,\r
156                                                                                                                                 200 / portTICK_RATE_MS,\r
157                                                                                                                                 250 / portTICK_RATE_MS,\r
158                                                                                                                                 300 / portTICK_RATE_MS,\r
159                                                                                                                                 350 / portTICK_RATE_MS,\r
160                                                                                                                                 400 / portTICK_RATE_MS,\r
161                                                                                                                                 450 / portTICK_RATE_MS,\r
162                                                                                                                                 500  / portTICK_RATE_MS };\r
163 \r
164         /* Co-routines MUST start with a call to crSTART. */\r
165         crSTART( xHandle );\r
166 \r
167         for( ;; )\r
168         {\r
169                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
170                 flash. */\r
171                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
172 \r
173                 if( xResult != pdPASS )\r
174                 {\r
175                         /* For the reasons stated at the top of the file we should always\r
176                         find that we can post to the queue.  If we could not then an error\r
177                         has occurred. */\r
178                         uxCoRoutineFlashStatus = pdFAIL;\r
179                 }\r
180 \r
181                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
182         }\r
183 \r
184         /* Co-routines MUST end with a call to crEND. */\r
185         crEND();\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
190 {\r
191 /* Even though this is a co-routine the variable do not need to be\r
192 static as we do not need it to maintain their state between blocks. */\r
193 signed portBASE_TYPE xResult;\r
194 unsigned portBASE_TYPE uxLEDToFlash;\r
195 \r
196         /* Co-routines MUST start with a call to crSTART. */\r
197         crSTART( xHandle );\r
198         ( void ) uxIndex;\r
199         \r
200         for( ;; )\r
201         {\r
202                 /* Block to wait for the number of the LED to flash. */\r
203                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
204 \r
205                 if( xResult != pdPASS )\r
206                 {\r
207                         /* We would not expect to wake unless we received something. */\r
208                         uxCoRoutineFlashStatus = pdFAIL;\r
209                 }\r
210                 else\r
211                 {\r
212                         /* We received the number of an LED to flash - flash it! */\r
213                         /* Added by MPi, PDR00_Offset is added in order to make the \r
214                         vParTestToggleLED() work. */ \r
215                         vParTestToggleLED( uxLEDToFlash +  PDR00_Offset );\r
216                 }\r
217         }\r
218 \r
219         /* Co-routines MUST end with a call to crEND. */\r
220         crEND();\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )\r
225 {\r
226         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
227         or not. */\r
228         return uxCoRoutineFlashStatus;\r
229 }\r
230 \r