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