/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup I2C_TwoBoardsPolling_Slave
  @{
*/

/** @defgroup I2C_TwoBoardsPolling_Slave_Macros Macros
  @{
*/

/* Slave Address */
#define SLAVE_ADDR          (0XB0)

#define DATA_BUF_SIZE       (32)

/**@} end of group I2C_TwoBoardsPolling_Slave_Macros */

/** @defgroup I2C_TwoBoardsPolling_Slave_Variables Variables
  @{
*/

uint8_t dataBufPt = 0;
uint8_t dataBuf[DATA_BUF_SIZE] = {0};

/**@} end of group I2C_TwoBoardsPolling_Slave_Variables */

/** @defgroup I2C_TwoBoardsPolling_Slave_Functions Functions
  @{
*/

/* I2C Init */
void I2C_Init(void);
/* Data buffer init */
void DataBufInit(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_DelayInit(APM_DELAY_10US);
    I2C_Init();

    while(1)
    {
        DataBufInit();

        dataBufPt = 0;

        while(I2C_ReadStatusFlag(I2C_FLAG_ADDR) == RESET);
        /* Clear ADDR flag by reading STS3 register */
        (void)I2C->STS3;

        /* Wait end of reception */
        while(dataBufPt < DATA_BUF_SIZE)
        {
            /* STS1_RXBNEF = 1 and STS3_RWMF = 0 */
            if((I2C_ReadStatusFlag(I2C_FLAG_RXBNE) == SET) && (I2C_ReadStatusFlag(I2C_FLAG_RWMF) == RESET))
            {
                dataBuf[dataBufPt++] = I2C_RxData();
            }
        }

        /* Stop condition is detected */
        while(I2C_ReadStatusFlag(I2C_FLAG_STOP) == RESET);
        I2C->CTRL2 = I2C->CTRL2;

        APM_Delay(50);

        dataBufPt = 0;

        while(I2C_ReadStatusFlag(I2C_FLAG_ADDR) == RESET);
        /* Clear ADDR flag by reading STS3 register */
        (void)I2C->STS1;
        (void)I2C->STS3;
        /* Wait end of transmission */
        while(dataBufPt < DATA_BUF_SIZE)
        {
            /* STS1_TXBEF = 1 and STS3_RWMF = 1 */
            if(I2C_ReadStatusFlag(I2C_FLAG_TXBE | I2C_FLAG_RWMF) == SET)
            {
                I2C_TxData(dataBuf[dataBufPt++]);
            }
            if(I2C_ReadStatusFlag(I2C_FLAG_ACKERR) == SET)
            {
                break;
            }
        }
        APM_Delay(50);
        I2C_ClearStatusFlag(I2C_FLAG_ACKERR);
    }
}

/*!
 * @brief       I2C Init
 *
 * @param       None
 *
 * @retval      None
 */
void I2C_Init(void)
{
    I2C_Config_T i2cConfig;

    /* ACK when current byte being received  */
    i2cConfig.ack = I2C_ACK_CURRENT;
    /* Set Address  */
    i2cConfig.addr = SLAVE_ADDR;
    /* 7-bit address */
    i2cConfig.addrMode = I2C_ADDR_7_BIT;
    /* duty cycle = 2 */
    i2cConfig.dutyCycle = I2C_DUTYCYCLE_2;
    /* Input clock frequency is 48MHZ */
    i2cConfig.inputClkFreqMhz = 48;
    /* Disable interrupt */
    i2cConfig.interrupt = I2C_INT_NONE;
    /* Output clock frequency is 100KHZ */
    i2cConfig.outputClkFreqHz = 100000;
    I2C_Config(&i2cConfig);

    /* Enable I2C */
    I2C_Enable();
}

/*!
 * @brief       Data buffer init
 *
 * @param       None
 *
 * @retval      None
 */
void DataBufInit(void)
{
    uint8_t i;

    for(i = 0; i < DATA_BUF_SIZE; i++)
    {
        dataBuf[i] = 0;
    }
}

/**@} end of group I2C_TwoBoardsPolling_Slave_Functions */
/**@} end of group I2C_TwoBoardsPolling_Slave */
/**@} end of group Examples */
