/* 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 i2c_master_int_sample.c
 *
 * @brief i2c_master_int_sample源文件
 */

/* ===========================================  Includes  =========================================== */
#include "ac7840x.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "i2c_drv.h"
#include "i2c_hw.h"
#include "i2c_master_int_sample.h"
#include <string.h>

/* ============================================  Define  ============================================ */
#define I2C0_INSTANCE     0       /* i2c 序列号 */

#define I2C_RX_SIZE       256     /* i2c 接收数据大小 */
#define I2C_TX_SIZE       256     /* i2c 发送数据大小 */

#define I2C_SLAVE_ADDRESS 0x50    /* i2c从机地址 */

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


/* ==========================================  Variables  =========================================== */
uint8_t g_rxBuff[I2C_RX_SIZE] = {0}; 
uint8_t g_txBuff[I2C_TX_SIZE] = {0};

i2c_master_state_t g_i2c_master_state;
/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */
 
/*!
* @brief I2C0 Init
*
* @param[in] none
* @return none
*/
void I2C0_Init(void)
{
    /* I2C 配置结构体 */
    i2c_master_user_config_t i2c_master_config;
    
    /* 清I2C 配置结构体 */
    memset(&i2c_master_config, 0, sizeof(i2c_master_user_config_t));
    memset(&g_i2c_master_state, 0, sizeof(i2c_master_state_t));
    
    /* PA2->SDA,?PPA3->SCL */
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)2U, PORT_MUX_ALT3);
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)3U, PORT_MUX_ALT3);
    
    i2c_master_config.is10bitAddr    = false;                  /* 7bit地址模式 */
    i2c_master_config.slaveAddress   = I2C_SLAVE_ADDRESS;      /* i2c 从机地址 */
    i2c_master_config.transferType   = I2C_USING_INTERRUPTS;   /* i2c 使用中断方式 */
    i2c_master_config.syncEn         = false;                  /* i2c 同步禁能 */
    i2c_master_config.arbitration    = true;                   /* i2c 仲裁禁能 */
    i2c_master_config.baudRate       = 100000;                 /* i2c 波特率设置 */
    i2c_master_config.callback       = NULL;                   /* 无回调函数 */
    
    I2C_DRV_MasterInit(I2C0_INSTANCE,&i2c_master_config,&g_i2c_master_state);  /* i2c 调用底层函数后生效 */
}

/*!
* @brief I2C0 发送数据
*
* @param[in] none
* @return none
*/
void I2C0_MasterTx_Test(void)
{
    uint16_t i = 0;
    
    for(i = 0; i < I2C_TX_SIZE; i++)
    {
        g_txBuff[i] = i;
    }
      
    I2C_DRV_MasterSendData(I2C0_INSTANCE,g_txBuff,I2C_TX_SIZE,ENABLE);  /* I2C 主机发送数据 */
}

