/* 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
 ******************************************************************************/

#include <string.h>
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "eio_i2c_drv.h"
#include "eio_i2c_master_dma_sample.h"
#include "dma_drv.h"

#define EIO_I2C_INSTANCE  0     /* I2C instance */

#define EIO_I2C_ADDRESS   0x50  /* I2C address */

#define EIO_I2C_TX_LEN    256   /* EIO I2C tx length */
#define EIO_I2C_RX_LEN    256   /* EIO I2C rx length */

#if    defined ( __ICCARM__ )        /* IAR Compiler */
eio_i2c_master_state_t eio_i2c_master_state @0x20000400;
#elif  defined ( __CC_ARM )        /* Arm Compiler */
eio_i2c_master_state_t eio_i2c_master_state __attribute__ ((at(0x20000400)));
#endif
eio_i2c_master_user_config_t eio_i2c_master_user_config;
eio_device_state_t  eio_device_state;
#if    defined ( __ICCARM__ )        /* IAR Compiler */
uint8_t g_I2C_Tx_Buf[EIO_I2C_TX_LEN] @0x20000000 = {0};
uint8_t g_I2C_Rx_Buf[EIO_I2C_RX_LEN] @0x20000200 = {0};
#elif  defined ( __CC_ARM )        /* Arm Compiler */
uint8_t g_I2C_Tx_Buf[EIO_I2C_TX_LEN] __attribute__ ((at(0x20000000))) = {0};
uint8_t g_I2C_Rx_Buf[EIO_I2C_RX_LEN] __attribute__ ((at(0x20000200))) = {0};
#endif

dma_chn_state_t s_eioi2c_dma_txchn_state = {0};
dma_chn_state_t * const g_dma_chn_state[1] = {&s_eioi2c_dma_txchn_state};  /* dma 通道状态 */

dma_state_t  g_dma_state = {                  /* i2c0 dma 状态 */ 
    .virtChnState[0] = &s_eioi2c_dma_txchn_state,
};             

dma_channel_config_t dma_i2c0_txchn_config = {
    .channelPriority = DMA_CHN_PRIORITY_HIGH,  /* dma 通道优先级 */
    .virtChnConfig   = 1,                      /* dma 通道号 */
    .source          = DMA_REQ_EIO_SHIFTER0,   /* i2c0 发送的dma通道请求  */
    .callback        = NULL,                   /* 无回调函数 */
    .enableTrigger   = DISABLE,                /* dma 周期触发禁能 */
};
const dma_channel_config_t * const g_dma_chn_config_Arr[1] = {&dma_i2c0_txchn_config};   /* dma 通道配置结构体 */


/*!
* @brief EIO I2C0 Init
*        配置I2C波特率为100K，中断方式传输
*        PD0作为I2C_SCL，PD1作为I2C_SDA
* @param[in] none
* @return none
*/
void EIO_I2C0_Master_Init(void)
{ 
    /* EIO function */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)0U, PORT_MUX_ALT6);  /* PD0 */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)1U, PORT_MUX_ALT6);  /* PD1 */
    
    GPIO_DRV_SetPullSel(PORTD, (gpio_channel_type_t)0U,PORT_INTERNAL_PULL_UP_ENABLED);
    GPIO_DRV_SetPullSel(PORTD, (gpio_channel_type_t)1U,PORT_INTERNAL_PULL_UP_ENABLED);
    
    memset(&eio_i2c_master_state,0,sizeof(eio_i2c_master_state_t));
    memset(&eio_i2c_master_user_config,0,sizeof(eio_i2c_master_user_config_t));
    memset(&eio_device_state,0,sizeof(eio_device_state_t));
    
    eio_i2c_master_user_config.sclPin            = 0;                            /* EIO I2C scl pin */
    eio_i2c_master_user_config.sdaPin            = 1;                            /* EIO I2C sda pin */
    eio_i2c_master_user_config.driverType        = EIO_DRIVER_TYPE_DMA;          /* I2C interrupt type */
    eio_i2c_master_user_config.slaveAddress      = EIO_I2C_ADDRESS;              /* I2C slave address */
    eio_i2c_master_user_config.baudRate          = 100000;                       /* I2C baudrate 100k */
    eio_i2c_master_user_config.txDMAChannel      = 1U,                           /*! set I2C tx DMA channel 1*/
    eio_i2c_master_state.eioCommon.resourceIndex = 0;                            /* EIO resource index */
    
    DMA_DRV_Init(&g_dma_state,g_dma_chn_state,g_dma_chn_config_Arr,1);  
    
	EIO_DRV_DeinitDevice(EIO_I2C_INSTANCE);
    EIO_DRV_InitDevice(EIO_I2C_INSTANCE,&eio_device_state);                                        /* device init */
    EIO_I2C_DRV_MasterInit(EIO_I2C_INSTANCE,&eio_i2c_master_user_config,&eio_i2c_master_state);    /* master configure */
    EIO_I2C_DRV_MasterSetSlaveAddr(&eio_i2c_master_state,eio_i2c_master_user_config.slaveAddress); /* set slave address */
}

/*!
* @brief EIO I2C0 send data
*        对发送数据赋值，并发送
* @param[in] none
* @return none
*/
void EIO_I2C0_MasterTx_Test(void)
{
    uint32_t i = 0;
    
    for(i = 0; i < EIO_I2C_TX_LEN; i++)
    {
        g_I2C_Tx_Buf[i] = i;
    }
    
    EIO_I2C_DRV_MasterSendData(&eio_i2c_master_state,g_I2C_Tx_Buf,EIO_I2C_TX_LEN,ENABLE);  /* EIO I2C send data */
    while (EIO_I2C_DRV_MasterGetStatus(&eio_i2c_master_state, NULL) == STATUS_BUSY);       /* 确认传输是否完成 */
}

