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


eio_i2c_master_state_t g_eio_i2c_master_state;
eio_i2c_master_user_config_t g_eio_i2c_master_user_config;
eio_device_state_t  g_eio_device_state;

uint8_t g_I2C_Tx_Buf[EIO_I2C_TX_LEN] = {0};
uint8_t g_I2C_Rx_Buf[EIO_I2C_RX_LEN] = {0};



/*!
* @brief EIO I2C0 Init
*        使用PD0和PD1模拟I2C_SCL和I2C_SDA
* @param[in] none
* @return none
*/
void EIO_I2C0_Master_Init(void)
{ 
    /* GPIO config for EIO function */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)0U, PORT_MUX_ALT6);   /* PD0：EIO_D0 */
    GPIO_DRV_SetMuxModeSel(PORTD, (gpio_channel_type_t)1U, PORT_MUX_ALT6);   /* PD1：EIO_D1 */
    
    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(&g_eio_i2c_master_state,0,sizeof(eio_i2c_master_state_t));
    memset(&g_eio_i2c_master_user_config,0,sizeof(eio_i2c_master_user_config_t));
    memset(&g_eio_device_state,0,sizeof(eio_device_state_t));
    
    g_eio_i2c_master_user_config.sclPin            = 0;                            /* EIO I2C scl pin */
    g_eio_i2c_master_user_config.sdaPin            = 1;                            /* EIO I2C sda pin */
    g_eio_i2c_master_user_config.driverType        = EIO_DRIVER_TYPE_POLLING;      /* I2C Polling type */
    g_eio_i2c_master_user_config.slaveAddress      = EIO_I2C_ADDRESS;              /* I2C slave address */
    g_eio_i2c_master_user_config.baudRate          = 100000;                       /* I2C baudrate 100k */
    
    g_eio_i2c_master_state.eioCommon.resourceIndex = 0;                            /* EIO resource index */
    
    EIO_DRV_InitDevice(EIO_I2C_INSTANCE,&g_eio_device_state);                                        /* device init */
    EIO_I2C_DRV_MasterInit(EIO_I2C_INSTANCE,&g_eio_i2c_master_user_config,&g_eio_i2c_master_state);    /* master configure */
    EIO_I2C_DRV_MasterSetSlaveAddr(&g_eio_i2c_master_state,g_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_MasterSendDataBlocking(&g_eio_i2c_master_state,g_I2C_Tx_Buf,EIO_I2C_TX_LEN,ENABLE,3000); /*发送数据*/
       
}

