/* 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) 2023. 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: V1.0.0 demo
 ******************************************************************************/


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

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac7803x.h"
#include "ac780x_gpio.h"
#include "ac780x_eio_i2s_drv.h"
#include "eio_i2s_dma_slave_sample.h"


/* ============================================  Define  ============================================ */
#define I2S_TRANSFER_SIZE     (10U)            /*i2s transfer length*/


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


/* ==========================================  Variables  =========================================== */
uint8_t i2sslaveTxBuffer[I2S_TRANSFER_SIZE] = {0};   /*send buffer*/

eio_device_state_t       EIODeviceState;        /* EIO deviceState,used for initialization*/
eio_i2s_slave_state_t    I2SSlaveState;         /* I2S Slave State,used for initialization*/

eio_i2s_slave_user_config_t eio_i2s_SlaveConfig =
{
    .driverType =  EIO_DRIVER_TYPE_DMA,              /*dma type*/
    .bitsWidth = 16U,                                /*bit wide, Number of bits in a word - multiple of 8*/
    .txPin = 0U,                                     /*eio pin to use for transmit,EIO_D0*/
    .rxPin = 1U,                                     /*eio pin to use for receive,EIO_D1*/
    .sckPin = 2U,                                    /*eio pin to use for serial clock,EIO_D2*/
    .wsPin = 3U,                                     /*eio pin to use for word select,EIO_D3*/
    .callback = NULL,                                /*user callback function,no callback */
    .callbackParam = NULL,                           /*param[in]eter for the callback function*/
    .txDMAChannel = DMA0_CHANNEL0,                   /*tx DMA channel number. Only used in DMA mode*/
    .rxDMAChannel = DMA0_CHANNEL1,                   /*rx DMA channel number. Only used in DMA mode*/
};

/* ====================================  Functions declaration  ===================================== */
/*!
* @brief   EIO_SetChannelFunc
*          GPIO config for EIO function
* @param   none
*
* @return  none
*/
static void EIO_SetChannelFunc(void)
{
    GPIO_SetFunc(GPIOB, GPIO_PIN12, GPIO_FUN5);  /*PB12:EIO_D0 */
    GPIO_SetFunc(GPIOB, GPIO_PIN11, GPIO_FUN5);  /*PB11:EIO_D1 */
    GPIO_SetFunc(GPIOA, GPIO_PIN11, GPIO_FUN4);  /*PA11:EIO_D2 */
    GPIO_SetFunc(GPIOA, GPIO_PIN10, GPIO_FUN4);  /*PA10:EIO_D3 */
}

/*!
* @brief   EIO_I2S_Test
*          Slave sends 10 bytes of data in a loop
* @param   none
*
* @return  none
*/
void EIO_I2S_Test(void)
{
    uint32_t i = 0;

    for (i = 0; i < I2S_TRANSFER_SIZE; i++)                                                  /*Send Data Configuration*/
    {
        i2sslaveTxBuffer[i] = i;
    }

    EIO_SetChannelFunc();
    EIO_DRV_DeinitDevice(0);                                                                 /* Uninit the EIO device */
    EIO_DRV_InitDevice(0, &EIODeviceState);                                                  /* Init the EIO device */
    EIO_I2S_DRV_SlaveInit(0, &eio_i2s_SlaveConfig, &I2SSlaveState);                        /* I2S Slave initialization*/

    while (1)
    {
        EIO_I2S_DRV_SlaveSendData(&I2SSlaveState, i2sslaveTxBuffer, I2S_TRANSFER_SIZE);      /*slave send data*/
        while (EIO_I2S_DRV_SlaveGetStatus(&I2SSlaveState, NULL) != STATUS_SUCCESS);          /*Waiting for sending to complete*/
    }
}

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


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