2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Defines the 'dice' tasks as described at the top of main.c
\r
76 /* Kernel includes. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Delays used within the dice functionality. All delays are defined in milliseconds. */
\r
82 #define diceDELAY_BETWEEN_RANDOM_NUMBERS_ms ( 20 / portTICK_PERIOD_MS )
\r
83 #define diceSHAKE_TIME ( ( 2000 / portTICK_PERIOD_MS ) / diceDELAY_BETWEEN_RANDOM_NUMBERS_ms )
\r
84 #define diceSHORT_PAUSE_BEFORE_SHAKE ( 250 / portTICK_PERIOD_MS )
\r
85 #define diceDELAY_WHILE_DISPLAYING_RESULT ( 5000 / portTICK_PERIOD_MS )
\r
87 /* Macro to access the display ports. */
\r
88 #define dice7SEG_Value( x ) ( *( pucDisplayOutput[ x ] ) )
\r
90 /* Checks the semaphore use to communicate button push events. A block time
\r
91 can be specified - this is the time to wait for a button push to occur should
\r
92 one have not already occurred. */
\r
93 #define prvButtonHit( ucIndex, xTicksToWait ) xSemaphoreTake( xSemaphores[ ucIndex ], xTicksToWait )
\r
95 /* Defines the outputs required for each digit on the display. */
\r
96 static const char cDisplaySegments[ 2 ][ 11 ] =
\r
98 { 0x48, 0xeb, 0x8c, 0x89, 0x2b, 0x19, 0x18, 0xcb, 0x08, 0x09, 0xf7 }, /* Left display. */
\r
99 { 0xa0, 0xf3, 0xc4, 0xc1, 0x93, 0x89, 0x88, 0xe3, 0x80, 0x81, 0x7f } /* Right display. */
\r
102 /* The semaphores used to communicate button push events between the button
\r
103 input interrupt handlers and the dice tasks. Two dice tasks are created so two
\r
104 semaphores are required. */
\r
105 static SemaphoreHandle_t xSemaphores[ 2 ] = { 0 };
\r
107 /* Defines the ports used to write to the display. This variable is defined in
\r
108 partest.c, which contains the LED set/clear/toggle functions. */
\r
109 extern volatile unsigned char *pucDisplayOutput[ 2 ];
\r
111 /*-----------------------------------------------------------*/
\r
114 * Defines the 'dice' tasks as described at the top of main.c
\r
116 void vDiceTask( void *pvParameters )
\r
118 unsigned char ucDiceValue, ucIndex;
\r
119 unsigned long ulDiceRunTime;
\r
120 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );
\r
124 /* Two instances of this task are created so the task parameter is used
\r
125 to pass in a constant that indicates whether this task is controlling
\r
126 the left side or right side display. The constant is used as an index
\r
127 into the arrays defined at file scope within this file. */
\r
128 ucIndex = ( unsigned char ) pvParameters;
\r
130 /* A binary semaphore is used to signal button push events. Create the
\r
131 semaphore before it is used. */
\r
132 vSemaphoreCreateBinary( xSemaphores[ ucIndex ] );
\r
134 /* Make sure the semaphore starts in the wanted state - no button pushes
\r
135 pending. This call will just clear any button pushes that are latched.
\r
136 Passing in 0 as the block time means the call will not wait for any further
\r
137 button pushes but instead return immediately. */
\r
138 prvButtonHit( ucIndex, 0 );
\r
140 /* Seed the random number generator. */
\r
141 srand( ( unsigned char ) diceSHAKE_TIME );
\r
146 /* Start the task proper. A loop will be performed each time a button is
\r
147 pushed. The task will remain in the blocked state (sleeping) until a
\r
148 button is pushed. */
\r
151 /* Wait for a button push. This task will enter the Blocked state
\r
152 (will not run again) until after a button has been pushed. */
\r
153 prvButtonHit( ucIndex, portMAX_DELAY );
\r
155 /* The next line will only execute after a button has been pushed -
\r
156 initialise the variable used to control the time the dice is shaken
\r
158 ulDiceRunTime = diceSHAKE_TIME;
\r
160 /* Suspend the flash tasks so this task has exclusive access to the
\r
162 vSuspendFlashTasks( ucIndex, pdTRUE );
\r
164 /* Clear the display and pause for a short time, before starting to
\r
166 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
167 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );
\r
169 /* Keep generating and displaying random numbers until the shake time
\r
171 while( ulDiceRunTime > 0 )
\r
175 /* Generate and display a random number. */
\r
176 ucDiceValue = rand() % 6 + 1;
\r
177 dice7SEG_Value( ucIndex ) = ( dice7SEG_Value( ucIndex ) | 0xf7 ) & cDisplaySegments[ ucIndex ][ ucDiceValue ];
\r
179 /* Block/sleep for a very short time before generating the next
\r
181 vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );
\r
186 /* Clear any button pushes that are pending because a button bounced, or
\r
187 was pressed while the dice were shaking. Again a block time of zero is
\r
188 used so the function does not wait for any pushes but instead returns
\r
190 prvButtonHit( ucIndex, 0 );
\r
192 /* Delay for a short while to display the dice shake result. Use a queue
\r
193 peek here instead of a vTaskDelay() allows the delay to be interrupted by
\r
194 a button push. If a button is pressed xQueuePeek() will return but the
\r
195 button push will remain pending to be read again at the top of this for
\r
196 loop. It is safe to uses a queue function on a semaphore handle as
\r
197 semaphores are implemented as macros that uses queues, so the two are
\r
198 basically the same thing. */
\r
199 xQueuePeek( xSemaphores[ ucIndex ], NULL, diceDELAY_WHILE_DISPLAYING_RESULT );
\r
201 /* Clear the display then resume the tasks or co-routines that were using
\r
202 the segments of the display. */
\r
203 *pucDisplayOutput[ ucIndex ] = 0xff;
\r
204 vSuspendFlashTasks( ucIndex, pdFALSE );
\r
207 /*-----------------------------------------------------------*/
\r
209 /* Handler for the SW2 button push interrupt. */
\r
210 __interrupt void vExternalInt8Handler( void )
\r
212 short sHigherPriorityTaskWoken = pdFALSE;
\r
214 /* Reset the interrupt. */
\r
217 /* Check the semaphore has been created before attempting to use it. */
\r
218 if( xSemaphores[ configLEFT_DISPLAY ] != NULL )
\r
220 /* Send a message via the semaphore to the dice task that controls the
\r
221 left side display. This will unblock the task if it is blocked waiting
\r
222 for a button push. */
\r
223 xSemaphoreGiveFromISR( xSemaphores[ configLEFT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
226 /* If sending the semaphore unblocked a task, and the unblocked task has a
\r
227 priority that is higher than the currently running task, then force a context
\r
229 if( sHigherPriorityTaskWoken != pdFALSE )
\r
231 portYIELD_FROM_ISR();
\r
234 /*-----------------------------------------------------------*/
\r
236 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */
\r
237 __interrupt void vExternalInt9Handler( void )
\r
239 short sHigherPriorityTaskWoken = pdFALSE;
\r
241 /* Reset the interrupt. */
\r
244 if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )
\r
246 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );
\r
249 if( sHigherPriorityTaskWoken != pdFALSE )
\r
251 portYIELD_FROM_ISR();
\r