/*
 * Created on: Jul 27, 2024
 */

#ifndef USER_SRC_31FL3729_H_
#define USER_SRC_31FL3729_H_



#include <ch32v00x.h>
#include <stdint.h>



#define FL3729_SDB_PORT         GPIOC
#define FL3729_SDB_PIN          GPIO_Pin_3

#define FL3729_BASE_ADDR        0x68

#define FL3729_ADPIN_GND        0
#define FL3729_ADPIN_SCL        1
#define FL3729_ADPIN_SDA        2
#define FL3729_ADPIN_VCC        3

#define FL3729_REG_OUTPUT       0x01
#define FL3729_REG_SCALING      0x90
#define FL3729_REG_CONFIG       0xA0
#define FL3729_REG_G_CURRENT    0xA1        // global current
#define FL3729_REG_PUPD         0xB0        // pull up / pull down
#define FL3729_REG_SS           0xB1        // spread spectrum
#define FL3729_REG_PWM          0xB2
#define FL3729_REG_RESET        0xCF


#define FL3729_GCC_MAX          64          // maximum global current level

#define FL3729_CONF_SSD_SHDN    (0 << 0)    // software shutdown
#define FL3729_CONF_SSD_NRML    (1 << 0)    // normal operation

#define FL3729_CONF_OSDE_OFF    (0 << 1)
#define FL3729_CONF_OSDE_OPEN   (1 << 1)
#define FL3729_CONF_OSDE_SHORT  (2 << 1)

#define FL3729_CONF_LOGIC_LOLEV (0 << 3)
#define FL3729_CONF_LOGIC_HILEV (1 << 3)

#define FL3729_CONF_MATRIX_9x15 0x00
#define FL3729_CONF_MATRIX_8x16 0x10
#define FL3729_CONF_MATRIX_7x16 0x20
#define FL3729_CONF_MATRIX_6x16 0x30
#define FL3729_CONF_MATRIX_5x16 0x40
#define FL3729_CONF_MATRIX_4x16 0x50
#define FL3729_CONF_MATRIX_3x16 0x60
#define FL3729_CONF_MATRIX_2x16 0x70
#define FL3729_CONF_MATRIX_1x16 0x80    // note: all of SW1-SW9 are ALWAYS ON in this mode

#define FL3729_SS_CLT_1980us    0
#define FL3729_SS_CLT_1200us    (1 << 0)
#define FL3729_SS_CLT_820us     (2 << 0)
#define FL3729_SS_CLT_660us     (3 << 0)

#define FL3729_SS_RNG_5pct      0
#define FL3729_SS_RNG_15pct     (1 << 2)
#define FL3729_SS_RNG_24pct     (2 << 2)
#define FL3729_SS_RNG_34pct     (3 << 2)

#define FL3729_RESET_VALUE      0xAE



void is31fl3729_init(uint8_t i2c_addr, uint8_t config, uint8_t global_current);

uint8_t is31fl3729_get_addr(uint8_t adpin);

void is31fl3729_set_global_current(uint8_t i2c_addr, uint8_t global_current);
void is31fl3729_set_scaling_current(uint8_t i2c_addr, uint8_t cs, uint8_t current);
void is31fl3729_set_scaling_current_multi(uint8_t i2c_addr, const uint8_t *current, uint8_t count);

void is31fl3729_set_outputs(uint8_t i2c_addr, uint8_t sw, const uint8_t *out, uint8_t len);



#endif /* USER_SRC_31FL3729_H_ */
