
#include "mcu.h"
#include "Uart_FM3.h"

/*
 *  Internal Function
 */
static void InitPort(void);



int32_t main(void)
{
    int32_t size;
    uint8_t tx_buf[] = "Echo back sample. Please hit any key.\n";
    uint8_t rx_buf[10];
    
    InitPort();
    
    Uart_Init();
    
    /* 
     * print message
     */
    size = sizeof(tx_buf);
    Uart_BufTx(tx_buf, &size);


    /*
     * Echo back
     */
    for (;;) {
        size = 1;
        Uart_BufRx(rx_buf, &size);
        if (size != 0) {
            Uart_BufTx(rx_buf, &size);
        }
    }

    
}

/*
 *  InitPort
 */
static void InitPort(void)
{
    /*
     * Set I/O Port
     */

    /* ADC */
    FM3_GPIO->ADE_f.AN31 = 0;  // disable AN31(P22/SOT0_0) 

    /* Ch0 */
    FM3_GPIO->PFR2_f.P1 = 1;   // P21/SIN0_0
    FM3_GPIO->EPFR07_f.SIN0S1 = 0;
    FM3_GPIO->EPFR07_f.SIN0S0 = 0;
    FM3_GPIO->PFR2_f.P2 = 1;   // P22/SOT0_0
    FM3_GPIO->EPFR07_f.SOT0B1 = 0;
    FM3_GPIO->EPFR07_f.SOT0B0 = 1;

    /* Ch3 */
    FM3_GPIO->PFR4_f.P8 = 1;   // P48/SIN3_2
    FM3_GPIO->EPFR07_f.SIN3S1 = 1;
    FM3_GPIO->EPFR07_f.SIN3S0 = 1;
    FM3_GPIO->PFR4_f.P9 = 1;   // P49/SOT3_2
    FM3_GPIO->EPFR07_f.SOT3B1 = 1;
    FM3_GPIO->EPFR07_f.SOT3B0 = 1;

    /* Ch4 */
    FM3_GPIO->PFR0_f.P5 = 1;   // P05/SIN4_2
    FM3_GPIO->EPFR08_f.SIN4S1 = 1;
    FM3_GPIO->EPFR08_f.SIN4S0 = 1;
    FM3_GPIO->PFR0_f.P6 = 1;   // P06/SOT4_2
    FM3_GPIO->EPFR08_f.SOT4B1 = 1;
    FM3_GPIO->EPFR08_f.SOT4B0 = 1;

}
