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