#include <stdio.h>
#include "OB6625.h"
#include "acmp.h"
#include "uart.h"
#include "gpio.h"

int main(void)
{
    uint32_t nState = 0;

    SystemCoreClockUpdate();

    // Open UART
    GPIO_SetPinMFType(OB_GPIO2, GPIO_PIN9, GPIO_MF_UART0_TX);
    GPIO_SetPinMFType(OB_GPIO2, GPIO_PIN10, GPIO_MF_UART0_RX);
    UART_Open(OB_UART0, 19200);

    printf("\nThis sample code demonstrates ACMP0 function. Using ACMP0_P as ACMP0\n\r");
    printf("The compare result reflects on ACMP0_O\n\r");

    /* Configure ACMP0 Comparator 0. Enable ACMP0, select channel 0 as pisitive input */
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN0, GPIO_MF_OPA_CMP_I);
    GPIO_SetPinMFType(OB_GPIO0, GPIO_PIN1, GPIO_MF_OPA_CMP_I);
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN0, GPIO_MF_ACMP_O);

    ACMP_Open(ACMP_NO_0, PSEL_INPUT_ACMPX_0P, NSEL_INPUT_ACMPX_0N, HYS_RANGE_0);

    /* Check Comparator 0 Output Status */
    if (ACMP_GetOutputStatus(ACMP_NO_0, ACMP_OUTSTA_TYPE_SRC))
    {
        printf(" Comparator output = P > N \n\r");
        nState = 0x01;
    }
    else
    {
        printf(" Comparator output = P < N \n\r");
        nState = 0x00;
    }
    while(1)
    {
        /* Check Comparator 0 Output Status */
        if (ACMP_GetOutputStatus(ACMP_NO_0, ACMP_OUTSTA_TYPE_SRC))
        {
            if (nState == 0x00)
            {
                printf(" P > N \n\r");
                nState = 0x01;
            }
        }
        else
        {
            if (nState == 0x01)
            {
                printf(" P < N \n\r");
                nState = 0x00;
            }
        }
    }
}
