#include "stdio.h"
#include "OB6625.h"
#include "gpio.h"
#include "pwm.h"
#include "uart.h"

int main(void)
{
    PWM_OUT_CONFIG pwm_out;

    SystemCoreClockUpdate();
    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 will output PWM channel 0A and channel 0B\n\r");
    printf(" PWM channel 0A Output ,low plus = 250us, High plus = 750u  \n\r");
    printf(" PWM channel 0B Output ,low plus = 150us, High plus = 2650u \n\r");
    printf(" PWM channel 1A Output ,low plus = 250us, High plus = 750u  \n\r");
    printf(" PWM channel 1B Output ,low plus = 150us, High plus = 2650u \n\r");

    // Common Setting
    pwm_out.MODE0    = PWM_MODE_UP_DOWN;
    pwm_out.MODE1    = PWM_MODE_UP_DOWN;
    pwm_out.DBEnable = 0;
    pwm_out.DBMode   = 0;
    pwm_out.DBDelay  = 0;
    //--------------------------------channel 0A--------------------
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN3, GPIO_MF_PWM);
    pwm_out.Channel       = PWM_CH0A;
    pwm_out.CounterSource = PWM_COUNTER0;
    pwm_out.Duty          = 250;  // low plus = 250us, High plus = 750us,
    pwm_out.Period        = 1000; // Period = low plus + High plus = 1000us
    pwm_out.Comparator    = PWM_COMPARATOR0;
    pwm_out.Invert        = 0x00;
    pwm_out.Interrupt     = 0x00;
    PWM_OutputDuty(&pwm_out);
    //--------------------------------channel 0B--------------------
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN4, GPIO_MF_PWM);
    pwm_out.Channel       = PWM_CH0B;
    pwm_out.CounterSource = PWM_COUNTER1;
    pwm_out.Duty          = 150;  // low plus = 150us, High plus = 2650us,
    pwm_out.Period        = 2700; // Period = low plus + High plus = 2700us
    pwm_out.Comparator    = PWM_COMPARATOR1;
    pwm_out.Invert        = 0x00;
    pwm_out.Interrupt     = 0x00;
    PWM_OutputDuty(&pwm_out);
    //--------------------------------channel 1A--------------------
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN5, GPIO_MF_PWM);
    pwm_out.Channel       = PWM_CH1A;
    pwm_out.CounterSource = PWM_COUNTER0;
    pwm_out.Duty          = 250;  // low plus = 250us, High plus = 750us,
    pwm_out.Period        = 1000; // Period = low plus + High plus = 1000us
    pwm_out.Comparator    = PWM_COMPARATOR2;
    pwm_out.Invert        = 0x00;
    pwm_out.Interrupt     = 0x00;
    PWM_OutputDuty(&pwm_out);
    //--------------------------------channel 1B--------------------
    GPIO_SetPinMFType(OB_GPIO1, GPIO_PIN6, GPIO_MF_PWM);
    pwm_out.Channel       = PWM_CH1B;
    pwm_out.CounterSource = PWM_COUNTER1;
    pwm_out.Duty          = 150;  // low plus = 150us, High plus = 2650us,
    pwm_out.Period        = 2700; // Period = low plus + High plus = 2700us
    pwm_out.Comparator    = PWM_COMPARATOR3;
    pwm_out.Invert        = 0x00;
    pwm_out.Interrupt     = 0x00;
    PWM_OutputDuty(&pwm_out);
    //----------------------------------Enable PWM----------------------
    PWM_Start(pwm_out.MODE0, pwm_out.MODE1);
    while(1);
}
