]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/DiceTask.c
a07c5cbb7e75c31ef8c3f7272fa6487161fb8f20
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / DiceTask.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 \r
68 /* \r
69  * Defines the 'dice' tasks as described at the top of main.c\r
70  */\r
71 \r
72 \r
73 /* Kernel includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* Delays used within the dice functionality.  All delays are defined in milliseconds. */\r
79 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms             ( 20 / portTICK_RATE_MS )\r
80 #define diceSHAKE_TIME                                                  ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )\r
81 #define diceSHORT_PAUSE_BEFORE_SHAKE                    ( 250 / portTICK_RATE_MS )\r
82 #define diceDELAY_WHILE_DISPLAYING_RESULT               ( 5000 / portTICK_RATE_MS )\r
83 \r
84 /* Macro to access the display ports. */\r
85 #define dice7SEG_Value( x )             ( *( pucDisplayOutput[ x ] ) )\r
86 \r
87 /* Checks the semaphore use to communicate button push events.  A block time\r
88 can be specified - this is the time to wait for a button push to occur should\r
89 one have not already occurred. */\r
90 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )\r
91 \r
92 /* Defines the outputs required for each digit on the display. */\r
93 static const char cDisplaySegments[ 2 ][ 11 ] =\r
94 {\r
95         { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */\r
96         { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }  /* Right display. */\r
97 };\r
98 \r
99 /* The semaphores used to communicate button push events between the button\r
100 input interrupt handlers and the dice tasks.  Two dice tasks are created so two\r
101 semaphores are required. */\r
102 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };\r
103 \r
104 /* Defines the ports used to write to the display.  This variable is defined in\r
105 partest.c, which contains the LED set/clear/toggle functions. */\r
106 extern volatile unsigned char *pucDisplayOutput[ 2 ];\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* \r
111  * Defines the 'dice' tasks as described at the top of main.c\r
112  */\r
113 void vDiceTask( void *pvParameters )\r
114 {\r
115 unsigned char ucDiceValue, ucIndex;\r
116 unsigned long ulDiceRunTime;\r
117 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
118 \r
119 \r
120 \r
121         /* Two instances of this task are created so the task parameter is used\r
122         to pass in a constant that indicates whether this task is controlling\r
123         the left side or right side display.  The constant is used as an index\r
124         into the arrays defined at file scope within this file. */\r
125         ucIndex = ( unsigned char ) pvParameters;\r
126         \r
127         /* A binary semaphore is used to signal button push events.  Create the\r
128         semaphore before it is used. */\r
129         vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );\r
130 \r
131         /* Make sure the semaphore starts in the wanted state - no button pushes \r
132         pending. This call will just clear any button pushes that are latched.\r
133         Passing in 0 as the block time means the call will not wait for any further\r
134         button pushes but instead return immediately. */\r
135         prvButtonHit( ucIndex, 0 );\r
136 \r
137         /* Seed the random number generator. */\r
138         srand( ( unsigned char ) diceSHAKE_TIME );\r
139 \r
140 \r
141 \r
142 \r
143         /* Start the task proper.  A loop will be performed each time a button is\r
144         pushed.  The task will remain in the blocked state (sleeping) until a \r
145         button is pushed. */\r
146         for( ;; )\r
147         {\r
148                 /* Wait for a button push.  This task will enter the Blocked state\r
149                 (will not run again) until after a button has been pushed. */\r
150                 prvButtonHit( ucIndex, portMAX_DELAY );\r
151                 \r
152                 /* The next line will only execute after a button has been pushed -\r
153                 initialise the variable used to control the time the dice is shaken\r
154                 for. */\r
155                 ulDiceRunTime = diceSHAKE_TIME;                         \r
156 \r
157                 /* Suspend the flash tasks so this task has exclusive access to the\r
158                 display. */\r
159                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
160 \r
161                 /* Clear the display and pause for a short time, before starting to\r
162                 shake. */\r
163                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
164                 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );\r
165 \r
166                 /* Keep generating and displaying random numbers until the shake time\r
167                 expires. */\r
168                 while( ulDiceRunTime > 0 )\r
169                 {\r
170                         ulDiceRunTime--;\r
171 \r
172                         /* Generate and display a random number. */\r
173                         ucDiceValue = rand() % 6 + 1;\r
174                         dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];\r
175 \r
176                         /* Block/sleep for a very short time before generating the next\r
177                         random number. */\r
178                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );\r
179                 }\r
180 \r
181 \r
182 \r
183                 /* Clear any button pushes that are pending because a button bounced, or\r
184                 was pressed while the dice were shaking.  Again a block time of zero is \r
185                 used so the function does not wait for any pushes but instead returns\r
186                 immediately. */\r
187                 prvButtonHit( ucIndex, 0 );\r
188 \r
189                 /* Delay for a short while to display the dice shake result.  Use a queue\r
190                 peek here instead of a vTaskDelay() allows the delay to be interrupted by\r
191                 a button push.  If a button is pressed xQueuePeek() will return but the\r
192                 button push will remain pending to be read again at the top of this for\r
193                 loop.  It is safe to uses a queue function on a semaphore handle as\r
194                 semaphores are implemented as macros that uses queues, so the two are \r
195                 basically the same thing. */\r
196                 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );\r
197 \r
198                 /* Clear the display then resume the tasks or co-routines that were using\r
199                 the segments of the display. */\r
200                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
201                 vSuspendFlashTasks( ucIndex, pdFALSE );\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 /* Handler for the SW2 button push interrupt. */\r
207 __interrupt void vExternalInt8Handler( void )\r
208 {\r
209 short sHigherPriorityTaskWoken = pdFALSE;\r
210 \r
211         /* Reset the interrupt. */\r
212         EIRR1_ER8 = 0;\r
213 \r
214         /* Check the semaphore has been created before attempting to use it. */\r
215         if( xSemaphores[ configLEFT_DISPLAY ] != NULL )\r
216         {\r
217                 /* Send a message via the semaphore to the dice task that controls the\r
218                 left side display.  This will unblock the task if it is blocked waiting\r
219                 for a button push. */\r
220                 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );\r
221         }\r
222 \r
223         /* If sending the semaphore unblocked a task, and the unblocked task has a\r
224         priority that is higher than the currently running task, then force a context\r
225         switch. */\r
226         if( sHigherPriorityTaskWoken != pdFALSE )\r
227         {\r
228                 portYIELD_FROM_ISR();\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */\r
234 __interrupt void vExternalInt9Handler( void )\r
235 {\r
236 short sHigherPriorityTaskWoken = pdFALSE;\r
237 \r
238         /* Reset the interrupt. */\r
239         EIRR1_ER9 = 0;\r
240 \r
241         if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )\r
242         {\r
243                 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );\r
244         }\r
245 \r
246         if( sHigherPriorityTaskWoken != pdFALSE )\r
247         {\r
248                 portYIELD_FROM_ISR();\r
249         }\r
250 }\r
251 \r
252 \r
253 \r
254 \r
255 \r
256 \r
257 \r