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

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 "str71x.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 = 4000000;

//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
    }


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

#define UARTGPIO GPIO0

    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;


    // RTC Oscillator Frequency value = 32 768 Hz
#define RCCU_RTC_Osc  32768

#define RCCU_Div2_Mask  0x00008000
#define RCCU_CK2_16_Mask    0x00000008
#define RCCU_CSU_CKSEL_Mask 0x00000001
#define RCCU_MX_Mask   0x00000030
#define RCCU_MX_Index  0x04
#define RCCU_DX_Mask   0x00000007
        {
        unsigned int Tmp;
        unsigned char Div = 1;
        unsigned char Mul = 1;

        /* Configure the UARTX as following:
        - Baudrate = 9600 Bps
        - No parity
        - 8 data bits
        - 1 stop bit */
        Tmp = (RCCU->CFR & RCCU_Div2_Mask)? (__io_Main_Osc / 2) :  __io_Main_Osc;

        if ((RCCU->CCR & 0x04)==0x04)
            {
            Tmp = RCCU_RTC_Osc;
            }

        else if ((RCCU->CFR & RCCU_CK2_16_Mask)==0)
            {
            Div = 16;
            }
        else if (RCCU->CFR & RCCU_CSU_CKSEL_Mask)
            {
            Mul=(RCCU->PLL1CR & RCCU_MX_Mask ) >> RCCU_MX_Index;
            switch ( Mul )
                {
                case 0: Mul = 20; break;
                case 1: Mul = 12; break;
                case 2: Mul = 24; break;
                case 3: Mul = 16; break;
                }
            Div = ( RCCU->PLL1CR & RCCU_DX_Mask ) + 1;
            }

        Div <<=  PCU->PDIVR & 0x3;

        //UART0->BR = (u16)(((Tmp * Mul) / Div)/(16*9600));
        UART0->BR = (unsigned short)(((Tmp * Mul) / Div)/(16*9600));  
        }


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





