]> git.donarmstrong.com Git - kiibohd-controller.git/blobdiff - Scan/STLcd/lcd_scan.c
Adding 16-bit brightness control to LCD backlight
[kiibohd-controller.git] / Scan / STLcd / lcd_scan.c
index 5cc7c89a5297b871a6b49dd3cf12599a4950bd18..49a1b82e6f6ed547c0e3d2f66085d2f18c06c29a 100644 (file)
@@ -21,6 +21,7 @@
 
 // Project Includes
 #include <cli.h>
+#include <kll.h>
 #include <led.h>
 #include <print.h>
 
@@ -32,7 +33,7 @@
 // ----- Defines -----
 
 #define LCD_TOTAL_VISIBLE_PAGES 4
-#define LCD_PAGE_LEN 132
+#define LCD_PAGE_LEN 128
 
 
 
@@ -158,6 +159,13 @@ 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 );
 }
@@ -251,7 +259,6 @@ inline void LCD_setup()
        // Initialize SPI
        SPI_setup();
 
-
        // Setup Register Control Signal (A0)
        // Start in display register mode (1)
        GPIOC_PDDR |= (1<<7);
@@ -267,6 +274,52 @@ inline void LCD_setup()
 
        // 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)
+       // Pre-scalar calculations
+       // 0 -      72 MHz - Highest power usage/best result
+       // 1 -      36 MHz
+       // 2 -      18 MHz
+       // 3 -       9 MHz - Slightly visible flicker (peripheral vision)
+       // 4 -   4 500 kHz - Visible flickering
+       // 5 -   2 250 kHz
+       // 6 -   1 125 kHz
+       // 7 - 562 500  Hz
+       // System clock, /w prescalar setting
+       FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define );
+
+       /* Write frequency TODO API
+       FTM0_SC = 0;
+       FTM0_CNT = 0;
+       FTM0_MOD = mod;
+       FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
+       */
+
+       // 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);
 }
 
 
@@ -295,10 +348,23 @@ void cliFunc_lcdTest( char* args )
        //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 ) );
+       //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 )