]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/lcd.c
2b17f6ec974bd8fc9fae37b6b482b322910b49e8
[freertos] / FreeRTOS / Demo / PIC32MX_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 /* peripheral library include */\r
97 #include <plib.h>\r
98 \r
99 /* Scheduler includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 #include "queue.h"\r
103 \r
104 /* Demo includes. */\r
105 #include "lcd.h"\r
106 \r
107 /*\r
108  * The LCD is written to by more than one task so is controlled by this\r
109  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
110  * access the LCD directly.  Other tasks wanting to display a message send\r
111  * the message to the gatekeeper.\r
112  */\r
113 static void vLCDTask( void *pvParameters );\r
114 \r
115 /*\r
116  * Setup the peripherals required to communicate with the LCD.\r
117  */\r
118 static void prvSetupLCD( void );\r
119 \r
120 /* \r
121  * Move to the first (0) or second (1) row of the LCD. \r
122  */\r
123 static void prvLCDGotoRow( unsigned short usRow );\r
124 \r
125 /* \r
126  * Write a string of text to the LCD. \r
127  */\r
128 static void prvLCDPutString( char *pcString );\r
129 \r
130 /* \r
131  * Clear the LCD. \r
132  */\r
133 static void prvLCDClear( void );\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* Brief delay to permit the LCD to catch up with commands. */\r
138 #define lcdVERY_SHORT_DELAY     ( 1 )\r
139 #define lcdSHORT_DELAY          ( 8 / portTICK_PERIOD_MS )\r
140 #define lcdLONG_DELAY           ( 15 / portTICK_PERIOD_MS )\r
141 \r
142 /* LCD specific definitions. */\r
143 #define LCD_CLEAR_DISPLAY_CMD                   0x01\r
144 #define LCD_CURSOR_HOME_CMD                             0x02\r
145 #define LCD_ENTRY_MODE_CMD                              0x04\r
146 #define LCD_ENTRY_MODE_INCREASE                 0x02\r
147 #define LCD_DISPLAY_CTRL_CMD                    0x08\r
148 #define LCD_DISPLAY_CTRL_DISPLAY_ON             0x04\r
149 #define LCD_FUNCTION_SET_CMD                    0x20\r
150 #define LCD_FUNCTION_SET_8_BITS                 0x10\r
151 #define LCD_FUNCTION_SET_2_LINES                0x08\r
152 #define LCD_FUNCTION_SET_LRG_FONT               0x04\r
153 #define LCD_NEW_LINE                                    0xC0\r
154 #define LCD_COMMAND_ADDRESS                             0x00\r
155 #define LCD_DATA_ADDRESS                                0x01\r
156 \r
157 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
158 #define lcdQUEUE_SIZE           3\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /* The queue used to send messages to the LCD task. */\r
163 QueueHandle_t xLCDQueue;\r
164 \r
165 /* LCD access functions. */\r
166 static void prvLCDCommand( char cCommand );\r
167 static void prvLCDData( char cChar );\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 QueueHandle_t xStartLCDTask( void )\r
172 {\r
173         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
174         are received via this queue. */\r
175         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));\r
176 \r
177         /* Start the task that will write to the LCD.  The LCD hardware is\r
178         initialised from within the task itself so delays can be used. */\r
179         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
180 \r
181         return xLCDQueue;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static void prvLCDGotoRow( unsigned short usRow )\r
186 {\r
187         if(usRow == 0) \r
188         {\r
189                 prvLCDCommand( LCD_CURSOR_HOME_CMD );\r
190         } \r
191         else \r
192         {\r
193                 prvLCDCommand( LCD_NEW_LINE );\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static void prvLCDCommand( char cCommand ) \r
199 {\r
200         PMPSetAddress( LCD_COMMAND_ADDRESS );\r
201         PMPMasterWrite( cCommand );\r
202         vTaskDelay( lcdSHORT_DELAY );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvLCDData( char cChar )\r
207 {\r
208         PMPSetAddress( LCD_DATA_ADDRESS );\r
209         PMPMasterWrite( cChar );\r
210         vTaskDelay( lcdVERY_SHORT_DELAY );\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvLCDPutString( char *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(LCD_CLEAR_DISPLAY_CMD);\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         /* Open the PMP port */\r
238         mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |\r
239                           PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |\r
240                           PMP_WRITE_POL_HI | PMP_READ_POL_HI),\r
241                          (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |\r
242                           PMP_WAIT_END_4),\r
243                           PMP_PEN_0, 0);\r
244                          \r
245         /* Wait for the LCD to power up correctly. */\r
246         vTaskDelay( lcdLONG_DELAY );\r
247         vTaskDelay( lcdLONG_DELAY );\r
248         vTaskDelay( lcdLONG_DELAY );\r
249 \r
250         /* Set up the LCD function. */\r
251         prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );\r
252         \r
253         /* Turn the display on. */\r
254         prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );\r
255         \r
256         /* Clear the display. */\r
257         prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );\r
258         vTaskDelay( lcdLONG_DELAY );    \r
259         \r
260         /* Increase the cursor. */\r
261         prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );\r
262         vTaskDelay( lcdLONG_DELAY );                    \r
263         vTaskDelay( lcdLONG_DELAY );                    \r
264         vTaskDelay( lcdLONG_DELAY );\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void vLCDTask(void *pvParameters)\r
269 {\r
270 xLCDMessage xMessage;\r
271 unsigned short usRow = 0;\r
272 \r
273         /* Initialise the hardware.  This uses delays so must not be called prior\r
274         to the scheduler being started. */\r
275         prvSetupLCD();\r
276 \r
277         /* Welcome message. */\r
278         prvLCDPutString( "www.FreeRTOS.org" );\r
279 \r
280         for(;;)\r
281         {\r
282                 /* Wait for a message to arrive that requires displaying. */\r
283                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
284 \r
285                 /* Clear the current display value. */\r
286                 prvLCDClear();\r
287 \r
288                 /* Switch rows each time so we can see that the display is still being\r
289                 updated. */\r
290                 prvLCDGotoRow( usRow & 0x01 );\r
291                 usRow++;\r
292                 prvLCDPutString( xMessage.pcMessage );\r
293 \r
294                 /* Delay the requested amount of time to ensure the text just written \r
295                 to the LCD is not overwritten. */\r
296                 vTaskDelay( xMessage.xMinDisplayTime );         \r
297         }\r
298 }\r
299 \r
300 \r
301 \r
302 \r