]> git.sur5r.net Git - freertos/blob - Demo/dsPIC_MPLAB/lcd.c
Add printf-stdarg.c to the new MicroBlaze demo.
[freertos] / Demo / dsPIC_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include "queue.h"\r
58 \r
59 /* Demo includes. */\r
60 #include "lcd.h"\r
61 \r
62 /*\r
63  * The LCD is written to by more than one task so is controlled by this\r
64  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
65  * access the LCD directly.  Other tasks wanting to display a message send\r
66  * the message to the gatekeeper.\r
67  */\r
68 static void vLCDTask( void *pvParameters );\r
69 \r
70 /*\r
71  * Setup the peripherals required to communicate with the LCD.\r
72  */\r
73 static void prvSetupLCD( void );\r
74 \r
75 /* \r
76  * Move to the first (0) or second (1) row of the LCD. \r
77  */\r
78 static void prvLCDGotoRow( unsigned portSHORT usRow );\r
79 \r
80 /* \r
81  * Write a string of text to the LCD. \r
82  */\r
83 static void prvLCDPutString( portCHAR *pcString );\r
84 \r
85 /* \r
86  * Clear the LCD. \r
87  */\r
88 static void prvLCDClear( void );\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* Brief delay to permit the LCD to catch up with commands. */\r
93 #define lcdVERY_SHORT_DELAY     ( 1 )\r
94 #define lcdSHORT_DELAY          ( 4 / portTICK_RATE_MS )\r
95 #define lcdLONG_DELAY           ( 15 / portTICK_RATE_MS )\r
96 \r
97 /* LCD commands. */\r
98 #define lcdCLEAR                        ( 0x01 )\r
99 #define lcdHOME                         ( 0x02 )\r
100 #define lcdLINE2                        ( 0xc0 )\r
101 \r
102 /* SFR that seems to be missing from the standard header files. */\r
103 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
104 \r
105 /* LCD R/W signal. */\r
106 #define  lcdRW  LATDbits.LATD5       \r
107 \r
108 /* LCD lcdRS signal. */\r
109 #define  lcdRS  LATBbits.LATB15      \r
110 \r
111 /* LCD lcdE signal . */\r
112 #define  lcdE   LATDbits.LATD4       \r
113 \r
114 /* Control signal pin direction. */\r
115 #define  RW_TRIS        TRISDbits.TRISD5 \r
116 #define  RS_TRIS        TRISBbits.TRISB15\r
117 #define  E_TRIS         TRISDbits.TRISD4\r
118 \r
119 /* Port for LCD data */\r
120 #define  lcdDATA      LATE           \r
121 #define  lcdDATAPORT  PORTE\r
122 \r
123 /* I/O setup for data Port. */\r
124 #define  TRISDATA  TRISE          \r
125 \r
126 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
127 #define lcdQUEUE_SIZE           3\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* The queue used to send messages to the LCD task. */\r
131 xQueueHandle xLCDQueue;\r
132 \r
133 static void prvLCDCommand( portCHAR cCommand );\r
134 static void prvLCDData( portCHAR cChar );\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 xQueueHandle xStartLCDTask( void )\r
139 {\r
140         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
141         are received via this queue. */\r
142         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
143 \r
144         /* Start the task that will write to the LCD.  The LCD hardware is\r
145         initialised from within the task itself so delays can be used. */\r
146         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
147 \r
148         return xLCDQueue;\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
153 {\r
154         if( usRow == 0 )\r
155         {\r
156                 prvLCDCommand( lcdHOME );\r
157         }\r
158         else\r
159         {\r
160                 prvLCDCommand( lcdLINE2 );\r
161         }\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvLCDCommand( portCHAR cCommand ) \r
166 {\r
167         /* Prepare RD0 - RD7. */\r
168         lcdDATA &= 0xFF00;               \r
169 \r
170         /* Command byte to lcd. */\r
171     lcdDATA |= cCommand;                  \r
172 \r
173         /* Ensure lcdRW is 0. */\r
174         lcdRW = 0;                       \r
175     lcdRS = 0;\r
176 \r
177         /* Toggle lcdE line. */\r
178     lcdE = 1;                        \r
179     vTaskDelay( lcdVERY_SHORT_DELAY );\r
180     lcdE = 0;\r
181 \r
182         vTaskDelay( lcdSHORT_DELAY );\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvLCDData( portCHAR cChar )\r
187 {\r
188         /* ensure lcdRW is 0. */\r
189         lcdRW = 0;                                       \r
190 \r
191         /* Assert register select to 1. */\r
192     lcdRS = 1;                       \r
193 \r
194         /* Prepare RD0 - RD7. */\r
195         lcdDATA &= 0xFF00;               \r
196 \r
197         /* Data byte to lcd. */\r
198     lcdDATA |= cChar;                 \r
199     lcdE = 1;                           \r
200         Nop();\r
201     Nop();\r
202     Nop();\r
203 \r
204         /* Toggle lcdE signal. */\r
205     lcdE = 0;                       \r
206 \r
207         /* Negate register select to 0. */\r
208     lcdRS = 0;                      \r
209 \r
210         vTaskDelay( lcdVERY_SHORT_DELAY );\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvLCDPutString( portCHAR *pcString )\r
215 {\r
216         /* Write out each character with appropriate delay between each. */\r
217         while( *pcString )\r
218         {\r
219                 prvLCDData( *pcString );\r
220                 pcString++;\r
221                 vTaskDelay( lcdSHORT_DELAY );\r
222         }\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static void prvLCDClear( void )\r
227 {\r
228         prvLCDCommand( lcdCLEAR );\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvSetupLCD( void )\r
233 {\r
234         /* Wait for proper power up. */\r
235         vTaskDelay( lcdLONG_DELAY );\r
236                         \r
237         /* Set initial states for the data and control pins */\r
238         LATE &= 0xFF00; \r
239 \r
240         /* R/W state set low. */\r
241     lcdRW = 0;                       \r
242 \r
243         /* lcdRS state set low. */\r
244         lcdRS = 0;                       \r
245 \r
246         /* lcdE state set low. */\r
247         lcdE = 0;                        \r
248 \r
249         /* Set data and control pins to outputs */\r
250         TRISE &= 0xFF00;\r
251 \r
252         /* lcdRW pin set as output. */\r
253         RW_TRIS = 0;                  \r
254 \r
255         /* lcdRS pin set as output. */\r
256         RS_TRIS = 0;                  \r
257 \r
258         /* lcdE pin set as output. */\r
259         E_TRIS = 0;                   \r
260 \r
261         /* 1st LCD initialization sequence */\r
262         lcdDATA &= 0xFF00;\r
263     lcdDATA |= 0x0038;\r
264     lcdE = 1;   \r
265     Nop();\r
266     Nop();\r
267     Nop();\r
268 \r
269         /* Toggle lcdE signal. */\r
270     lcdE = 0;                        \r
271 \r
272         vTaskDelay( lcdSHORT_DELAY );\r
273         vTaskDelay( lcdSHORT_DELAY );\r
274         vTaskDelay( lcdSHORT_DELAY );\r
275       \r
276         /* 2nd LCD initialization sequence */\r
277         lcdDATA &= 0xFF00;\r
278     lcdDATA |= 0x0038;\r
279     lcdE = 1;   \r
280     Nop();\r
281     Nop();\r
282     Nop();      \r
283 \r
284         /* Toggle lcdE signal. */\r
285     lcdE = 0;                        \r
286 \r
287     vTaskDelay( lcdSHORT_DELAY );\r
288 \r
289         /* 3rd LCD initialization sequence */\r
290         lcdDATA &= 0xFF00;\r
291     lcdDATA |= 0x0038;\r
292     lcdE = 1;           \r
293     Nop();\r
294     Nop();\r
295     Nop();      \r
296 \r
297         /* Toggle lcdE signal. */\r
298     lcdE = 0;                        \r
299 \r
300         vTaskDelay( lcdSHORT_DELAY );\r
301 \r
302 \r
303         /* Function set. */\r
304     prvLCDCommand( 0x38 );              \r
305 \r
306         /* Display on/off control, cursor blink off (0x0C). */\r
307     prvLCDCommand( 0x0C );              \r
308 \r
309         /* Entry mode set (0x06). */\r
310     prvLCDCommand( 0x06 );              \r
311 \r
312         prvLCDCommand( lcdCLEAR );        \r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static void vLCDTask( void *pvParameters )\r
317 {\r
318 xLCDMessage xMessage;\r
319 unsigned portSHORT usRow = 0;\r
320 \r
321         /* Initialise the hardware.  This uses delays so must not be called prior\r
322         to the scheduler being started. */\r
323         prvSetupLCD();\r
324 \r
325         /* Welcome message. */\r
326         prvLCDPutString( "www.FreeRTOS.org" );\r
327 \r
328         for( ;; )\r
329         {\r
330                 /* Wait for a message to arrive that requires displaying. */\r
331                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
332 \r
333                 /* Clear the current display value. */\r
334                 prvLCDClear();\r
335 \r
336                 /* Switch rows each time so we can see that the display is still being\r
337                 updated. */\r
338                 prvLCDGotoRow( usRow & 0x01 );\r
339                 usRow++;\r
340                 prvLCDPutString( xMessage.pcMessage );\r
341 \r
342                 /* Delay the requested amount of time to ensure the text just written \r
343                 to the LCD is not overwritten. */\r
344                 vTaskDelay( xMessage.xMinDisplayTime );         \r
345         }\r
346 }\r
347 \r
348 \r
349 \r
350 \r