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