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