2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
68 * Defines the 'dice' tasks as described at the top of main.c
\r
72 /* Kernel includes. */
\r
73 #include "FreeRTOS.h"
\r
77 /* Delays used within the dice functionality. All delays are defined in milliseconds. */
\r
78 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 / portTICK_RATE_MS )
\r
79 #define diceSHAKE_TIME ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
\r
80 #define diceSHORT_PAUSE_BEFORE_SHAKE ( 250 / portTICK_RATE_MS )
\r
81 #define diceDELAY_WHILE_DISPLAYING_RESULT ( 5000 / portTICK_RATE_MS )
\r
83 /* Macro to access the display ports. */
\r
84 #define dice7SEG_Value( x ) ( *( pucDisplayOutput[ x ] ) )
\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
91 /* Defines the outputs required for each digit on the display. */
\r
92 static const char cDisplaySegments[ 2 ][ 11 ] =
\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
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 xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
\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
107 /*-----------------------------------------------------------*/
\r
110 * Defines the 'dice' tasks as described at the top of main.c
\r
112 void vDiceTask( void *pvParameters )
\r
114 unsigned char ucDiceValue, ucIndex;
\r
115 unsigned long ulDiceRunTime;
\r
116 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
\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
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
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
136 /* Seed the random number generator. */
\r
137 srand( ( unsigned char ) diceSHAKE_TIME );
\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
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
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
154 ulDiceRunTime = diceSHAKE_TIME;
\r
156 /* Suspend the flash tasks so this task has exclusive access to the
\r
158 vSuspendFlashTasks( ucIndex, pdTRUE );
\r
160 /* Clear the display and pause for a short time, before starting to
\r
162 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
163 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );
\r
165 /* Keep generating and displaying random numbers until the shake time
\r
167 while( ulDiceRunTime > 0 )
\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
175 /* Block/sleep for a very short time before generating the next
\r
177 vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );
\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
186 prvButtonHit( ucIndex, 0 );
\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
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
203 /*-----------------------------------------------------------*/
\r
205 /* Handler for the SW2 button push interrupt. */
\r
206 __interrupt void vExternalInt8Handler( void )
\r
208 short sHigherPriorityTaskWoken = pdFALSE;
\r
210 /* Reset the interrupt. */
\r
213 /* Check the semaphore has been created before attempting to use it. */
\r
214 if( xSemaphores[ configLEFT_DISPLAY ] != NULL )
\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
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
225 if( sHigherPriorityTaskWoken != pdFALSE )
\r
227 portYIELD_FROM_ISR();
\r
230 /*-----------------------------------------------------------*/
\r
232 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */
\r
233 __interrupt void vExternalInt9Handler( void )
\r
235 short sHigherPriorityTaskWoken = pdFALSE;
\r
237 /* Reset the interrupt. */
\r
240 if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )
\r
242 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
245 if( sHigherPriorityTaskWoken != pdFALSE )
\r
247 portYIELD_FROM_ISR();
\r