/*
 * uart_test.c
 *
 *  Created on: 10.09.2012
 *      Author: Gromph
 */
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "eddy/General.h"
#include "eddy/RS232.h"
#include <util/delay.h>

void flash (uint8_t farbe)
{
	DDRB|=(0b00001111);	//PORTB 0-3 als Ausgang
	PORTB|=15;			//LED's aus durch high setzen
	PORTB &= ~(farbe);  //LED an durch Low
	_delay_ms(200);
	PORTB|=farbe;
	_delay_ms(200);
}
//******************************
/*
** Baud Rate für die Serielle Schnittstelle
*/
#define BAUD_RATE		9600
/*
** Where the byte via UART Interrupt will be saved
** Use 'volatile', otherwise, data is lost
*/
volatile uint8_t	UART_Byte;
volatile uint8_t	UART_Byte_Status;
/*
** Define a stream for the printf function, using the UART
*/
FILE uart_str = FDEV_SETUP_STREAM(RS232_putchar, NULL, _FDEV_SETUP_RW);
/*******************************************************
 Function: RS232_Init

 Purpose: Initialise the RS232 serial link

 Input Parameter: None

 Return Value: None

*******************************************************/
void RS232_Init (void)
	{
/*
** Enable TXEN and RXEN in register UCSRB
*/
	UCSR0B |= (1 << TXEN0);
	UCSR0B |= (1 << RXEN0);
/*
** Set transmission type, Asynchron 8N1
*/
	UCSR0C |= (1 << UMSEL00)|(3<<UCSZ00);
/*
** Set baud rate
*/
    UBRR0H = (uint8_t) ((F_CPU / (BAUD_RATE * 16L) - 1)>>8);
    UBRR0L = (uint8_t) (F_CPU / (BAUD_RATE * 16L) - 1);
/*
** Open a channel for printf
*/
	stdout = &uart_str;
//	fdevopen (RS232_putchar, NULL);
/*
** Set the UART CMD bytes
*/
	UART_Byte = 0;
	UART_Byte_Status = 0;
	}
/*******************************************************
 Function: RS232_putchar

 Purpose: Initialise function "printf "

 Input Parameter: char buffer

 Return Value:
 	Value = 0

*******************************************************/
int RS232_putchar (char c, FILE *stream)
	{
/*
** Check on NEWLINE
*/
	if (c == '\n')
  		{
		RS232_putchar ('\r', stream);
		}
/*
** Wait until previous character is sent
*/
	loop_until_bit_is_set(UCSR0A, UDRE0);
/*
** Send character to UART
*/
	UDR0 = c;

	return 0;
	}
/*******************************************************
 Function: RS232_PutByte

 Purpose: Send to the UART a data byte

 Input Parameter: the byte

 Return Value: None
 	Value = 0

*******************************************************/
void RS232_PutByte (uint8_t Byte)
	{
/*
** Wait until previous character is sent
*/
	while (!(UCSR0A & (1<<UDRE0)));
/*
** Send byte to UART
*/
	UDR0 = Byte;

	return;
	}
/*******************************************************
 Function: RS232_Put_uint16

 Purpose: Send a two byte word (uin16_t) to the UART, LSB first

 Input Parameter: Address of uint16_t

 Return Value: None

*******************************************************/
void RS232_Put_uint16 (uint16_t *Word)
	{
	uint8_t 	i;
	uint8_t		*py;

	py = (uint8_t*) Word;
	for (i=0;i<2;i++)
		{
		RS232_PutByte (*py++);
		}

	return;
	}
/*******************************************************
 Function: RS232_Put_uint32

 Purpose: Send a four byte word (uin32_t) to the UART, LSB first

 Input Parameter:  Address of uint32_t

 Return Value: None

*******************************************************/
void RS232_Put_uint32 (uint32_t *DWord)
	{
	uint8_t 	i;
	uint8_t		*py;

	py = (uint8_t*) DWord;

	for (i=0;i<4;i++)
		{
		RS232_PutByte (*py++);
		}

	return;
	}
/*******************************************************
 Function: RS232_GetByte

 Purpose: Get from the UART a byte

 Input Parameter: None

 Return Value: The UART byte

*******************************************************/
uint8_t RS232_GetByte (void)
	{
/*
** Wait until byte is available
*/
	while (!(UCSR0A &(1<<RXC0)));
/*
** Get and return the byte from UART
*/
	return UDR0;
	}


int main (void)
{
	uint8_t tmp;
	uint16_t tmp1;
	while (1)
	{
		tmp = 202;
		tmp1 = 403;
		printf ("tmp = %d\n", tmp);
		RS232_Put_uint16 (tmp1);
		flash(1);
	}

}
