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