]> begriffs open source - cmsis-freertos/blob - Demo/PIC32MX_MPLAB/lcd.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / PIC32MX_MPLAB / lcd.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /* peripheral library include */
71 #include <plib.h>
72
73 /* Scheduler includes. */
74 #include "FreeRTOS.h"
75 #include "task.h"
76 #include "queue.h"
77
78 /* Demo includes. */
79 #include "lcd.h"
80
81 /*
82  * The LCD is written to by more than one task so is controlled by this
83  * 'gatekeeper' task.  This is the only task that is actually permitted to
84  * access the LCD directly.  Other tasks wanting to display a message send
85  * the message to the gatekeeper.
86  */
87 static void vLCDTask( void *pvParameters );
88
89 /*
90  * Setup the peripherals required to communicate with the LCD.
91  */
92 static void prvSetupLCD( void );
93
94 /* 
95  * Move to the first (0) or second (1) row of the LCD. 
96  */
97 static void prvLCDGotoRow( unsigned short usRow );
98
99 /* 
100  * Write a string of text to the LCD. 
101  */
102 static void prvLCDPutString( char *pcString );
103
104 /* 
105  * Clear the LCD. 
106  */
107 static void prvLCDClear( void );
108
109 /*-----------------------------------------------------------*/
110
111 /* Brief delay to permit the LCD to catch up with commands. */
112 #define lcdVERY_SHORT_DELAY     ( 1 )
113 #define lcdSHORT_DELAY          ( 8 / portTICK_PERIOD_MS )
114 #define lcdLONG_DELAY           ( 15 / portTICK_PERIOD_MS )
115
116 /* LCD specific definitions. */
117 #define LCD_CLEAR_DISPLAY_CMD                   0x01
118 #define LCD_CURSOR_HOME_CMD                             0x02
119 #define LCD_ENTRY_MODE_CMD                              0x04
120 #define LCD_ENTRY_MODE_INCREASE                 0x02
121 #define LCD_DISPLAY_CTRL_CMD                    0x08
122 #define LCD_DISPLAY_CTRL_DISPLAY_ON             0x04
123 #define LCD_FUNCTION_SET_CMD                    0x20
124 #define LCD_FUNCTION_SET_8_BITS                 0x10
125 #define LCD_FUNCTION_SET_2_LINES                0x08
126 #define LCD_FUNCTION_SET_LRG_FONT               0x04
127 #define LCD_NEW_LINE                                    0xC0
128 #define LCD_COMMAND_ADDRESS                             0x00
129 #define LCD_DATA_ADDRESS                                0x01
130
131 /* The length of the queue used to send messages to the LCD gatekeeper task. */
132 #define lcdQUEUE_SIZE           3
133
134 /*-----------------------------------------------------------*/
135
136 /* The queue used to send messages to the LCD task. */
137 QueueHandle_t xLCDQueue;
138
139 /* LCD access functions. */
140 static void prvLCDCommand( char cCommand );
141 static void prvLCDData( char cChar );
142
143 /*-----------------------------------------------------------*/
144
145 QueueHandle_t xStartLCDTask( void )
146 {
147         /* Create the queue used by the LCD task.  Messages for display on the LCD
148         are received via this queue. */
149         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));
150
151         /* Start the task that will write to the LCD.  The LCD hardware is
152         initialised from within the task itself so delays can be used. */
153         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
154
155         return xLCDQueue;
156 }
157 /*-----------------------------------------------------------*/
158
159 static void prvLCDGotoRow( unsigned short usRow )
160 {
161         if(usRow == 0) 
162         {
163                 prvLCDCommand( LCD_CURSOR_HOME_CMD );
164         } 
165         else 
166         {
167                 prvLCDCommand( LCD_NEW_LINE );
168         }
169 }
170 /*-----------------------------------------------------------*/
171
172 static void prvLCDCommand( char cCommand ) 
173 {
174         PMPSetAddress( LCD_COMMAND_ADDRESS );
175         PMPMasterWrite( cCommand );
176         vTaskDelay( lcdSHORT_DELAY );
177 }
178 /*-----------------------------------------------------------*/
179
180 static void prvLCDData( char cChar )
181 {
182         PMPSetAddress( LCD_DATA_ADDRESS );
183         PMPMasterWrite( cChar );
184         vTaskDelay( lcdVERY_SHORT_DELAY );
185 }
186 /*-----------------------------------------------------------*/
187
188 static void prvLCDPutString( char *pcString )
189 {
190         /* Write out each character with appropriate delay between each. */
191         while(*pcString)
192         {
193                 prvLCDData(*pcString);
194                 pcString++;
195                 vTaskDelay(lcdSHORT_DELAY);
196         }
197 }
198 /*-----------------------------------------------------------*/
199
200 static void prvLCDClear(void)
201 {
202         prvLCDCommand(LCD_CLEAR_DISPLAY_CMD);
203 }
204 /*-----------------------------------------------------------*/
205
206 static void prvSetupLCD(void)
207 {
208         /* Wait for proper power up. */
209         vTaskDelay( lcdLONG_DELAY );
210         
211         /* Open the PMP port */
212         mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |
213                           PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |
214                           PMP_WRITE_POL_HI | PMP_READ_POL_HI),
215                          (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |
216                           PMP_WAIT_END_4),
217                           PMP_PEN_0, 0);
218                          
219         /* Wait for the LCD to power up correctly. */
220         vTaskDelay( lcdLONG_DELAY );
221         vTaskDelay( lcdLONG_DELAY );
222         vTaskDelay( lcdLONG_DELAY );
223
224         /* Set up the LCD function. */
225         prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );
226         
227         /* Turn the display on. */
228         prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );
229         
230         /* Clear the display. */
231         prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );
232         vTaskDelay( lcdLONG_DELAY );    
233         
234         /* Increase the cursor. */
235         prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );
236         vTaskDelay( lcdLONG_DELAY );                    
237         vTaskDelay( lcdLONG_DELAY );                    
238         vTaskDelay( lcdLONG_DELAY );
239 }
240 /*-----------------------------------------------------------*/
241
242 static void vLCDTask(void *pvParameters)
243 {
244 xLCDMessage xMessage;
245 unsigned short usRow = 0;
246
247         /* Initialise the hardware.  This uses delays so must not be called prior
248         to the scheduler being started. */
249         prvSetupLCD();
250
251         /* Welcome message. */
252         prvLCDPutString( "www.FreeRTOS.org" );
253
254         for(;;)
255         {
256                 /* Wait for a message to arrive that requires displaying. */
257                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
258
259                 /* Clear the current display value. */
260                 prvLCDClear();
261
262                 /* Switch rows each time so we can see that the display is still being
263                 updated. */
264                 prvLCDGotoRow( usRow & 0x01 );
265                 usRow++;
266                 prvLCDPutString( xMessage.pcMessage );
267
268                 /* Delay the requested amount of time to ensure the text just written 
269                 to the LCD is not overwritten. */
270                 vTaskDelay( xMessage.xMinDisplayTime );         
271         }
272 }
273
274
275
276