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

UART.c

This file implements the io_putchar and io_getchar functions
for the STR75x 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 "str75x.h"

unsigned char __io_init_done = 0;

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
    }


#define MRCC_Peripheral_UART1    0x00200000
#define MRCC_Peripheral_GPIO     0x01000000
#define UART_WordLength_Mask            0xFF9F  /* UART Word Length Mask */
#define UART_StopBits_1             0xFFF7  /* One stop bit */
#define UART_Parity_Mask                0xFF79  /* UART Parity Mask */
#define UART_WordLength_8D          0x0060  /* 8 bits Data */
#define UART_Parity_No              0x0000  /* Parity Disable */
#define UART_HardwareFlowControl_Mask   0x3FFF  /* UART Hardware Flow Control Mask */
#define UART_Mode_Mask                  0xFCFF  /* UART Mode Mask */
#define UART_HardwareFlowControl_None       0x0000/* HFC Disable */
#define UART_Mode_Tx_Rx             0x0300  /* UART Tx and Rx Enabled */
#define UART_FIFO_Enable            0x0010  /* FIFOs Enable */
#define UART_TxRxFIFOLevel_Mask         0xFFC0  /* UART Tx Rx FIFO Level Mask */
#define UART_FIFOLevel_1_2 0x0002    /* FIFO size 16 bytes, FIFO level 8 bytes */

void __io_init( void )
    {
    /* Enable UART1 and GPIO Clocks */
    MRCC->PCLKEN |= MRCC_Peripheral_UART1 | MRCC_Peripheral_GPIO;

    // Configure the GPIO pins
    // Configure the UART1_Tx and UART1_RTS pin as Alternate function Push-Pull
    GPIO0->PC0 |= 0x00A00000;
    GPIO0->PC1 |= 0x00A00000;
    GPIO0->PC2 |= 0x00A00000;
    // Configure the UART1_Rx and UART1_CTS as input floating
    GPIO0->PC0 |= 0x00500000;
    GPIO0->PC1 &= 0xFFAFFFFF;
    GPIO0->PC2 &= 0xFFAFFFFF;

    //configure UART1
    /*
    UART_InitStructure.UART_WordLength = UART_WordLength_8D;
    UART_InitStructure.UART_StopBits = UART_StopBits_1;
    UART_InitStructure.UART_Parity = UART_Parity_No ;
    UART_InitStructure.UART_BaudRate = 9600;
    UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
    UART_InitStructure.UART_FIFO = UART_FIFO_Enable;
    UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2;
    UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2;
    UART_Init(&UART_InitStructure);
    */
        {

        unsigned int IntegerDivider = 0;
        unsigned int FractionalDivider = 0;
        //MRCC_ClocksTypeDef  MRCC_ClocksStatus;
        /* 8 data bits, one stop bit, no parity*/
        UART1->LCR = 
            (UART1->LCR & UART_WordLength_Mask & UART_StopBits_1 & UART_Parity_Mask)
            | UART_WordLength_8D | UART_Parity_No;
        ;

        /* Configure the BaudRate --------------------------------------------------*/
        /* Get the APB frequency */
        //MRCC_GetClocksStatus(&MRCC_ClocksStatus);
        //APBClock = MRCC_ClocksStatus.PCLK_Frequency;

        /* Determine the integer part */
        IntegerDivider = ((100) * (__io_Main_Osc) / (16 * (9600)));
        UART1->IBRD = IntegerDivider / 100; 

        /* Determine the fractional part */
        FractionalDivider = IntegerDivider - (100 * (UART1->IBRD));
        UART1->FBRD = ((((FractionalDivider * 64) + 50) / 100));

        /* Disable Hardware Flow Control, select TxRx mode */
        UART1->CR =  ( UART1->CR & UART_HardwareFlowControl_Mask & UART_Mode_Mask)
            | UART_HardwareFlowControl_None | UART_Mode_Tx_Rx   ;

        /* Enable the FIFOs */
        UART1->LCR |= UART_FIFO_Enable;

        /* set FIFO levels */
        UART1->IFLS = (UART1->IFLS &  UART_TxRxFIFOLevel_Mask)
            | (UART_FIFOLevel_1_2 << 3) | UART_FIFOLevel_1_2;
        }

    // Enable the UART
    // Enable the selected UART by setting the UARTEN bit in the CR register
    UART1->CR |= 1;

    // IO init done!
    __io_init_done = 1;
    }



// send the character on UART1.
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 );
        }

    //wait for UART ready to send
    while(UART1->FR & 0x20);
    // Transmit one byte
    UART1->DR = c;
    }

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

    //wait for something to read
    while(UART1->FR & 0x10);
    //Read data
    value = UART1->DR;

    return (int)(value);
    }


int getchar()
    {
    return __io_getchar();
    }






