]> git.sur5r.net Git - freertos/blob - Demo/RX200_RX210-RSK_Renesas/RTOSDemo/ButtonAndLCD.c
Change version numbers to V7.1.0.
[freertos] / Demo / RX200_RX210-RSK_Renesas / RTOSDemo / ButtonAndLCD.c
1 /*\r
2     FreeRTOS V7.1.0 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include "queue.h"\r
58 #include "semphr.h"\r
59 \r
60 /* Hardware specifics. */\r
61 #include "iodefine.h"\r
62 #include "lcd.h"\r
63 \r
64 /* States used by the LCD tasks. */\r
65 #define lcdRIGHT_TO_LEFT        0U\r
66 #define lcdLEFT_TO_RIGHT        1U\r
67 #define lcdRUNNING                      0U\r
68 \r
69 /* Characters on each line. */\r
70 #define lcdSTRING_LEN           8 \r
71 \r
72 /* Commands sent from the IRQ to the task controlling the second line of the\r
73 display. */\r
74 #define lcdSHIFT_BACK_COMMAND           0x01U\r
75 #define lcdSTART_STOP_COMMAND           0x02U\r
76 #define lcdSHIFT_FORWARD_COMMAND        0x03U\r
77 \r
78 /* The length of the queue used to send commands from the ISRs. */\r
79 #define lcdCOMMAND_QUEUE_LENGTH         32U\r
80 \r
81 /* Defines the minimum time that must pass between consecutive button presses\r
82 to accept a button press as a unique press rather than just a bounce. */\r
83 #define lcdMIN_TIME_BETWEEN_INTERRUPTS_MS ( 125UL / portTICK_RATE_MS )\r
84 \r
85 /* Button interrupt handlers. */\r
86 #pragma interrupt ( prvIRQ1_Handler( vect = 65, enable ) )\r
87 static void prvIRQ1_Handler( void );\r
88 \r
89 #pragma interrupt ( prvIRQ3_Handler( vect = 67, enable ) )\r
90 static void prvIRQ3_Handler( void );\r
91 \r
92 #pragma interrupt ( prvIRQ4_Handler(vect = 68, enable ) )\r
93 static void prvIRQ4_Handler( void );\r
94 \r
95 /* \r
96  * Setup the IO needed for the buttons to generate interrupts. \r
97  */\r
98 static void prvSetupButtonIOAndInterrupts( void );\r
99 \r
100 /*\r
101  * A task that simply scrolls a string from left to right, then back from the\r
102  * right to the left.  This is done on the first line of the display.\r
103  */\r
104 static void prvLCDTaskLine1( void *pvParameters );\r
105 \r
106 /*\r
107  * If no buttons are pushed, then this task acts as per prvLCDTaskLine1(), but\r
108  * using the second line of the display.\r
109  *\r
110  * Using the buttons, it is possible to start and stop the scrolling of the \r
111  * text.  Once the scrolling has been stopped, other buttons can be used to\r
112  * manually scroll the text either left or right.\r
113  */ \r
114 static void prvLCDTaskLine2( void *pvParameters );\r
115 \r
116 /*\r
117  * Looks at the direction the string is currently being scrolled in, and moves\r
118  * the index into the portion of the string that can be seen on the display\r
119  * either forward or backward as appropriate. \r
120  */\r
121 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength );\r
122 \r
123 /* \r
124  * Displays lcdSTRING_LEN characters starting from pcString on the line of the\r
125  * display requested by ucLine.\r
126  */\r
127 static void prvDisplayNextString( unsigned char ucLine, char *pcString );\r
128 \r
129 /*\r
130  * Called from the IRQ interrupts, which are generated on button pushes.  Send\r
131  * ucCommand to the task on the button command queue if \r
132  * lcdMIN_TIME_BETWEEN_INTERRUPTS_MS milliseconds have passed since the button\r
133  * was last pushed (for debouncing). \r
134  */\r
135 static portBASE_TYPE prvSendCommandOnDebouncedInput( portTickType *pxTimeLastInterrupt, unsigned char ucCommand );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* The queue used to pass commands from the button interrupt handlers to the\r
140 prvLCDTaskLine2() task. */\r
141 static xQueueHandle xButtonCommandQueue = NULL;\r
142 \r
143 /* The mutex used to ensure only one task writes to the display at any one\r
144 time. */\r
145 static xSemaphoreHandle xLCDMutex = NULL;\r
146 \r
147 /* The string that is scrolled up and down the first line of the display. */\r
148 static const char cDataString1[] = "        http://www.FreeRTOS.org        ";\r
149 \r
150 /* The string that is scrolled/nudged up and down the second line of the \r
151 display. */\r
152 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
153 \r
154 /* Structures passed into the two tasks to inform them which line to use on the\r
155 display, how long to delay for, and which string to use. */\r
156 struct _LCD_Params xLCDLine1 = \r
157 {\r
158         LCD_LINE1, 215, ( char * ) cDataString1 \r
159 };\r
160 \r
161 struct _LCD_Params xLCDLine2 = \r
162 {\r
163         LCD_LINE2, 350, ( char * ) cDataString2\r
164 };\r
165 \r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void vStartButtonAndLCDDemo( void )\r
170 {\r
171         prvSetupButtonIOAndInterrupts();\r
172         InitialiseDisplay();\r
173 \r
174         /* Create the mutex used to guard the LCD. */\r
175         xLCDMutex = xSemaphoreCreateMutex();\r
176         configASSERT( xLCDMutex );\r
177         \r
178         /* Create the queue used to pass commands from the IRQ interrupts to the\r
179         prvLCDTaskLine2() task. */\r
180         xButtonCommandQueue = xQueueCreate( lcdCOMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );\r
181         configASSERT( xButtonCommandQueue );\r
182 \r
183         /* Start the two tasks as described at the top of this file. */\r
184         xTaskCreate( prvLCDTaskLine1, "LCD1", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine1, tskIDLE_PRIORITY + 1, NULL );\r
185         xTaskCreate( prvLCDTaskLine2, "LCD2", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine2, tskIDLE_PRIORITY + 2, NULL );\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void prvLCDTaskLine1( void *pvParameters )\r
190 {\r
191 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;\r
192 unsigned short usPosition = 0U;\r
193 unsigned char ucDirection = lcdRIGHT_TO_LEFT;\r
194         \r
195         for( ;; )\r
196         {\r
197                 vTaskDelay( pxLCDParamaters->Speed / portTICK_RATE_MS );                \r
198 \r
199                 /* Write the string. */\r
200                 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
201 \r
202                 /* Move the string in whichever direction the scroll is currently going\r
203                 in. */\r
204                 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void prvLCDTaskLine2( void *pvParameters )\r
210 {\r
211 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;\r
212 unsigned short usPosition = 0U;\r
213 unsigned char ucDirection = lcdRIGHT_TO_LEFT, ucStatus = lcdRUNNING, ucQueueData;\r
214 portTickType xDelayTicks = ( pxLCDParamaters->Speed / portTICK_RATE_MS );\r
215         \r
216         for(;;)\r
217         {\r
218                 /* Wait for a message from an IRQ handler. */\r
219                 if( xQueueReceive( xButtonCommandQueue, &ucQueueData, xDelayTicks ) != pdPASS )\r
220                 {\r
221                         /* A message was not received before xDelayTicks ticks passed, so\r
222                         generate the next string to display and display it. */\r
223                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
224                         \r
225                         /* Move the string in whichever direction the scroll is currently \r
226                         going in. */\r
227                         prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );                       \r
228                 }\r
229                 else\r
230                 {\r
231                         /* A command was received.  Process it. */\r
232                         switch( ucQueueData )\r
233                         {\r
234                                 case lcdSTART_STOP_COMMAND :\r
235 \r
236                                         /* If the LCD is running, top it.  If the LCD is stopped, start\r
237                                         it. */\r
238                                         ucStatus = !ucStatus;\r
239                                         \r
240                                         if( ucStatus == lcdRUNNING )\r
241                                         {\r
242                                                 xDelayTicks = ( pxLCDParamaters->Speed / portTICK_RATE_MS );\r
243                                         }\r
244                                         else\r
245                                         {\r
246                                                 xDelayTicks = portMAX_DELAY;\r
247                                         }\r
248                                         break;\r
249 \r
250                                         \r
251                                 case lcdSHIFT_BACK_COMMAND :\r
252 \r
253                                         if( ucStatus != lcdRUNNING )\r
254                                         {\r
255                                                 /* If not already at the start of the display.... */\r
256                                                 if( usPosition != 0U )\r
257                                                 {\r
258                                                         /* ....move the display position back by one char. */\r
259                                                         usPosition--;                                                                                           \r
260                                                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
261                                                 }\r
262                                         }\r
263                                         break;\r
264                                 \r
265                                 \r
266                                 case lcdSHIFT_FORWARD_COMMAND :\r
267 \r
268                                         if( ucStatus != lcdRUNNING )\r
269                                         {\r
270                                                 /* If not already at the end of the display.... */\r
271                                                 if( usPosition != ( strlen( pxLCDParamaters->ptr_str ) - ( lcdSTRING_LEN - 1 ) ) )\r
272                                                 {\r
273                                                         /* ....move the display position forward by one \r
274                                                         char. */\r
275                                                         usPosition++;\r
276                                                         prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );\r
277                                                 }\r
278                                         }\r
279                                         break;\r
280                         }\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 void prvSetupButtonIOAndInterrupts( void )\r
287 {\r
288         /* Configure SW 1-3 pin settings */\r
289         PORT3.PDR.BIT.B1 = 0;           /* Switch 1 - Port 3.1 - IRQ1 */\r
290         PORT3.PDR.BIT.B3 = 0;           /* Switch 2 - Port 3.3 - IRQ3 */\r
291         PORT3.PDR.BIT.B4 = 0;           /* Switch 3 - Port 3.4 - IRQ4 */\r
292 \r
293         PORT3.PMR.BIT.B1 = 1;\r
294         PORT3.PMR.BIT.B3 = 1;\r
295         PORT3.PMR.BIT.B4 = 1;\r
296 \r
297         MPC.PWPR.BIT.B0WI = 0;          /* Writing to the PFSWE bit is enabled */\r
298         MPC.PWPR.BIT.PFSWE = 1;         /* Writing to the PFS register is enabled */\r
299         MPC.P31PFS.BIT.ISEL = 1;\r
300         MPC.P33PFS.BIT.ISEL = 1;\r
301         MPC.P34PFS.BIT.ISEL = 1;\r
302 \r
303         /* IRQ1 */\r
304         ICU.IER[ 0x08 ].BIT.IEN1 = 1;   \r
305         ICU.IPR[ 65 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
306         ICU.IR[ 65 ].BIT.IR = 0;\r
307         ICU.IRQCR[ 1 ].BIT.IRQMD = 2;   /* Rising edge */\r
308 \r
309         /* IRQ3 */\r
310         ICU.IER[ 0x08 ].BIT.IEN3 = 1;   \r
311         ICU.IPR[ 67 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
312         ICU.IR[ 67 ].BIT.IR = 0;\r
313         ICU.IRQCR[ 3 ].BIT.IRQMD = 2;   /* Rising edge */\r
314 \r
315         /* IRQ4 */\r
316         ICU.IER[ 0x08 ].BIT.IEN4 = 1;   \r
317         ICU.IPR[ 68 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
318         ICU.IR[ 68 ].BIT.IR = 0;\r
319         ICU.IRQCR[ 4 ].BIT.IRQMD = 2;   /* Rising edge */\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength )\r
324 {\r
325         /* Check which way to scroll. */\r
326         if( *pucDirection == lcdRIGHT_TO_LEFT )\r
327         {\r
328                 /* Move to the next character. */\r
329                 ( *pusPosition )++;\r
330                 \r
331                 /* Has the end of the string been reached? */\r
332                 if( ( *pusPosition ) == ( xStringLength - ( lcdSTRING_LEN - 1 ) ) )\r
333                 {\r
334                         /* Switch direction. */\r
335                         *pucDirection = lcdLEFT_TO_RIGHT;\r
336                         ( *pusPosition )--;                             \r
337                 }\r
338         }\r
339         else\r
340         {\r
341                 /* Move (backward) to the next character. */\r
342                 ( *pusPosition )--;\r
343                 if( *pusPosition == 0U )\r
344                 {\r
345                         /* Switch Direction. */\r
346                         *pucDirection = lcdRIGHT_TO_LEFT;                               \r
347                 }\r
348         }\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 static void prvDisplayNextString( unsigned char ucLine, char *pcString )\r
353 {\r
354 static char cSingleLine[ lcdSTRING_LEN + 1 ];\r
355 \r
356         xSemaphoreTake( xLCDMutex, portMAX_DELAY );\r
357         strncpy( cSingleLine, pcString, lcdSTRING_LEN );\r
358         DisplayString( ucLine, cSingleLine );\r
359         xSemaphoreGive( xLCDMutex );\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 static portBASE_TYPE prvSendCommandOnDebouncedInput( portTickType *pxTimeLastInterrupt, unsigned char ucCommand )\r
364 {\r
365 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
366 portTickType xCurrentTickCount;\r
367         \r
368         /* Check the time now for debouncing purposes. */\r
369         xCurrentTickCount = xTaskGetTickCountFromISR();\r
370         \r
371         /* Has enough time passed since the button was last push to accept it as a\r
372         unique press? */\r
373         if( ( xCurrentTickCount - *pxTimeLastInterrupt ) > lcdMIN_TIME_BETWEEN_INTERRUPTS_MS )\r
374         {\r
375                 xQueueSendToBackFromISR( xButtonCommandQueue, &ucCommand, &xHigherPriorityTaskWoken );\r
376         }\r
377 \r
378         /* Remember the time now, so debounce can be performed again on the next\r
379         interrupt. */   \r
380         *pxTimeLastInterrupt = xCurrentTickCount;\r
381         \r
382         return xHigherPriorityTaskWoken;\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 static void prvIRQ1_Handler( void )\r
387 {\r
388 static portTickType xTimeLastInterrupt = 0UL;\r
389 static const unsigned char ucCommand = lcdSHIFT_BACK_COMMAND;\r
390 portBASE_TYPE xHigherPriorityTaskWoken;\r
391 \r
392         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
393         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 static void prvIRQ3_Handler(void)\r
398 {\r
399 static portTickType xTimeLastInterrupt = 0UL;\r
400 static const unsigned char ucCommand = lcdSTART_STOP_COMMAND;\r
401 portBASE_TYPE xHigherPriorityTaskWoken;\r
402 \r
403         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
404         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
405 }\r
406 /*-----------------------------------------------------------*/\r
407 \r
408 static void prvIRQ4_Handler(void)\r
409 {\r
410 static portTickType xTimeLastInterrupt = 0UL;\r
411 static const unsigned char ucCommand = lcdSHIFT_FORWARD_COMMAND;\r
412 portBASE_TYPE xHigherPriorityTaskWoken;\r
413 \r
414         xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );\r
415         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
416 }\r
417 \r