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