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

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac780x_eio_i2c_drv.h"
#include "eio_i2c_master_polling_sample.h"
#include "ac780x_gpio.h"
#include "ac7803x.h"
#include "system_ac7803x.h"

/* ============================================  Define  ============================================ */
#define EIO_I2C_INSTANCE  0     /* I2C instance */

#define EIO_I2C_ADDRESS   0x50  /* AT24C02 address */

#define AT24C02_PAGE_SIZE  0x8   /*Page size = 8*/

#define EIO_I2C_TX_LEN    256   /* EIO I2C tx length */
#define EIO_I2C_RX_LEN    256   /* EIO I2C rx length */

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

/* ==========================================  Variables  =========================================== */
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};


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

/* ======================================  Functions define  ======================================== */
/*!
* @brief EIO I2C0 Init
*        EIO I2C mode is initialized, EIO I2C only support master mode,PB12 is used to be I2C SCL,PB11 is used to be I2C SDA.
*        buadrate is 100K
* @param[in] none
* @return none
*/
void EIO_I2C0_Master_Init(void)
{
    /* GPIO config for EIO function */
    GPIO_SetFunc(GPIOB, GPIO_PIN12, GPIO_FUN5);  /*PB12:EIO_D0 */
    GPIO_SetFunc(GPIOB, GPIO_PIN11, GPIO_FUN5);  /*PB11:EIO_D1 */

    GPIO_SetPullup(GPIOB, GPIO_PIN12, ENABLE);   /*Pin configuration pull-up*/
    GPIO_SetPullup(GPIOB, GPIO_PIN11, ENABLE);   /*Pin configuration pull-up*/

    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_Master_Test
* EIO I2C use polling mode  send 256 bytes to AT24C02, and read 256 bytes from AT24C02
*
* @param[in] none
* @return none
*/
void EIO_I2C0_Master_Test(void)
{
    uint16_t i = 0;
    uint8_t wrTimes = 0;
    uint8_t tempWrBuf[AT24C02_PAGE_SIZE + 1];
    uint8_t startAddr = 0;
    status_t status;

    printf("send data:\r\n");
    for (i = 0; i < EIO_I2C_TX_LEN; i++)
    {
        g_I2C_Tx_Buf[i] = i;
        printf("%d ", g_I2C_Tx_Buf[i]);
    }
    printf("\r\n");

    wrTimes = EIO_I2C_TX_LEN / AT24C02_PAGE_SIZE;

    for (i = 0; i < wrTimes; i++)
    {
        tempWrBuf[0] = startAddr;
        startAddr   += AT24C02_PAGE_SIZE;
        memcpy(&tempWrBuf[1], &g_I2C_Tx_Buf[i * AT24C02_PAGE_SIZE], AT24C02_PAGE_SIZE);/*write one page: 8 bytes*/
        status = EIO_I2C_DRV_MasterSendDataBlocking(&g_eio_i2c_master_state, tempWrBuf, (AT24C02_PAGE_SIZE + 1), TRUE, 3000); /*send data*/
        if (STATUS_SUCCESS != status)
        {
            printf("write fail, status %d,wrTimes %d\r\n", status, i);
            while (1);
        }
        mdelay(5); /*need to wait for 5ms to send the next write instruction*/
    }

    mdelay(100);
    startAddr = 0; /*read from 0 addr*/
    status = EIO_I2C_DRV_MasterSendDataBlocking(&g_eio_i2c_master_state, &startAddr, 1, FALSE, 3000); /*send data*/
    status = EIO_I2C_DRV_MasterReceiveDataBlocking(&g_eio_i2c_master_state, g_I2C_Rx_Buf, EIO_I2C_RX_LEN, TRUE, 3000); /*read data*/

    if (STATUS_SUCCESS != status)
    {
        printf("read data fail,status %d\r\n", status);
    }
    else
    {
        printf("rece data:\r\n");
        for (i = 0; i < EIO_I2C_RX_LEN; i++)
        {
            printf("%d ", g_I2C_Rx_Buf[i]);
        }
        printf("\r\n");
    }
}

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