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