]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crflash.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / Common / Minimal / crflash.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * This demo application file demonstrates the use of queues to pass data\r
72  * between co-routines.\r
73  *\r
74  * N represents the number of 'fixed delay' co-routines that are created and\r
75  * is set during initialisation.\r
76  *\r
77  * N 'fixed delay' co-routines are created that just block for a fixed\r
78  * period then post the number of an LED onto a queue.  Each such co-routine\r
79  * uses a different block period.  A single 'flash' co-routine is also created\r
80  * that blocks on the same queue, waiting for the number of the next LED it\r
81  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
82  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
83  * LED N-1 is caused to flash at a different rate.\r
84  *\r
85  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
86  * flash co-routine is created with co-routine priority 1.  This means that\r
87  * the queue should never contain more than a single item.  This is because\r
88  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
89  * a priority greater than the tasks posting to the queue it is guaranteed to\r
90  * have emptied the queue and blocked once again before the queue can contain\r
91  * any more date.  An error is indicated if an attempt to post data to the\r
92  * queue fails - indicating that the queue is already full.\r
93  *\r
94  */\r
95 \r
96 /* Scheduler includes. */\r
97 #include "FreeRTOS.h"\r
98 #include "croutine.h"\r
99 #include "queue.h"\r
100 \r
101 /* Demo application includes. */\r
102 #include "partest.h"\r
103 #include "crflash.h"\r
104 \r
105 /* The queue should only need to be of length 1.  See the description at the\r
106 top of the file. */\r
107 #define crfQUEUE_LENGTH         1\r
108 \r
109 #define crfFIXED_DELAY_PRIORITY         0\r
110 #define crfFLASH_PRIORITY                       1\r
111 \r
112 /* Only one flash co-routine is created so the index is not significant. */\r
113 #define crfFLASH_INDEX                          0\r
114 \r
115 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
116 created. */\r
117 #define crfMAX_FLASH_TASKS                      8\r
118 \r
119 /* We don't want to block when posting to the queue. */\r
120 #define crfPOSTING_BLOCK_TIME           0\r
121 \r
122 /*\r
123  * The 'fixed delay' co-routine as described at the top of the file.\r
124  */\r
125 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
126 \r
127 /*\r
128  * The 'flash' co-routine as described at the top of the file.\r
129  */\r
130 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );\r
131 \r
132 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
133 'flash' co-routine. */\r
134 static QueueHandle_t xFlashQueue;\r
135 \r
136 /* This will be set to pdFALSE if we detect an error. */\r
137 static BaseType_t xCoRoutineFlashStatus = pdPASS;\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * See the header file for details.\r
143  */\r
144 void vStartFlashCoRoutines( UBaseType_t uxNumberToCreate )\r
145 {\r
146 UBaseType_t uxIndex;\r
147 \r
148         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
149         {\r
150                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
151         }\r
152 \r
153         /* Create the queue used to pass data between the co-routines. */\r
154         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( UBaseType_t ) );\r
155 \r
156         if( xFlashQueue )\r
157         {\r
158                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
159                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
160                 {\r
161                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
162                 }\r
163 \r
164                 /* Create the 'flash' co-routine. */\r
165                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
166         }\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
171 {\r
172 /* Even though this is a co-routine the xResult variable does not need to be\r
173 static as we do not need it to maintain its state between blocks. */\r
174 BaseType_t xResult;\r
175 /* The uxIndex parameter of the co-routine function is used as an index into\r
176 the xFlashRates array to obtain the delay period to use. */\r
177 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,\r
178                                                                                                                                 200 / portTICK_PERIOD_MS,\r
179                                                                                                                                 250 / portTICK_PERIOD_MS,\r
180                                                                                                                                 300 / portTICK_PERIOD_MS,\r
181                                                                                                                                 350 / portTICK_PERIOD_MS,\r
182                                                                                                                                 400 / portTICK_PERIOD_MS,\r
183                                                                                                                                 450 / portTICK_PERIOD_MS,\r
184                                                                                                                                 500  / portTICK_PERIOD_MS };\r
185 \r
186         /* Co-routines MUST start with a call to crSTART. */\r
187         crSTART( xHandle );\r
188 \r
189         for( ;; )\r
190         {\r
191                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
192                 flash. */\r
193                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
194 \r
195                 if( xResult != pdPASS )\r
196                 {\r
197                         /* For the reasons stated at the top of the file we should always\r
198                         find that we can post to the queue.  If we could not then an error\r
199                         has occurred. */\r
200                         xCoRoutineFlashStatus = pdFAIL;\r
201                 }\r
202 \r
203                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
204         }\r
205 \r
206         /* Co-routines MUST end with a call to crEND. */\r
207         crEND();\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )\r
212 {\r
213 /* Even though this is a co-routine the variable do not need to be\r
214 static as we do not need it to maintain their state between blocks. */\r
215 BaseType_t xResult;\r
216 UBaseType_t uxLEDToFlash;\r
217 \r
218         /* Co-routines MUST start with a call to crSTART. */\r
219         crSTART( xHandle );\r
220         ( void ) uxIndex;\r
221         \r
222         for( ;; )\r
223         {\r
224                 /* Block to wait for the number of the LED to flash. */\r
225                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
226 \r
227                 if( xResult != pdPASS )\r
228                 {\r
229                         /* We would not expect to wake unless we received something. */\r
230                         xCoRoutineFlashStatus = pdFAIL;\r
231                 }\r
232                 else\r
233                 {\r
234                         /* We received the number of an LED to flash - flash it! */\r
235                         vParTestToggleLED( uxLEDToFlash );\r
236                 }\r
237         }\r
238 \r
239         /* Co-routines MUST end with a call to crEND. */\r
240         crEND();\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 BaseType_t xAreFlashCoRoutinesStillRunning( void )\r
245 {\r
246         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
247         or not. */\r
248         return xCoRoutineFlashStatus;\r
249 }\r
250 \r