comparison Scan/STLcd/lcd_scan.c @ 331:9e31d92caf12

Initial STLcd code. - Basic screen initialization and clear is working - Currently SPI is set to a low speed for easy logic analyzer debugging
author Jacob Alexander <haata@kiibohd.com>
date Tue, 14 Apr 2015 00:40:48 -0700
parents
children 2e0074f75855
comparison
equal deleted inserted replaced
330:f4d4cad283c6 331:9e31d92caf12
1 /* Copyright (C) 2015 by Jacob Alexander
2 *
3 * This file is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This file is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this file. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/ScanLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <led.h>
25 #include <print.h>
26
27 // Local Includes
28 #include "lcd_scan.h"
29
30
31
32 // ----- Defines -----
33
34 #define LCD_TOTAL_VISIBLE_PAGES 4
35 #define LCD_PAGE_LEN 132
36
37
38
39 // ----- Macros -----
40
41 // Number of entries in the SPI0 TxFIFO
42 #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
43
44
45
46 // ----- Structs -----
47
48 // ----- Function Declarations -----
49
50 // CLI Functions
51 void cliFunc_lcdCmd( char* args );
52 void cliFunc_lcdInit( char* args );
53 void cliFunc_lcdTest( char* args );
54
55
56
57 // ----- Variables -----
58
59 // Full Toggle State
60 uint8_t cliFullToggleState = 0;
61
62 // Normal/Reverse Toggle State
63 uint8_t cliNormalReverseToggleState = 0;
64
65 // Scan Module command dictionary
66 CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
67 CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
68 CLIDict_Entry( lcdTest, "Test out the LCD display." );
69
70 CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
71 CLIDict_Item( lcdCmd ),
72 CLIDict_Item( lcdInit ),
73 CLIDict_Item( lcdTest ),
74 { 0, 0, 0 } // Null entry for dictionary end
75 };
76
77
78
79 // ----- Interrupt Functions -----
80
81
82
83 // ----- Functions -----
84
85 inline void SPI_setup()
86 {
87 // Enable SPI internal clock
88 SIM_SCGC6 |= SIM_SCGC6_SPI0;
89
90 // Setup MOSI (SOUT) and SCLK (SCK)
91 PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
92 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
93
94 // Setup SS (PCS)
95 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
96
97 // Master Mode, CS0
98 SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
99
100 // DSPI Clock and Transfer Attributes
101 // Frame Size: 8 bits
102 // MSB First
103 // CLK Low by default
104 SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
105 | SPI_CTAR_ASC(7)
106 | SPI_CTAR_DT(7)
107 | SPI_CTAR_CSSCK(7)
108 | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
109 }
110
111 // Write buffer to SPI FIFO
112 void SPI_write( uint8_t *buffer, uint8_t len )
113 {
114
115 for ( uint8_t byte = 0; byte < len; byte++ )
116 {
117 // Wait for SPI TxFIFO to have 4 or fewer entries
118 while ( !( SPI0_SR & SPI_SR_TFFF ) )
119 delayMicroseconds(10);
120
121 // Write byte to TxFIFO
122 // CS0, CTAR0
123 SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
124
125 // Indicate transfer has completed
126 while ( !( SPI0_SR & SPI_SR_TCF ) );
127 SPI0_SR |= SPI_SR_TCF;
128 }
129 }
130
131 // Write to a control register
132 void LCD_writeControlReg( uint8_t byte )
133 {
134 // Wait for TxFIFO to be empt
135 while ( SPI0_TxFIFO_CNT != 0 );
136
137 // Set A0 low to enter control register mode
138 GPIOC_PCOR |= (1<<7);
139
140 // Write byte to SPI FIFO
141 SPI_write( &byte, 1 );
142
143 // Wait for TxFIFO to be empty
144 while ( SPI0_TxFIFO_CNT != 0 );
145
146 // Make sure data has transferred
147 delayMicroseconds(10); // XXX Adjust if SPI speed changes
148
149 // Set A0 high to go back to display register mode
150 GPIOC_PSOR |= (1<<7);
151 }
152
153 // Write to display register
154 // Pages 0-7 normal display
155 // Page 8 icon buffer
156 void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
157 {
158 // Set the register page
159 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
160
161 // Write buffer to SPI
162 SPI_write( buffer, len );
163 }
164
165 inline void LCD_clearPage( uint8_t page )
166 {
167 // Set the register page
168 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
169
170 // Set display start line
171 LCD_writeControlReg( 0x40 );
172
173 // Reset Column Address
174 LCD_writeControlReg( 0x10 );
175 LCD_writeControlReg( 0x00 );
176
177 for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
178 {
179 uint8_t byte = 0;
180
181 // Write buffer to SPI
182 SPI_write( &byte, 1 );
183 }
184
185 // Wait for TxFIFO to be empty
186 while ( SPI0_TxFIFO_CNT != 0 );
187 }
188
189 // Clear Display
190 void LCD_clear()
191 {
192 // Setup each page
193 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
194 {
195 LCD_clearPage( page );
196 }
197
198 // Reset Page, Start Line, and Column Address
199 // Page
200 LCD_writeControlReg( 0xB0 );
201
202 // Start Line
203 LCD_writeControlReg( 0x40 );
204
205 // Reset Column Address
206 LCD_writeControlReg( 0x10 );
207 LCD_writeControlReg( 0x00 );
208 }
209
210 // Intialize display
211 void LCD_initialize()
212 {
213 // ADC Select (Normal)
214 LCD_writeControlReg( 0xA0 );
215
216 // LCD Off
217 LCD_writeControlReg( 0xAE );
218
219 // COM Scan Output Direction
220 LCD_writeControlReg( 0xC0 );
221
222 // LCD Bias (1/6 bias)
223 LCD_writeControlReg( 0xA2 );
224
225 // Power Supply Operating Mode (Internal Only)
226 LCD_writeControlReg( 0x2F );
227
228 // Internal Rb/Ra Ratio
229 LCD_writeControlReg( 0x26 );
230
231 // Reset
232 LCD_writeControlReg( 0xE2 );
233
234 // Electric volume mode set, and value
235 LCD_writeControlReg( 0x81 );
236 LCD_writeControlReg( 0x00 );
237
238 // LCD On
239 LCD_writeControlReg( 0xAF );
240
241 // Clear Display RAM
242 LCD_clear();
243 }
244
245 // Setup
246 inline void LCD_setup()
247 {
248 // Register Scan CLI dictionary
249 CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
250
251 // Initialize SPI
252 SPI_setup();
253
254
255 // Setup Register Control Signal (A0)
256 // Start in display register mode (1)
257 GPIOC_PDDR |= (1<<7);
258 PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
259 GPIOC_PSOR |= (1<<7);
260
261 // Setup LCD Reset pin (RST)
262 // 0 - Reset, 1 - Normal Operation
263 // Start in normal mode (1)
264 GPIOC_PDDR |= (1<<8);
265 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
266 GPIOC_PSOR |= (1<<8);
267
268 // Run LCD intialization sequence
269 LCD_initialize();
270 }
271
272
273 // LCD State processing loop
274 inline uint8_t LCD_scan()
275 {
276 // NOP - Screen Refresh
277 //LCD_writeControlReg( 0xE3 );
278 return 0;
279 }
280
281
282
283 // ----- CLI Command Functions -----
284
285 void cliFunc_lcdInit( char* args )
286 {
287 print( NL ); // No \r\n by default after the command is entered
288 LCD_initialize();
289 }
290
291 void cliFunc_lcdTest( char* args )
292 {
293 print( NL ); // No \r\n by default after the command is entered
294
295 //LCD_initialize();
296 // Test pattern
297 uint8_t pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
298 //uint8_t pattern[] = { 0xFF, 0x00, 0x96, 0xFF, 0x00, 0xFF, 0x00 };
299
300 // Write to page D0
301 LCD_writeDisplayReg( 0, pattern, sizeof( pattern ) );
302 }
303
304 void cliFunc_lcdCmd( char* args )
305 {
306 char* curArgs;
307 char* arg1Ptr;
308 char* arg2Ptr = args;
309
310 print( NL ); // No \r\n by default after the command is entered
311
312 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
313 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
314
315 // No args
316 if ( *arg1Ptr == '\0' )
317 return;
318
319 // SPI Command
320 uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
321
322 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
323 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
324
325 // Single Arg
326 if ( *arg1Ptr == '\0' )
327 goto cmd;
328
329 // TODO Deal with a0
330 cmd:
331 info_msg("Sending - ");
332 printHex( cmd );
333 print( NL );
334 LCD_writeControlReg( cmd );
335 }
336