#include "stdio.h"
#include "OB90A64U1.h"
#include "gpio.h"
#include "uart.h"
#include "I2S.h"

int main(void)
{
    uint32_t nBuff, Loop;
    SystemCoreClockUpdate();
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN14, GPIO_MF_UART_RX);
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN13, GPIO_MF_UART_TX);
    UART_Open(OB_UART0, 19200);

    /* I2S SPI multi-function pin */
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN10, GPIO_MF_I2S_MCLK); //MCLK
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN6, GPIO_MF_I2S_WS);    //WS
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN7, GPIO_MF_I2S_CLK);   //CLK
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN8, GPIO_MF_I2S_DI);    //DI
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN9, GPIO_MF_I2S_DO);    //DO

    printf("+----------------------------------------------------------+\n");
    printf("|            I2S Driver Sample Code (slave mode)           |\n");
    printf("+----------------------------------------------------------+\n");
    printf("      I2S configuration:\n");
    printf("      Sample rate 16 kHz\n");
    printf("      Word width 16 bits\n");
    printf("      Stereo mode\n");
    printf("      I2S format\n");
    printf("      TX value: 0x8765 1234, 0x8766 1235, ..., \n");

    I2S_Open(OB_I2S, I2S_MODE_MASTER, (16000 * 16 * 2), I2S_WIDTH_16BIT, I2S_MONO_STEREO, I2S_FORMAT_I2S);
    nBuff = 0x87651234;
    Loop = 0;
    while(1)
    {
        if(OB_I2S->IEST_b.TXTHST == 1)
        {
            OB_I2S->TXFIFO = nBuff;
            if(Loop == 2000)
            {
                nBuff = 0x87651234;
                Loop = 0;
            }
            else
            {
                nBuff = nBuff + 0x00010001;
                Loop++;
            }
        }
    }
}
