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