
#include "platform.h"
#include "hal_headfile.h"
#include "debug.h"
#include "audio.h"
#include "config.h"
#include "hal_asi.h"

audio_asi_handle_typedef_t  iis_init_typedef_struct;

void audio_iis_init(void)
{
    asi_pins_type_t pins;
    asi_config_type_t asi_config_struct;
    asi_control_type_t asi_control_struct;
    asi_interrupt_enable_type_t enable_type;
#if 1
    pins.sck = GPIO0;
    pins.ws = GPIO1;
    pins.sdo = GPIO2;
    pins.sdi = GPIO3;
#endif

#if 0
    pins.sck = GPIO8;
    pins.ws = GPIO9;
    pins.sdo = GPIO10;
    pins.sdi = GPIO11;
#endif
    hal_asi_pins_init(&pins);

    asi_config_struct.clk_source = ASI_CLK_PLL_AUDIO;
    asi_config_struct.clk_rate = 49152000;
    //asi_config_struct.samplerate = denoise_param.iis_smplerate;
    asi_config_struct.samplerate = 48*1000;
    asi_config_struct.sample_bit   =   ASI_SAMPLE_BIT_16BITS;
    asi_config_struct.data_format       =   ASI_DAT_FMT_LOW_16BITS;
    asi_config_struct.data_mode         =   ASI_I2S_STANDARD;
    asi_config_struct.mode       =   ASI_SLAVE_MODE;
    asi_config_struct.chn_nums    = ASI_TWO_CHANNEL;
    asi_config_struct.chn_width  = ASI_CHANNEL_WIDTH_32BITS;
    asi_control_struct.tx_enable = ASI_TX_ENABLE;
    asi_control_struct.tx_dma_enable = ASI_TX_DMA_DISABLE;
    asi_control_struct.rx_enable = ASI_RX_ENABLE;
    asi_control_struct.rx_dma_enable = ASI_RX_DMA_DISABLE;
    asi_control_struct.tx_stc_t = ASI_TX_SRC;
    asi_control_struct.rx_stc_t = ASI_RX_SRC;
    asi_control_struct.slot_enable = ASI_2_CHANNEL_ENABLE;
    asi_control_struct.io_sta = ASI_IO_STA_HIGH_IMPEDANCE;
    enable_type.asi_rx_fifo_of_enable = ASI_DISABLE;
    enable_type.asi_rx_fifo_uf_enable = ASI_DISABLE;
    enable_type.asi_tx_fifo_of_enable = ASI_DISABLE;
    enable_type.asi_tx_fifo_uf_enable = ASI_DISABLE;
    enable_type.asi_rx_irq_enable = ASI_DISABLE;
    enable_type.asi_tx_irq_enable = ASI_DISABLE;
    hal_asi_init(ASI0, &asi_config_struct, (asi_pins_type_t *)NULL, &asi_control_struct, &enable_type);
}

void asi0_irq_handler(void)
{
    if ((DEV_ASI0->IE & ASI_IE_RX_IRQ) && (DEV_ASI0->PD & ASI_PD_RX_IRQ))
    {
        hal_asi_interrupt_clear_interrupt_pending(ASI0, ASI_PD_RX_IRQ);
    }
}
