/******************************************************************
*  Copyright (C) 2009 Ram Narula  
*  Filename: compatArduino.cpp
*  Version: 0.11
*  Date: 16 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"
#include "definePin.h"
#include "compatArduino.h"

ArdSerial::ArdSerial(u8 port_) : port(port_)
{
}


void ArdSerial::begin(u32 Baudrate_)
{
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;
  
  
  // 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 ArdSerial::print(u16 w)  
{
  while (!(USART1->SR & USART_FLAG_TXE));
  return(USART1->DR = (w & 0x1FF)); //upto 9 bits
}

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

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

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

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

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



void pinMode(u8 Pxn,GPIOMode_TypeDef GPIOMode)
{
	definePin	myPin=definePin(Pxn);
	doGPIO		myGPIOPin=doGPIO(myPin.GPIObank,myPin.GPIOPin,GPIOMode);
}

void digitalWrite(u8 Pxn,bool data)
{
	definePin	myPin=definePin(Pxn);
	(data)?myPin.GPIObank->BSRR|=myPin.GPIOPin:myPin.GPIObank->BRR|=myPin.GPIOPin;
}

bool digitalRead(u8 Pxn)
{
	definePin	myPin=definePin(Pxn);
	return (bool)(myPin.GPIObank->IDR & myPin.GPIOPin);
}
	

/*
definePin::definePin(u8 Pxn)
{

switch ((Pxn & 0xF0) >> 4)		//AND with 11110000 and right shift 4 bits
{						
	case 1:
		GPIObank=GPIOA;
		break;
	case 2:
		GPIObank=GPIOB;
		break;
	case 3:
		GPIObank=GPIOC;
		break;
	case 4:
		GPIObank=GPIOD;
		break;
	case 5:
		GPIObank=GPIOE;
		break;
	case 6:
		GPIObank=GPIOF;
		break;
	case 7:
		GPIObank=GPIOG;
		break;
}

switch (Pxn & 0x0F)				//AND with 00001111 to get the first 4 bits Pin number
{
	case 0:
		GPIOPin=GPIO_Pin_0;
		break;
	case 1:
		GPIOPin=GPIO_Pin_1;
		break;
	case 2:
		GPIOPin=GPIO_Pin_2;
		break;
	case 3:
		GPIOPin=GPIO_Pin_3;
		break;
	case 4:
		GPIOPin=GPIO_Pin_4;
		break;
	case 5:
		GPIOPin=GPIO_Pin_5;
		break;
	case 6:
		GPIOPin=GPIO_Pin_6;
		break;
	case 7:
		GPIOPin=GPIO_Pin_7;
		break;
	case 8:
		GPIOPin=GPIO_Pin_8;
		break;
	case 9:
		GPIOPin=GPIO_Pin_9;
		break;
	case 10:
		GPIOPin=GPIO_Pin_10;
		break;
	case 11:
		GPIOPin=GPIO_Pin_11;
		break;
	case 12:
		GPIOPin=GPIO_Pin_12;
		break;
	case 13:
		GPIOPin=GPIO_Pin_13;
		break;
	case 14:
		GPIOPin=GPIO_Pin_14;
		break;
	case 15:
		GPIOPin=GPIO_Pin_15;
		break;
	default:
		break;
}			

}

*/
/*
char *s=&Px[2];
int pin;


pin= *s - 48; s++; 							//0 char value is 48
if(*s) { pin=( pin * 10 )+( *s - 48); }		//get second digit if any

*/