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

UART.c

This file implements the io_putchar and io_getchar functions
for the STM32F10x 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 "stm32f10x_lib.h"
//#include "stm32F10x.h"


unsigned char __io_init_done = 0;

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
    }

void __io_init( void )
  {
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  /* Enable GPIOx and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

/* Enable USART1 clocks */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  /* Configure the GPIO ports */
  /* Configure USARTx_Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USARTx_Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge 
        - USART LastBit: The clock pulse of the last data bit is not output to 
                         the SCLK pin
  */
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
  
  /* Configure the USARTx */ 
  USART_Init(USART1, &USART_InitStructure);
  /* Enable the USART1 */
  USART_Cmd(USART1, ENABLE);

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

   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
      {
      }
   USART_SendData(USART1, (u8)c);
   }

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

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

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

   while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
      {
      }
   value = USART_ReceiveData(USART1);

   return (int)(value);
   }


int getchar()
   {
   return __io_getchar();
   }
