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