]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/DiceTask.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / DiceTask.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\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
11      *                                                                       *\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
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \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
25 \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
29     >>! kernel.\r
30 \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
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \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
53 \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
57 \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
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* \r
67  * Defines the 'dice' tasks as described at the top of main.c\r
68  */\r
69 \r
70 \r
71 /* Kernel includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 #include "semphr.h"\r
75 \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
81 \r
82 /* Macro to access the display ports. */\r
83 #define dice7SEG_Value( x )             ( *( pucDisplayOutput[ x ] ) )\r
84 \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
89 \r
90 /* Defines the outputs required for each digit on the display. */\r
91 static const char cDisplaySegments[ 2 ][ 11 ] =\r
92 {\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
95 };\r
96 \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
101 \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
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /* \r
109  * Defines the 'dice' tasks as described at the top of main.c\r
110  */\r
111 void vDiceTask( void *pvParameters )\r
112 {\r
113 unsigned char ucDiceValue, ucIndex;\r
114 unsigned long ulDiceRunTime;\r
115 extern void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks );\r
116 \r
117 \r
118 \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
124         \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
128 \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
134 \r
135         /* Seed the random number generator. */\r
136         srand( ( unsigned char ) diceSHAKE_TIME );\r
137 \r
138 \r
139 \r
140 \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
144         for( ;; )\r
145         {\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
149                 \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
152                 for. */\r
153                 ulDiceRunTime = diceSHAKE_TIME;                         \r
154 \r
155                 /* Suspend the flash tasks so this task has exclusive access to the\r
156                 display. */\r
157                 vSuspendFlashTasks( ucIndex, pdTRUE );\r
158 \r
159                 /* Clear the display and pause for a short time, before starting to\r
160                 shake. */\r
161                 *pucDisplayOutput[ ucIndex ] = 0xff;\r
162                 vTaskDelay( diceSHORT_PAUSE_BEFORE_SHAKE );\r
163 \r
164                 /* Keep generating and displaying random numbers until the shake time\r
165                 expires. */\r
166                 while( ulDiceRunTime > 0 )\r
167                 {\r
168                         ulDiceRunTime--;\r
169 \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
173 \r
174                         /* Block/sleep for a very short time before generating the next\r
175                         random number. */\r
176                         vTaskDelay( diceDELAY_BETWEEN_RANDOM_NUMBERS_ms );\r
177                 }\r
178 \r
179 \r
180 \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
184                 immediately. */\r
185                 prvButtonHit( ucIndex, 0 );\r
186 \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
195 \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
200         }\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /* Handler for the SW2 button push interrupt. */\r
205 __interrupt void vExternalInt8Handler( void )\r
206 {\r
207 short sHigherPriorityTaskWoken = pdFALSE;\r
208 \r
209         /* Reset the interrupt. */\r
210         EIRR1_ER8 = 0;\r
211 \r
212         /* Check the semaphore has been created before attempting to use it. */\r
213         if( xSemaphores[ configLEFT_DISPLAY ] != NULL )\r
214         {\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
219         }\r
220 \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
223         switch. */\r
224         if( sHigherPriorityTaskWoken != pdFALSE )\r
225         {\r
226                 portYIELD_FROM_ISR();\r
227         }\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 /* As per vExternalInt8Handler(), but for SW3 and the right side display. */\r
232 __interrupt void vExternalInt9Handler( void )\r
233 {\r
234 short sHigherPriorityTaskWoken = pdFALSE;\r
235 \r
236         /* Reset the interrupt. */\r
237         EIRR1_ER9 = 0;\r
238 \r
239         if( xSemaphores[ configRIGHT_DISPLAY ] != NULL )\r
240         {\r
241                 xSemaphoreGiveFromISR( xSemaphores[ configRIGHT_DISPLAY ], &sHigherPriorityTaskWoken );\r
242         }\r
243 \r
244         if( sHigherPriorityTaskWoken != pdFALSE )\r
245         {\r
246                 portYIELD_FROM_ISR();\r
247         }\r
248 }\r
249 \r
250 \r
251 \r
252 \r
253 \r
254 \r
255 \r