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