/****************************************************/
/*	Written by Wolfgang Wierczeyko					*/
/*													*/
/* 	Programm zum Erzeugen eines RC- Summensignals   */
/* 	                           						*/
/*	Spi Interface an Kompass MM3            		*/
/*	Uart: kommunikation mit PC						*/
/*  Prozessor:: ATmega8 @ 8 MHz						*/


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




#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>

#define SPI_RESET 1
#define SPI_TRANSFER 2
#define SPI_WAIT_TRANSFER 3
#define SPI_WAIT_DRDY 4
#define SPI_DRDY1 5
#define SPI_DRDY2 6

#define X_ACHSE 1
#define Y_ACHSE 2
#define Z_ACHSE 3


/* Altes was man gewöhnt ist aber langsam verdrängt*/
#define sbi(ADDRESS,BIT) ((ADDRESS) |= (1<<(BIT)))
#define cbi(ADDRESS,BIT) ((ADDRESS) &= ~(1<<(BIT)))
#define outp(VAL,ADRESS) ((ADRESS) = (VAL))

volatile uint8_t Servo, i, interrupt_outcompare1b;
volatile uint8_t spi_status, mm3_achse, wait_zyklus;
volatile uint8_t toggle = 0;
volatile uint16_t Wert[10], mm3_wert, x_wert, y_wert, z_wert;
/**********************************************************************/
SIGNAL( SIG_SPI)
{
	switch (spi_status)
	{
		case SPI_DRDY1:				/* 1. Byte holen */
			mm3_wert = SPDR;
			SPDR = 0x00;			/* ". Übertragung auslösen */
			spi_status = SPI_DRDY2;
			goto LEAF1;
		
		case SPI_DRDY2:				/* 2. Byte holen */
			mm3_wert <<= 8;			/* Ins HighByte schieben */
			mm3_wert |= SPDR;
			
			/*Werte den Achsen zuweisen und Achse weiterschalten*/ 
			switch ( mm3_achse )
			{
				case X_ACHSE:
					x_wert = mm3_wert;
					mm3_achse = Y_ACHSE;
					break;
				case Y_ACHSE:
					y_wert = mm3_wert;
					mm3_achse = Z_ACHSE;
					break;

				case Z_ACHSE:
					z_wert = mm3_wert;
					mm3_achse = X_ACHSE;
					break;
			}
			spi_status = SPI_RESET;
	}
	LEAF1: asm("nop");		
}
/**********************************************************************/
SIGNAL( SIG_OVERFLOW0) /* Timer0 Mädchen für alles mögliche */
{
	toggle_PC3();
/* SPI***/
	switch (spi_status)
	{
		case SPI_RESET:
			PORTC |=(1<<PC1);						/* Reset auf High */
			spi_status = SPI_TRANSFER;
			goto LEAF;
		case SPI_TRANSFER:
			PORTC &=~(1<<PC1);						/* Reset auf Low */
			switch (mm3_achse)
			{
				case X_ACHSE:	SPDR = 0x31; break;
				case Y_ACHSE:	SPDR = 0x32; break;
				case Z_ACHSE:	SPDR = 0x33; break;
				
			}
			wait_zyklus = 16;
			spi_status = SPI_WAIT_TRANSFER;
			goto LEAF;
		case SPI_WAIT_TRANSFER:
			if ( wait_zyklus-- == 0) { SPDR = 0x00; spi_status = SPI_DRDY1; }
			break;
			
	}
	LEAF: asm ( "nop");

}

/**********************************************************************/
SIGNAL( SIG_OUTPUT_COMPARE1A )
{
	cbi(PORTC,0);					/* fallende Flanke nach 400µSec */
}
/**********************************************************************/
SIGNAL( SIG_OUTPUT_COMPARE1B )
{
	TCNT1  = 0;		/* Timer zurücksetzen */
	sbi(PORTC,0);	/* Steigende Flanke Summensignal*/

	OCR1B  = Wert[Servo]; 	/* Länge des Lowpegels für nächstes Signal festlegen*/
	Servo++;				/* Servos bis 9 durchshiften */
	if (Servo > 8) Servo = 0;
}
/**********************************************************************/
void init_timer()
{
/** Timer 0 ***/
	TCCR0 |= ( 1 << CS01);  /* Prescaler auf 8 */

	TIMSK |= ( 1 << TOIE0);	/* Overflow Int zulassen */
/** Timer 1 ***/
	TCCR1B = ( 1<< CS11);			/* Prescaler Timer1 auf 8 		*/
	OCR1A  = 400;					/* OutputCompare 400 microSec   */
	OCR1B  = 2000;					/* 								*/
	TIMSK  |= ( 1 << OCIE1A ) | ( 1 << OCIE1B ); /* Interrupts zulassen */
}
/**********************************************************************/
void init_spi()
{
	SPCR  = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); /* IntSPI ein, SPI Ein, Master, tcl/64 */
	DDRB  = (1<<PB3)|(1<<PB5);				/* SCK,Mosi auf Ausgang */
//	PORTB |= (1<< PB4);				
	PORTC &=~ (1<<PC2);						/* SS auf low */
	PORTC &=~(1<<PC1);						/* Reset auf Low */

	spi_status 	= SPI_RESET;				/* Start SPI für Kompass erstmal Reset */
	mm3_achse	= X_ACHSE;					/* Startachse ist X */
}

/**********************************************************************/
void init_uart()
{

	UBRRL = (uint8_t)25;		/* Baudrate auf 38.4		*/
	UBRRH = (uint8_t)0;		/*    """"""				*/

//	PORTD |= _BV(PD0); //Pullup aktivieren					

	UCSRB  = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); 	/* enable RX, TX, interrupt RX */
	UCSRC  = (1<<URSEL)|(1<<UCSZ1) | (1<<UCSZ0);/* 8 Data, No parity, 1 Stop  	*/
	UCSRA  = (1 << RXC) | (1 << TXC) | (1 << U2X);

}
/**********************************************************************/
void init_io()
{
	
	DDRC |= ((1<<PINC3)|(1<<PINC2)|(1<<PINC1)|(1<<PINC0));/* Pin 0..3 auf Ausgang 4..7 auf Eingang */
	PORTC |= ( 1<<PINC4);
}
/**********************************************************************/
void toggle_PC3() /* nur zum testen des timers */
{
	if( toggle++ == 0 )			/* toggle nach Vergleich inkrementieren */
		PORTC |= (1<<PC3);
	else
		PORTC &= ~(1<<PC3);

	if ( toggle > 1 ) toggle =0; /* nur Werte 0 und 1 zulassen */

}



void send_tx_0_test( unsigned char z) /* Ausgabe auf Uart */

{

	UDR = z;
}

/**********************************************************************/
void main()
{
	char S_Buffer[10],T_Buffer[]= "Teststring" ;
	char sende;
	int  AusgabeX,AusgabeY,AusgabeZ, i,l,u;


	init_io();					/* Ein-, Ausgänge initialisieren */
	init_timer();
	init_spi();					/* Timer initialisieren */
	init_uart();			
	
	for ( i=0; i < 8; i++ )		/* Servowerte vorbesetzen  Mittelstellung */
		Wert[i] = 1500;
	Wert[8] = 10000;			/* Austastlücke hier 10msec eventuell anpassen */
	Servo = 0;					/* Startservo 0 */
	PORTC |= 0xF1;				/* Summensignal auf LOW  Pullup aktiviereb*/

						
	TCNT1  = 0;					/* Timer1 zurücksetzen */

	sei();						/* Globale Interrupts zulassen */	

	while(1)					/* Endlosschleife */
	{

		if ( spi_status == SPI_RESET)
		{
			
			cli();
			AusgabeX = x_wert;
			AusgabeY = y_wert;
			AusgabeZ = z_wert;
			sei();

			itoa(AusgabeX,S_Buffer,10);
			l = strlen ( S_Buffer);
			for ( i=0; i<=l; i++) { send_tx_0_test( S_Buffer[i]); for ( u=0; u<1000;u++);}
			send_tx_0_test(' ');
			send_tx_0_test(';');

			itoa(AusgabeY,S_Buffer,10);
			l = strlen ( S_Buffer);
			for ( i=0; i<=l; i++) { send_tx_0_test( S_Buffer[i]); for ( u=0; u<1000;u++);}
			send_tx_0_test(' ');
			send_tx_0_test(';');

			itoa(AusgabeZ,S_Buffer,10);
			l = strlen ( S_Buffer);
			for ( i=0; i<=l; i++) { send_tx_0_test( S_Buffer[i]); for ( u=0; u<1000;u++);}
			send_tx_0_test(0x0d);
			send_tx_0_test(0x0a);
		}

	}		


}
