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

#define I2C_SLAVE_ADDR  0x60
#define I2C_BUS_CLOCK   100000

int main(void)
{
    uint8_t  TX_Buff[10] = {0,1,2,3,4,5,6,7,8,9};
    uint8_t  RX_Buff[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 Master(Kingston) Driver Sample Code                         |\n\r");
    printf("+----------------------------------------------------------------------+\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_MasterOpen(OB_I2C0, I2C_BUS_CLOCK);

    if (I2C_MasterTransmit(OB_I2C0, I2C_SLAVE_ADDR, TX_Buff, 10) == TRUE)
    {
        printf(" Transmit PASS,\n\r");
    }
    else
    {
        printf(" Transmit FAILED,\n\r");
    }

    if (I2C_MasterReceive(OB_I2C0, I2C_SLAVE_ADDR, RX_Buff, 0x100) == TRUE)
    {
        uint32_t nLoop;
        for(nLoop = 0x00; nLoop < 0x100; nLoop++)
        {
            printf(" I2C0 Data = %02X,\n\r", RX_Buff[nLoop]);
        }
    }
    else
    {
        printf(" Receive FAILED,\n\r");
    }

    while(1);
}

