2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 * Defines the 'dice' tasks as described at the top of main.c
\r
58 /* Kernel includes. */
\r
59 #include "FreeRTOS.h"
\r
63 /* Delays used within the dice functionality. All delays are defined in milliseconds. */
\r
64 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 / portTICK_RATE_MS )
\r
65 #define diceSHAKE_TIME ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
\r
66 #define diceSHORT_PAUSE_BEFORE_SHAKE ( 250 / portTICK_RATE_MS )
\r
67 #define diceDELAY_WHILE_DISPLAYING_RESULT ( 5000 / portTICK_RATE_MS )
\r
69 /* Macro to access the display ports. */
\r
70 #define dice7SEG_Value( x ) ( *( pucDisplayOutput[ x ] ) )
\r
72 /* Checks the semaphore use to communicate button push events. A block time
\r
73 can be specified - this is the time to wait for a button push to occur should
\r
74 one have not already occurred. */
\r
75 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
\r
77 /* Defines the outputs required for each digit on the display. */
\r
78 static const char cDisplaySegments[ 2 ][ 11 ] =
\r
80 { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */
\r
81 { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f } /* Right display. */
\r
84 /* The semaphores used to communicate button push events between the button
\r
85 input interrupt handlers and the dice tasks. Two dice tasks are created so two
\r
86 semaphores are required. */
\r
87 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
\r
89 /* Defines the ports used to write to the display. This variable is defined in
\r
90 partest.c, which contains the LED set/clear/toggle functions. */
\r
91 extern volatile unsigned char *pucDisplayOutput[ 2 ];
\r
93 /*-----------------------------------------------------------*/
\r
96 * Defines the 'dice' tasks as described at the top of main.c
\r
98 void vDiceTask( void *pvParameters )
\r
100 unsigned char ucDiceValue, ucIndex;
\r
101 unsigned long ulDiceRunTime;
\r
102 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
\r
106 /* Two instances of this task are created so the task parameter is used
\r
107 to pass in a constant that indicates whether this task is controlling
\r
108 the left side or right side display. The constant is used as an index
\r
109 into the arrays defined at file scope within this file. */
\r
110 ucIndex = ( unsigned char ) pvParameters;
\r
112 /* A binary semaphore is used to signal button push events. Create the
\r
113 semaphore before it is used. */
\r
114 vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );
\r
116 /* Make sure the semaphore starts in the wanted state - no button pushes
\r
117 pending. This call will just clear any button pushes that are latched.
\r
118 Passing in 0 as the block time means the call will not wait for any further
\r
119 button pushes but instead return immediately. */
\r
120 prvButtonHit( ucIndex, 0 );
\r
122 /* Seed the random number generator. */
\r
123 srand( ( unsigned char ) diceSHAKE_TIME );
\r
128 /* Start the task proper. A loop will be performed each time a button is
\r
129 pushed. The task will remain in the blocked state (sleeping) until a
\r
130 button is pushed. */
\r
133 /* Wait for a button push. This task will enter the Blocked state
\r
134 (will not run again) until after a button has been pushed. */
\r
135 prvButtonHit( ucIndex, portMAX_DELAY );
\r
137 /* The next line will only execute after a button has been pushed -
\r
138 initialise the variable used to control the time the dice is shaken
\r
140 ulDiceRunTime = diceSHAKE_TIME;
\r
142 /* Suspend the flash tasks so this task has exclusive access to the
\r
144 vSuspendFlashTasks( ucIndex, pdTRUE );
\r
146 /* Clear the display and pause for a short time, before starting to
\r
148 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
149 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );
\r
151 /* Keep generating and displaying random numbers until the shake time
\r
153 while( ulDiceRunTime > 0 )
\r
157 /* Generate and display a random number. */
\r
158 ucDiceValue = rand() % 6 + 1;
\r
159 dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];
\r
161 /* Block/sleep for a very short time before generating the next
\r
163 vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );
\r
168 /* Clear any button pushes that are pending because a button bounced, or
\r
169 was pressed while the dice were shaking. Again a block time of zero is
\r
170 used so the function does not wait for any pushes but instead returns
\r
172 prvButtonHit( ucIndex, 0 );
\r
174 /* Delay for a short while to display the dice shake result. Use a queue
\r
175 peek here instead of a vTaskDelay() allows the delay to be interrupted by
\r
176 a button push. If a button is pressed xQueuePeek() will return but the
\r
177 button push will remain pending to be read again at the top of this for
\r
178 loop. It is safe to uses a queue function on a semaphore handle as
\r
179 semaphores are implemented as macros that uses queues, so the two are
\r
180 basically the same thing. */
\r
181 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );
\r
183 /* Clear the display then resume the tasks or co-routines that were using
\r
184 the segments of the display. */
\r
185 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
186 vSuspendFlashTasks( ucIndex, pdFALSE );
\r
189 /*-----------------------------------------------------------*/
\r
191 /* Handler for the SW2 button push interrupt. */
\r
192 __interrupt void vExternalInt8Handler( void )
\r
194 short sHigherPriorityTaskWoken = pdFALSE;
\r
196 /* Reset the interrupt. */
\r
199 /* Check the semaphore has been created before attempting to use it. */
\r
200 if( xSemaphores[ configLEFT_DISPLAY ] != NULL )
\r
202 /* Send a message via the semaphore to the dice task that controls the
\r
203 left side display. This will unblock the task if it is blocked waiting
\r
204 for a button push. */
\r
205 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
208 /* If sending the semaphore unblocked a task, and the unblocked task has a
\r
209 priority that is higher than the currently running task, then force a context
\r
211 if( sHigherPriorityTaskWoken != pdFALSE )
\r
213 portYIELD_FROM_ISR();
\r
216 /*-----------------------------------------------------------*/
\r
218 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */
\r
219 __interrupt void vExternalInt9Handler( void )
\r
221 short sHigherPriorityTaskWoken = pdFALSE;
\r
223 /* Reset the interrupt. */
\r
226 if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )
\r
228 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
231 if( sHigherPriorityTaskWoken != pdFALSE )
\r
233 portYIELD_FROM_ISR();
\r