]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/DiceTask.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / DiceTask.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* \r
72  * Defines the 'dice' tasks as described at the top of main.c\r
73  */\r
74 \r
75 \r
76 /* Kernel includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "semphr.h"\r
80 \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
86 \r
87 /* Macro to access the display ports. */\r
88 #define dice7SEG_Value( x )             ( *( pucDisplayOutput[ x ] ) )\r
89 \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
94 \r
95 /* Defines the outputs required for each digit on the display. */\r
96 static const char cDisplaySegments[ 2 ][ 11 ] =\r
97 {\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
100 };\r
101 \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
106 \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
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* \r
114  * Defines the 'dice' tasks as described at the top of main.c\r
115  */\r
116 void vDiceTask( void *pvParameters )\r
117 {\r
118 unsigned char ucDiceValue, ucIndex;\r
119 unsigned long ulDiceRunTime;\r
120 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
121 \r
122 \r
123 \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
129         \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
133 \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
139 \r
140         /* Seed the random number generator. */\r
141         srand( ( unsigned char ) diceSHAKE_TIME );\r
142 \r
143 \r
144 \r
145 \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
149         for( ;; )\r
150         {\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
154                 \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
157                 for. */\r
158                 ulDiceRunTime = diceSHAKE_TIME;                         \r
159 \r
160                 /* Suspend the flash tasks so this task has exclusive access to the\r
161                 display. */\r
162                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
163 \r
164                 /* Clear the display and pause for a short time, before starting to\r
165                 shake. */\r
166                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
167                 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );\r
168 \r
169                 /* Keep generating and displaying random numbers until the shake time\r
170                 expires. */\r
171                 while( ulDiceRunTime > 0 )\r
172                 {\r
173                         ulDiceRunTime--;\r
174 \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
178 \r
179                         /* Block/sleep for a very short time before generating the next\r
180                         random number. */\r
181                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );\r
182                 }\r
183 \r
184 \r
185 \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
189                 immediately. */\r
190                 prvButtonHit( ucIndex, 0 );\r
191 \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
200 \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
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /* Handler for the SW2 button push interrupt. */\r
210 __interrupt void vExternalInt8Handler( void )\r
211 {\r
212 short sHigherPriorityTaskWoken = pdFALSE;\r
213 \r
214         /* Reset the interrupt. */\r
215         EIRR1_ER8 = 0;\r
216 \r
217         /* Check the semaphore has been created before attempting to use it. */\r
218         if( xSemaphores[ configLEFT_DISPLAY ] != NULL )\r
219         {\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
224         }\r
225 \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
228         switch. */\r
229         if( sHigherPriorityTaskWoken != pdFALSE )\r
230         {\r
231                 portYIELD_FROM_ISR();\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */\r
237 __interrupt void vExternalInt9Handler( void )\r
238 {\r
239 short sHigherPriorityTaskWoken = pdFALSE;\r
240 \r
241         /* Reset the interrupt. */\r
242         EIRR1_ER9 = 0;\r
243 \r
244         if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )\r
245         {\r
246                 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );\r
247         }\r
248 \r
249         if( sHigherPriorityTaskWoken != pdFALSE )\r
250         {\r
251                 portYIELD_FROM_ISR();\r
252         }\r
253 }\r
254 \r
255 \r
256 \r
257 \r
258 \r
259 \r
260 \r