]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/flash.c
bbf97f59524ecfe264d13c246878230871eda235
[freertos] / Demo / MB96350_Softune_Dice_Kit / flash.c
1 /*\r
2         FreeRTOS.org V5.1.1 - 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     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /**\r
51  * This version of flash .c is for use on systems that have limited stack space\r
52  * and no display facilities.  The complete version can be found in the \r
53  * Demo/Common/Full directory.\r
54  * \r
55  * Three tasks are created, each of which flash an LED at a different rate.  The first \r
56  * LED flashes every 200ms, the second every 400ms, the third every 600ms.\r
57  *\r
58  * The LED flash tasks provide instant visual feedback.  They show that the scheduler \r
59  * is still operational.\r
60  *\r
61  */\r
62 \r
63 \r
64 #include <stdlib.h>\r
65 \r
66 /* Scheduler include files. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 #include "croutine.h"\r
70 \r
71 /* Demo program include files. */\r
72 #include "partest.h"\r
73 #include "flash.h"\r
74 \r
75 #define ledSTACK_SIZE           configMINIMAL_STACK_SIZE\r
76 #define ledNUMBER_OF_LEDS       ( 7 )\r
77 #define ledFLASH_RATE_BASE      ( ( portTickType ) 333 )\r
78 \r
79 #define ledMAX_FLASH_CO_ROUTINES        7\r
80 #define ledCO_ROUTINE_PRIORITY          0\r
81 \r
82 /* The task that is created three times. */\r
83 static void vLEDFlashTask( void *pvParameters );\r
84 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );\r
85 \r
86 /* This task is created once, but itself creates 7 co-routines. */\r
87 static void vLEDCoRoutineControlTask( void *pvParameters );\r
88 \r
89 static xTaskHandle xFlashTaskHandles[ ledNUMBER_OF_LEDS ] = { 0 };\r
90 static xTaskHandle xCoroutineTask;\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 void vStartLEDFlashTasks( unsigned portBASE_TYPE uxPriority )\r
95 {\r
96 signed short sLEDTask;\r
97 \r
98         /* Create the three tasks that flash segments on the first LED. */\r
99         for( sLEDTask = 0; sLEDTask < ledNUMBER_OF_LEDS; ++sLEDTask )\r
100         {\r
101                 /* Spawn the task. */\r
102                 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", ledSTACK_SIZE, ( void * ) sLEDTask, uxPriority, &( xFlashTaskHandles[ sLEDTask ] ) );\r
103         }\r
104 \r
105         /* Create the task in which the co-routines run. */\r
106         xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", ledSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
111 {\r
112 short sLEDTask;\r
113 \r
114         if( ucIndex == 0 )\r
115         {\r
116                 for( sLEDTask = 0; sLEDTask < ledNUMBER_OF_LEDS; ++sLEDTask )\r
117                 {\r
118                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
119                         {\r
120                                 if( sSuspendTasks == pdTRUE )\r
121                                 {\r
122                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
123                                 }\r
124                                 else\r
125                                 {\r
126                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
127                                 }\r
128                         }\r
129                 }\r
130         }\r
131         else\r
132         {\r
133                 if( sSuspendTasks == pdTRUE )\r
134                 {\r
135                         vTaskSuspend( xCoroutineTask );\r
136                 }\r
137                 else\r
138                 {\r
139                         vTaskResume( xCoroutineTask );\r
140                 }\r
141         }\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void vLEDFlashTask( void * pvParameters )\r
146 {\r
147 portTickType xFlashRate, xLastFlashTime;\r
148 unsigned short usLED;\r
149 \r
150         /* The LED to flash is passed in as the task parameter. */\r
151         usLED = ( unsigned short ) pvParameters;\r
152 \r
153         /* Calculate the rate at which this task is going to toggle its LED. */\r
154         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );\r
155         xFlashRate /= portTICK_RATE_MS;\r
156 \r
157         /* We will turn the LED on and off again in the delay period, so each\r
158         delay is only half the total period. */\r
159         xFlashRate /= ( portTickType ) 2;\r
160 \r
161         /* We need to initialise xLastFlashTime prior to the first call to \r
162         vTaskDelayUntil(). */\r
163         xLastFlashTime = xTaskGetTickCount();\r
164 \r
165         for(;;)\r
166         {\r
167                 /* Delay for half the flash period then turn the LED on. */\r
168                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
169                 vParTestToggleLED( usLED );\r
170 \r
171                 /* Delay for half the flash period then turn the LED off. */\r
172                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
173                 vParTestToggleLED( usLED );\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void vLEDCoRoutineControlTask( void *pvParameters )\r
179 {\r
180 unsigned short usCoroutine;\r
181 \r
182         ( void ) pvParameters;\r
183 \r
184         for( usCoroutine = 0; usCoroutine < ledMAX_FLASH_CO_ROUTINES; usCoroutine++ )\r
185         {\r
186                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
187         }\r
188 \r
189         for( ;; )\r
190         {\r
191                 vCoRoutineSchedule();\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )\r
197 {\r
198 /* The usIndex parameter of the co-routine function is used as an index into\r
199 the xFlashRates array to obtain the delay period to use. */\r
200 static const portTickType xFlashRates[ ledMAX_FLASH_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,\r
201                                                                                                                                 300 / portTICK_RATE_MS,\r
202                                                                                                                                 450 / portTICK_RATE_MS,\r
203                                                                                                                                 600 / portTICK_RATE_MS,\r
204                                                                                                                                 750 / portTICK_RATE_MS,\r
205                                                                                                                                 900 / portTICK_RATE_MS,\r
206                                                                                                                                 1050 / portTICK_RATE_MS };\r
207 \r
208         /* Co-routines MUST start with a call to crSTART. */\r
209         crSTART( xHandle );\r
210 \r
211         for( ;; )\r
212         {\r
213                 vParTestToggleLED( usIndex + 8 );\r
214                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
215         }\r
216 \r
217         /* Co-routines MUST end with a call to crEND. */\r
218         crEND();\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 \r