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