]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/DiceTask.c
Extend FX16 functionality.
[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 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "semphr.h"\r
53 \r
54 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms             ( 20 )\r
55 #define diceRUN_TIME    ( 2000 / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )\r
56 \r
57 \r
58 #define diceEND_DELAY                   ( 5000 / portTICK_RATE_MS )\r
59 \r
60 #define dice7SEG_Value( x )             *( pucDisplayOutput[ x ] )\r
61 \r
62 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )\r
63 \r
64 static const char cDisplaySegments[ 2 ][ 11 ] =\r
65 {\r
66         { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 },\r
67         { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f }\r
68 };\r
69 \r
70 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };\r
71 \r
72 extern volatile unsigned char *pucDisplayOutput[ 2 ];\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 void vDiceTask( void *pvParameters )\r
77 {\r
78 unsigned char ucDiceValue, ucIndex;\r
79 unsigned long ulDiceRunTime;\r
80 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
81 \r
82         /* Two instances of this task are created so the task parameter is used\r
83         to pass in an index that allows this task to know which file scope variables\r
84         it should use.  Cast this index into a usable type. */\r
85         ucIndex = ( unsigned char ) pvParameters;\r
86         \r
87         /* A binary semaphore is used to signal button push events.  Create the\r
88         semaphore before it is used. */\r
89         vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );\r
90 \r
91         /* Make sure the semaphore starts in the wanted state - no button pushes \r
92         pending. This call will just clear any button pushes that are latched.\r
93         Passing in 0 as the block time means the call will not wait for any further\r
94         button pushes. */\r
95         prvButtonHit( ucIndex, 0 );\r
96 \r
97         /* Seed the random number generator. */\r
98         srand( ( unsigned char ) diceRUN_TIME );\r
99 \r
100         for( ;; )\r
101         {\r
102                 /* Wait for a button push.  This task will enter the Blocked state\r
103                 (will not run again) until after a button has been pushed. */\r
104                 prvButtonHit( ucIndex, portMAX_DELAY );\r
105                 \r
106                 /* The next line will only execute after a button has been pushed -\r
107                 initialise the variable used to shake the dice. */\r
108                 ulDiceRunTime = diceRUN_TIME;;                          \r
109 \r
110                 /* Suspend the flash tasks so this task has exclusive access to the\r
111                 display. */\r
112                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
113 \r
114                 while( ulDiceRunTime > 0 )\r
115                 {\r
116                         ulDiceRunTime--;\r
117 \r
118                         /* Generate and display a random number. */\r
119                         ucDiceValue = rand() % 6 + 1;\r
120                         dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];\r
121 \r
122                         /* Block/sleep for a very short time before generating the next\r
123                         random number. */\r
124                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms / portTICK_RATE_MS );\r
125                 }\r
126 \r
127                 /* Wait for a short time before resuming (un-suspending) the flash \r
128                 task.  The flash tasks are only restarted if a button is not pushed\r
129                 during this delay - if a button is pushed then the dice are shaken\r
130                 again. \r
131 \r
132                 First...clear any button pushes that are already pending.  Again a\r
133                 block time of zero is used so the function does not wait for any \r
134                 pushes. */\r
135                 prvButtonHit( ucIndex, 0 );\r
136 \r
137                 /* Second...peek the semaphore.  This task will block/sleep until a\r
138                 button is pushed again, but because the peek function is used a \r
139                 button being pushed will unblock the task but remain pending. */\r
140                 if( xQueuePeek( xSemaphores[ ucIndex ], NULL, diceEND_DELAY ) == pdFALSE )\r
141                 {\r
142                         *pucDisplayOutput[ ucIndex ] = 0xff;\r
143                         vSuspendFlashTasks( ucIndex, pdFALSE );\r
144                 }\r
145         }\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 __interrupt void vExternalInt8Handler( void )\r
150 {\r
151 short sHigherPriorityTaskWoken = pdFALSE;\r
152 \r
153         /* Reset the interrupt. */\r
154         EIRR1_ER8 = 0;\r
155 \r
156         xSemaphoreGiveFromISR( xSemaphores[ 0 ], &sHigherPriorityTaskWoken );\r
157 \r
158         if( sHigherPriorityTaskWoken != pdFALSE )\r
159         {\r
160                 portYIELD_FROM_ISR();\r
161         }\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 __interrupt void vExternalInt9Handler( void )\r
166 {\r
167 short sHigherPriorityTaskWoken = pdFALSE;\r
168 \r
169         /* Reset the interrupt. */\r
170         EIRR1_ER9 = 0;\r
171 \r
172         xSemaphoreGiveFromISR( xSemaphores[ 1 ], &sHigherPriorityTaskWoken );\r
173 \r
174         if( sHigherPriorityTaskWoken != pdFALSE )\r
175         {\r
176                 portYIELD_FROM_ISR();\r
177         }\r
178 }\r
179 \r
180 \r
181 \r
182 \r
183 \r
184 \r
185 \r