/* 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_SLAVE_ADDRESS 0x56 /* i2c从机地址 */

#define I2C_CMD_BUF_LEN (2)
// #define I2C_TST_BUF_LEN (256)

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

/* ==========================================  Variables  =========================================== */
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);
    GPIO_DRV_SetPullSel(PORTA, 2U, PORT_INTERNAL_PULL_UP_ENABLED);
    GPIO_DRV_SetPullSel(PORTA, 3U, PORT_INTERNAL_PULL_UP_ENABLED);

    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 调用底层函数后生效 */
}

/**
 * @prototype I2C_Transmit1KBytes(void)
 *
 * @param[in] void
 * @return	 void
 *
 * @brief  	 发送数据.
 */
void I2C_TransmitData(uint8_t *data, uint8_t len)
{
    /*发送数据.*/
    I2C_DRV_MasterSendDataBlocking(I2C0_INSTANCE, data, len, ENABLE, 10); /* I2C 主机发送数据 */

    /*打印发送数据.*/
    printf("\r\nSend Data: ");
    for (uint16_t ii = 0; ii < len; ii++)
    {
        printf("0x%X ", *(data + ii));
    }
    printf("\r\n");
}

/**
 * @prototype I2C_Receive1KBytes(void)
 *
 * @param[in] void
 * @return	 void
 *
 * @brief  	 接收数据.
 */
void I2C_ReceiveData(uint8_t *data, uint8_t len)
{
    /*接收数据.*/
    I2C_DRV_MasterReceiveDataBlocking(I2C0_INSTANCE, data, len, ENABLE, 10); /* I2C 主机接收数据 */

    /*打印发送数据.*/
    printf("\r\nReceive Data: ");
    for (uint16_t ii = 0; ii < len; ii++)
    {
        printf("0x%X ", *(data + ii));
    }
    printf("\r\n");
}
