/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

/*!
* @file eio_i2s_dma_master_sample.c
*
* @brief This file contains the user eio i2s function.
*
*/

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "ckgen_drv.h"
#include "eio_i2s_drv.h"
#include "eio_I2S_interrupt_master_sample.h"

/* ============================================  Define  ============================================ */
#define I2S_TRANSFER_SIZE     (10U)             /* 传输长度 */
#define SET_EIO_SOURCECLOCK   (1U)              /* 是否重新配置EIO时钟参数， 1重新配置， 0使用默认配置 */

#define EIO_D0_FUNC        PORT_MUX_ALT6       /* EIO复用功能 */
#define EIO_D1_FUNC        PORT_MUX_ALT6
#define EIO_D2_FUNC        PORT_MUX_ALT4
#define EIO_D3_FUNC        PORT_MUX_ALT4

/* EIO引脚 */
#define EIO_D0_CHANNEL     PORTD,0UL            /** PD0 **/
#define EIO_D1_CHANNEL     PORTD,1UL            /** PD1 **/
#define EIO_D2_CHANNEL     PORTA,0UL            /** PA0 **/
#define EIO_D3_CHANNEL     PORTA,1UL            /** PA1 **/

/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
uint8_t g_FlagI2SRxFull = 0;                    /*I2S_EVENT_RX_FULL事件发生标志位*/
uint8_t i2smasterTxBuffer[I2S_TRANSFER_SIZE];   /* 发送BUFFFER */

eio_device_state_t       EIODeviceState;        /* EIO deviceState, 用于初始化 */
eio_i2s_master_state_t   I2SMasterState;        /* I2S Master State 用于初始化*/


/*                          I2S Master Config                               */
eio_i2s_master_user_config_t eio_i2s1_MasterConfig = {
    .driverType =  EIO_DRIVER_TYPE_INTERRUPTS,    /* 中断模式 */
    .baudRate = 352800U,                          /* 波特率配置 */
    .bitsWidth = 16U,                             /* LRCLK 位宽 */
    .txPin = 1U,                                  /* 发送引脚，EIO0, 即PD1 */
    .rxPin = 0U,                                  /* 接收引脚，EIO1, 即PD0 */
    .sckPin = 2U,                                 /* BCLK引脚，EIO2, 即PA0 */
    .wsPin = 3U,                                  /* LRCLK引脚，EIO3, 即PA1 */
    .callback = NULL,                             /* 回调索引 */
    .callbackParam = NULL,                        /* 回调参数 */
    .txDMAChannel = 0U,                           /* Tx DMA配置 */
    .rxDMAChannel = 0U,                           /* RX DMA配置 */
};

/* ====================================  Functions declaration  ===================================== */

/*!
* @brief   EIO_SetChannelFunc, EIO 功能GPIO初始化
* @param   none
*
* @return  none
*/
static void EIO_SetChannelFunc(void)
{
    GPIO_DRV_SetMuxModeSel(EIO_D0_CHANNEL, EIO_D0_FUNC);
    GPIO_DRV_SetMuxModeSel(EIO_D1_CHANNEL, EIO_D1_FUNC);
    GPIO_DRV_SetMuxModeSel(EIO_D2_CHANNEL, EIO_D2_FUNC);
    GPIO_DRV_SetMuxModeSel(EIO_D3_CHANNEL, EIO_D3_FUNC);
}

/*!
* @brief   EIO_I2S_Test, 主机以500ms的周期一直发送数据
* @param   none
*
* @return  none
*/
void EIO_I2S_Test(void)
{ 
    module_clk_config_t clkConfig;                                                           /* 时钟配置 */
    uint8_t i = 0;
    
    EIO_SetChannelFunc();                                                                    /* EIO 功能复用*/

    EIO_DRV_DeinitDevice(0);                                                                 /* Uninit the EIO device */
    EIO_DRV_InitDevice(0, &EIODeviceState);                                                  /* Init the EIO device */

#if SET_EIO_SOURCECLOCK                                                                      /* EIO 时钟源配置， 1为重新配置，0使用默认配置 */
    clkConfig.div = 0;
    clkConfig.source = VHSIDIV2_CLK;
    CKGEN_DRV_SetModuleClock(EIO_CLK,&clkConfig);
#endif

    I2SMasterState.clockContinueOutput = false;                                              /* BCLK & LRCLK 持续输出去能 */
    EIO_I2S_DRV_MasterInit(0,  &eio_i2s1_MasterConfig, &I2SMasterState);                     /* I2S Master 初始化 */

    for(i=0; i<I2S_TRANSFER_SIZE; i++)                                                       /* 发送BUFFER 数据配置*/
    {
         i2smasterTxBuffer[i] = i;
    }
    
    while(1)
    {
        EIO_I2S_DRV_MasterSendData(&I2SMasterState, i2smasterTxBuffer, I2S_TRANSFER_SIZE);   /* 开始发送数据 */
        while (EIO_I2S_DRV_MasterGetStatus(&I2SMasterState, NULL) == STATUS_BUSY);           /*确认传输是否完成*/

        OSIF_TimeDelay(500);
    }

}

/* ======================================  Functions define  ======================================== */


/* =============================================  EOF  ============================================== */
