]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/DiceTask.c
Tidy up and prepare for release.
[freertos] / Demo / MB96350_Softune_Dice_Kit / DiceTask.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 /* \r
52  * Defines the 'dice' tasks as described at the top of main.c\r
53  */\r
54 \r
55 \r
56 /* Kernel includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "semphr.h"\r
60 \r
61 /* Delays used within the dice functionality.  All delays are defined in milliseconds. */\r
62 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms             ( 20 / portTICK_RATE_MS )\r
63 #define diceSHAKE_TIME                                                  ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )\r
64 #define diceSHORT_PAUSE_BEFORE_SHAKE                    ( 250 / portTICK_RATE_MS )\r
65 #define diceDELAY_WHILE_DISPLAYING_RESULT               ( 5000 / portTICK_RATE_MS )\r
66 \r
67 /* Macro to access the display ports. */\r
68 #define dice7SEG_Value( x )             ( *( pucDisplayOutput[ x ] ) )\r
69 \r
70 /* Checks the semaphore use to communicate button push events.  A block time\r
71 can be specified - this is the time to wait for a button push to occur should\r
72 one have not already occurred. */\r
73 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )\r
74 \r
75 /* Defines the outputs required for each digit on the display. */\r
76 static const char cDisplaySegments[ 2 ][ 11 ] =\r
77 {\r
78         { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */\r
79         { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }  /* Right display. */\r
80 };\r
81 \r
82 /* The semaphores used to communicate button push events between the button\r
83 input interrupt handlers and the dice tasks.  Two dice tasks are created so two\r
84 semaphores are required. */\r
85 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };\r
86 \r
87 /* Defines the ports used to write to the display.  This variable is defined in\r
88 partest.c, which contains the LED set/clear/toggle functions. */\r
89 extern volatile unsigned char *pucDisplayOutput[ 2 ];\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* \r
94  * Defines the 'dice' tasks as described at the top of main.c\r
95  */\r
96 void vDiceTask( void *pvParameters )\r
97 {\r
98 unsigned char ucDiceValue, ucIndex;\r
99 unsigned long ulDiceRunTime;\r
100 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
101 \r
102 \r
103 \r
104         /* Two instances of this task are created so the task parameter is used\r
105         to pass in a constant that indicates whether this task is controlling\r
106         the left side or right side display.  The constant is used as an index\r
107         into the arrays defined at file scope within this file. */\r
108         ucIndex = ( unsigned char ) pvParameters;\r
109         \r
110         /* A binary semaphore is used to signal button push events.  Create the\r
111         semaphore before it is used. */\r
112         vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );\r
113 \r
114         /* Make sure the semaphore starts in the wanted state - no button pushes \r
115         pending. This call will just clear any button pushes that are latched.\r
116         Passing in 0 as the block time means the call will not wait for any further\r
117         button pushes but instead return immediately. */\r
118         prvButtonHit( ucIndex, 0 );\r
119 \r
120         /* Seed the random number generator. */\r
121         srand( ( unsigned char ) diceSHAKE_TIME );\r
122 \r
123 \r
124 \r
125 \r
126         /* Start the task proper.  A loop will be performed each time a button is\r
127         pushed.  The task will remain in the blocked state (sleeping) until a \r
128         button is pushed. */\r
129         for( ;; )\r
130         {\r
131                 /* Wait for a button push.  This task will enter the Blocked state\r
132                 (will not run again) until after a button has been pushed. */\r
133                 prvButtonHit( ucIndex, portMAX_DELAY );\r
134                 \r
135                 /* The next line will only execute after a button has been pushed -\r
136                 initialise the variable used to control the time the dice is shaken\r
137                 for. */\r
138                 ulDiceRunTime = diceSHAKE_TIME;                         \r
139 \r
140                 /* Suspend the flash tasks so this task has exclusive access to the\r
141                 display. */\r
142                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
143 \r
144                 /* Clear the display and pause for a short time, before starting to\r
145                 shake. */\r
146                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
147                 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );\r
148 \r
149                 /* Keep generating and displaying random numbers until the shake time\r
150                 expires. */\r
151                 while( ulDiceRunTime > 0 )\r
152                 {\r
153                         ulDiceRunTime--;\r
154 \r
155                         /* Generate and display a random number. */\r
156                         ucDiceValue = rand() % 6 + 1;\r
157                         dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];\r
158 \r
159                         /* Block/sleep for a very short time before generating the next\r
160                         random number. */\r
161                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );\r
162                 }\r
163 \r
164 \r
165 \r
166                 /* Clear any button pushes that are pending because a button bounced, or\r
167                 was pressed while the dice were shaking.  Again a block time of zero is \r
168                 used so the function does not wait for any pushes but instead returns\r
169                 immediately. */\r
170                 prvButtonHit( ucIndex, 0 );\r
171 \r
172                 /* Delay for a short while to display the dice shake result.  Use a queue\r
173                 peek here instead of a vTaskDelay() allows the delay to be interrupted by\r
174                 a button push.  If a button is pressed xQueuePeek() will return but the\r
175                 button push will remain pending to be read again at the top of this for\r
176                 loop.  It is safe to uses a queue function on a semaphore handle as\r
177                 semaphores are implemented as macros that uses queues, so the two are \r
178                 basically the same thing. */\r
179                 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );\r
180 \r
181                 /* Clear the display then resume the tasks or co-routines that were using\r
182                 the segments of the display. */\r
183                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
184                 vSuspendFlashTasks( ucIndex, pdFALSE );\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 /* Handler for the SW2 button push interrupt. */\r
190 __interrupt void vExternalInt8Handler( void )\r
191 {\r
192 short sHigherPriorityTaskWoken = pdFALSE;\r
193 \r
194         /* Reset the interrupt. */\r
195         EIRR1_ER8 = 0;\r
196 \r
197         /* Check the semaphore has been created before attempting to use it. */\r
198         if( xSemaphores[ configLEFT_DISPLAY ] != NULL )\r
199         {\r
200                 /* Send a message via the semaphore to the dice task that controls the\r
201                 left side display.  This will unblock the task if it is blocked waiting\r
202                 for a button push. */\r
203                 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );\r
204         }\r
205 \r
206         /* If sending the semaphore unblocked a task, and the unblocked task has a\r
207         priority that is higher than the currently running task, then force a context\r
208         switch. */\r
209         if( sHigherPriorityTaskWoken != pdFALSE )\r
210         {\r
211                 portYIELD_FROM_ISR();\r
212         }\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */\r
217 __interrupt void vExternalInt9Handler( void )\r
218 {\r
219 short sHigherPriorityTaskWoken = pdFALSE;\r
220 \r
221         /* Reset the interrupt. */\r
222         EIRR1_ER9 = 0;\r
223 \r
224         if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )\r
225         {\r
226                 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );\r
227         }\r
228 \r
229         if( sHigherPriorityTaskWoken != pdFALSE )\r
230         {\r
231                 portYIELD_FROM_ISR();\r
232         }\r
233 }\r
234 \r
235 \r
236 \r
237 \r
238 \r
239 \r
240 \r