#include "stdio.h"
#include "OB90R32A2.h"
#include "i2c.h"
#include "gpio.h"
#include "uart.h"

#define I2C_SLAVE0_ADDR  0x60
#define I2C_SLAVE1_ADDR  0xA0

int main(void)
{
    uint8_t  Buff[0x100];
    uint32_t nLoop = 0;
    uint32_t nReceiveSize = 0x100;

    SystemCoreClockUpdate();

    // Open UART
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN1, GPIO_MF_UART0_RX);   // P0.1
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN0, GPIO_MF_UART0_TX);   // P0.0
    UART_Open(OB_UART0, 19200);

    printf("+-------------------------------------------------------+\n\r");
    printf("| I2C Slave(Kingston) Driver Sample Code                |\n\r");
    printf("+-------------------------------------------------------+\n\r");

    // Set Pin Mutiple Function
    GPIO_SetPinMFType(OB_GPIO3, GPIO_PIN4, GPIO_MF_I2C0_SDA);   //SDA
    GPIO_SetPinMFType(OB_GPIO3, GPIO_PIN5, GPIO_MF_I2C0_SCL);   //SCL

    I2C_SetSlaveAddr(OB_I2C0, 0, I2C_SLAVE0_ADDR);
    I2C_SetSlaveAddr(OB_I2C0, 1, I2C_SLAVE1_ADDR);

    I2C_SlaveOpen(OB_I2C0);

    // Transmit
    for (nLoop = 0; nLoop < 0x100; nLoop++)
    {
        Buff[nLoop] = (uint8_t)nLoop;
    }

    if (I2C_SlaveTransmit(OB_I2C0, Buff, 0x100) == FALSE)
    {
        printf(" Transmit Failed \n\r");
        while(1);

    }
    else
    {
        printf(" Transmit Succeeded \n\r");
        printf(" Set the data (0x00 ~ 0xFF) \n\r");
    }

    // Receive
    if (I2C_SlaveReceive(OB_I2C0, Buff, &nReceiveSize) == FALSE)
    {
        printf(" Receive Failed \n\r");
    }
    else
    {
        printf(" Receive Succeeded : \n\r");
        for (nLoop = 0x00; nLoop < nReceiveSize; nLoop++)
        {
            printf(" , %02X", Buff[nLoop]);
        }
    }
    while(1);
}

