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