]> git.sur5r.net Git - freertos/blob - Demo/PIC32MX_MPLAB/lcd.c
Update to V4.7.1
[freertos] / Demo / PIC32MX_MPLAB / lcd.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /* peripheral library include */\r
44 #include <plib.h>\r
45 \r
46 /* Scheduler includes. */\r
47 #include "FreeRTOS.h"\r
48 #include "task.h"\r
49 #include "queue.h"\r
50 \r
51 /* Demo includes. */\r
52 #include "lcd.h"\r
53 \r
54 /*\r
55  * The LCD is written to by more than one task so is controlled by this\r
56  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
57  * access the LCD directly.  Other tasks wanting to display a message send\r
58  * the message to the gatekeeper.\r
59  */\r
60 static void vLCDTask( void *pvParameters );\r
61 \r
62 /*\r
63  * Setup the peripherals required to communicate with the LCD.\r
64  */\r
65 static void prvSetupLCD( void );\r
66 \r
67 /* \r
68  * Move to the first (0) or second (1) row of the LCD. \r
69  */\r
70 static void prvLCDGotoRow( unsigned portSHORT usRow );\r
71 \r
72 /* \r
73  * Write a string of text to the LCD. \r
74  */\r
75 static void prvLCDPutString( portCHAR *pcString );\r
76 \r
77 /* \r
78  * Clear the LCD. \r
79  */\r
80 static void prvLCDClear( void );\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* Brief delay to permit the LCD to catch up with commands. */\r
85 #define lcdVERY_SHORT_DELAY     ( 1 )\r
86 #define lcdSHORT_DELAY          ( 4 / portTICK_RATE_MS )\r
87 #define lcdLONG_DELAY           ( 15 / portTICK_RATE_MS )\r
88 \r
89 /* LCD specific definitions. */\r
90 #define LCD_CLEAR_DISPLAY_CMD                   0x01\r
91 #define LCD_CURSOR_HOME_CMD                             0x02\r
92 #define LCD_ENTRY_MODE_CMD                              0x04\r
93 #define LCD_ENTRY_MODE_INCREASE                 0x02\r
94 #define LCD_DISPLAY_CTRL_CMD                    0x08\r
95 #define LCD_DISPLAY_CTRL_DISPLAY_ON             0x04\r
96 #define LCD_FUNCTION_SET_CMD                    0x20\r
97 #define LCD_FUNCTION_SET_8_BITS                 0x10\r
98 #define LCD_FUNCTION_SET_2_LINES                0x08\r
99 #define LCD_FUNCTION_SET_LRG_FONT               0x04\r
100 #define LCD_NEW_LINE                                    0xC0\r
101 #define LCD_COMMAND_ADDRESS                             0x00\r
102 #define LCD_DATA_ADDRESS                                0x01\r
103 \r
104 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
105 #define lcdQUEUE_SIZE           3\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* The queue used to send messages to the LCD task. */\r
110 xQueueHandle xLCDQueue;\r
111 \r
112 /* LCD access functions. */\r
113 static void prvLCDCommand( portCHAR cCommand );\r
114 static void prvLCDData( portCHAR cChar );\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 xQueueHandle xStartLCDTask( void )\r
119 {\r
120         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
121         are received via this queue. */\r
122         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));\r
123 \r
124         /* Start the task that will write to the LCD.  The LCD hardware is\r
125         initialised from within the task itself so delays can be used. */\r
126         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
127 \r
128         return xLCDQueue;\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
133 {\r
134         if(usRow == 0) \r
135         {\r
136                 prvLCDCommand( LCD_CURSOR_HOME_CMD );\r
137         } \r
138         else \r
139         {\r
140                 prvLCDCommand( LCD_NEW_LINE );\r
141         }\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void prvLCDCommand( portCHAR cCommand ) \r
146 {\r
147         PMPSetAddress( LCD_COMMAND_ADDRESS );\r
148         PMPMasterWrite( cCommand );\r
149         vTaskDelay( lcdSHORT_DELAY );\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvLCDData( portCHAR cChar )\r
154 {\r
155         PMPSetAddress( LCD_DATA_ADDRESS );\r
156         PMPMasterWrite( cChar );\r
157         vTaskDelay( lcdVERY_SHORT_DELAY );\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 static void prvLCDPutString( portCHAR *pcString )\r
162 {\r
163         /* Write out each character with appropriate delay between each. */\r
164         while(*pcString)\r
165         {\r
166                 prvLCDData(*pcString);\r
167                 pcString++;\r
168                 vTaskDelay(lcdSHORT_DELAY);\r
169         }\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void prvLCDClear(void)\r
174 {\r
175         prvLCDCommand(LCD_CLEAR_DISPLAY_CMD);\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void prvSetupLCD(void)\r
180 {\r
181         /* Wait for proper power up. */\r
182         vTaskDelay( lcdLONG_DELAY );\r
183         \r
184         /* Open the PMP port */\r
185         mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |\r
186                           PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |\r
187                           PMP_WRITE_POL_HI | PMP_READ_POL_HI),\r
188                          (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |\r
189                           PMP_WAIT_END_4),\r
190                           PMP_PEN_0, 0);\r
191                          \r
192         /* Wait for the LCD to power up correctly. */\r
193         vTaskDelay( lcdLONG_DELAY );\r
194         vTaskDelay( lcdLONG_DELAY );\r
195         vTaskDelay( lcdLONG_DELAY );\r
196 \r
197         /* Set up the LCD function. */\r
198         prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );\r
199         \r
200         /* Turn the display on. */\r
201         prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );\r
202         \r
203         /* Clear the display. */\r
204         prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );\r
205         vTaskDelay( lcdLONG_DELAY );    \r
206         \r
207         /* Increase the cursor. */\r
208         prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );\r
209         vTaskDelay( lcdLONG_DELAY );                    \r
210         vTaskDelay( lcdLONG_DELAY );                    \r
211         vTaskDelay( lcdLONG_DELAY );\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 static void vLCDTask(void *pvParameters)\r
216 {\r
217 xLCDMessage xMessage;\r
218 unsigned portSHORT usRow = 0;\r
219 \r
220         /* Initialise the hardware.  This uses delays so must not be called prior\r
221         to the scheduler being started. */\r
222         prvSetupLCD();\r
223 \r
224         /* Welcome message. */\r
225         prvLCDPutString( "www.FreeRTOS.org" );\r
226 \r
227         for(;;)\r
228         {\r
229                 /* Wait for a message to arrive that requires displaying. */\r
230                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
231 \r
232                 /* Clear the current display value. */\r
233                 prvLCDClear();\r
234 \r
235                 /* Switch rows each time so we can see that the display is still being\r
236                 updated. */\r
237                 prvLCDGotoRow( usRow & 0x01 );\r
238                 usRow++;\r
239                 prvLCDPutString( xMessage.pcMessage );\r
240 \r
241                 /* Delay the requested amount of time to ensure the text just written \r
242                 to the LCD is not overwritten. */\r
243                 vTaskDelay( xMessage.xMinDisplayTime );         \r
244         }\r
245 }\r
246 \r
247 \r
248 \r
249 \r