/********************************************************************

UART.c

This file implements the io_putchar and io_getchar functions
for all the STRx devices.

It is the source code for the IO_Putchar.a default library.

It has been written by using the ST libs and then inlining and
simplifying the code for optimization.

You can duplicate it and include the copy in your project,
then you'll be able to modify it for using different parameters:
baudrate, stop bits, other UART, etc.

*********************************************************************/



#include "str73x.h"

unsigned char __io_init_done = 0;


#define UART0_Rx_Pin (0x0001<<8)   // TQFP 64: pin N 63 , TQFP 144 pin N 143
#define UART0_Tx_Pin (0x0001<<9)   // TQFP 64: pin N 64 , TQFP 144 pin N 144

#define UART_FIFOEnableBit    10
#define UART_TxFull           0x0200
#define UART_TxEmpty          0x0002
#define UART_RxHalfFull      0x0100
#define UART_RxBufFull       0x0001
#define UART_TimeOutIdle     0x0080

unsigned int __io_Main_Osc = 8000000;

//called by the user app to tell us what is the main osc frequency
void __io_SetMainOscFreq( unsigned int NewFreq )
    {
    __io_Main_Osc=NewFreq; //save new freq value
    __io_init_done = 0; //force reinit at next putchar
    }

/*******************************************************************************
* Function Name  : PRCCU_GetFrequencyValue
* Description    : Retrieves the value of the clock passed as parameter.
* Input          : PRCCU_CLOCK : the Clock to get its value .
*                  This parameter can be one of the following values:
*                   - PRCCU_CLOCK_EXT
*                   - PRCCU_CLOCK_MCLK
* Output         : None
* Return         : The value of the clock passed as parameter in Hz.
*******************************************************************************/
#define CMU_CKSEL0_CKOSC  0x0001
#define RC_oscillator    2340000   /* Typical Reset Value of the Internal RC in Hz */
#define PRCCU_DIV2_Enable  0x8000
#define PRCCU_CK2_16_Disable  0x08
#define PRCCU_CSU_CKSEL_Enable  0x01
unsigned int myPRCCU_GetFrequencyValue ()
    {
    unsigned char MUL_Factor=1, DIV_Factor=1;
    unsigned int Tmp=0, Tmp_CLOCK2=0, CLOCK1=0;

    if((CMU->CTRL & CMU_CKSEL0_CKOSC) == CMU_CKSEL0_CKOSC)
        {
        CLOCK1 = __io_Main_Osc ;
        }
    else
        {
        CLOCK1 = RC_oscillator;
        }

    /* Get the value of the fMCLK to CPU and peripherals */
    /* Depending on the status of the DIV2_EN bit get the CLOCK2 value  */
    if((PRCCU->CFR & PRCCU_DIV2_Enable) != 0)
        {
        Tmp_CLOCK2 = CLOCK1 / 2;  /* Set CLOCK2 value to Half CLOCK1 */
        }
    else /* DIV2_EN bit is reset */
        {
        /* Set CLOCK2 value equal to CLOCK1 */
        Tmp_CLOCK2 =  CLOCK1;
        }

    if((PRCCU->CFR & PRCCU_CK2_16_Disable) != 0)
        {
        if ((PRCCU->CFR & PRCCU_CSU_CKSEL_Enable) !=0 )
            {
            /* Get the PLL Multiplication and the Division Factor */
            Tmp = PRCCU->PLLCR;
            MUL_Factor =  ((Tmp & 0x30) >> 4);
            switch (MUL_Factor)
                {
                case 0: MUL_Factor = 20; break;
                case 1: MUL_Factor = 12; break;
                case 2: MUL_Factor = 28; break;
                case 3: MUL_Factor = 16; break;
                }

            DIV_Factor = (Tmp& 0x07) +1;

            /* return the value of the Main Clock MCLK*/
            return(Tmp_CLOCK2 * MUL_Factor /DIV_Factor) ;
            }
        else /* CSU_CKSEL bit Reset */
            {
            return Tmp_CLOCK2; /* Set the MCLK value to CLOCK2  */
            }
        }
    else /* CK2_16 bit reset */
        {
        /* Set the MCLK value to CLOCK2 / 16  */
        return (Tmp_CLOCK2 /16) ;
        }
    }


void __io_init( void )
    { 
    // Configure the GPIO pins



    unsigned char delay=0;
    /* UART0 Clock Enable */
    CFG->PCGR0 |= 0x0001 << 4;
    for (delay = 8 ; delay != 0 ; delay--); /* Peripheral Stabilization */
    /* GPIO6 Clock Enable */
    CFG->PCGR0 |= 0x0001 << 24;
    for (delay = 8 ; delay != 0 ; delay--); /* Peripheral Stabilization */

#define UARTGPIO GPIO6

    UARTGPIO->PC0|=UART0_Tx_Pin;
    UARTGPIO->PC1|=UART0_Tx_Pin;
    UARTGPIO->PC2|=UART0_Tx_Pin;

    UARTGPIO->PC0&=~UART0_Rx_Pin;
    UARTGPIO->PC1|=UART0_Rx_Pin;
    UARTGPIO->PC2&=~UART0_Rx_Pin;

    /* Configure the UART0 as following:
    - Baudrate = 9600 Bps
    - No parity
    - 8 data bits
    - 1 stop bit */

    UART0->CR=0x00A9;

    // Reset the FIFOs
    UART0->RxRSTR=0xFFFF;

    //UART_FifoReset  (UART0 , UART_TxFIFO); // Reset the UART_TxFIFO
    UART0->TxRSTR=0xFFFF;


    /* UART Configuration */

        {
        unsigned int tmpBaudRate = 0;
        /*Configure BaudRate*/
        tmpBaudRate = (unsigned int)((myPRCCU_GetFrequencyValue(/*PRCCU_CLOCK_MCLK*/) * 10) / (16*9600)); //BaudRate 9600

        /*Search the reload value (Integer)*/
        if(tmpBaudRate -((tmpBaudRate/10)*10) < 5)
            {
            UART0->BR = tmpBaudRate /10;
            }
        else
            {
            UART0->BR = (tmpBaudRate /10)+1;
            }
        }


    UART0->CR=0x01A9; // Enable Rx


    // IO init done!
    __io_init_done = 1;
    }






/* send the character on UART0. */
void __io_putchar( char c )
    {
    //init UART if needed
    if( !__io_init_done )
        {
        __io_init();
        }

    /* \n is not enough. Need \r too! */
    if( c == 0x0A )
        {
        __io_putchar( 0x0D );
        } 

    if (UART0->CR & (0x0001<<UART_FIFOEnableBit))// if FIFO ENABLED
        {
        while((UART0->SR & UART_TxFull)); // while the UART_TxFIFO contain 16 characters.
        }
    else                  // if FIFO DISABLED
        {
        while (!(UART0->SR & UART_TxEmpty)); // while the transmit shift register not empty
        }

    UART0->TxBUFR = c;

    while(!(UART0->SR & UART_TxEmpty)); // wait until the data transmission is finished

    }

int
putchar (c)
int c;
    {
    __io_putchar( (char) c );
    return c;
    }

int __io_getchar()
    {
    unsigned char  value;
    unsigned short wStatus;

    //init UART if needed
    if( !__io_init_done )
        {
        __io_init();
        }

    while(!(UART0->SR & UART_RxBufFull));  // If data received
    UART0->TOR=0xFF;// reload the Timeout counter
    while (!((wStatus=UART0->SR) & (UART_TimeOutIdle|UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle
    value = (unsigned char)UART0->RxBUFR; // then read the Receive Buffer Register

    return (int)(value);
    }


int getchar()
    {
    return __io_getchar();
    }






