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>
28 // Interconnect module if compiled in
29 #if defined(ConnectEnabled_define)
30 #include <connect_scan.h>
38 // ----- Defines -----
40 #define LCD_TOTAL_VISIBLE_PAGES 4
41 #define LCD_TOTAL_PAGES 9
42 #define LCD_PAGE_LEN 128
48 // Number of entries in the SPI0 TxFIFO
49 #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
53 // ----- Structs -----
55 // ----- Function Declarations -----
58 void cliFunc_lcdCmd ( char* args );
59 void cliFunc_lcdColor( char* args );
60 void cliFunc_lcdDisp ( char* args );
61 void cliFunc_lcdInit ( char* args );
62 void cliFunc_lcdTest ( char* args );
66 // ----- Variables -----
68 // Default Image - Displays on startup
69 const uint8_t STLcdDefaultImage[] = { STLcdDefaultImage_define };
72 uint8_t cliFullToggleState = 0;
74 // Normal/Reverse Toggle State
75 uint8_t cliNormalReverseToggleState = 0;
77 // Scan Module command dictionary
78 CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
79 CLIDict_Entry( lcdColor, "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
80 CLIDict_Entry( lcdDisp, "Write byte(s) to given page starting at given address. i.e. 0x1 0x5 0xFF 0x00" );
81 CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
82 CLIDict_Entry( lcdTest, "Test out the LCD display." );
84 CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
85 CLIDict_Item( lcdCmd ),
86 CLIDict_Item( lcdColor ),
87 CLIDict_Item( lcdDisp ),
88 CLIDict_Item( lcdInit ),
89 CLIDict_Item( lcdTest ),
90 { 0, 0, 0 } // Null entry for dictionary end
95 // ----- Interrupt Functions -----
99 // ----- Functions -----
101 inline void SPI_setup()
103 // Enable SPI internal clock
104 SIM_SCGC6 |= SIM_SCGC6_SPI0;
106 // Setup MOSI (SOUT) and SCLK (SCK)
107 PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
108 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
111 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
114 SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
116 // DSPI Clock and Transfer Attributes
117 // Frame Size: 8 bits
119 // CLK Low by default
120 SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
124 | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
127 // Write buffer to SPI FIFO
128 void SPI_write( uint8_t *buffer, uint8_t len )
131 for ( uint8_t byte = 0; byte < len; byte++ )
133 // Wait for SPI TxFIFO to have 4 or fewer entries
134 while ( !( SPI0_SR & SPI_SR_TFFF ) )
135 delayMicroseconds(10);
137 // Write byte to TxFIFO
139 SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
141 // Indicate transfer has completed
142 while ( !( SPI0_SR & SPI_SR_TCF ) );
143 SPI0_SR |= SPI_SR_TCF;
147 // Write to a control register
148 void LCD_writeControlReg( uint8_t byte )
150 // Wait for TxFIFO to be empt
151 while ( SPI0_TxFIFO_CNT != 0 );
153 // Set A0 low to enter control register mode
154 GPIOC_PCOR |= (1<<7);
156 // Write byte to SPI FIFO
157 SPI_write( &byte, 1 );
159 // Wait for TxFIFO to be empty
160 while ( SPI0_TxFIFO_CNT != 0 );
162 // Make sure data has transferred
163 delayMicroseconds(10); // XXX Adjust if SPI speed changes
165 // Set A0 high to go back to display register mode
166 GPIOC_PSOR |= (1<<7);
169 // Write to display register
170 // Pages 0-7 normal display
171 // Page 8 icon buffer
172 void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
174 // Set the register page
175 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
177 // Set display start line
178 LCD_writeControlReg( 0x40 );
180 // Reset Column Address
181 LCD_writeControlReg( 0x10 );
182 LCD_writeControlReg( 0x00 );
184 // Write buffer to SPI
185 SPI_write( buffer, len );
188 inline void LCD_clearPage( uint8_t page )
190 // Set the register page
191 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
193 // Set display start line
194 LCD_writeControlReg( 0x40 );
196 // Reset Column Address
197 LCD_writeControlReg( 0x10 );
198 LCD_writeControlReg( 0x00 );
200 for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
204 // Write buffer to SPI
205 SPI_write( &byte, 1 );
208 // Wait for TxFIFO to be empty
209 while ( SPI0_TxFIFO_CNT != 0 );
216 for ( uint8_t page = 0; page < LCD_TOTAL_PAGES; page++ )
218 LCD_clearPage( page );
221 // Reset Page, Start Line, and Column Address
223 LCD_writeControlReg( 0xB0 );
226 LCD_writeControlReg( 0x40 );
228 // Reset Column Address
229 LCD_writeControlReg( 0x10 );
230 LCD_writeControlReg( 0x00 );
234 void LCD_initialize()
236 // ADC Select (Normal)
237 LCD_writeControlReg( 0xA0 );
240 LCD_writeControlReg( 0xAE );
242 // COM Scan Output Direction
243 LCD_writeControlReg( 0xC0 );
245 // LCD Bias (1/6 bias)
246 LCD_writeControlReg( 0xA2 );
248 // Power Supply Operating Mode (Internal Only)
249 LCD_writeControlReg( 0x2F );
251 // Internal Rb/Ra Ratio
252 LCD_writeControlReg( 0x26 );
255 LCD_writeControlReg( 0xE2 );
257 // Electric volume mode set, and value
258 LCD_writeControlReg( 0x81 );
259 LCD_writeControlReg( 0x00 );
262 LCD_writeControlReg( 0xAF );
269 inline void LCD_setup()
271 // Register Scan CLI dictionary
272 CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
277 // Setup Register Control Signal (A0)
278 // Start in display register mode (1)
279 GPIOC_PDDR |= (1<<7);
280 PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
281 GPIOC_PSOR |= (1<<7);
283 // Setup LCD Reset pin (RST)
284 // 0 - Reset, 1 - Normal Operation
285 // Start in normal mode (1)
286 GPIOC_PDDR |= (1<<8);
287 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
288 GPIOC_PSOR |= (1<<8);
290 // Run LCD intialization sequence
293 // Write default image to LCD
294 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
295 LCD_writeDisplayReg( page, (uint8_t*)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
298 SIM_SCGC6 |= SIM_SCGC6_FTM0;
299 FTM0_CNT = 0; // Reset counter
305 // Set FTM to PWM output - Edge Aligned, Low-true pulses
306 FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
310 // Base FTM clock selection (72 MHz system clock)
311 // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
312 // Higher pre-scalar will use the most power (also look the best)
313 // Pre-scalar calculations
314 // 0 - 72 MHz -> 549 Hz
315 // 1 - 36 MHz -> 275 Hz
316 // 2 - 18 MHz -> 137 Hz
317 // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
318 // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
319 // 5 - 2 250 kHz -> 17 Hz
320 // 6 - 1 125 kHz -> 9 Hz
321 // 7 - 562 500 Hz -> 4 Hz
322 // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
323 // Which will reduce the brightness range
325 // System clock, /w prescalar setting
326 FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
329 FTM0_C0V = STLcdBacklightRed_define;
330 PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
333 FTM0_C1V = STLcdBacklightGreen_define;
334 PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
337 FTM0_C2V = STLcdBacklightBlue_define;
338 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
342 // LCD State processing loop
343 inline uint8_t LCD_scan()
350 // ----- Capabilities -----
352 // Takes 1 8 bit length and 4 16 bit arguments, each corresponding to a layer index
353 // Ordered from top to bottom
354 // The first argument indicates how many numbers to display (max 4), set to 0 to load default image
355 uint16_t LCD_layerStackExact[4];
356 uint8_t LCD_layerStackExact_size = 0;
357 typedef struct LCD_layerStackExact_args {
360 } LCD_layerStackExact_args;
361 void LCD_layerStackExact_capability( uint8_t state, uint8_t stateType, uint8_t *args )
363 // Display capability name
364 if ( stateType == 0xFF && state == 0xFF )
366 print("LCD_layerStackExact_capability(num,layer1,layer2,layer3,layer4)");
371 LCD_layerStackExact_args *stack_args = (LCD_layerStackExact_args*)args;
373 // Number data for LCD
374 const uint8_t numbers[10][128] = {
375 { STLcdNumber0_define },
376 { STLcdNumber1_define },
377 { STLcdNumber2_define },
378 { STLcdNumber3_define },
379 { STLcdNumber4_define },
380 { STLcdNumber5_define },
381 { STLcdNumber6_define },
382 { STLcdNumber7_define },
383 { STLcdNumber8_define },
384 { STLcdNumber9_define },
387 // Color data for numbers
388 const uint16_t colors[10][3] = {
389 { STLcdNumber0Color_define },
390 { STLcdNumber1Color_define },
391 { STLcdNumber2Color_define },
392 { STLcdNumber3Color_define },
393 { STLcdNumber4Color_define },
394 { STLcdNumber5Color_define },
395 { STLcdNumber6Color_define },
396 { STLcdNumber7Color_define },
397 { STLcdNumber8Color_define },
398 { STLcdNumber9Color_define },
401 // Only display if there are layers active
402 if ( stack_args->numArgs > 0 )
404 // Set the color according to the "top-of-stack" layer
405 uint16_t layerIndex = stack_args->layers[0];
406 FTM0_C0V = colors[ layerIndex ][0];
407 FTM0_C1V = colors[ layerIndex ][1];
408 FTM0_C2V = colors[ layerIndex ][2];
410 // Iterate through each of the pages
411 // XXX Many of the values here are hard-coded
412 // Eventually a proper font rendering engine should take care of things like this... -HaaTa
413 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
415 // Set the register page
416 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
418 // Set starting address
419 LCD_writeControlReg( 0x10 );
420 LCD_writeControlReg( 0x00 );
423 for ( uint16_t layer = 0; layer < stack_args->numArgs; layer++ )
425 layerIndex = stack_args->layers[ layer ];
427 // Default to 0, if over 9
428 if ( layerIndex > 9 )
433 // Write page of number to display
434 SPI_write( (uint8_t*)&numbers[ layerIndex ][ page * 32 ], 32 );
437 // Blank out rest of display
439 for ( uint8_t c = 0; c < 4 - stack_args->numArgs; c++ )
441 for ( uint8_t byte = 0; byte < 32; byte++ )
443 SPI_write( &data, 1 );
450 // Set default backlight
451 FTM0_C0V = STLcdBacklightRed_define;
452 FTM0_C1V = STLcdBacklightGreen_define;
453 FTM0_C2V = STLcdBacklightBlue_define;
455 // Write default image
456 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
457 LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
461 // Determines the current layer stack, and sets the LCD output accordingly
462 // Will only work on a master node when using the interconnect (use LCD_layerStackExact_capability instead)
463 uint16_t LCD_layerStack_prevSize = 0;
464 uint16_t LCD_layerStack_prevTop = 0;
465 void LCD_layerStack_capability( uint8_t state, uint8_t stateType, uint8_t *args )
467 // Display capability name
468 if ( stateType == 0xFF && state == 0xFF )
470 print("LCD_layerStack_capability()");
474 // Parse the layer stack, top to bottom
475 extern uint16_t macroLayerIndexStack[];
476 extern uint16_t macroLayerIndexStackSize;
478 // Ignore if the stack size hasn't changed and the top of the stack is the same
479 if ( macroLayerIndexStackSize == LCD_layerStack_prevSize
480 && macroLayerIndexStack[macroLayerIndexStackSize - 1] == LCD_layerStack_prevTop )
484 LCD_layerStack_prevSize = macroLayerIndexStackSize;
485 LCD_layerStack_prevTop = macroLayerIndexStack[macroLayerIndexStackSize - 1];
487 LCD_layerStackExact_args stack_args;
488 memset( stack_args.layers, 0, sizeof( stack_args.layers ) );
490 // Use the LCD_layerStackExact_capability to set the LCD using the determined stack
491 // Construct argument set for capability
492 stack_args.numArgs = macroLayerIndexStackSize;
493 for ( uint16_t layer = 1; layer <= macroLayerIndexStackSize; layer++ )
495 stack_args.layers[ layer - 1 ] = macroLayerIndexStack[ macroLayerIndexStackSize - layer ];
498 // Only deal with the interconnect if it has been compiled in
499 #if defined(ConnectEnabled_define)
500 if ( Connect_master )
503 extern const Capability CapabilitiesList[];
505 // Broadcast layerStackExact remote capability (0xFF is the broadcast id)
506 Connect_send_RemoteCapability(
508 LCD_layerStackExact_capability_index,
511 CapabilitiesList[ LCD_layerStackExact_capability_index ].argCount,
512 (uint8_t*)&stack_args
516 // Call LCD_layerStackExact directly
517 LCD_layerStackExact_capability( state, stateType, (uint8_t*)&stack_args );
522 // ----- CLI Command Functions -----
524 void cliFunc_lcdInit( char* args )
529 void cliFunc_lcdTest( char* args )
531 // Write default image
532 for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
533 LCD_writeDisplayReg( page, (uint8_t *)&STLcdDefaultImage[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
536 void cliFunc_lcdCmd( char* args )
540 char* arg2Ptr = args;
542 print( NL ); // No \r\n by default after the command is entered
544 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
545 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
548 if ( *arg1Ptr == '\0' )
552 uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
554 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
555 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
558 if ( *arg1Ptr == '\0' )
563 info_msg("Sending - ");
566 LCD_writeControlReg( cmd );
569 void cliFunc_lcdColor( char* args )
573 char* arg2Ptr = args;
576 uint16_t rgb[3]; // Red, Green, Blue
578 // Parse integers from 3 arguments
579 for ( uint8_t color = 0; color < 3; color++ )
582 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
584 // Give up if not enough args given
585 if ( *arg1Ptr == '\0' )
588 // Convert argument to integer
589 rgb[ color ] = numToInt( arg1Ptr );
598 void cliFunc_lcdDisp( char* args )
602 char* arg2Ptr = args;
604 // First process page and starting address
606 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
608 // Stop processing args if no more are found
609 if ( *arg1Ptr == '\0' )
611 uint8_t page = numToInt( arg1Ptr );
614 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
616 // Stop processing args if no more are found
617 if ( *arg1Ptr == '\0' )
619 uint8_t address = numToInt( arg1Ptr );
621 // Set the register page
622 LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
624 // Set starting address
625 LCD_writeControlReg( 0x10 | ( ( 0xF0 & address ) >> 4 ) );
626 LCD_writeControlReg( 0x00 | ( 0x0F & address ));
632 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
634 // Stop processing args if no more are found
635 if ( *arg1Ptr == '\0' )
638 uint8_t value = numToInt( arg1Ptr );
640 // Write buffer to SPI
641 SPI_write( &value, 1 );