]> begriffs open source - cmsis-freertos/blob - Demo/PIC24_MPLAB/lcd.c
osThreadTerminate: thread state check added before delete operation
[cmsis-freertos] / Demo / PIC24_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 /* Scheduler includes. */
71 #include "FreeRTOS.h"
72 #include "task.h"
73 #include "queue.h"
74
75 /* Demo includes. */
76 #include "lcd.h"
77
78 /*
79  * The LCD is written to by more than one task so is controlled by this
80  * 'gatekeeper' task.  This is the only task that is actually permitted to
81  * access the LCD directly.  Other tasks wanting to display a message send
82  * the message to the gatekeeper.
83  */
84 static void vLCDTask( void *pvParameters );
85
86 /*
87  * Setup the peripherals required to communicate with the LCD.
88  */
89 static void prvSetupLCD( void );
90
91 /* 
92  * Move to the first (0) or second (1) row of the LCD. 
93  */
94 static void prvLCDGotoRow( unsigned short usRow );
95
96 /* 
97  * Write a string of text to the LCD. 
98  */
99 static void prvLCDPutString( char *pcString );
100
101 /* 
102  * Clear the LCD. 
103  */
104 static void prvLCDClear( void );
105
106 /*-----------------------------------------------------------*/
107
108 /* Brief delay to permit the LCD to catch up with commands. */
109 #define lcdSHORT_DELAY          3
110
111 /* SFR that seems to be missing from the standard header files. */
112 #define PMAEN                           *( ( unsigned short * ) 0x60c )
113
114 /* LCD commands. */
115 #define lcdDEFAULT_FUNCTION     0x3c
116 #define lcdDISPLAY_CONTROL      0x0c
117 #define lcdCLEAR_DISPLAY        0x01
118 #define lcdENTRY_MODE           0x06
119
120 /* The length of the queue used to send messages to the LCD gatekeeper task. */
121 #define lcdQUEUE_SIZE           3
122 /*-----------------------------------------------------------*/
123
124 /* The queue used to send messages to the LCD task. */
125 QueueHandle_t xLCDQueue;
126
127
128 /*-----------------------------------------------------------*/
129
130 QueueHandle_t xStartLCDTask( void )
131 {
132         /* Create the queue used by the LCD task.  Messages for display on the LCD
133         are received via this queue. */
134         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
135
136         /* Start the task that will write to the LCD.  The LCD hardware is
137         initialised from within the task itself so delays can be used. */
138         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
139
140         return xLCDQueue;
141 }
142 /*-----------------------------------------------------------*/
143
144 static void prvLCDGotoRow( unsigned short usRow )
145 {
146         if( usRow == 0 )
147         {
148                 PMADDR = 0x0000;
149                 PMDIN1 = 0x02;
150         }
151         else
152         {
153                 PMADDR = 0x0000;
154                 PMDIN1 = 0xc0;
155         }
156
157         vTaskDelay( lcdSHORT_DELAY );
158 }
159 /*-----------------------------------------------------------*/
160
161 static void prvLCDPutString( char *pcString )
162 {
163         /* Write out each character with appropriate delay between each. */
164         while( *pcString )
165         {
166                 PMADDR = 0x0001;
167                 PMDIN1 = *pcString;
168                 pcString++;
169                 vTaskDelay( lcdSHORT_DELAY );
170         }
171 }
172 /*-----------------------------------------------------------*/
173
174 static void prvLCDClear( void )
175 {
176         /* Clear the display. */
177         PMADDR = 0x0000;
178         PMDIN1 = lcdCLEAR_DISPLAY;
179         vTaskDelay( lcdSHORT_DELAY );   
180 }
181 /*-----------------------------------------------------------*/
182
183 static void prvSetupLCD( void )
184 {
185         /* Setup the PMP. */
186         PMCON = 0x83BF;
187         PMMODE = 0x3FF;
188         PMAEN = 1;
189         PMADDR = 0x0000;
190         vTaskDelay( lcdSHORT_DELAY );
191
192         /* Set the default function. */
193         PMDIN1 = lcdDEFAULT_FUNCTION;
194         vTaskDelay( lcdSHORT_DELAY );
195
196         /* Set the display control. */
197         PMDIN1 = lcdDISPLAY_CONTROL;
198         vTaskDelay( lcdSHORT_DELAY );
199
200         /* Clear the display. */
201         PMDIN1 = lcdCLEAR_DISPLAY;
202         vTaskDelay( lcdSHORT_DELAY );
203
204         /* Set the entry mode. */
205         PMDIN1 = lcdENTRY_MODE;
206         vTaskDelay( lcdSHORT_DELAY );
207 }
208 /*-----------------------------------------------------------*/
209
210 static void vLCDTask( void *pvParameters )
211 {
212 xLCDMessage xMessage;
213 unsigned short usRow = 0;
214
215         /* Remove compiler warnigns. */
216         ( void ) pvParameters;
217
218         /* Initialise the hardware.  This uses delays so must not be called prior
219         to the scheduler being started. */
220         prvSetupLCD();
221
222         /* Welcome message. */
223         prvLCDPutString( "www.FreeRTOS.org" );
224
225         for( ;; )
226         {
227                 /* Wait for a message to arrive that requires displaying. */
228                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
229
230                 /* Clear the current display value. */
231                 prvLCDClear();
232
233                 /* Switch rows each time so we can see that the display is still being
234                 updated. */
235                 prvLCDGotoRow( usRow & 0x01 );
236                 usRow++;
237                 prvLCDPutString( xMessage.pcMessage );
238
239                 /* Delay the requested amount of time to ensure the text just written 
240                 to the LCD is not overwritten. */
241                 vTaskDelay( xMessage.xMinDisplayTime );         
242         }
243 }
244
245
246
247