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