#include "stdio.h"
#include "OB90R32A1.h"
#include "uart.h"
#include "gpio.h"
#include "adc.h"

int main(void)
{
    uint32_t Result[8], nLength;

    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);


    //--------------------------------
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN2, GPIO_MF_ADC);   //P1.2 = ADC channel 2

    printf("\nThis sample code demonstrate ADC channel_2 and channel_8(bandgap) conversion \n\r");

    // Enable ADC
    ADC_Open(OB_ADC,  ADC_MODE_12BIT);

    // lst Input Source = Channel_2
    // 2nd Input Source = Channel_8(bandgap)
    ADC_SetInputSource(OB_ADC,  ADC_INPUT_1ST, ADC_CHANNEL_2);
    ADC_SetInputSource(OB_ADC,  ADC_INPUT_2ND, ADC_CHANNEL_8);

    nLength = 0x02; //Channel_2 and Channel_8(bandgap)
    ADC_StartConvert(OB_ADC,  nLength);
    ADC_GetSampleResult(OB_ADC,  Result, nLength);
    printf(" ADC Value (Channel_2) = 0x%04X \n\r", Result[0]);
    printf(" ADC Value (Channel_8(bandgap)) = 0x%04X \n\r", Result[1]);
    while(1);
}
