// DAC业务驱动(I2C接口)
// 杭州秒寻科技有限公司
// 周涛
// 2023-09-30

#include "includes.h"

uint8 driverapp_dac_init()
{
    uint8 byFlag = 0;

    byFlag = driverapp_dac_init_reg(); // 初始化默认值
    if (!byFlag)
    {
        return 0;
    }

    return 1;
}

// 写数据
uint8 driverapp_dac_write_data(uint8 byRegAddress, uint8 *pbyData, uint16 wLen)
{
    uint8 byFlag = 0;

    byFlag = driver_dacgpio_write_data(DAC_SLAVE_ADDRESS, byRegAddress, pbyData, wLen);
    if (!byFlag)
    {
        return 0;
    }

    return 1;
}

// 读数据
uint8 driverapp_dac_read_data(uint8 byRegAddress, uint8 *pbyData, uint16 wLen)
{
    uint8 byFlag = 0;

    byFlag = driver_dacgpio_read_data(DAC_SLAVE_ADDRESS, byRegAddress, pbyData, wLen);
    if (!byFlag)
    {
        return 0;
    }

    return 1;
}

// 初始化寄存器
uint8 driverapp_dac_init_reg()
{
    uint8 byInData = 0;
    uint8 byOutData = 0;

    // 16号寄存器
    byInData = 0xC1; // 单倍采样
    driverapp_dac_write_data(DAC_REG_ADDRESS_16, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_16, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 17号寄存器，省电模式、I2S格式
    byInData = 0x00; // I2S格式
    driverapp_dac_write_data(DAC_REG_ADDRESS_17, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_17, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 18号寄存器，DAC操作模式、数字滤波滚降
    byInData = 0x00; // 数字滚降
    driverapp_dac_write_data(DAC_REG_ADDRESS_18, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_18, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 19号寄存器
    byInData = 0x00; // 左右通道正常输出，都不翻转
    driverapp_dac_write_data(DAC_REG_ADDRESS_19, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_19, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 20号寄存器
    byInData = 0x03; // 左右通道静音
    driverapp_dac_write_data(DAC_REG_ADDRESS_20, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_20, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 22号寄存器
    byInData = 0x10; // 去加重(48KHz)
    driverapp_dac_write_data(DAC_REG_ADDRESS_22, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_22, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 24号寄存器
    byInData = 0x80; // 左通道衰减0db
    driverapp_dac_write_data(DAC_REG_ADDRESS_24, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_24, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    // 25号寄存器
    byInData = 0x80; // 右通道衰减0db
    driverapp_dac_write_data(DAC_REG_ADDRESS_25, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_25, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    return 1;
}

// DAC初始化配置
uint8 driverapp_dac_init_config(uint8 bySwitch, uint8 byGain)
{
    uint8 byFlag = 0;

    // 关闭DAC
    if (0 == bySwitch)
    {
        byFlag = driverapp_dac_turn_off(1); // 左通道
        if (!byFlag)
        {
            return 0;
        }
    }
    // 打开DAC
    else
    {
        byFlag = driverapp_dac_turn_on(1); // 左通道
        if (!byFlag)
        {
            return 0;
        }
    }

    // 配置增益
    byFlag = driverapp_dac_set_attenuation(1, byGain); // 左通道
    if (!byFlag)
    {
        return 0;
    }

    return 1;
}

// 关闭DAC
// byChannel:通道编号，1：左，2：右
uint8 driverapp_dac_turn_off(uint8 byChannel)
{
    uint8 byFlag = 0;
    uint8 byInData = 0;
    uint8 byOutData = 0;

    if (1 == byChannel)
    {
        byInData = 0x01; // 左通道静音
    }
    else if (2 == byChannel)
    {
        byInData = 0x01 << 1; // 右通道静音
    }
    else
    {
        return 0;
    }

    // 打开硬件输出静音功能
    driver_dacgpio_amute(0);

    // 读出寄存器原值
    byFlag = driverapp_dac_read_data(DAC_REG_ADDRESS_20, &byOutData, 1);
    if (!byFlag)
    {
        return 0;
    }
    byInData = byInData | byOutData;

    driverapp_dac_write_data(DAC_REG_ADDRESS_20, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_20, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    return 1;
}

// 打开DAC
// byChannel:通道编号，1：左，2：右
uint8 driverapp_dac_turn_on(uint8 byChannel)
{
    uint8 byFlag = 0;
    uint8 byInData = 0;
    uint8 byOutData = 0;

    if (1 == byChannel)
    {
        byInData = ~0x01; // 左通道关闭静音
    }
    else if (2 == byChannel)
    {
        byInData = ~(0x01 << 1); // 右通道关闭静音
    }
    else
    {
        return 0;
    }

    // 关闭输出静音功能
    driver_dacgpio_amute(1);

    // 读出寄存器原值
    byFlag = driverapp_dac_read_data(DAC_REG_ADDRESS_20, &byOutData, 1);
    if (!byFlag)
    {
        return 0;
    }
    byInData = byInData & byOutData;

    driverapp_dac_write_data(DAC_REG_ADDRESS_20, &byInData, 1);
    driverapp_dac_read_data(DAC_REG_ADDRESS_20, &byOutData, 1);
    if (byInData != byOutData)
    {
        return 0;
    }

    return 1;
}

// 调节增益
// byChannel:通道，1：左，2：右
// byAttenuation:衰减值，255-129，255表示0衰减，254表示0.5db衰减，步进0.5db
uint8 driverapp_dac_set_attenuation(uint8 byChannel, uint8 byAttenuation)
{
    uint8 byOutData = 0;

    if (byChannel == 1)
    {
        driverapp_dac_write_data(DAC_REG_ADDRESS_24, &byAttenuation, 1); // 左声道
        driverapp_dac_read_data(DAC_REG_ADDRESS_24, &byOutData, 1);      // 左声道
        if (byAttenuation != byOutData)
        {
            return 0;
        }
    }
    else if (byChannel == 2)
    {
        driverapp_dac_write_data(DAC_REG_ADDRESS_25, &byAttenuation, 1); // 右声道
        driverapp_dac_read_data(DAC_REG_ADDRESS_24, &byOutData, 1);      // 右声道
        if (byAttenuation != byOutData)
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }

    return 1;
}