]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/DiceTask.c
Prepare for V5.3.0 release.
[freertos] / Demo / MB96350_Softune_Dice_Kit / DiceTask.c
1 /*\r
2         FreeRTOS.org V5.3.0 - 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the 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.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* \r
54  * Defines the 'dice' tasks as described at the top of main.c\r
55  */\r
56 \r
57 \r
58 /* Kernel includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "semphr.h"\r
62 \r
63 /* Delays used within the dice functionality.  All delays are defined in milliseconds. */\r
64 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms             ( 20 / portTICK_RATE_MS )\r
65 #define diceSHAKE_TIME                                                  ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )\r
66 #define diceSHORT_PAUSE_BEFORE_SHAKE                    ( 250 / portTICK_RATE_MS )\r
67 #define diceDELAY_WHILE_DISPLAYING_RESULT               ( 5000 / portTICK_RATE_MS )\r
68 \r
69 /* Macro to access the display ports. */\r
70 #define dice7SEG_Value( x )             ( *( pucDisplayOutput[ x ] ) )\r
71 \r
72 /* Checks the semaphore use to communicate button push events.  A block time\r
73 can be specified - this is the time to wait for a button push to occur should\r
74 one have not already occurred. */\r
75 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )\r
76 \r
77 /* Defines the outputs required for each digit on the display. */\r
78 static const char cDisplaySegments[ 2 ][ 11 ] =\r
79 {\r
80         { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */\r
81         { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }  /* Right display. */\r
82 };\r
83 \r
84 /* The semaphores used to communicate button push events between the button\r
85 input interrupt handlers and the dice tasks.  Two dice tasks are created so two\r
86 semaphores are required. */\r
87 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };\r
88 \r
89 /* Defines the ports used to write to the display.  This variable is defined in\r
90 partest.c, which contains the LED set/clear/toggle functions. */\r
91 extern volatile unsigned char *pucDisplayOutput[ 2 ];\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* \r
96  * Defines the 'dice' tasks as described at the top of main.c\r
97  */\r
98 void vDiceTask( void *pvParameters )\r
99 {\r
100 unsigned char ucDiceValue, ucIndex;\r
101 unsigned long ulDiceRunTime;\r
102 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
103 \r
104 \r
105 \r
106         /* Two instances of this task are created so the task parameter is used\r
107         to pass in a constant that indicates whether this task is controlling\r
108         the left side or right side display.  The constant is used as an index\r
109         into the arrays defined at file scope within this file. */\r
110         ucIndex = ( unsigned char ) pvParameters;\r
111         \r
112         /* A binary semaphore is used to signal button push events.  Create the\r
113         semaphore before it is used. */\r
114         vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );\r
115 \r
116         /* Make sure the semaphore starts in the wanted state - no button pushes \r
117         pending. This call will just clear any button pushes that are latched.\r
118         Passing in 0 as the block time means the call will not wait for any further\r
119         button pushes but instead return immediately. */\r
120         prvButtonHit( ucIndex, 0 );\r
121 \r
122         /* Seed the random number generator. */\r
123         srand( ( unsigned char ) diceSHAKE_TIME );\r
124 \r
125 \r
126 \r
127 \r
128         /* Start the task proper.  A loop will be performed each time a button is\r
129         pushed.  The task will remain in the blocked state (sleeping) until a \r
130         button is pushed. */\r
131         for( ;; )\r
132         {\r
133                 /* Wait for a button push.  This task will enter the Blocked state\r
134                 (will not run again) until after a button has been pushed. */\r
135                 prvButtonHit( ucIndex, portMAX_DELAY );\r
136                 \r
137                 /* The next line will only execute after a button has been pushed -\r
138                 initialise the variable used to control the time the dice is shaken\r
139                 for. */\r
140                 ulDiceRunTime = diceSHAKE_TIME;                         \r
141 \r
142                 /* Suspend the flash tasks so this task has exclusive access to the\r
143                 display. */\r
144                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
145 \r
146                 /* Clear the display and pause for a short time, before starting to\r
147                 shake. */\r
148                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
149                 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );\r
150 \r
151                 /* Keep generating and displaying random numbers until the shake time\r
152                 expires. */\r
153                 while( ulDiceRunTime > 0 )\r
154                 {\r
155                         ulDiceRunTime--;\r
156 \r
157                         /* Generate and display a random number. */\r
158                         ucDiceValue = rand() % 6 + 1;\r
159                         dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];\r
160 \r
161                         /* Block/sleep for a very short time before generating the next\r
162                         random number. */\r
163                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );\r
164                 }\r
165 \r
166 \r
167 \r
168                 /* Clear any button pushes that are pending because a button bounced, or\r
169                 was pressed while the dice were shaking.  Again a block time of zero is \r
170                 used so the function does not wait for any pushes but instead returns\r
171                 immediately. */\r
172                 prvButtonHit( ucIndex, 0 );\r
173 \r
174                 /* Delay for a short while to display the dice shake result.  Use a queue\r
175                 peek here instead of a vTaskDelay() allows the delay to be interrupted by\r
176                 a button push.  If a button is pressed xQueuePeek() will return but the\r
177                 button push will remain pending to be read again at the top of this for\r
178                 loop.  It is safe to uses a queue function on a semaphore handle as\r
179                 semaphores are implemented as macros that uses queues, so the two are \r
180                 basically the same thing. */\r
181                 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );\r
182 \r
183                 /* Clear the display then resume the tasks or co-routines that were using\r
184                 the segments of the display. */\r
185                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
186                 vSuspendFlashTasks( ucIndex, pdFALSE );\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /* Handler for the SW2 button push interrupt. */\r
192 __interrupt void vExternalInt8Handler( void )\r
193 {\r
194 short sHigherPriorityTaskWoken = pdFALSE;\r
195 \r
196         /* Reset the interrupt. */\r
197         EIRR1_ER8 = 0;\r
198 \r
199         /* Check the semaphore has been created before attempting to use it. */\r
200         if( xSemaphores[ configLEFT_DISPLAY ] != NULL )\r
201         {\r
202                 /* Send a message via the semaphore to the dice task that controls the\r
203                 left side display.  This will unblock the task if it is blocked waiting\r
204                 for a button push. */\r
205                 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );\r
206         }\r
207 \r
208         /* If sending the semaphore unblocked a task, and the unblocked task has a\r
209         priority that is higher than the currently running task, then force a context\r
210         switch. */\r
211         if( sHigherPriorityTaskWoken != pdFALSE )\r
212         {\r
213                 portYIELD_FROM_ISR();\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */\r
219 __interrupt void vExternalInt9Handler( void )\r
220 {\r
221 short sHigherPriorityTaskWoken = pdFALSE;\r
222 \r
223         /* Reset the interrupt. */\r
224         EIRR1_ER9 = 0;\r
225 \r
226         if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )\r
227         {\r
228                 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );\r
229         }\r
230 \r
231         if( sHigherPriorityTaskWoken != pdFALSE )\r
232         {\r
233                 portYIELD_FROM_ISR();\r
234         }\r
235 }\r
236 \r
237 \r
238 \r
239 \r
240 \r
241 \r
242 \r