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