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