2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * Defines the 'dice' tasks as described at the top of main.c
\r
81 /* Kernel includes. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Delays used within the dice functionality. All delays are defined in milliseconds. */
\r
87 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 / portTICK_RATE_MS )
\r
88 #define diceSHAKE_TIME ( ( 2000 / portTICK_RATE_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
\r
89 #define diceSHORT_PAUSE_BEFORE_SHAKE ( 250 / portTICK_RATE_MS )
\r
90 #define diceDELAY_WHILE_DISPLAYING_RESULT ( 5000 / portTICK_RATE_MS )
\r
92 /* Macro to access the display ports. */
\r
93 #define dice7SEG_Value( x ) ( *( pucDisplayOutput[ x ] ) )
\r
95 /* Checks the semaphore use to communicate button push events. A block time
\r
96 can be specified - this is the time to wait for a button push to occur should
\r
97 one have not already occurred. */
\r
98 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
\r
100 /* Defines the outputs required for each digit on the display. */
\r
101 static const char cDisplaySegments[ 2 ][ 11 ] =
\r
103 { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */
\r
104 { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f } /* Right display. */
\r
107 /* The semaphores used to communicate button push events between the button
\r
108 input interrupt handlers and the dice tasks. Two dice tasks are created so two
\r
109 semaphores are required. */
\r
110 static xSemaphoreHandle xSemaphores[ 2 ] = { 0 };
\r
112 /* Defines the ports used to write to the display. This variable is defined in
\r
113 partest.c, which contains the LED set/clear/toggle functions. */
\r
114 extern volatile unsigned char *pucDisplayOutput[ 2 ];
\r
116 /*-----------------------------------------------------------*/
\r
119 * Defines the 'dice' tasks as described at the top of main.c
\r
121 void vDiceTask( void *pvParameters )
\r
123 unsigned char ucDiceValue, ucIndex;
\r
124 unsigned long ulDiceRunTime;
\r
125 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
\r
129 /* Two instances of this task are created so the task parameter is used
\r
130 to pass in a constant that indicates whether this task is controlling
\r
131 the left side or right side display. The constant is used as an index
\r
132 into the arrays defined at file scope within this file. */
\r
133 ucIndex = ( unsigned char ) pvParameters;
\r
135 /* A binary semaphore is used to signal button push events. Create the
\r
136 semaphore before it is used. */
\r
137 vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );
\r
139 /* Make sure the semaphore starts in the wanted state - no button pushes
\r
140 pending. This call will just clear any button pushes that are latched.
\r
141 Passing in 0 as the block time means the call will not wait for any further
\r
142 button pushes but instead return immediately. */
\r
143 prvButtonHit( ucIndex, 0 );
\r
145 /* Seed the random number generator. */
\r
146 srand( ( unsigned char ) diceSHAKE_TIME );
\r
151 /* Start the task proper. A loop will be performed each time a button is
\r
152 pushed. The task will remain in the blocked state (sleeping) until a
\r
153 button is pushed. */
\r
156 /* Wait for a button push. This task will enter the Blocked state
\r
157 (will not run again) until after a button has been pushed. */
\r
158 prvButtonHit( ucIndex, portMAX_DELAY );
\r
160 /* The next line will only execute after a button has been pushed -
\r
161 initialise the variable used to control the time the dice is shaken
\r
163 ulDiceRunTime = diceSHAKE_TIME;
\r
165 /* Suspend the flash tasks so this task has exclusive access to the
\r
167 vSuspendFlashTasks( ucIndex, pdTRUE );
\r
169 /* Clear the display and pause for a short time, before starting to
\r
171 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
172 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );
\r
174 /* Keep generating and displaying random numbers until the shake time
\r
176 while( ulDiceRunTime > 0 )
\r
180 /* Generate and display a random number. */
\r
181 ucDiceValue = rand() % 6 + 1;
\r
182 dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];
\r
184 /* Block/sleep for a very short time before generating the next
\r
186 vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );
\r
191 /* Clear any button pushes that are pending because a button bounced, or
\r
192 was pressed while the dice were shaking. Again a block time of zero is
\r
193 used so the function does not wait for any pushes but instead returns
\r
195 prvButtonHit( ucIndex, 0 );
\r
197 /* Delay for a short while to display the dice shake result. Use a queue
\r
198 peek here instead of a vTaskDelay() allows the delay to be interrupted by
\r
199 a button push. If a button is pressed xQueuePeek() will return but the
\r
200 button push will remain pending to be read again at the top of this for
\r
201 loop. It is safe to uses a queue function on a semaphore handle as
\r
202 semaphores are implemented as macros that uses queues, so the two are
\r
203 basically the same thing. */
\r
204 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );
\r
206 /* Clear the display then resume the tasks or co-routines that were using
\r
207 the segments of the display. */
\r
208 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
209 vSuspendFlashTasks( ucIndex, pdFALSE );
\r
212 /*-----------------------------------------------------------*/
\r
214 /* Handler for the SW2 button push interrupt. */
\r
215 __interrupt void vExternalInt8Handler( void )
\r
217 short sHigherPriorityTaskWoken = pdFALSE;
\r
219 /* Reset the interrupt. */
\r
222 /* Check the semaphore has been created before attempting to use it. */
\r
223 if( xSemaphores[ configLEFT_DISPLAY ] != NULL )
\r
225 /* Send a message via the semaphore to the dice task that controls the
\r
226 left side display. This will unblock the task if it is blocked waiting
\r
227 for a button push. */
\r
228 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
231 /* If sending the semaphore unblocked a task, and the unblocked task has a
\r
232 priority that is higher than the currently running task, then force a context
\r
234 if( sHigherPriorityTaskWoken != pdFALSE )
\r
236 portYIELD_FROM_ISR();
\r
239 /*-----------------------------------------------------------*/
\r
241 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */
\r
242 __interrupt void vExternalInt9Handler( void )
\r
244 short sHigherPriorityTaskWoken = pdFALSE;
\r
246 /* Reset the interrupt. */
\r
249 if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )
\r
251 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
254 if( sHigherPriorityTaskWoken != pdFALSE )
\r
256 portYIELD_FROM_ISR();
\r