]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX200_RX210-RSK_Renesas/RTOSDemo/ButtonAndLCD.c
Update version number to V8.0.0 (without the release candidate number).
[freertos] / FreeRTOS / Demo / RX200_RX210-RSK_Renesas / RTOSDemo / ButtonAndLCD.c
1 /*\r
2     FreeRTOS V8.0.0 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Scheduler includes. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 #include "queue.h"\r
70 #include "semphr.h"\r
71 \r
72 /* Hardware specifics. */\r
73 #include "iodefine.h"\r
74 #include "lcd.h"\r
75 \r
76 /* States used by the LCD tasks. */\r
77 #define lcdRIGHT_TO_LEFT        0U\r
78 #define lcdLEFT_TO_RIGHT        1U\r
79 #define lcdRUNNING                      0U\r
80 \r
81 /* Characters on each line. */\r
82 #define lcdSTRING_LEN           8 \r
83 \r
84 /* Commands sent from the IRQ to the task controlling the second line of the\r
85 display. */\r
86 #define lcdSHIFT_BACK_COMMAND           0x01U\r
87 #define lcdSTART_STOP_COMMAND           0x02U\r
88 #define lcdSHIFT_FORWARD_COMMAND        0x03U\r
89 \r
90 /* The length of the queue used to send commands from the ISRs. */\r
91 #define lcdCOMMAND_QUEUE_LENGTH         32U\r
92 \r
93 /* Defines the minimum time that must pass between consecutive button presses\r
94 to accept a button press as a unique press rather than just a bounce. */\r
95 #define lcdMIN_TIME_BETWEEN_INTERRUPTS_MS ( 125UL / portTICK_PERIOD_MS )\r
96 \r
97 /* Button interrupt handlers. */\r
98 #pragma interrupt ( prvIRQ1_Handler( vect = 65, enable ) )\r
99 static void prvIRQ1_Handler( void );\r
100 \r
101 #pragma interrupt ( prvIRQ3_Handler( vect = 67, enable ) )\r
102 static void prvIRQ3_Handler( void );\r
103 \r
104 #pragma interrupt ( prvIRQ4_Handler(vect = 68, enable ) )\r
105 static void prvIRQ4_Handler( void );\r
106 \r
107 /* \r
108  * Setup the IO needed for the buttons to generate interrupts. \r
109  */\r
110 static void prvSetupButtonIOAndInterrupts( void );\r
111 \r
112 /*\r
113  * A task that simply scrolls a string from left to right, then back from the\r
114  * right to the left.  This is done on the first line of the display.\r
115  */\r
116 static void prvLCDTaskLine1( void *pvParameters );\r
117 \r
118 /*\r
119  * If no buttons are pushed, then this task acts as per prvLCDTaskLine1(), but\r
120  * using the second line of the display.\r
121  *\r
122  * Using the buttons, it is possible to start and stop the scrolling of the \r
123  * text.  Once the scrolling has been stopped, other buttons can be used to\r
124  * manually scroll the text either left or right.\r
125  */ \r
126 static void prvLCDTaskLine2( void *pvParameters );\r
127 \r
128 /*\r
129  * Looks at the direction the string is currently being scrolled in, and moves\r
130  * the index into the portion of the string that can be seen on the display\r
131  * either forward or backward as appropriate. \r
132  */\r
133 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength );\r
134 \r
135 /* \r
136  * Displays lcdSTRING_LEN characters starting from pcString on the line of the\r
137  * display requested by ucLine.\r
138  */\r
139 static void prvDisplayNextString( unsigned char ucLine, char *pcString );\r
140 \r
141 /*\r
142  * Called from the IRQ interrupts, which are generated on button pushes.  Send\r
143  * ucCommand to the task on the button command queue if \r
144  * lcdMIN_TIME_BETWEEN_INTERRUPTS_MS milliseconds have passed since the button\r
145  * was last pushed (for debouncing). \r
146  */\r
147 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand );\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /* The queue used to pass commands from the button interrupt handlers to the\r
152 prvLCDTaskLine2() task. */\r
153 static QueueHandle_t xButtonCommandQueue = NULL;\r
154 \r
155 /* The mutex used to ensure only one task writes to the display at any one\r
156 time. */\r
157 static SemaphoreHandle_t xLCDMutex = NULL;\r
158 \r
159 /* The string that is scrolled up and down the first line of the display. */\r
160 static const char cDataString1[] = "        http://www.FreeRTOS.org        ";\r
161 \r
162 /* The string that is scrolled/nudged up and down the second line of the \r
163 display. */\r
164 static const char cDataString2[] = "........Rx210 Highlights....1.56 DMips/MHz....DSP functions....1.62V-5.5V operation....200 uA/MHz....Up to 512 kBytes Flash....up to 64 kbytes SRAM....EE Dataflash with 100k w/e....1.3 uA in Real Time Clock Mode....Powerful Motor control timer....4 x 16-bit timers....4 x 8-bit timers....Full Real Time Clock calendar with calibration and alarm functions....Up to 16 channels 1 uS 12-bit ADC, with Dual group programmable SCAN, 3 sample and holds, sample accumulate function....DMA controller....Data Transfer Controller....Up to 9 serial Channels....Up to 6 USARTs ( with Simple I2C / SPI )....USART ( with unique Frame based protocol support )....Multimaster IIC....RSPI....Temperature Sensor....Event Link Controller....Comparators....Safety features include CRC....March X....Dual watchdog Timers with window and independent oscillator....ADC self test....I/O Pin Test....Supported with E1 on chip debugger and RSK210 evaluation system....Rx210 Highlights........";\r
165 \r
166 /* Structures passed into the two tasks to inform them which line to use on the\r
167 display, how long to delay for, and which string to use. */\r
168 struct _LCD_Params xLCDLine1 = \r
169 {\r
170         LCD_LINE1, 215, ( char * ) cDataString1 \r
171 };\r
172 \r
173 struct _LCD_Params xLCDLine2 = \r
174 {\r
175         LCD_LINE2, 350, ( char * ) cDataString2\r
176 };\r
177 \r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vStartButtonAndLCDDemo( void )\r
182 {\r
183         prvSetupButtonIOAndInterrupts();\r
184         InitialiseDisplay();\r
185 \r
186         /* Create the mutex used to guard the LCD. */\r
187         xLCDMutex = xSemaphoreCreateMutex();\r
188         configASSERT( xLCDMutex );\r
189         \r
190         /* Create the queue used to pass commands from the IRQ interrupts to the\r
191         prvLCDTaskLine2() task. */\r
192         xButtonCommandQueue = xQueueCreate( lcdCOMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );\r
193         configASSERT( xButtonCommandQueue );\r
194 \r
195         /* Start the two tasks as described at the top of this file. */\r
196         xTaskCreate( prvLCDTaskLine1, "LCD1", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine1, tskIDLE_PRIORITY + 1, NULL );\r
197         xTaskCreate( prvLCDTaskLine2, "LCD2", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine2, tskIDLE_PRIORITY + 2, NULL );\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvLCDTaskLine1( void *pvParameters )\r
202 {\r
203 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;\r
204 unsigned short usPosition = 0U;\r
205 unsigned char ucDirection = lcdRIGHT_TO_LEFT;\r
206         \r
207         for( ;; )\r
208         {\r
209                 vTaskDelay( pxLCDParamaters->Speed / portTICK_PERIOD_MS );              \r
210 \r
211                 /* Write the string. */\r
212                 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
213 \r
214                 /* Move the string in whichever direction the scroll is currently going\r
215                 in. */\r
216                 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvLCDTaskLine2( void *pvParameters )\r
222 {\r
223 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;\r
224 unsigned short usPosition = 0U;\r
225 unsigned char ucDirection = lcdRIGHT_TO_LEFT, ucStatus = lcdRUNNING, ucQueueData;\r
226 TickType_t xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );\r
227         \r
228         for(;;)\r
229         {\r
230                 /* Wait for a message from an IRQ handler. */\r
231                 if( xQueueReceive( xButtonCommandQueue, &ucQueueData, xDelayTicks ) != pdPASS )\r
232                 {\r
233                         /* A message was not received before xDelayTicks ticks passed, so\r
234                         generate the next string to display and display it. */\r
235                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
236                         \r
237                         /* Move the string in whichever direction the scroll is currently \r
238                         going in. */\r
239                         prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );                       \r
240                 }\r
241                 else\r
242                 {\r
243                         /* A command was received.  Process it. */\r
244                         switch( ucQueueData )\r
245                         {\r
246                                 case lcdSTART_STOP_COMMAND :\r
247 \r
248                                         /* If the LCD is running, top it.  If the LCD is stopped, start\r
249                                         it. */\r
250                                         ucStatus = !ucStatus;\r
251                                         \r
252                                         if( ucStatus == lcdRUNNING )\r
253                                         {\r
254                                                 xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );\r
255                                         }\r
256                                         else\r
257                                         {\r
258                                                 xDelayTicks = portMAX_DELAY;\r
259                                         }\r
260                                         break;\r
261 \r
262                                         \r
263                                 case lcdSHIFT_BACK_COMMAND :\r
264 \r
265                                         if( ucStatus != lcdRUNNING )\r
266                                         {\r
267                                                 /* If not already at the start of the display.... */\r
268                                                 if( usPosition != 0U )\r
269                                                 {\r
270                                                         /* ....move the display position back by one char. */\r
271                                                         usPosition--;                                                                                           \r
272                                                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
273                                                 }\r
274                                         }\r
275                                         break;\r
276                                 \r
277                                 \r
278                                 case lcdSHIFT_FORWARD_COMMAND :\r
279 \r
280                                         if( ucStatus != lcdRUNNING )\r
281                                         {\r
282                                                 /* If not already at the end of the display.... */\r
283                                                 if( usPosition != ( strlen( pxLCDParamaters->ptr_str ) - ( lcdSTRING_LEN - 1 ) ) )\r
284                                                 {\r
285                                                         /* ....move the display position forward by one \r
286                                                         char. */\r
287                                                         usPosition++;\r
288                                                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
289                                                 }\r
290                                         }\r
291                                         break;\r
292                         }\r
293                 }\r
294         }\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 void prvSetupButtonIOAndInterrupts( void )\r
299 {\r
300         /* Configure SW 1-3 pin settings */\r
301         PORT3.PDR.BIT.B1 = 0;           /* Switch 1 - Port 3.1 - IRQ1 */\r
302         PORT3.PDR.BIT.B3 = 0;           /* Switch 2 - Port 3.3 - IRQ3 */\r
303         PORT3.PDR.BIT.B4 = 0;           /* Switch 3 - Port 3.4 - IRQ4 */\r
304 \r
305         PORT3.PMR.BIT.B1 = 1;\r
306         PORT3.PMR.BIT.B3 = 1;\r
307         PORT3.PMR.BIT.B4 = 1;\r
308 \r
309         MPC.PWPR.BIT.B0WI = 0;          /* Writing to the PFSWE bit is enabled */\r
310         MPC.PWPR.BIT.PFSWE = 1;         /* Writing to the PFS register is enabled */\r
311         MPC.P31PFS.BIT.ISEL = 1;\r
312         MPC.P33PFS.BIT.ISEL = 1;\r
313         MPC.P34PFS.BIT.ISEL = 1;\r
314 \r
315         /* IRQ1 */\r
316         ICU.IER[ 0x08 ].BIT.IEN1 = 1;   \r
317         ICU.IPR[ 65 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
318         ICU.IR[ 65 ].BIT.IR = 0;\r
319         ICU.IRQCR[ 1 ].BIT.IRQMD = 2;   /* Rising edge */\r
320 \r
321         /* IRQ3 */\r
322         ICU.IER[ 0x08 ].BIT.IEN3 = 1;   \r
323         ICU.IPR[ 67 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
324         ICU.IR[ 67 ].BIT.IR = 0;\r
325         ICU.IRQCR[ 3 ].BIT.IRQMD = 2;   /* Rising edge */\r
326 \r
327         /* IRQ4 */\r
328         ICU.IER[ 0x08 ].BIT.IEN4 = 1;   \r
329         ICU.IPR[ 68 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
330         ICU.IR[ 68 ].BIT.IR = 0;\r
331         ICU.IRQCR[ 4 ].BIT.IRQMD = 2;   /* Rising edge */\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength )\r
336 {\r
337         /* Check which way to scroll. */\r
338         if( *pucDirection == lcdRIGHT_TO_LEFT )\r
339         {\r
340                 /* Move to the next character. */\r
341                 ( *pusPosition )++;\r
342                 \r
343                 /* Has the end of the string been reached? */\r
344                 if( ( *pusPosition ) == ( xStringLength - ( lcdSTRING_LEN - 1 ) ) )\r
345                 {\r
346                         /* Switch direction. */\r
347                         *pucDirection = lcdLEFT_TO_RIGHT;\r
348                         ( *pusPosition )--;                             \r
349                 }\r
350         }\r
351         else\r
352         {\r
353                 /* Move (backward) to the next character. */\r
354                 ( *pusPosition )--;\r
355                 if( *pusPosition == 0U )\r
356                 {\r
357                         /* Switch Direction. */\r
358                         *pucDirection = lcdRIGHT_TO_LEFT;                               \r
359                 }\r
360         }\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 static void prvDisplayNextString( unsigned char ucLine, char *pcString )\r
365 {\r
366 static char cSingleLine[ lcdSTRING_LEN + 1 ];\r
367 \r
368         xSemaphoreTake( xLCDMutex, portMAX_DELAY );\r
369         strncpy( cSingleLine, pcString, lcdSTRING_LEN );\r
370         DisplayString( ucLine, cSingleLine );\r
371         xSemaphoreGive( xLCDMutex );\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand )\r
376 {\r
377 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
378 TickType_t xCurrentTickCount;\r
379         \r
380         /* Check the time now for debouncing purposes. */\r
381         xCurrentTickCount = xTaskGetTickCountFromISR();\r
382         \r
383         /* Has enough time passed since the button was last push to accept it as a\r
384         unique press? */\r
385         if( ( xCurrentTickCount - *pxTimeLastInterrupt ) > lcdMIN_TIME_BETWEEN_INTERRUPTS_MS )\r
386         {\r
387                 xQueueSendToBackFromISR( xButtonCommandQueue, &ucCommand, &xHigherPriorityTaskWoken );\r
388         }\r
389 \r
390         /* Remember the time now, so debounce can be performed again on the next\r
391         interrupt. */   \r
392         *pxTimeLastInterrupt = xCurrentTickCount;\r
393         \r
394         return xHigherPriorityTaskWoken;\r
395 }\r
396 /*-----------------------------------------------------------*/\r
397 \r
398 static void prvIRQ1_Handler( void )\r
399 {\r
400 static TickType_t xTimeLastInterrupt = 0UL;\r
401 static const unsigned char ucCommand = lcdSHIFT_BACK_COMMAND;\r
402 portBASE_TYPE xHigherPriorityTaskWoken;\r
403 \r
404         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
405         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
406 }\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 static void prvIRQ3_Handler(void)\r
410 {\r
411 static TickType_t xTimeLastInterrupt = 0UL;\r
412 static const unsigned char ucCommand = lcdSTART_STOP_COMMAND;\r
413 portBASE_TYPE xHigherPriorityTaskWoken;\r
414 \r
415         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
416         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 static void prvIRQ4_Handler(void)\r
421 {\r
422 static TickType_t xTimeLastInterrupt = 0UL;\r
423 static const unsigned char ucCommand = lcdSHIFT_FORWARD_COMMAND;\r
424 portBASE_TYPE xHigherPriorityTaskWoken;\r
425 \r
426         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
427         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
428 }\r
429 \r