/******************************************************************
*  Copyright (C) 2009 Ram Narula  
*  Filename: Serial.cpp
*  Version: 0.11
*  Date: 14 July 2009
*  Information: http://www.xduino.com/    
******************************************************************/
/******************************************************************
*   This file is part of Xduino
*
*   Xduino is free software: you can redistribute it and/or modify
*   it under the terms of the GNU Lesser General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*	
*   Xduino is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public License
*   along with Xduino.  If not, see .
******************************************************************/

extern "C" {
#include "stm32f10x_lib.h"
}
#include "Serial.h"
#include "doGPIO.h"


Serial::Serial(u8 port_,u32 Baudrate_) : port(port_),Baudrate(Baudrate_)
{
USART_TypeDef* UxARTx;

//Initialize RCC and GPIO modes for both Rx and Tx pins
  switch(port) 
  {
	case 1: 
		UxARTx=USART1;
	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

		doGPIO(GPIOA,GPIO_Pin_10,GPIO_Mode_IN_FLOATING);
		doGPIO(GPIOA,GPIO_Pin_9,GPIO_Mode_AF_PP); 
		break;
	case 2: 
		UxARTx=USART2;
	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

		doGPIO(GPIOA,GPIO_Pin_3,GPIO_Mode_IN_FLOATING);
		doGPIO(GPIOA,GPIO_Pin_2,GPIO_Mode_AF_PP);
		break;
	case 3: 
		UxARTx=USART3;
	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

		doGPIO(GPIOB,GPIO_Pin_11,GPIO_Mode_IN_FLOATING);
		doGPIO(GPIOB,GPIO_Pin_10,GPIO_Mode_AF_PP);
		break;
	case 4: 
		UxARTx=UART4;
	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

		doGPIO(GPIOC,GPIO_Pin_11,GPIO_Mode_IN_FLOATING);
		doGPIO(GPIOC,GPIO_Pin_10,GPIO_Mode_AF_PP);
		break;
	case 5: 
		UxARTx=UART5;
	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);

		doGPIO(GPIOD,GPIO_Pin_2,GPIO_Mode_IN_FLOATING);
		doGPIO(GPIOC,GPIO_Pin_12,GPIO_Mode_AF_PP);
		break;
	default:
		UxARTx=USART1;
  }

  	
  USART_InitTypeDef USART_InitStructure;
  
  /*RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);		// start GPIO clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);	// start UxART clock

  doGPIO(GPIOA,GPIO_Pin_10,GPIO_Mode_IN_FLOATING);		// USART1 Rx GPIOA pin 10 
  doGPIO(GPIOA,GPIO_Pin_9,GPIO_Mode_AF_PP);				// USART1 Tx GPIOA Pin 9 */
  
  // USART1 Baudrate 8N1 no hardware flow control
  USART_InitStructure.USART_BaudRate = Baudrate;
  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_CPOL = USART_CPOL_Low;
//  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
//  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

  USART_Init(UxARTx, &USART_InitStructure);
  USART_Cmd(UxARTx, ENABLE); // and finally start USART1

}

u16 Serial::print(u16 w)  
{
  while (!(USART1->SR & USART_FLAG_TXE));
  return(USART1->DR = (w & 0x1FF)); //upto 9 bits
}

void Serial::print(char *s)
{
	while(*s)   // Check for end of string
	{
      print(*s++);
   	}
}

void Serial::println(char *s)
{
	print(s);
	print("\r\n");
}

char Serial::read(void)  
{
  while (!(USART1->SR & USART_FLAG_RXNE));
  return (USART1->DR & 0xFF); //upto 8 bits
}

u16 Serial::getdata(void)  
{
  while (!(USART1->SR & USART_FLAG_RXNE));
  return ((u16)(USART1->DR & 0x1FF)); //upto 9 bits
}

bool Serial::available(void)  
{
  return (bool)(USART1->SR & USART_FLAG_RXNE);
}