1 /* Copyright (C) 2015 by Jacob Alexander
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.
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.
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/>.
17 // ----- Includes -----
20 #include <Lib/ScanLib.h>
33 // ----- Defines -----
35 #define LCD_TOTAL_VISIBLE_PAGES 4
36 #define LCD_TOTAL_PAGES 9
37 #define LCD_PAGE_LEN 128
43 // Number of entries in the SPI0 TxFIFO
44 #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
48 // ----- Structs -----
50 // ----- Function Declarations -----
53 void cliFunc_lcdCmd ( char* args );
54 void cliFunc_lcdColor( char* args );
55 void cliFunc_lcdDisp ( char* args );
56 void cliFunc_lcdInit ( char* args );
57 void cliFunc_lcdTest ( char* args );
61 // ----- Variables -----
63 // Default Image - Displays on startup
64 const uint8_t STLcdDefaultImage[] = { STLcdDefaultImage_define };
67 uint8_t cliFullToggleState = 0;
69 // Normal/Reverse Toggle State
70 uint8_t cliNormalReverseToggleState = 0;
72 // Scan Module command dictionary
73 CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
74 CLIDict_Entry( lcdColor, "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
75 CLIDict_Entry( lcdDisp, "Write byte(s) to given page starting at given address. i.e. 0x1 0x5 0xFF 0x00" );
76 CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
77 CLIDict_Entry( lcdTest, "Test out the LCD display." );
79 CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
80 CLIDict_Item( lcdCmd ),
81 CLIDict_Item( lcdColor ),
82 CLIDict_Item( lcdDisp ),
83 CLIDict_Item( lcdInit ),
84 CLIDict_Item( lcdTest ),
85 { 0, 0, 0 } // Null entry for dictionary end
90 // ----- Interrupt Functions -----
94 // ----- Functions -----
96 inline void SPI_setup()
98 // Enable SPI internal clock
99 SIM_SCGC6 |= SIM_SCGC6_SPI0;
101 // Setup MOSI (SOUT) and SCLK (SCK)
102 PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
103 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
106 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
109 SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
111 // DSPI Clock and Transfer Attributes
112 // Frame Size: 8 bits
114 // CLK Low by default
115 SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
119 | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
122 // Write buffer to SPI FIFO
123 void SPI_write( uint8_t *buffer, uint8_t len )
126 for ( uint8_t byte = 0; byte < len; byte++ )
128 // Wait for SPI TxFIFO to have 4 or fewer entries
129 while ( !( SPI0_SR & SPI_SR_TFFF ) )
130 delayMicroseconds(10);
132 // Write byte to TxFIFO
134 SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
136 // Indicate transfer has completed
137 while ( !( SPI0_SR & SPI_SR_TCF ) );
138 SPI0_SR |= SPI_SR_TCF;
142 // Write to a control register
143 void LCD_writeControlReg( uint8_t byte )
145 // Wait for TxFIFO to be empt
146 while ( SPI0_TxFIFO_CNT != 0 );
148 // Set A0 low to enter control register mode
149 GPIOC_PCOR |= (1<<7);
151 // Write byte to SPI FIFO
152 SPI_write( &byte, 1 );
154 // Wait for TxFIFO to be empty
155 while ( SPI0_TxFIFO_CNT != 0 );
157 // Make sure data has transferred
158 delayMicroseconds(10); // XXX Adjust if SPI speed changes
160 // Set A0 high to go back to display register mode
161 GPIOC_PSOR |= (1<<7);
164 // Write to display register
165 // Pages 0-7 normal display
166 // Page 8 icon buffer
167 void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
169 // Set the register page
170 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
172 // Set display start line
173 LCD_writeControlReg( 0x40 );
175 // Reset Column Address
176 LCD_writeControlReg( 0x10 );
177 LCD_writeControlReg( 0x00 );
179 // Write buffer to SPI
180 SPI_write( buffer, len );
183 inline void LCD_clearPage( uint8_t page )
185 // Set the register page
186 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
188 // Set display start line
189 LCD_writeControlReg( 0x40 );
191 // Reset Column Address
192 LCD_writeControlReg( 0x10 );
193 LCD_writeControlReg( 0x00 );
195 for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
199 // Write buffer to SPI
200 SPI_write( &byte, 1 );
203 // Wait for TxFIFO to be empty
204 while ( SPI0_TxFIFO_CNT != 0 );
211 for ( uint8_t page = 0; page < LCD_TOTAL_PAGES; page++ )
213 LCD_clearPage( page );
216 // Reset Page, Start Line, and Column Address
218 LCD_writeControlReg( 0xB0 );
221 LCD_writeControlReg( 0x40 );
223 // Reset Column Address
224 LCD_writeControlReg( 0x10 );
225 LCD_writeControlReg( 0x00 );
229 void LCD_initialize()
231 // ADC Select (Normal)
232 LCD_writeControlReg( 0xA0 );
235 LCD_writeControlReg( 0xAE );
237 // COM Scan Output Direction
238 LCD_writeControlReg( 0xC0 );
240 // LCD Bias (1/6 bias)
241 LCD_writeControlReg( 0xA2 );
243 // Power Supply Operating Mode (Internal Only)
244 LCD_writeControlReg( 0x2F );
246 // Internal Rb/Ra Ratio
247 LCD_writeControlReg( 0x26 );
250 LCD_writeControlReg( 0xE2 );
252 // Electric volume mode set, and value
253 LCD_writeControlReg( 0x81 );
254 LCD_writeControlReg( 0x00 );
257 LCD_writeControlReg( 0xAF );
264 inline void LCD_setup()
266 // Register Scan CLI dictionary
267 CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
272 // Setup Register Control Signal (A0)
273 // Start in display register mode (1)
274 GPIOC_PDDR |= (1<<7);
275 PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
276 GPIOC_PSOR |= (1<<7);
278 // Setup LCD Reset pin (RST)
279 // 0 - Reset, 1 - Normal Operation
280 // Start in normal mode (1)
281 GPIOC_PDDR |= (1<<8);
282 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
283 GPIOC_PSOR |= (1<<8);
285 // Run LCD intialization sequence
288 // Write default image to LCD
289 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
290 LCD_writeDisplayReg( page, (uint8_t*)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
293 SIM_SCGC6 |= SIM_SCGC6_FTM0;
294 FTM0_CNT = 0; // Reset counter
300 // Set FTM to PWM output - Edge Aligned, Low-true pulses
301 FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
305 // Base FTM clock selection (72 MHz system clock)
306 // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
307 // Higher pre-scalar will use the most power (also look the best)
308 // Pre-scalar calculations
309 // 0 - 72 MHz -> 549 Hz
310 // 1 - 36 MHz -> 275 Hz
311 // 2 - 18 MHz -> 137 Hz
312 // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
313 // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
314 // 5 - 2 250 kHz -> 17 Hz
315 // 6 - 1 125 kHz -> 9 Hz
316 // 7 - 562 500 Hz -> 4 Hz
317 // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
318 // Which will reduce the brightness range
320 // System clock, /w prescalar setting
321 FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
324 FTM0_C0V = STLcdBacklightRed_define;
325 PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
328 FTM0_C1V = STLcdBacklightGreen_define;
329 PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
332 FTM0_C2V = STLcdBacklightBlue_define;
333 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
337 // LCD State processing loop
338 inline uint8_t LCD_scan()
345 // ----- CLI Command Functions -----
347 void cliFunc_lcdInit( char* args )
352 void cliFunc_lcdTest( char* args )
354 // Write default image
355 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
356 LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
359 void cliFunc_lcdCmd( char* args )
363 char* arg2Ptr = args;
365 print( NL ); // No \r\n by default after the command is entered
367 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
368 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
371 if ( *arg1Ptr == '\0' )
375 uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
377 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
378 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
381 if ( *arg1Ptr == '\0' )
386 info_msg("Sending - ");
389 LCD_writeControlReg( cmd );
392 void cliFunc_lcdColor( char* args )
396 char* arg2Ptr = args;
399 uint16_t rgb[3]; // Red, Green, Blue
401 // Parse integers from 3 arguments
402 for ( uint8_t color = 0; color < 3; color++ )
405 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
407 // Give up if not enough args given
408 if ( *arg1Ptr == '\0' )
411 // Convert argument to integer
412 rgb[ color ] = numToInt( arg1Ptr );
421 void cliFunc_lcdDisp( char* args )
425 char* arg2Ptr = args;
427 // First process page and starting address
429 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
431 // Stop processing args if no more are found
432 if ( *arg1Ptr == '\0' )
434 uint8_t page = numToInt( arg1Ptr );
437 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
439 // Stop processing args if no more are found
440 if ( *arg1Ptr == '\0' )
442 uint8_t address = numToInt( arg1Ptr );
444 // Set the register page
445 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
447 // Set starting address
448 LCD_writeControlReg( 0x10 | ( ( 0xF0 & address ) >> 4 ) );
449 LCD_writeControlReg( 0x00 | ( 0x0F & address ));
455 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
457 // Stop processing args if no more are found
458 if ( *arg1Ptr == '\0' )
461 uint8_t value = numToInt( arg1Ptr );
463 // Write buffer to SPI
464 SPI_write( &value, 1 );