/*
** ===================================================================
** Název souboru   : jk_xtea.c
** Mikrokontroler  : ATmega128, ATmega16,...
** Popis           : Funkce sifrovani vyuzivajici algoritmus XTEA s 128bit klicem a overenim antireplay
**                 :  
** Autor           : Jan Kristel (kristel_ml@volny,cz)
**                 : xx.04.2014
** ===================================================================
*/

#define AVR_AT90USB162
#define F_CPU 8000000UL
#include "RFM12Bx.h"


#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h> 
#include <util/delay.h>
#include <string.h>





 void portInit() {
    HI(CS);
    HI(SDI);
    LO(SCK);
    DDRB |= (1<<CS) | (1<<SDI) | (1<<SCK);
	DDRD &= ~ (1<<NIRQ);  // pro jistotu vstup
  }






  unsigned int writeCmd(unsigned int cmd) {
    unsigned char i;
    unsigned int recv;
    recv = 0;
    LO(SCK);
    LO(CS);
    for(i=0; i<16; i++) {
      if(cmd&0x8000) HI(SDI); else LO(SDI);
      HI(SCK);
      recv<<=1;
      if( PINB&(1<<SDO) ) {
        recv|=0x0001;
      }
      LO(SCK);
      cmd<<=1;
    }
    HI(CS);
    return recv;
  }


  void rfInit() {
    writeCmd(0x80E7); //EL,EF,868band,12.0pF
 // writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC   --- nastavuje se ve fci
    writeCmd(0xA640); //frequency select
    writeCmd(0xC647); //4.8kbps
    writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm
    writeCmd(0xC2AC); //AL,!ml,DIG,DQD4
    writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR
    writeCmd(0xCED4); //SYNC=2DD4
    writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN
    writeCmd(0x9850); //!mp,90kHz,MAX OUT
    writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDITCBW0
    writeCmd(0xE000); //NOT USED
    writeCmd(0xC800); //NOT USED
    writeCmd(0xC040); //1.66MHz,2.2V
  }

void rftransmitter_mode()
{
	 writeCmd(0x8239) ;   // er,!ebb,ET,ES,EX,!eb,!ew,DC 
	 writeCmd(0x0000) ;
}


void rfreceiver_mode()
{
	writeCmd(0x8299) ;   // er,!ebb,ET,ES,EX,!eb,!ew,DC (bug was here)
  	writeCmd(0x0000) ;

  	rffifo_reset() ;	   // vymazání paměti FIFO pro dalí příjem dat
}
//------ukoncovaci  a startovaci paket ---
void rfpacket_end()
{		
      rfSend(0xAA); // DUMMY BYTES
      rfSend(0xAA);
      rfSend(0xAA);
}

void rfpacket_head()
{		
      writeCmd(0x0000);
      rfSend(0xAA); // PREAMBLE
      rfSend(0xAA);
      rfSend(0xAA);
      rfSend(0x2D); // SYNC
      rfSend(0xD4);
}
//------



//-----reset fifo---
void rffifo_reset()
{		
	writeCmd(0xCA81) ;
  	writeCmd(0xCA83) ;
}
//----------posle byte---

  void rfSend(unsigned char data){
    while(PIND&(1<<NIRQ));               //--- tady se meni PIN(mcu)
	_delay_us(100);
    writeCmd(0xB800 + data);
  }

//----- zajisti poslani celeho pole----  
 void rfSend_char(volatile char* byte){
	
	rfpacket_head();
    while( *byte != 0x00 ) {
	    rfSend(*byte);
		byte++;
		}
		
	rfpacket_end();
	
}	
  
  
  
//----------prijem ------- 
  
 unsigned char rfRecv() 
{
	unsigned int data;  
	while(PIND&(1<<NIRQ));
    // ceka na dokonceni prijmu

	 data = writeCmd(0x0000);
	 writeCmd(0x0000);
     if ( (data&0x8000) ) 
	   {
        data = writeCmd(0xB000);
        return (data&0x00FF);
	   }
} 

