From c51dfef958bce4a792b66db337d5c7cdf0956fc4 Mon Sep 17 00:00:00 2001 From: Scott Wilson Date: Sun, 3 Dec 2017 10:32:07 -0500 Subject: Add support for LFKeyboard products: LFK78, LFK87 and SMK65 --- keyboards/lfkeyboards/issi.c | 244 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 keyboards/lfkeyboards/issi.c (limited to 'keyboards/lfkeyboards/issi.c') diff --git a/keyboards/lfkeyboards/issi.c b/keyboards/lfkeyboards/issi.c new file mode 100644 index 000000000..e675a5986 --- /dev/null +++ b/keyboards/lfkeyboards/issi.c @@ -0,0 +1,244 @@ +#ifdef ISSI_ENABLE + +#include +#include +#include +#include +#include +#include +#include "issi.h" +#include "print.h" +#include "TWIlib.h" + +#define ISSI_ADDR_DEFAULT 0xE8 + +#define ISSI_REG_CONFIG 0x00 +#define ISSI_REG_CONFIG_PICTUREMODE 0x00 +#define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08 + +#define ISSI_CONF_PICTUREMODE 0x00 +#define ISSI_CONF_AUTOFRAMEMODE 0x04 +#define ISSI_CONF_AUDIOMODE 0x08 + +#define ISSI_REG_PICTUREFRAME 0x01 + +#define ISSI_REG_SHUTDOWN 0x0A +#define ISSI_REG_AUDIOSYNC 0x06 + +#define ISSI_COMMANDREGISTER 0xFD +#define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine' +uint8_t control[8][9] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, +}; +ISSIDeviceStruct *issi_devices[4] = {0, 0, 0, 0}; + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif + +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +#define I2C_WRITE 0 +#define F_SCL 400000UL // SCL frequency +#define Prescaler 1 +#define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2) + +uint8_t i2c_start(uint8_t address) +{ + // reset TWI control register + TWCR = 0; + // transmit START condition + TWCR = (1<> 1; + ISSIDeviceStruct *device = issi_devices[device_addr]; + if(device == 0){ + return; + } + // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm); + uint8_t x = cx - 1; // funciton takes 1 based counts, but we need 0... + uint8_t y = cy - 1; // creating them once for less confusion + uint8_t control_reg = (y << 1) | (matrix & 0x01); + if(pwm == 0){ + cbi(device->led_ctrl[control_reg], x); + cbi(device->led_blink_ctrl[control_reg], x); + }else{ + sbi(device->led_ctrl[control_reg], x); + sbi(device->led_blink_ctrl[control_reg], x); + } + uint8_t pwm_reg = 0; + switch(matrix & 0x01){ + case 0: + pwm_reg = 0x00; + break; + case 1: + pwm_reg = 0x08; + break; + } + pwm_reg += (y << 4) + x; + device->led_pwm[pwm_reg] = pwm; + device->led_dirty = 1; +} + +void update_issi(uint8_t device_addr, uint8_t blocking) +{ + // This seems to take about 6ms + ISSIDeviceStruct *device = issi_devices[device_addr]; + if(device != 0){ + if(device->fn_dirty){ + device->fn_dirty = 0; + setFrame(device_addr, ISSI_BANK_FUNCTIONREG); + TWITransmitData(&device->fn_device_addr, sizeof(device->fn_registers) + 2, 0, 1); + } + if(device->led_dirty){ + device->led_dirty = 0; + setFrame(device_addr, 0); + TWITransmitData(&device->led_device_addr, 0xB6, 0, blocking); + } + } +} + +void issi_init(void) +{ + TWIInit(); + for(uint8_t device_addr = 0; device_addr < 4; device_addr++){ + // If this device has been previously allocated, free it + if(issi_devices[device_addr] != 0){ + free(issi_devices[device_addr]); + } + // Try to shutdown the device, if this fails skip this device + writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00); + while (!isTWIReady()){_delay_us(1);} + if(TWIInfo.errorCode != 0xFF){ + xprintf("ISSI init failed %d %02X %02X\n", device_addr, TWIInfo.mode, TWIInfo.errorCode); + continue; + } + // Allocate the device structure - calloc zeros it for us + ISSIDeviceStruct *device = (ISSIDeviceStruct *)calloc(sizeof(ISSIDeviceStruct) * 2, 1); + issi_devices[device_addr] = device; + device->fn_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1; + device->fn_register_addr = 0; + device->led_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1; + device->led_register_addr = 0; + // set dirty bits so that all of the buffered data is written out + device->fn_dirty = 1; + device->led_dirty = 1; + update_issi(device_addr, 1); + // Set the function register to picture mode + // device->fn_reg[ISSI_REG_CONFIG] = ISSI_REG_CONFIG_PICTUREMODE; + writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01); + } + + // Shutdown and set all registers to 0 + // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00); + // for(uint8_t bank = 0; bank <= 7; bank++){ + // for (uint8_t reg = 0x00; reg <= 0xB3; reg++) { + // writeRegister8(device_addr, bank, reg, 0x00); + // } + // } + // for (uint8_t reg = 0; reg <= 0x0C; reg++) { + // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, reg, 0x00); + // } + // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE); + // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01); + // picture mode + // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x01, 0x01); + + //Enable blink + // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x05, 0x48B); + + //Enable Breath + +} + +#endif \ No newline at end of file -- cgit v1.2.3-24-g4f1b