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