]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/dsPIC_MPLAB/lcd.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / dsPIC_MPLAB / lcd.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 \r
101 /* Demo includes. */\r
102 #include "lcd.h"\r
103 \r
104 /*\r
105  * The LCD is written to by more than one task so is controlled by this\r
106  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
107  * access the LCD directly.  Other tasks wanting to display a message send\r
108  * the message to the gatekeeper.\r
109  */\r
110 static void vLCDTask( void *pvParameters );\r
111 \r
112 /*\r
113  * Setup the peripherals required to communicate with the LCD.\r
114  */\r
115 static void prvSetupLCD( void );\r
116 \r
117 /*\r
118  * Move to the first (0) or second (1) row of the LCD.\r
119  */\r
120 static void prvLCDGotoRow( unsigned short usRow );\r
121 \r
122 /*\r
123  * Write a string of text to the LCD.\r
124  */\r
125 static void prvLCDPutString( char *pcString );\r
126 \r
127 /*\r
128  * Clear the LCD.\r
129  */\r
130 static void prvLCDClear( void );\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /* Brief delay to permit the LCD to catch up with commands. */\r
135 #define lcdVERY_SHORT_DELAY     ( 1 )\r
136 #define lcdSHORT_DELAY          ( 4 / portTICK_PERIOD_MS )\r
137 #define lcdLONG_DELAY           ( 15 / portTICK_PERIOD_MS )\r
138 \r
139 /* LCD commands. */\r
140 #define lcdCLEAR                        ( 0x01 )\r
141 #define lcdHOME                         ( 0x02 )\r
142 #define lcdLINE2                        ( 0xc0 )\r
143 \r
144 /* SFR that seems to be missing from the standard header files. */\r
145 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
146 \r
147 /* LCD R/W signal. */\r
148 #define  lcdRW  LATDbits.LATD5\r
149 \r
150 /* LCD lcdRS signal. */\r
151 #define  lcdRS  LATBbits.LATB15\r
152 \r
153 /* LCD lcdE signal . */\r
154 #define  lcdE   LATDbits.LATD4\r
155 \r
156 /* Control signal pin direction. */\r
157 #define  RW_TRIS        TRISDbits.TRISD5\r
158 #define  RS_TRIS        TRISBbits.TRISB15\r
159 #define  E_TRIS         TRISDbits.TRISD4\r
160 \r
161 /* Port for LCD data */\r
162 #define  lcdDATA      LATE\r
163 #define  lcdDATAPORT  PORTE\r
164 \r
165 /* I/O setup for data Port. */\r
166 #define  TRISDATA  TRISE\r
167 \r
168 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
169 #define lcdQUEUE_SIZE           3\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 /* The queue used to send messages to the LCD task. */\r
173 QueueHandle_t xLCDQueue;\r
174 \r
175 static void prvLCDCommand( char cCommand );\r
176 static void prvLCDData( char cChar );\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 \r
180 QueueHandle_t xStartLCDTask( void )\r
181 {\r
182         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
183         are received via this queue. */\r
184         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
185 \r
186         /* Start the task that will write to the LCD.  The LCD hardware is\r
187         initialised from within the task itself so delays can be used. */\r
188         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
189 \r
190         return xLCDQueue;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static void prvLCDGotoRow( unsigned short usRow )\r
195 {\r
196         if( usRow == 0 )\r
197         {\r
198                 prvLCDCommand( lcdHOME );\r
199         }\r
200         else\r
201         {\r
202                 prvLCDCommand( lcdLINE2 );\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvLCDCommand( char cCommand )\r
208 {\r
209         /* Prepare RD0 - RD7. */\r
210         lcdDATA &= 0xFF00;\r
211 \r
212         /* Command byte to lcd. */\r
213     lcdDATA |= cCommand;\r
214 \r
215         /* Ensure lcdRW is 0. */\r
216         lcdRW = 0;\r
217     lcdRS = 0;\r
218 \r
219         /* Toggle lcdE line. */\r
220     lcdE = 1;\r
221     vTaskDelay( lcdVERY_SHORT_DELAY );\r
222     lcdE = 0;\r
223 \r
224         vTaskDelay( lcdSHORT_DELAY );\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvLCDData( char cChar )\r
229 {\r
230         /* ensure lcdRW is 0. */\r
231         lcdRW = 0;\r
232 \r
233         /* Assert register select to 1. */\r
234     lcdRS = 1;\r
235 \r
236         /* Prepare RD0 - RD7. */\r
237         lcdDATA &= 0xFF00;\r
238 \r
239         /* Data byte to lcd. */\r
240     lcdDATA |= cChar;\r
241     lcdE = 1;\r
242         Nop();\r
243     Nop();\r
244     Nop();\r
245 \r
246         /* Toggle lcdE signal. */\r
247     lcdE = 0;\r
248 \r
249         /* Negate register select to 0. */\r
250     lcdRS = 0;\r
251 \r
252         vTaskDelay( lcdVERY_SHORT_DELAY );\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvLCDPutString( char *pcString )\r
257 {\r
258         /* Write out each character with appropriate delay between each. */\r
259         while( *pcString )\r
260         {\r
261                 prvLCDData( *pcString );\r
262                 pcString++;\r
263                 vTaskDelay( lcdSHORT_DELAY );\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvLCDClear( void )\r
269 {\r
270         prvLCDCommand( lcdCLEAR );\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvSetupLCD( void )\r
275 {\r
276         /* Wait for proper power up. */\r
277         vTaskDelay( lcdLONG_DELAY );\r
278 \r
279         /* Set initial states for the data and control pins */\r
280         LATE &= 0xFF00;\r
281 \r
282         /* R/W state set low. */\r
283     lcdRW = 0;\r
284 \r
285         /* lcdRS state set low. */\r
286         lcdRS = 0;\r
287 \r
288         /* lcdE state set low. */\r
289         lcdE = 0;\r
290 \r
291         /* Set data and control pins to outputs */\r
292         TRISE &= 0xFF00;\r
293 \r
294         /* lcdRW pin set as output. */\r
295         RW_TRIS = 0;\r
296 \r
297         /* lcdRS pin set as output. */\r
298         RS_TRIS = 0;\r
299 \r
300         /* lcdE pin set as output. */\r
301         E_TRIS = 0;\r
302 \r
303         /* 1st LCD initialization sequence */\r
304         lcdDATA &= 0xFF00;\r
305     lcdDATA |= 0x0038;\r
306     lcdE = 1;\r
307     Nop();\r
308     Nop();\r
309     Nop();\r
310 \r
311         /* Toggle lcdE signal. */\r
312     lcdE = 0;\r
313 \r
314         vTaskDelay( lcdSHORT_DELAY );\r
315         vTaskDelay( lcdSHORT_DELAY );\r
316         vTaskDelay( lcdSHORT_DELAY );\r
317 \r
318         /* 2nd LCD initialization sequence */\r
319         lcdDATA &= 0xFF00;\r
320     lcdDATA |= 0x0038;\r
321     lcdE = 1;\r
322     Nop();\r
323     Nop();\r
324     Nop();\r
325 \r
326         /* Toggle lcdE signal. */\r
327     lcdE = 0;\r
328 \r
329     vTaskDelay( lcdSHORT_DELAY );\r
330 \r
331         /* 3rd LCD initialization sequence */\r
332         lcdDATA &= 0xFF00;\r
333     lcdDATA |= 0x0038;\r
334     lcdE = 1;\r
335     Nop();\r
336     Nop();\r
337     Nop();\r
338 \r
339         /* Toggle lcdE signal. */\r
340     lcdE = 0;\r
341 \r
342         vTaskDelay( lcdSHORT_DELAY );\r
343 \r
344 \r
345         /* Function set. */\r
346     prvLCDCommand( 0x38 );\r
347 \r
348         /* Display on/off control, cursor blink off (0x0C). */\r
349     prvLCDCommand( 0x0C );\r
350 \r
351         /* Entry mode set (0x06). */\r
352     prvLCDCommand( 0x06 );\r
353 \r
354         prvLCDCommand( lcdCLEAR );\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 static void vLCDTask( void *pvParameters )\r
359 {\r
360 xLCDMessage xMessage;\r
361 unsigned short usRow = 0;\r
362 \r
363         /* Initialise the hardware.  This uses delays so must not be called prior\r
364         to the scheduler being started. */\r
365         prvSetupLCD();\r
366 \r
367         /* Welcome message. */\r
368         prvLCDPutString( "www.FreeRTOS.org" );\r
369 \r
370         for( ;; )\r
371         {\r
372                 /* Wait for a message to arrive that requires displaying. */\r
373                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
374 \r
375                 /* Clear the current display value. */\r
376                 prvLCDClear();\r
377 \r
378                 /* Switch rows each time so we can see that the display is still being\r
379                 updated. */\r
380                 prvLCDGotoRow( usRow & 0x01 );\r
381                 usRow++;\r
382                 prvLCDPutString( xMessage.pcMessage );\r
383 \r
384                 /* Delay the requested amount of time to ensure the text just written\r
385                 to the LCD is not overwritten. */\r
386                 vTaskDelay( xMessage.xMinDisplayTime );\r
387         }\r
388 }\r
389 \r
390 \r
391 \r
392 \r