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