/* 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) 2022. 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 i2c_slave_interrupt.c
 *
 * @brief This file contains the user I2C function.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "i2c_slave_interrupt.h"
#include "string.h"

/* ============================================  Define  ============================================ */

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

/* ==========================================  Variables  =========================================== */
uint8_t g_flag = 0;
uint8_t rxBuff[I2C0_DATA_LEN] = {0};
uint8_t txBuff[I2C0_DATA_LEN] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};

/* ====================================  Functions declaration  ===================================== */

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

/*!
* @brief I2C0 CallBack
*
* @param[in] none
* @return none
*/
void I2C0_CallBack(void *device, uint32_t wpara, uint32_t lpara)
{
    static uint8_t index = 0;

    if (wpara & I2C_STATUS0_SAMF_Msk) /* 从机地址匹配*/
    {
        I2C_ClearStatus0(I2C0, I2C_STATUS0_SAMF_Msk);
        I2C_SendAck(I2C0);
        index = 0;
    }
    if (wpara & I2C_STATUS0_BND_Msk) /* 数据接收完成*/
    {
        if (wpara & I2C_STATUS0_SRW_Msk) /* write*/
        {

        }
        else/* read*/
        {
            rxBuff[index++] = I2C_ReadDataReg(I2C0);
            if (index == I2C0_DATA_LEN)
            {
                g_flag = 1;
            }
        }
    }
}

/*!
* @brief I2C0 Init
*
* @param[in] none
* @return none
*/
void I2C0_Init(void)
{
    I2C_ConfigType i2cConfig;

    /*清零结构体变量.*/
    memset(&i2cConfig, 0x00, sizeof(i2cConfig));

    /*初始化引脚功能为I2C.*/
    GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
    GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);

    /*无论是主机还是从机模式都需要配置的参数.*/
    i2cConfig.mode              = I2C_SLAVE;                 /*设置从机模式.*/
    i2cConfig.extAddrEn         = DISABLE;                   /*10bit扩展地址禁能.*/
    i2cConfig.interruptEn       = ENABLE;                    /*I2C中断,BND/SAMF/ARBLOST.*/
    i2cConfig.nackInterruptEn   = DISABLE;                   /*NACK中断.*/
    i2cConfig.ssInterruptEn     = DISABLE;                   /*总线start或stop中断.*/
    i2cConfig.i2cEn             = ENABLE;                    /*使能模块.*/
    i2cConfig.callBack          = I2C0_CallBack;             /*中断回调函数.*/

    /*从机模式需要配置的参数,配置成主机模式可忽略.*/
    i2cConfig.slaveConfig.slaveAddr      = I2C0_SLAVE_ADDR;  /*从机地址.*/
    i2cConfig.slaveConfig.slaveRangeAddr = 0;                /*从机范围地址.*/
    i2cConfig.slaveConfig.glitchFilterCnt = 0;               /*毛刺过滤.*/
    i2cConfig.slaveConfig.stretchEn      = ENABLE;           /*从机SCL延伸功能.*/
    i2cConfig.slaveConfig.rangeAddrEn    = DISABLE;          /*禁能范围地址.*/
    i2cConfig.slaveConfig.monitorEn      = DISABLE;          /*禁能从机监测功能.*/
    i2cConfig.slaveConfig.generalCallEn  = DISABLE;          /*从机SCL广播地址.*/
    i2cConfig.slaveConfig.wakeupEn       = DISABLE;          /*唤醒功能,仅从机时有效.*/
    i2cConfig.slaveConfig.rxOFInterruptEn   = DISABLE;       /*接收缓存溢出中断.*/
    i2cConfig.slaveConfig.txUFInterruptEn   = DISABLE;       /*发送缓存溢出中断.*/

    I2C_Init(I2C0, &i2cConfig);
    I2C_SendAck(I2C0);
}

/*!
* @brief I2C0 Receive Data
*
* @param[in] none
* @return none
*/
void I2C0_DataHandle(void)
{
    if (g_flag == 1)
    {
        I2C_SlaveTransmitReceiveInt(I2C0, txBuff, I2C0_DATA_LEN);
        g_flag = 0;
    }
}
/* =============================================  EOF  ============================================== */
