/*
 * Copyright (c) 2020 Great Master Wu. All rights reserved.
 * Author    Date          ChangeLog
 * Master    2020-08-09    Initialize version
 * This file is used for user, add yourself sensor at here
 */

#include "sensor_register.h"
#include "spi.h"
#include "twim.h"

inv_icm4x6xx_t icm42686_device;
sensor_serial_if_t icm4x6xx_driver;

da7280_dev_t hatic_device;
sensor_serial_if_t da7280_driver;

sgm4151x_dev_t sgm4151x_device;
sensor_serial_if_t sgm4151x_driver;

aw2013_dev_t aw2013_device;
sensor_serial_if_t aw2013_driver;

tiny1616_dev_t tiny1616_device;
sensor_serial_if_t tiny1616_driver;

#pragma diag_suppress 1441


static void icm42686_refgister(sensor_serial_if_t *serif)
{
    /* Initialize serial inteface between MCU and Icm426xx */
    serif->context   = NULL;  /* no need */
    serif->read_reg  = sensor_serial_read_reg;
    serif->write_reg = sensor_serial_write_reg;
    serif->max_read  = 1024*32*32;  /* maximum number of bytes allowed per serial read */
    serif->max_write = 1024*32*32;  /* maximum number of bytes allowed per serial write */
#if 1
    serif->serif_type = SERIAL_SPI4;
    (const nrf_drv_spi_t*)serif->instance = &icm_spi_instance;
#else
    serif->serif_type = SERIAL_I2C;
    serif->dev_addr  = 0x69;
#endif

    (const nrf_drv_twi_t*)serif->instance = &cs_twi_instance;
}


inv_icm4x6xx_t *icm42686_init(void)
{
    icm42686_refgister(&icm4x6xx_driver);

    icm42686_device.name = "icm42688";
    icm42686_device.driver = &icm4x6xx_driver;
    icm42686_device.fifo_mode_en = FIFO_MODE;
    icm42686_device.interface = ICM_SPI;
    icm42686_device.int_cfg = INT1_PLUSED_MODE | INT1_POLARITY_ACTIVE_HIGH;
    icm42686_device.fifo_mode_en = FIFO_MODE;

    return &icm42686_device;
}


static void sgm4151x_register(sensor_serial_if_t *serif)
{
    serif->context    = NULL;
    serif->read_reg   = sensor_serial_read_reg;
    serif->write_reg  = sensor_serial_write_reg;
    serif->serif_type = SERIAL_I2C;
    serif->dev_addr   = SGM4151X_7BIT_ADDR;
    (const nrf_drv_twi_t*)serif->instance = &cs_twi_instance;
}

sgm4151x_dev_t *sgm4151x_init(void)
{
    sgm4151x_register(&sgm4151x_driver);
    sgm4151x_device.driver = &sgm4151x_driver;

    sgm4151x_device.name = "pmic";
    //reg00
    sgm4151x_device.iindpm = IINDPM_700MA;
    // reg01
    sgm4151x_device.sys_min_vol = SYS_MIN_VOLTAGE_3_5V;
    // reg02
    sgm4151x_device.fast_charge_current = FAST_CHARGE_MA_TO_REG_VALUE(500); // max 525 is spec
    // reg03
    sgm4151x_device.iprechg = IPRECHG_MA_TO_REG_VALUE(200);
    // look in bat spec, may be the value is 2.1ms, but the ic min is 60
    sgm4151x_device.iterm = ITERM_MA_TO_REG_VALUE(60);  
    
    // REG04
    sgm4151x_device.vreg = VREF_MV_TO_REG_VALUE(4208);

    sgm4151x_device_init(&sgm4151x_device);

    return &sgm4151x_device; 
}


static void da7280_register(sensor_serial_if_t *serif)
{
    serif->context    = NULL;
    serif->read_reg   = sensor_serial_read_reg;
    serif->write_reg  = sensor_serial_write_reg;
    serif->serif_type = SERIAL_I2C;
    serif->dev_addr   = DA7280_7BIT_ADDR;
    (const nrf_drv_twi_t*)serif->instance = &haptic_twi_instance;
}

da7280_dev_t *da7280_init(void)
{
    da7280_register(&da7280_driver);
    hatic_device.driver = &da7280_driver;
    hatic_device.name = "haptic";
    
    return &hatic_device;
}


static void aw2013_register(sensor_serial_if_t *serif)
{
    serif->context    = NULL;
    serif->read_reg   = sensor_serial_read_reg;
    serif->write_reg  = sensor_serial_write_reg;
    serif->serif_type = SERIAL_I2C;
    serif->dev_addr   = AW2013_DEV_7BIT_ADDR;
    (const nrf_drv_twi_t*)serif->instance = &cs_twi_instance;
}


aw2013_dev_t *aw2013_init(void)
{
    aw2013_register(&aw2013_driver);
    aw2013_device.driver = &aw2013_driver;
    aw2013_device.name = "aw2013";
    
    aw2013_init_default(&aw2013_device);
    
    return &aw2013_device;
}


static void tiny1616_register(sensor_serial_if_t *serif)
{
    serif->context    = NULL;
    serif->read_reg   = sensor_serial_read_reg;
    serif->write_reg  = sensor_serial_write_reg;
    serif->serif_type = SERIAL_I2C;
    serif->dev_addr   = 0x12;
    (const nrf_drv_twi_t*)serif->instance = &cs_twi_instance;
}


tiny1616_dev_t *tiny1616_init(void)
{
    tiny1616_register(&tiny1616_driver);
    tiny1616_device.driver = &tiny1616_driver;
    tiny1616_device.name = "tiny1616";

    return &tiny1616_device;
}

