]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/lcd.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V7.5.0 - 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 lcdSHORT_DELAY          3\r
105 \r
106 /* SFR that seems to be missing from the standard header files. */\r
107 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
108 \r
109 /* LCD commands. */\r
110 #define lcdDEFAULT_FUNCTION     0x3c\r
111 #define lcdDISPLAY_CONTROL      0x0c\r
112 #define lcdCLEAR_DISPLAY        0x01\r
113 #define lcdENTRY_MODE           0x06\r
114 \r
115 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
116 #define lcdQUEUE_SIZE           3\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* The queue used to send messages to the LCD task. */\r
120 xQueueHandle xLCDQueue;\r
121 \r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 xQueueHandle xStartLCDTask( void )\r
126 {\r
127         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
128         are received via this queue. */\r
129         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
130 \r
131         /* Start the task that will write to the LCD.  The LCD hardware is\r
132         initialised from within the task itself so delays can be used. */\r
133         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
134 \r
135         return xLCDQueue;\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
140 {\r
141         if( usRow == 0 )\r
142         {\r
143                 PMADDR = 0x0000;\r
144                 PMDIN1 = 0x02;\r
145         }\r
146         else\r
147         {\r
148                 PMADDR = 0x0000;\r
149                 PMDIN1 = 0xc0;\r
150         }\r
151 \r
152         vTaskDelay( lcdSHORT_DELAY );\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 static void prvLCDPutString( portCHAR *pcString )\r
157 {\r
158         /* Write out each character with appropriate delay between each. */\r
159         while( *pcString )\r
160         {\r
161                 PMADDR = 0x0001;\r
162                 PMDIN1 = *pcString;\r
163                 pcString++;\r
164                 vTaskDelay( lcdSHORT_DELAY );\r
165         }\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void prvLCDClear( void )\r
170 {\r
171         /* Clear the display. */\r
172         PMADDR = 0x0000;\r
173         PMDIN1 = lcdCLEAR_DISPLAY;\r
174         vTaskDelay( lcdSHORT_DELAY );   \r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void prvSetupLCD( void )\r
179 {\r
180         /* Setup the PMP. */\r
181         PMCON = 0x83BF;\r
182         PMMODE = 0x3FF;\r
183         PMAEN = 1;\r
184         PMADDR = 0x0000;\r
185         vTaskDelay( lcdSHORT_DELAY );\r
186 \r
187         /* Set the default function. */\r
188         PMDIN1 = lcdDEFAULT_FUNCTION;\r
189         vTaskDelay( lcdSHORT_DELAY );\r
190 \r
191         /* Set the display control. */\r
192         PMDIN1 = lcdDISPLAY_CONTROL;\r
193         vTaskDelay( lcdSHORT_DELAY );\r
194 \r
195         /* Clear the display. */\r
196         PMDIN1 = lcdCLEAR_DISPLAY;\r
197         vTaskDelay( lcdSHORT_DELAY );\r
198 \r
199         /* Set the entry mode. */\r
200         PMDIN1 = lcdENTRY_MODE;\r
201         vTaskDelay( lcdSHORT_DELAY );\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 static void vLCDTask( void *pvParameters )\r
206 {\r
207 xLCDMessage xMessage;\r
208 unsigned portSHORT usRow = 0;\r
209 \r
210         /* Remove compiler warnigns. */\r
211         ( void ) pvParameters;\r
212 \r
213         /* Initialise the hardware.  This uses delays so must not be called prior\r
214         to the scheduler being started. */\r
215         prvSetupLCD();\r
216 \r
217         /* Welcome message. */\r
218         prvLCDPutString( "www.FreeRTOS.org" );\r
219 \r
220         for( ;; )\r
221         {\r
222                 /* Wait for a message to arrive that requires displaying. */\r
223                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
224 \r
225                 /* Clear the current display value. */\r
226                 prvLCDClear();\r
227 \r
228                 /* Switch rows each time so we can see that the display is still being\r
229                 updated. */\r
230                 prvLCDGotoRow( usRow & 0x01 );\r
231                 usRow++;\r
232                 prvLCDPutString( xMessage.pcMessage );\r
233 \r
234                 /* Delay the requested amount of time to ensure the text just written \r
235                 to the LCD is not overwritten. */\r
236                 vTaskDelay( xMessage.xMinDisplayTime );         \r
237         }\r
238 }\r
239 \r
240 \r
241 \r
242 \r