#include "platform.h"
#include "hal_headfile.h"
#include "board.h"
#include "debug.h"
#include "audio.h"
#include "config.h"
#include "power.h"

__ALIGNED(4) denoise_param_def   denoise_param;
__ALIGNED(4) AEC_param_def       aec_param;
__ALIGNED(4) agc_param_def       agc_param;
__ALIGNED(4) eq_param_def        eq_param;
__ALIGNED(4) drc_param_def       drc_param;
__ALIGNED(4) user_function_def   user_param;
__ALIGNED(4) set_param_def       set_param;



void param_update(uint8_t addr, uint8_t value);


void get_param_from_ext(void)
{
    denoise_param = *(denoise_param_def*)DENOISE_CFG_ADDR;
    aec_param = *(AEC_param_def*)AEC_CFG_ADDR;
	agc_param = *(agc_param_def *)AGC_CFG_ADDR;
    eq_param = *(eq_param_def*)EQ_CFG_ADDR;
    drc_param = *(drc_param_def*)DRC_CFG_ADDR;
    user_param = *(user_function_def*)HARD_WARE_CFG_ADDR;
}


void i2c_callback(uint8_t *buff, uint32_t length, i2c_event_t event);
/**
 * this function will init slave iic
 *
 * @param none:
 *
 * @return: none.
 */
void i2c_init(void)
{
	if(user_param.codec_bus == IIC_BUS)
	{
		hal_i2c_slave_init(I2C_0, I2C0_SLAVE_ADDRESS);
		iic_set_callback(i2c_callback);
	}
}

/**
 * Set IIC interrupt callback function
 *
 * @param none:
 *
 * @return: none.
 */
void i2c_callback(uint8_t *buff, uint32_t length, i2c_event_t event)
{
    uint8_t temp;

    if (I2C_READ == event) /*iic read*/
    {
        temp = buff[0];

        if (buff[0] == DENOISE_EN_ADDR) /*降噪功能查询*/
        {
            buff[0] = aec_param.enable;
        }
        else if (buff[0]  == DENOISE_LEVEL_ADDR) /*降噪等级查询*/
        {
            buff[0] =  aec_param.level;
        }
        else if (buff[0]  == DENOISE_MIC_GAIN_ADDR) /*MIC增益查询*/
        {
            buff[0] = aec_param.mian_mic_option;
        }
        else if(buff[0] == DENOISE_MIC0_GAIN_ADDR)
        {
        	uint32_t temp;
        	get_mic_gain(MIC_0, &temp);
        	buff[0] = (uint8_t)temp;
        }
        else if(buff[0] == DENOISE_MIC1_GAIN_ADDR)
        {
        	uint32_t temp;
        	get_mic_gain(MIC_1, &temp);
        	buff[0] = (uint8_t)temp;
        }
        else if (buff[0]  == DENOISE_MIC_CHOOSE_ADDR) /*MIC选择查询*/
        {
            buff[0] = aec_param.mian_mic_option;
        }
        else if (buff[0]  == DENOISE_HP_VOLUME_ADDR) /*HP输出音量查询*/
        {
            buff[0] = aec_param.hp_volume;
        }
        else if (buff[0]  == DENOISE_HP_OUT_CHOOSE_ADDR) /*HP输出方式查询*/
        {
            buff[0] = aec_param.output_option;
        }
        else if (buff[0]  == DAC_EQ_TYPE_ADDR) /*EQ配置*/
        {

        }

        debug("iic_slave read add:%x,va:%x", temp, buff[0]);
    }
    else if (I2C_WRITE == event)
    {
        debug("iic_slave write add:%x,va:%x\r\n", buff[0], buff[1]);
        set_param.addr = buff[0];
        set_param.value = buff[1];
        param_update(set_param.addr, set_param.value);
    }
}

void param_update(uint8_t addr, uint8_t value)
{
    if (addr == DENOISE_EN_ADDR) /*降噪使能设置*/
    {
    	aec_param.enable = value;
    }
    else if (addr == DENOISE_LEVEL_ADDR) /*设置降噪等级，重启生效*/
    {
    	aec_param.level = value;
    }
    else if (addr == DENOISE_MIC_GAIN_ADDR) /*MIC增益配置*/
    {
    	aec_param.mian_mic_option = value;
        set_mic_gain(MIC_0, value);
        set_mic_gain(MIC_1, value);
    }
    else if(addr == DENOISE_MIC0_GAIN_ADDR)
	{
		set_mic_gain(MIC_0, value);
	}
	else if(addr == DENOISE_MIC1_GAIN_ADDR)
	{
		set_mic_gain(MIC_1, value);
	}
    else if (addr == DENOISE_MIC_CHOOSE_ADDR) /*MIC选择配置*/
    {
    	aec_param.mian_mic_option = value;
    }
    else if (addr == DENOISE_HP_VOLUME_ADDR) /*输出音量配置*/
    {
    	aec_param.hp_volume = value;
        codec_dac_set_volume(value);
    }
    else if (addr == DENOISE_HP_OUT_CHOOSE_ADDR) /*输出方式选择*/
    {
    	aec_param.output_option = value;
    }
    else if (addr == DAC_EQ_TYPE_ADDR) /*EQ选择*/
    {
        eq_param.eq_type = value;
        codec_dac_set_eq_type(value);
    }
    else if (addr  == DENOISE_ENTER_WFI) /*系统进入WFI*/
    {
        Flag_system_wfi = 1;
    }
    else if (addr  == DENOISE_ENTER_HIBERNATE) /*系统进入HIBERNATE*/
    {
        Flag_system_hib = 1 ;
    }
    else if (addr  == SYSTEM_RESET) /*系统重启*/
    {
        system_restart();
    }
}

void system_restart(void)
{
    DEV_RMU->BOOT_f.RE_BOOT = 1;

    while (1)
    {
        ;/* code */
    }
}

