#ifndef _HAL_AUDIO_DAC_H_
#define _HAL_AUDIO_DAC_H_
#include "hal_audio.h"
#include "hal_dma.h"
typedef enum
{
    R_IIR0 = (1 << 24),
    R_IIR1 = (1 << 25),
    R_IIR2 = (1 << 26),
    R_IIR3 = (1 << 27),
    R_IIR4 = (1 << 28),
    R_IIR5 = (1 << 29),
    R_IIR6 = (1 << 30),
    R_IIR7 = (1 << 31),
} chr_iir_sel_typedef;

typedef enum
{
    L_IIR0 = (1 << 16),
    L_IIR1 = (1 << 17),
    L_IIR2 = (1 << 18),
    L_IIR3 = (1 << 19),
    L_IIR4 = (1 << 20),
    L_IIR5 = (1 << 21),
    L_IIR6 = (1 << 22),
    L_IIR7 = (1 << 23),
} chr_iil_sel_typedef;


typedef enum
{
    VOL_MUTE = 0UL,
    VOL_N40DB,
    VOL_N39DB,
    VOL_N38DB,
    VOL_N37DB,
    VOL_N36DB,
    VOL_N35DB,
    VOL_N34DB,
    VOL_N33DB,
    VOL_N32DB,
    VOL_N31DB,
    VOL_N30DB,
    VOL_N29DB,
    VOL_N28DB,
    VOL_N27DB,
    VOL_N26DB,
    VOL_N25DB,
    VOL_N24DB,
    VOL_N23DB,
    VOL_N22DB,
    VOL_N21DB,
    VOL_N20DB,
    VOL_N19DB,
    VOL_N18DB,
    VOL_N17DB,
    VOL_N16DB,
    VOL_N15DB,
    VOL_N14DB,
    VOL_N13DB,
    VOL_N12DB,
    VOL_N11DB,
    VOL_N10DB,
    VOL_N09DB,
    VOL_N08DB,
    VOL_N07DB,
    VOL_N06DB,
    VOL_N05DB,
    VOL_N04DB,
    VOL_N03DB,
    VOL_N02DB,
    VOL_N01DB,
    VOL_N00DB,
    VOL_P01DB,
    VOL_P02DB,
    VOL_P03DB,
    VOL_P04DB,
    VOL_P05DB,
    VOL_P06DB,
    VOL_P07DB,
    VOL_P08DB,
    VOL_P09DB,
    VOL_P10DB,
    VOL_P11DB,
    VOL_P12DB,
    VOL_P13DB,
    VOL_P14DB,
    VOL_P15DB,
    VOL_P16DB,
    VOL_P17DB,
    VOL_P18DB,
    VOL_P19DB,
    VOL_P20DB,
    VOL_P21DB,
    VOL_P22DB,
    VOL_NUM,
} dac_vol_sel_typedef;

typedef enum
{
    MIX_GAIN_1_1X,
    MIX_GAIN_1_2X,
    MIX_GAIN_1_4X,
} dac_mix_gain_typedef;



typedef enum
{
    RIGHT_MIXER_MIX_DAC0_LEFT_CHANNEL  = (1 << 0),
    RIGHT_MIXER_MIX_DAC0_RIGHT_CHANNEL = (1 << 1),
    RIGHT_MIXER_MIX_ADC1_DATA          = (1 << 2),
    RIGHT_MIXER_MIX_ADC3_DATA          = (1 << 3),
} dac_right_mixer_source_typedef;

typedef enum
{
    LEFT_MIXER_MIX_DAC0_LEFT_CHANNEL  = (1 << 0),
    LEFT_MIXER_MIX_DAC0_RIGHT_CHANNEL = (1 << 1),
    LEFT_MIXER_MIX_ADC0_DATA          = (1 << 2),
    LEFT_MIXER_MIX_ADC2_DATA          = (1 << 3),
} dac_left_mixer_source_typedef;



typedef struct _dac_iir_typedef
{
    uint32_t   _nb0;
    uint32_t   _nb1;
    uint32_t   _nb2;
    uint32_t   _na1;
    uint32_t   _na2;
    uint32_t   _7b0;
    uint32_t   _7b1;
    uint32_t   _7b2;
    uint32_t   _7a1;
    uint32_t   _7a2;
} dac_iir_typedef;

typedef struct _dac_drc_typedef
{
    uint32_t   lmth;
    uint32_t   slop;
    uint32_t   peak_at;
    uint32_t   peak_rt;
    uint32_t   gain_at;
    uint32_t   gain_rt;
} dac_drc_typedef;

typedef enum
{
    DAC_MONO   = 0x01UL,
    DAC_STEREO = 0x02UL,
} adc_track_typedef;

typedef enum
{
    DAC_SAMPLE_RATE_8K = 8000UL,
    DAC_SAMPLE_RATE_16K = 16000UL,
    DAC_SAMPLE_RATE_24K = 24000UL,
    DAC_SAMPLE_RATE_32K = 32000UL,
    DAC_SAMPLE_RATE_48K = 48000UL,
    DAC_SAMPLE_RATE_96K = 96000UL,
    DAC_SAMPLE_RATE_192K = 192000UL,
} dac_sample_rate_typedef;

typedef enum
{
    NONE_OUT  = 0x00UL,
    LINE_OUT  = 0x01UL,
    HEADPHONE = 0x02UL,
    LINE_OUT_HEADPHONE = 0x03UL,
} dac_outfrom_typedef;

typedef enum
{
    SLOT_32BIT = 0UL,
    SLOT_16BIT = 1UL,
} dac_slot_width_typedef;


typedef enum
{
    DAC_NONE,
    DAC_HPL,
	DAC_HPR,
	DAC_ALL,
} dac_channels_typedef;


typedef enum
{
    IIR_CH0 = 0UL,
    IIR_CH1,
    IIR_CH2,
    IIR_CH3,
    IIR_CH4,
    IIR_CH5,
    IIR_CH6,
    IIR_CH7,
} dac_ch_typedef;


typedef enum
{
    MIX_SOURCE_DAC_DETACHED = (1 << 0),/*DAC通道分离*/
    MIX_SOURCE_ADC_emulated = (1 << 1),/*使用模拟方式*/
    MIX_SOURCE_DAC_MERGED = (1 << 2),/*DAC通道合并*/
	MIX_SOURCE_ADC_digital = (1 << 3),/*使用数字方式*/

} dac_mix_sorce_def;

typedef enum
{
    LOW_BIT_USABLE = 0UL,
    HIGHT_BIT_USABLE = 1UL,
} usable_bit_pos_typedef;

typedef enum
{
    width_16bit_useful_16bit        = 0UL,
    width_32bit_useful_low_16bit    = 1UL,
    width_32bit_useful_high_16bit   = 2UL,
    width_32bit_useful_low_24bit    = 3UL,
    width_32bit_useful_high_24bit   = 4UL,
} dac_data_format_typedef;

typedef enum
{
    EQ_NORMAL_TYPE          = 0x00,      /**< normal mode. */
    EQ_POP_TYPE             = 0x01,      /**< pop mode. */
    EQ_ROCK_TYPE            = 0x02,      /**< rock mode. */
    EQ_JAZZ_TYPE            = 0x03,      /**< jazz mode. */
    EQ_CLASSICAL_TYPE       = 0x04,      /**< classical mode. */
    EQ_COUNTRY_TYPE         = 0x05,      /**< country mode. */
    EQ_SELT_TPYE            = 0x06,      /**< country mode. */
    EQ_TYPE_MAX             = 0x07,      /**< max mode. */
} eq_param_class_type;

typedef enum
{
    DAC_OUTPUT_NORMAL       = 0x00,      /**< normal mode. */
    DAC_OUTPUT_INVERSE      = 0x01,      /**< inverse mode. */
} dac_param_inverse_type ;

typedef struct  dac_init_typedef
{
    adc_track_typedef               track; /*声道数*/
    dac_sample_rate_typedef         sample_rate;/*采样率*/
    dac_outfrom_typedef             outfrom;/*输出类型*/
    src_sel_type_def                src_sel;/*AHB或SRC*/
    dac_data_format_typedef         data_format;/*数据对齐格式*/
    dac_mix_sorce_def               mix_source;/*mix设置*/
    uint32_t                        vol;/*音量设置*/
    eq_param_class_type             eq_type;/*EQ类型*/
    dac_param_inverse_type          inverse;/*输出为差分或正常*/

} dac_init_typedef;

typedef struct  _audio_dac_handle_typedef
{
    dac_init_typedef            init;
    DMA_HandleTypeDef           *dma_handle; /*!< Pointer DMA Handler */
    audio_state_type_def        state;
    uint32_t                    err_code;
} audio_dac_handle_typedef;


typedef struct _audio_adc_EQ_typedef
{
    uint32_t b0;
    uint32_t b1;
    uint32_t b2;

    uint32_t a1;
    uint32_t a2;
} audio_adc_EQ_typedef;


typedef struct  dac_drc_config_typedef_
{
    uint32_t drc_enable;
    uint32_t drc_lmth;
    uint32_t drc_slop;
    uint32_t drc_peak_at;
    uint32_t drc_peak_rt;
    uint32_t drc_gain_at;
    uint32_t drc_gain_rt;
} dac_drc_config_typedef_def;

HAL_StatusTypeDef  hal_audio_dac_init(audio_dac_handle_typedef *hdac);
HAL_StatusTypeDef dac_deinit(audio_dac_handle_typedef *hdac);
HAL_StatusTypeDef audio_dac_cmd(audio_dac_handle_typedef *hdac, Options_EnableEnum cmd);
HAL_StatusTypeDef audio_dac_set_volume(audio_dac_handle_typedef *hdac, uint32_t volume);
HAL_StatusTypeDef audio_dac_get_volume(audio_dac_handle_typedef *hdac, uint32_t *addr);
HAL_StatusTypeDef audio_dac_set_samplerate(audio_dac_handle_typedef *hdac, dac_sample_rate_typedef samplerate);
HAL_StatusTypeDef audio_dac_get_samplerate(audio_dac_handle_typedef *hdac, uint32_t *addr);
HAL_StatusTypeDef audio_dac_set_channels(audio_dac_handle_typedef *hdac, uint32_t channels);
HAL_StatusTypeDef audio_dac_get_channels(audio_dac_handle_typedef *hdac, uint32_t *addr);
HAL_StatusTypeDef audio_dac_set_samplebits(audio_dac_handle_typedef *hdac, dac_data_format_typedef samplebits);
HAL_StatusTypeDef audio_dac_get_samplebits(audio_dac_handle_typedef *hdac, dac_data_format_typedef *addr);
HAL_StatusTypeDef audio_dac_set_outform(audio_dac_handle_typedef *hdac, dac_outfrom_typedef outfrom);
HAL_StatusTypeDef audio_dac_get_outform(audio_dac_handle_typedef *hdac, dac_outfrom_typedef *addr);
HAL_StatusTypeDef audio_dac_set_data_source(audio_dac_handle_typedef *hdac, src_sel_type_def source);
HAL_StatusTypeDef audio_dac_get_data_source(audio_dac_handle_typedef *hdac, src_sel_type_def *addr);
HAL_StatusTypeDef audio_adc_EQ_cmd(dac_ch_typedef chn, Options_EnableEnum cmd);
HAL_StatusTypeDef audio_dac_drc_config(audio_dac_handle_typedef *hdac, dac_drc_config_typedef_def *ptr);
HAL_StatusTypeDef audio_dac_mix_get(audio_dac_handle_typedef *hdac, uint32_t *dac_r_mix, uint32_t *dac_l_mix);
HAL_StatusTypeDef audio_dac_set_mix_source(audio_dac_handle_typedef *hdac, dac_mix_sorce_def mix_source);
HAL_StatusTypeDef audio_dac_set_eq_type(audio_dac_handle_typedef *hdac, eq_param_class_type eqtype);
HAL_StatusTypeDef audio_dac_set_eq_param(audio_dac_handle_typedef *hdac, uint32_t *ptr);
HAL_StatusTypeDef get_dac_clk_div(dac_sample_rate_typedef sample, uint32_t *addr);

void audio_dac_option_cmd(dac_channels_typedef channel,uint32_t option);
void dac_set_volume(uint32_t volume);
void dac_set_hpr_volume(uint32_t volume);
void dac_set_hpl_volume(uint32_t volume);
void dac_headphone_cmd(uint32_t enable);
void dac_line_out_cmd(uint32_t enable);
void line_out_ramp_down_config(Options_EnableEnum  option);
void line_out_ramp_up_config(Options_EnableEnum  option);
void audio_dac_ibgm_set(void);
void audio_offsets(void);
void audio_EQ_init(void);
void disable_dac(void);
#endif
