123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- /* Copyright (C) 2015 by Jacob Alexander
- *
- * This file is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This file is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this file. If not, see <http://www.gnu.org/licenses/>.
- */
-
- // ----- Includes -----
-
- // Compiler Includes
- #include <Lib/ScanLib.h>
-
- // Project Includes
- #include <cli.h>
- #include <kll.h>
- #include <led.h>
- #include <print.h>
-
- // Local Includes
- #include "lcd_scan.h"
-
-
-
- // ----- Defines -----
-
- #define LCD_TOTAL_VISIBLE_PAGES 4
- #define LCD_PAGE_LEN 128
-
-
-
- // ----- Macros -----
-
- // Number of entries in the SPI0 TxFIFO
- #define SPI0_TxFIFO_CNT ( ( SPI0_SR & SPI_SR_TXCTR ) >> 12 )
-
-
-
- // ----- Structs -----
-
- // ----- Function Declarations -----
-
- // CLI Functions
- void cliFunc_lcdCmd ( char* args );
- void cliFunc_lcdColor( char* args );
- void cliFunc_lcdInit ( char* args );
- void cliFunc_lcdTest ( char* args );
-
-
-
- // ----- Variables -----
-
- // Full Toggle State
- uint8_t cliFullToggleState = 0;
-
- // Normal/Reverse Toggle State
- uint8_t cliNormalReverseToggleState = 0;
-
- // Scan Module command dictionary
- CLIDict_Entry( lcdCmd, "Send byte via SPI, second argument enables a0. Defaults to control." );
- CLIDict_Entry( lcdColor, "Set backlight color. 3 16-bit numbers: R G B. i.e. 0xFFF 0x1444 0x32" );
- CLIDict_Entry( lcdInit, "Re-initialize the LCD display." );
- CLIDict_Entry( lcdTest, "Test out the LCD display." );
-
- CLIDict_Def( lcdCLIDict, "ST LCD Module Commands" ) = {
- CLIDict_Item( lcdCmd ),
- CLIDict_Item( lcdColor ),
- CLIDict_Item( lcdInit ),
- CLIDict_Item( lcdTest ),
- { 0, 0, 0 } // Null entry for dictionary end
- };
-
-
-
- // ----- Interrupt Functions -----
-
-
-
- // ----- Functions -----
-
- inline void SPI_setup()
- {
- // Enable SPI internal clock
- SIM_SCGC6 |= SIM_SCGC6_SPI0;
-
- // Setup MOSI (SOUT) and SCLK (SCK)
- PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(2);
- PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(2);
-
- // Setup SS (PCS)
- PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(2);
-
- // Master Mode, CS0
- SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1);
-
- // DSPI Clock and Transfer Attributes
- // Frame Size: 8 bits
- // MSB First
- // CLK Low by default
- SPI0_CTAR0 = SPI_CTAR_FMSZ(7)
- | SPI_CTAR_ASC(7)
- | SPI_CTAR_DT(7)
- | SPI_CTAR_CSSCK(7)
- | SPI_CTAR_PBR(0) | SPI_CTAR_BR(7);
- }
-
- // Write buffer to SPI FIFO
- void SPI_write( uint8_t *buffer, uint8_t len )
- {
-
- for ( uint8_t byte = 0; byte < len; byte++ )
- {
- // Wait for SPI TxFIFO to have 4 or fewer entries
- while ( !( SPI0_SR & SPI_SR_TFFF ) )
- delayMicroseconds(10);
-
- // Write byte to TxFIFO
- // CS0, CTAR0
- SPI0_PUSHR = ( buffer[ byte ] & 0xff ) | SPI_PUSHR_PCS(1);
-
- // Indicate transfer has completed
- while ( !( SPI0_SR & SPI_SR_TCF ) );
- SPI0_SR |= SPI_SR_TCF;
- }
- }
-
- // Write to a control register
- void LCD_writeControlReg( uint8_t byte )
- {
- // Wait for TxFIFO to be empt
- while ( SPI0_TxFIFO_CNT != 0 );
-
- // Set A0 low to enter control register mode
- GPIOC_PCOR |= (1<<7);
-
- // Write byte to SPI FIFO
- SPI_write( &byte, 1 );
-
- // Wait for TxFIFO to be empty
- while ( SPI0_TxFIFO_CNT != 0 );
-
- // Make sure data has transferred
- delayMicroseconds(10); // XXX Adjust if SPI speed changes
-
- // Set A0 high to go back to display register mode
- GPIOC_PSOR |= (1<<7);
- }
-
- // Write to display register
- // Pages 0-7 normal display
- // Page 8 icon buffer
- void LCD_writeDisplayReg( uint8_t page, uint8_t *buffer, uint8_t len )
- {
- // Set the register page
- LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
-
- // Set display start line
- LCD_writeControlReg( 0x40 );
-
- // Reset Column Address
- LCD_writeControlReg( 0x10 );
- LCD_writeControlReg( 0x00 );
-
- // Write buffer to SPI
- SPI_write( buffer, len );
- }
-
- inline void LCD_clearPage( uint8_t page )
- {
- // Set the register page
- LCD_writeControlReg( 0xB0 | ( 0x0F & page ) );
-
- // Set display start line
- LCD_writeControlReg( 0x40 );
-
- // Reset Column Address
- LCD_writeControlReg( 0x10 );
- LCD_writeControlReg( 0x00 );
-
- for ( uint8_t page_reg = 0; page_reg < LCD_PAGE_LEN; page_reg++ )
- {
- uint8_t byte = 0;
-
- // Write buffer to SPI
- SPI_write( &byte, 1 );
- }
-
- // Wait for TxFIFO to be empty
- while ( SPI0_TxFIFO_CNT != 0 );
- }
-
- // Clear Display
- void LCD_clear()
- {
- // Setup each page
- for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
- {
- LCD_clearPage( page );
- }
-
- // Reset Page, Start Line, and Column Address
- // Page
- LCD_writeControlReg( 0xB0 );
-
- // Start Line
- LCD_writeControlReg( 0x40 );
-
- // Reset Column Address
- LCD_writeControlReg( 0x10 );
- LCD_writeControlReg( 0x00 );
- }
-
- // Intialize display
- void LCD_initialize()
- {
- // ADC Select (Normal)
- LCD_writeControlReg( 0xA0 );
-
- // LCD Off
- LCD_writeControlReg( 0xAE );
-
- // COM Scan Output Direction
- LCD_writeControlReg( 0xC0 );
-
- // LCD Bias (1/6 bias)
- LCD_writeControlReg( 0xA2 );
-
- // Power Supply Operating Mode (Internal Only)
- LCD_writeControlReg( 0x2F );
-
- // Internal Rb/Ra Ratio
- LCD_writeControlReg( 0x26 );
-
- // Reset
- LCD_writeControlReg( 0xE2 );
-
- // Electric volume mode set, and value
- LCD_writeControlReg( 0x81 );
- LCD_writeControlReg( 0x00 );
-
- // LCD On
- LCD_writeControlReg( 0xAF );
-
- // Clear Display RAM
- LCD_clear();
- }
-
- // Setup
- inline void LCD_setup()
- {
- // Register Scan CLI dictionary
- CLI_registerDictionary( lcdCLIDict, lcdCLIDictName );
-
- // Initialize SPI
- SPI_setup();
-
- // Setup Register Control Signal (A0)
- // Start in display register mode (1)
- GPIOC_PDDR |= (1<<7);
- PORTC_PCR7 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
- GPIOC_PSOR |= (1<<7);
-
- // Setup LCD Reset pin (RST)
- // 0 - Reset, 1 - Normal Operation
- // Start in normal mode (1)
- GPIOC_PDDR |= (1<<8);
- PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
- GPIOC_PSOR |= (1<<8);
-
- // Run LCD intialization sequence
- LCD_initialize();
-
- // Setup Backlight
- // TODO Expose default settings
- SIM_SCGC6 |= SIM_SCGC6_FTM0;
- FTM0_CNT = 0; // Reset counter
-
- // PWM Period
- // 16-bit maximum
- FTM0_MOD = 0xFFFF;
-
- // Set FTM to PWM output - Edge Aligned, Low-true pulses
- FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01
- FTM0_C1SC = 0x24;
- FTM0_C2SC = 0x24;
-
- // Base FTM clock selection (72 MHz system clock)
- // @ 0xFFFF period, 72 MHz / 0xFFFF * 2 = Actual period
- // Higher pre-scalar will use the most power (also look the best)
- // Pre-scalar calculations
- // 0 - 72 MHz -> 549 Hz
- // 1 - 36 MHz -> 275 Hz
- // 2 - 18 MHz -> 137 Hz
- // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
- // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
- // 5 - 2 250 kHz -> 17 Hz
- // 6 - 1 125 kHz -> 9 Hz
- // 7 - 562 500 Hz -> 4 Hz
- // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
- // Which will reduce the brightness range
-
- // System clock, /w prescalar setting
- FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
-
- // Red
- FTM0_C0V = STLcdBacklightRed_define;
- PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
-
- // Green
- FTM0_C1V = STLcdBacklightGreen_define;
- PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
-
- // Blue
- FTM0_C2V = STLcdBacklightBlue_define;
- PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4);
- }
-
-
- // LCD State processing loop
- inline uint8_t LCD_scan()
- {
- // NOP - Screen Refresh
- //LCD_writeControlReg( 0xE3 );
- return 0;
- }
-
-
-
- // ----- CLI Command Functions -----
-
- void cliFunc_lcdInit( char* args )
- {
- print( NL ); // No \r\n by default after the command is entered
- LCD_initialize();
- }
-
- void cliFunc_lcdTest( char* args )
- {
- print( NL ); // No \r\n by default after the command is entered
-
- //LCD_initialize();
- // Test pattern
- uint8_t pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
-
-
- uint8_t logo[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- //uint8_t pattern[] = { 0xFF, 0x00, 0x96, 0xFF, 0x00, 0xFF, 0x00 };
-
- // Write to page D0
- //LCD_writeDisplayReg( 0, pattern, sizeof( pattern ) );
-
- for ( uint8_t page = 0; page < LCD_TOTAL_VISIBLE_PAGES; page++ )
- {
- LCD_writeDisplayReg( page, &logo[page * LCD_PAGE_LEN], LCD_PAGE_LEN );
- }
- }
-
- void cliFunc_lcdCmd( char* args )
- {
- char* curArgs;
- char* arg1Ptr;
- char* arg2Ptr = args;
-
- print( NL ); // No \r\n by default after the command is entered
-
- curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
- CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
-
- // No args
- if ( *arg1Ptr == '\0' )
- return;
-
- // SPI Command
- uint8_t cmd = (uint8_t)numToInt( arg1Ptr );
-
- curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
- CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
-
- // Single Arg
- if ( *arg1Ptr == '\0' )
- goto cmd;
-
- // TODO Deal with a0
- cmd:
- info_msg("Sending - ");
- printHex( cmd );
- print( NL );
- LCD_writeControlReg( cmd );
- }
-
- void cliFunc_lcdColor( char* args )
- {
- char* curArgs;
- char* arg1Ptr;
- char* arg2Ptr = args;
-
- // Colors
- uint16_t rgb[3]; // Red, Green, Blue
-
- // Parse integers from 3 arguments
- for ( uint8_t color = 0; color < 3; color++ )
- {
- curArgs = arg2Ptr;
- CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
-
- // Give up if not enough args given
- if ( *arg1Ptr == '\0' )
- return;
-
- // Convert argument to integer
- rgb[ color ] = numToInt( arg1Ptr );
- }
-
- // Set PWM channels
- FTM0_C0V = rgb[0];
- FTM0_C1V = rgb[1];
- FTM0_C2V = rgb[2];
-
- print( NL ); // No \r\n by default after the command is entered
- }
|