#include "arm_atsam_protocol.h"
#if !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE)
# include <string.h>
# include "config.h"
# include "config_led.h"
# include "matrix.h"
# define I2C_LED_USE_DMA 1
DmacDescriptor dmac_desc;
DmacDescriptor dmac_desc_wb;
static uint8_t i2c_led_q[I2C_Q_SIZE]; static uint8_t i2c_led_q_s; static uint8_t i2c_led_q_e; static uint8_t i2c_led_q_full;
static uint8_t dma_sendbuf[I2C_DMA_MAX_SEND];
volatile uint8_t i2c_led_q_running;
#endif
void i2c0_init(void) {
DBGC(DC_I2C0_INIT_BEGIN);
CLK_set_i2c0_freq(CHAN_SERCOM_I2C0, FREQ_I2C0_DEFAULT);
PORT->Group[0].PMUX[4].bit.PMUXE = 2;
PORT->Group[0].PMUX[4].bit.PMUXO = 2;
PORT->Group[0].PINCFG[8].bit.PMUXEN = 1;
PORT->Group[0].PINCFG[9].bit.PMUXEN = 1;
SERCOM0->I2CM.CTRLA.bit.MODE = 5;
SERCOM0->I2CM.CTRLA.bit.SPEED = 0; SERCOM0->I2CM.CTRLA.bit.RUNSTDBY = 1;
SERCOM0->I2CM.CTRLA.bit.ENABLE = 1; while (SERCOM0->I2CM.SYNCBUSY.bit.ENABLE) {
DBGC(DC_I2C0_INIT_SYNC_ENABLING);
}
SERCOM0->I2CM.STATUS.bit.BUSSTATE = 1; while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) {
DBGC(DC_I2C0_INIT_SYNC_SYSOP);
}
while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) {
DBGC(DC_I2C0_INIT_WAIT_IDLE);
}
DBGC(DC_I2C0_INIT_COMPLETE);
}
uint8_t i2c0_start(uint8_t address) {
SERCOM0->I2CM.ADDR.bit.ADDR = address;
while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP) {
}
while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) {
}
while (SERCOM0->I2CM.STATUS.bit.RXNACK) {
}
return 1;
}
uint8_t i2c0_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) {
if (!length) return 0;
i2c0_start(address);
while (length) {
SERCOM0->I2CM.DATA.bit.DATA = *data;
while (SERCOM0->I2CM.INTFLAG.bit.MB == 0) {
}
while (SERCOM0->I2CM.STATUS.bit.RXNACK) {
}
data++;
length--;
}
i2c0_stop();
return 1;
}
void i2c0_stop(void) {
if (SERCOM0->I2CM.STATUS.bit.CLKHOLD || SERCOM0->I2CM.INTFLAG.bit.MB == 1 || SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1) {
SERCOM0->I2CM.CTRLB.bit.CMD = 3;
while (SERCOM0->I2CM.SYNCBUSY.bit.SYSOP)
;
while (SERCOM0->I2CM.STATUS.bit.CLKHOLD)
;
while (SERCOM0->I2CM.INTFLAG.bit.MB)
;
while (SERCOM0->I2CM.STATUS.bit.BUSSTATE != 1)
;
}
}
#if !defined(MD_BOOTLOADER) && defined(RGB_MATRIX_ENABLE)
void i2c1_init(void) {
DBGC(DC_I2C1_INIT_BEGIN);
CLK_set_i2c1_freq(CHAN_SERCOM_I2C1, FREQ_I2C1_DEFAULT);
PORT->Group[0].PMUX[8].bit.PMUXE = 2;
PORT->Group[0].PMUX[8].bit.PMUXO = 2;
PORT->Group[0].PINCFG[16].bit.PMUXEN = 1;
PORT->Group[0].PINCFG[17].bit.PMUXEN = 1;
SERCOM1->I2CM.CTRLA.bit.MODE = 5; SERCOM1->I2CM.CTRLA.bit.SPEED = 1; SERCOM1->I2CM.CTRLA.bit.RUNSTDBY = 1;
SERCOM1->I2CM.CTRLB.bit.SMEN = 1;
NVIC_EnableIRQ(SERCOM1_0_IRQn);
SERCOM1->I2CM.INTENSET.bit.ERROR = 1;
SERCOM1->I2CM.CTRLA.bit.ENABLE = 1; while (SERCOM1->I2CM.SYNCBUSY.bit.ENABLE) {
DBGC(DC_I2C1_INIT_SYNC_ENABLING);
}
SERCOM1->I2CM.STATUS.bit.BUSSTATE = 1; while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) {
DBGC(DC_I2C1_INIT_SYNC_SYSOP);
}
while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) {
DBGC(DC_I2C1_INIT_WAIT_IDLE);
}
DBGC(DC_I2C1_INIT_COMPLETE);
}
uint8_t i2c1_start(uint8_t address) {
SERCOM1->I2CM.ADDR.bit.ADDR = address;
while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP) {
}
while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) {
}
while (SERCOM1->I2CM.STATUS.bit.RXNACK) {
}
return 1;
}
uint8_t i2c1_transmit(uint8_t address, uint8_t *data, uint16_t length, uint16_t timeout) {
if (!length) return 0;
i2c1_start(address);
while (length) {
SERCOM1->I2CM.DATA.bit.DATA = *data;
while (SERCOM1->I2CM.INTFLAG.bit.MB == 0) {
}
while (SERCOM1->I2CM.STATUS.bit.RXNACK) {
}
data++;
length--;
}
i2c1_stop();
return 1;
}
void i2c1_stop(void) {
if (SERCOM1->I2CM.STATUS.bit.CLKHOLD || SERCOM1->I2CM.INTFLAG.bit.MB == 1 || SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1) {
SERCOM1->I2CM.CTRLB.bit.CMD = 3;
while (SERCOM1->I2CM.SYNCBUSY.bit.SYSOP)
;
while (SERCOM1->I2CM.STATUS.bit.CLKHOLD)
;
while (SERCOM1->I2CM.INTFLAG.bit.MB)
;
while (SERCOM1->I2CM.STATUS.bit.BUSSTATE != 1)
;
}
}
void i2c_led_send_CRWL(uint8_t drvid) {
uint8_t i2cdata[] = {ISSI3733_CMDRWL, ISSI3733_CMDRWL_WRITE_ENABLE_ONCE};
i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0);
}
void i2c_led_select_page(uint8_t drvid, uint8_t pageno) {
uint8_t i2cdata[] = {ISSI3733_CMDR, pageno};
i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0);
}
void i2c_led_send_GCR(uint8_t drvid) {
uint8_t i2cdata[] = {ISSI3733_GCCR, 0x00};
if (gcr_actual > LED_GCR_MAX) gcr_actual = LED_GCR_MAX;
i2cdata[1] = gcr_actual;
i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0);
}
void i2c_led_send_onoff(uint8_t drvid) {
# if I2C_LED_USE_DMA != 1
if (!i2c_led_q_running) {
# endif
i2c_led_send_CRWL(drvid);
i2c_led_select_page(drvid, 0);
# if I2C_LED_USE_DMA != 1
}
# endif
*issidrv[drvid].onoff = 0; i2c1_transmit(issidrv[drvid].addr, issidrv[drvid].onoff, ISSI3733_PG0_BYTES, 0);
}
void i2c_led_send_mode_op_gcr(uint8_t drvid, uint8_t mode, uint8_t operation) {
uint8_t i2cdata[] = {ISSI3733_CR, mode | operation, gcr_actual};
i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0);
}
void i2c_led_send_pur_pdr(uint8_t drvid, uint8_t pur, uint8_t pdr) {
uint8_t i2cdata[] = {ISSI3733_SWYR_PUR, pur, pdr};
i2c1_transmit(issidrv[drvid].addr, i2cdata, sizeof(i2cdata), 0);
}
void i2c_led_send_pwm(uint8_t drvid) {
# if I2C_LED_USE_DMA != 1
if (!i2c_led_q_running) {
# endif
i2c_led_send_CRWL(drvid);
i2c_led_select_page(drvid, 0);
# if I2C_LED_USE_DMA != 1
}
# endif
*issidrv[drvid].pwm = 0; i2c1_transmit(issidrv[drvid].addr, issidrv[drvid].pwm, ISSI3733_PG1_BYTES, 0);
}
uint8_t I2C3733_Init_Control(void) {
DBGC(DC_I2C3733_INIT_CONTROL_BEGIN);
I2C3733_Control_Set(0);
wait_ms(1);
sr_exp_data.bit.IRST = 0;
SR_EXP_WriteData();
wait_ms(1);
DBGC(DC_I2C3733_INIT_CONTROL_COMPLETE);
return 1;
}
uint8_t I2C3733_Init_Drivers(void) {
DBGC(DC_I2C3733_INIT_DRIVERS_BEGIN);
gcr_actual = ISSI3733_GCR_DEFAULT;
gcr_actual_last = gcr_actual;
if (gcr_actual > LED_GCR_MAX) gcr_actual = LED_GCR_MAX;
gcr_desired = gcr_actual;
i2c_led_send_CRWL(0);
i2c_led_select_page(0, 3);
i2c_led_send_mode_op_gcr(0, 0, ISSI3733_CR_SSD_NORMAL);
i2c_led_send_CRWL(1);
i2c_led_select_page(1, 3);
i2c_led_send_mode_op_gcr(1, 0, ISSI3733_CR_SSD_NORMAL);
i2c_led_send_CRWL(0);
i2c_led_select_page(0, 3);
i2c_led_send_pur_pdr(0, ISSI3733_SWYR_PUR_8000, ISSI3733_CSXR_PDR_8000);
i2c_led_send_CRWL(1);
i2c_led_select_page(1, 3);
i2c_led_send_pur_pdr(1, ISSI3733_SWYR_PUR_8000, ISSI3733_CSXR_PDR_8000);
DBGC(DC_I2C3733_INIT_DRIVERS_COMPLETE);
return 1;
}
void I2C_DMAC_LED_Init(void) {
Dmac *dmac = DMAC;
DBGC(DC_I2C_DMAC_LED_INIT_BEGIN);
dmac->CTRL.bit.DMAENABLE = 0; while (dmac->CTRL.bit.DMAENABLE) {
} dmac->CTRL.bit.SWRST = 1; while (dmac->CTRL.bit.SWRST) {
}
dmac->BASEADDR.reg = (uint32_t)&dmac_desc; dmac->WRBADDR.reg = (uint32_t)&dmac_desc_wb; dmac->CTRL.reg |= 0x0f00;
dmac->Channel[0].CHCTRLA.bit.ENABLE = 0; while (dmac->Channel[0].CHCTRLA.bit.ENABLE) {
} dmac->Channel[0].CHCTRLA.bit.SWRST = 1; while (dmac->Channel[0].CHCTRLA.bit.SWRST) {
}
dmac->Channel[0].CHCTRLA.bit.THRESHOLD = 0; dmac->Channel[0].CHCTRLA.bit.BURSTLEN = 0; dmac->Channel[0].CHCTRLA.bit.TRIGACT = 2; dmac->Channel[0].CHCTRLA.bit.TRIGSRC = SERCOM1_DMAC_ID_TX; dmac->Channel[0].CHCTRLA.bit.RUNSTDBY = 1;
NVIC_EnableIRQ(DMAC_0_IRQn);
dmac->Channel[0].CHINTENSET.bit.TCMPL = 1;
dmac->Channel[0].CHINTENSET.bit.TERR = 1;
dmac->CTRL.bit.DMAENABLE = 1; while (dmac->CTRL.bit.DMAENABLE == 0) {
}
DBGC(DC_I2C_DMAC_LED_INIT_COMPLETE);
}
void I2C3733_Control_Set(uint8_t state) {
DBGC(DC_I2C3733_CONTROL_SET_BEGIN);
sr_exp_data.bit.SDB_N = (state == 1 ? 1 : 0);
SR_EXP_WriteData();
DBGC(DC_I2C3733_CONTROL_SET_COMPLETE);
}
void i2c_led_desc_defaults(void) {
dmac_desc.BTCTRL.bit.STEPSIZE = 0; dmac_desc.BTCTRL.bit.STEPSEL = 0; dmac_desc.BTCTRL.bit.DSTINC = 0; dmac_desc.BTCTRL.bit.SRCINC = 1; dmac_desc.BTCTRL.bit.BEATSIZE = 0; dmac_desc.BTCTRL.bit.BLOCKACT = 0; dmac_desc.BTCTRL.bit.EVOSEL = 0; dmac_desc.BTCTRL.bit.VALID = 1; }
void i2c_led_prepare_send_dma(uint8_t *data, uint8_t len) {
i2c_led_desc_defaults();
dmac_desc.BTCNT.reg = len;
dmac_desc.SRCADDR.reg = (uint32_t)data + len;
dmac_desc.DSTADDR.reg = (uint32_t)&SERCOM1->I2CM.DATA.reg;
dmac_desc.DESCADDR.reg = 0;
}
void i2c_led_begin_dma(uint8_t drvid) {
DMAC->Channel[0].CHCTRLA.bit.ENABLE = 1;
SERCOM1->I2CM.ADDR.reg = (dmac_desc.BTCNT.reg << 16) | 0x2000 | issidrv[drvid].addr; }
void i2c_led_send_CRWL_dma(uint8_t drvid) {
*(dma_sendbuf + 0) = ISSI3733_CMDRWL;
*(dma_sendbuf + 1) = ISSI3733_CMDRWL_WRITE_ENABLE_ONCE;
i2c_led_prepare_send_dma(dma_sendbuf, 2);
i2c_led_begin_dma(drvid);
}
void i2c_led_select_page_dma(uint8_t drvid, uint8_t pageno) {
*(dma_sendbuf + 0) = ISSI3733_CMDR;
*(dma_sendbuf + 1) = pageno;
i2c_led_prepare_send_dma(dma_sendbuf, 2);
i2c_led_begin_dma(drvid);
}
void i2c_led_send_GCR_dma(uint8_t drvid) {
*(dma_sendbuf + 0) = ISSI3733_GCCR;
*(dma_sendbuf + 1) = gcr_actual;
i2c_led_prepare_send_dma(dma_sendbuf, 2);
i2c_led_begin_dma(drvid);
}
void i2c_led_send_pwm_dma(uint8_t drvid) {
memcpy(dma_sendbuf, issidrv[drvid].pwm, ISSI3733_PG1_BYTES);
*dma_sendbuf = 0; i2c_led_prepare_send_dma(dma_sendbuf, ISSI3733_PG1_BYTES);
i2c_led_begin_dma(drvid);
}
void i2c_led_send_onoff_dma(uint8_t drvid) {
memcpy(dma_sendbuf, issidrv[drvid].onoff, ISSI3733_PG0_BYTES);
*dma_sendbuf = 0; i2c_led_prepare_send_dma(dma_sendbuf, ISSI3733_PG0_BYTES);
i2c_led_begin_dma(drvid);
}
void i2c_led_q_init(void) {
memset(i2c_led_q, 0, I2C_Q_SIZE);
i2c_led_q_s = 0;
i2c_led_q_e = 0;
i2c_led_q_running = 0;
i2c_led_q_full = 0;
}
uint8_t i2c_led_q_isempty(void) { return i2c_led_q_s == i2c_led_q_e; }
uint8_t i2c_led_q_size(void) { return (i2c_led_q_e - i2c_led_q_s) % I2C_Q_SIZE; }
uint8_t i2c_led_q_available(void) {
return I2C_Q_SIZE - i2c_led_q_size() - 1; }
void i2c_led_q_add(uint8_t cmd) {
i2c_led_q[i2c_led_q_e] = cmd;
i2c_led_q_e = (i2c_led_q_e + 1) % I2C_Q_SIZE; }
void i2c_led_q_s_advance(void) {
i2c_led_q_s = (i2c_led_q_s + 1) % I2C_Q_SIZE; }
uint8_t i2c_led_q_request_room(uint8_t request_size) {
if (request_size > i2c_led_q_available()) {
i2c_led_q_full++;
if (i2c_led_q_full >= 100) {
DBG_LED_ON;
I2C_DMAC_LED_Init();
i2c_led_q_init();
return 1;
}
return 0;
}
i2c_led_q_full = 0;
return 1;
}
uint8_t i2c_led_q_run(void) {
if (i2c_led_q_isempty()) {
i2c_led_q_running = 0;
return 0;
}
if (i2c_led_q_running) return 1;
i2c_led_q_running = 1;
# if I2C_LED_USE_DMA != 1
while (!i2c_led_q_isempty()) {
# endif
if (i2c_led_q[i2c_led_q_s] == I2C_Q_CRWL) {
i2c_led_q_s_advance();
uint8_t drvid = i2c_led_q[i2c_led_q_s];
# if I2C_LED_USE_DMA == 1
i2c_led_send_CRWL_dma(drvid);
# else
i2c_led_send_CRWL(drvid);
# endif
} else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PAGE_SELECT) {
i2c_led_q_s_advance();
uint8_t drvid = i2c_led_q[i2c_led_q_s];
i2c_led_q_s_advance();
uint8_t page = i2c_led_q[i2c_led_q_s];
# if I2C_LED_USE_DMA == 1
i2c_led_select_page_dma(drvid, page);
# else
i2c_led_select_page(drvid, page);
# endif
} else if (i2c_led_q[i2c_led_q_s] == I2C_Q_PWM) {
i2c_led_q_s_advance();
uint8_t drvid = i2c_led_q[i2c_led_q_s];
# if I2C_LED_USE_DMA == 1
i2c_led_send_pwm_dma(drvid);
# else
i2c_led_send_pwm(drvid);
# endif
} else if (i2c_led_q[i2c_led_q_s] == I2C_Q_GCR) {
i2c_led_q_s_advance();
uint8_t drvid = i2c_led_q[i2c_led_q_s];
# if I2C_LED_USE_DMA == 1
i2c_led_send_GCR_dma(drvid);
# else
i2c_led_send_GCR(drvid);
# endif
} else if (i2c_led_q[i2c_led_q_s] == I2C_Q_ONOFF) {
i2c_led_q_s_advance();
uint8_t drvid = i2c_led_q[i2c_led_q_s];
# if I2C_LED_USE_DMA == 1
i2c_led_send_onoff_dma(drvid);
# else
i2c_led_send_onoff(drvid);
# endif
}
i2c_led_q_s_advance();
# if I2C_LED_USE_DMA != 1
}
i2c_led_q_running = 0;
# endif
return 1;
}
#endif