]> git.sur5r.net Git - freertos/blob - Demo/PIC24_MPLAB/lcd.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / PIC24_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Scheduler includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include "queue.h"\r
58 \r
59 /* Demo includes. */\r
60 #include "lcd.h"\r
61 \r
62 /*\r
63  * The LCD is written to by more than one task so is controlled by this\r
64  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
65  * access the LCD directly.  Other tasks wanting to display a message send\r
66  * the message to the gatekeeper.\r
67  */\r
68 static void vLCDTask( void *pvParameters );\r
69 \r
70 /*\r
71  * Setup the peripherals required to communicate with the LCD.\r
72  */\r
73 static void prvSetupLCD( void );\r
74 \r
75 /* \r
76  * Move to the first (0) or second (1) row of the LCD. \r
77  */\r
78 static void prvLCDGotoRow( unsigned portSHORT usRow );\r
79 \r
80 /* \r
81  * Write a string of text to the LCD. \r
82  */\r
83 static void prvLCDPutString( portCHAR *pcString );\r
84 \r
85 /* \r
86  * Clear the LCD. \r
87  */\r
88 static void prvLCDClear( void );\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* Brief delay to permit the LCD to catch up with commands. */\r
93 #define lcdSHORT_DELAY          3\r
94 \r
95 /* SFR that seems to be missing from the standard header files. */\r
96 #define PMAEN                           *( ( unsigned short * ) 0x60c )\r
97 \r
98 /* LCD commands. */\r
99 #define lcdDEFAULT_FUNCTION     0x3c\r
100 #define lcdDISPLAY_CONTROL      0x0c\r
101 #define lcdCLEAR_DISPLAY        0x01\r
102 #define lcdENTRY_MODE           0x06\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 /* The queue used to send messages to the LCD task. */\r
109 xQueueHandle xLCDQueue;\r
110 \r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 xQueueHandle xStartLCDTask( void )\r
115 {\r
116         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
117         are received via this queue. */\r
118         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );\r
119 \r
120         /* Start the task that will write to the LCD.  The LCD hardware is\r
121         initialised from within the task itself so delays can be used. */\r
122         xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
123 \r
124         return xLCDQueue;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static void prvLCDGotoRow( unsigned portSHORT usRow )\r
129 {\r
130         if( usRow == 0 )\r
131         {\r
132                 PMADDR = 0x0000;\r
133                 PMDIN1 = 0x02;\r
134         }\r
135         else\r
136         {\r
137                 PMADDR = 0x0000;\r
138                 PMDIN1 = 0xc0;\r
139         }\r
140 \r
141         vTaskDelay( lcdSHORT_DELAY );\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void prvLCDPutString( portCHAR *pcString )\r
146 {\r
147         /* Write out each character with appropriate delay between each. */\r
148         while( *pcString )\r
149         {\r
150                 PMADDR = 0x0001;\r
151                 PMDIN1 = *pcString;\r
152                 pcString++;\r
153                 vTaskDelay( lcdSHORT_DELAY );\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 static void prvLCDClear( void )\r
159 {\r
160         /* Clear the display. */\r
161         PMADDR = 0x0000;\r
162         PMDIN1 = lcdCLEAR_DISPLAY;\r
163         vTaskDelay( lcdSHORT_DELAY );   \r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static void prvSetupLCD( void )\r
168 {\r
169         /* Setup the PMP. */\r
170         PMCON = 0x83BF;\r
171         PMMODE = 0x3FF;\r
172         PMAEN = 1;\r
173         PMADDR = 0x0000;\r
174         vTaskDelay( lcdSHORT_DELAY );\r
175 \r
176         /* Set the default function. */\r
177         PMDIN1 = lcdDEFAULT_FUNCTION;\r
178         vTaskDelay( lcdSHORT_DELAY );\r
179 \r
180         /* Set the display control. */\r
181         PMDIN1 = lcdDISPLAY_CONTROL;\r
182         vTaskDelay( lcdSHORT_DELAY );\r
183 \r
184         /* Clear the display. */\r
185         PMDIN1 = lcdCLEAR_DISPLAY;\r
186         vTaskDelay( lcdSHORT_DELAY );\r
187 \r
188         /* Set the entry mode. */\r
189         PMDIN1 = lcdENTRY_MODE;\r
190         vTaskDelay( lcdSHORT_DELAY );\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static void vLCDTask( void *pvParameters )\r
195 {\r
196 xLCDMessage xMessage;\r
197 unsigned portSHORT usRow = 0;\r
198 \r
199         /* Initialise the hardware.  This uses delays so must not be called prior\r
200         to the scheduler being started. */\r
201         prvSetupLCD();\r
202 \r
203         /* Welcome message. */\r
204         prvLCDPutString( "www.FreeRTOS.org" );\r
205 \r
206         for( ;; )\r
207         {\r
208                 /* Wait for a message to arrive that requires displaying. */\r
209                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
210 \r
211                 /* Clear the current display value. */\r
212                 prvLCDClear();\r
213 \r
214                 /* Switch rows each time so we can see that the display is still being\r
215                 updated. */\r
216                 prvLCDGotoRow( usRow & 0x01 );\r
217                 usRow++;\r
218                 prvLCDPutString( xMessage.pcMessage );\r
219 \r
220                 /* Delay the requested amount of time to ensure the text just written \r
221                 to the LCD is not overwritten. */\r
222                 vTaskDelay( xMessage.xMinDisplayTime );         \r
223         }\r
224 }\r
225 \r
226 \r
227 \r
228 \r