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