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

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

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

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

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

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

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

/*!
* @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_MASTER;    /*设置主机模式.*/
    i2cConfig.extAddrEn         = DISABLE;       /*10bit扩展地址禁能.*/
    i2cConfig.interruptEn       = DISABLE;       /*I2C中断,BND/SAMF/ARBLOST.*/
    i2cConfig.nackInterruptEn   = DISABLE;       /*NACK中断.*/
    i2cConfig.ssInterruptEn     = DISABLE;       /*总线start或stop中断.*/
    i2cConfig.i2cEn             = ENABLE;        /*使能模块.*/
    i2cConfig.callBack          = NULL;          /*中断回调函数.*/

    /*主机模式需要配置的参数,配置成从机模式可忽略.*/
    /*波特率设置
    f = APB时钟/((sampleCnt+1)*(stepCnt+1)*2)
    当APB时钟为24M，所需波特率为100Kbps
    可配置半脉冲宽度stepCnt为7，每个采样点宽度sampleCnt为9
    则波特率 = 16M/((9+1)*(7+1)*2) = 100K
    */

    i2cConfig.masterConfig.sampleCnt = 9;        /*设置波特率为100Kbps,bandrate=(16M/(10*8*2))=100Kbps.*/
    i2cConfig.masterConfig.stepCnt   = 7;
    i2cConfig.masterConfig.ARBEn     = ENABLE;   /*设置主机仲裁功能.*/
    i2cConfig.masterConfig.SYNCEn    = ENABLE;   /*设置主机SCL同步功能.*/

    I2C_Init(I2C0, &i2cConfig);
}

/*!
* @brief I2C0 send data
*
* @param[in] none
* @return none
*/
void I2C0_SendData(void)
{
    I2C_MasterTransmitPoll(I2C0, I2C0_SLAVE_ADDR, g_txBuff, I2C0_DATA_LEN, ENABLE);
}

/* =============================================  EOF  ============================================== */
