/*********************************************
*
* RFM02 simple library
* 
* $HeadURL: file:///data/svnData/MLAB/Designs/duckweed_collector/SW/library/RF02/RF02.cpp $
* $Id: RF02.cpp 2130 2011-08-18 19:35:37Z kakl $
*
* code based on the code of "benedikt k." and "contrechoc" 
*
* tested on arduino 22
*
*********************************************/

#include <avr/io.h>
#include <stdlib.h>
 
 
#include "rf02.h"
#include <util/delay.h>

#define F_CPU 16000000UL
 
#define RF_PORT PORTB
#define RF_DDR  DDRB
#define RF_PIN  PINB

#define SDI             0        // SDI,  -> RF02   Atmega PB0 Arduino 8
#define SCK             1       // SCK,  -> RF02   Atmega PB1 Arduino 9 
#define CS              2       // nSEL, -> RF02   Atmega PB2 Arduino 10 
#define IRQ             4       // nIRQ, <- RF02   Atmega PB4 Arduino 12
//------------------// FSK: Pullupto VCC

#ifndef cbi
#define cbi(sfr, bit)     (_SFR_BYTE(sfr) &= ~_BV(bit)) 
#endif
#ifndef sbi
#define sbi(sfr, bit)     (_SFR_BYTE(sfr) |= _BV(bit))  
#endif

unsigned char _tx_buffer[32];

void rf02_prepAll()
{
        RF_PORT=(1<<CS);
        RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);

        for (unsigned char i=0; i<15; i++) _delay_ms(10);                       // wait until POR done

        rf02_trans(0xCC00);  // cargo cult settings

        rf02_trans(0x8000|0x1000|0x70|0x02); 
        rf02_trans(0xA000|0x640);

//      rf02_trans(0xD2C0);             // 33% PLL current
//      rf02_trans(0xC823); // 9600 Bd
        rf02_trans(0xD040);             // RATE/2
        rf02_trans(0xC811); // 19200 => 9600 Bd

        rf02_trans(0xC0E0);                     // power settings

        rf02_trans(0xC220);                     //0xC2A0
}

void rf02_trans(unsigned short  value)
{       
  uint8_t i;

        cbi(RF_PORT, CS);

        for (i=0; i<16; i++)
        {       if (value&0x8000) //0x8000
                        sbi(RF_PORT, SDI);
                else
                        cbi(RF_PORT, SDI);

                sbi(RF_PORT, SCK);
                value<<=1;
                _delay_us(0.3);
                cbi(RF_PORT, SCK);
        }
        sbi(RF_PORT, CS);
}


void rf02_txdata( unsigned char * data, uint8_t number)
{       
        uint8_t i,value;
        value=0xC6;          //1100 0110
        cbi(RF_PORT, CS);   //nSel

        for (i=0; i<8; i++)
                {       if (value&0x80)   //1000 0000 = 80
                                sbi(RF_PORT, SDI);
                        else
                                cbi(RF_PORT, SDI);

                        sbi(RF_PORT, SCK);
                        sbi(RF_PORT, SCK);
                        sbi(RF_PORT, SCK);
                        value<<=1;
                        cbi(RF_PORT, SCK);
                }

        rf02_shiftout(0xAA);//10101010    // RX AGC initiation
        rf02_shiftout(0xAA);
        rf02_shiftout(0xAA);
        rf02_shiftout(0x2D);//00101101    // preamble
        rf02_shiftout(0xD4);//11010100
        
        for (i=0; i<number; i++)     // payload
                rf02_shiftout(*data++);

        rf02_shiftout(0xAA); // dummy byte

        sbi(RF_PORT, CS);
        while(RF_PIN&(1<<IRQ));         // wait until transfer done
        rf02_trans(0xC464);                             // TX off after 10us
}

void rf02_shiftout(unsigned char value)
{       uint8_t j;
        for (j=0; j<8; j++)
        {       while(RF_PIN&(1<<IRQ));
        while(!(RF_PIN&(1<<IRQ)));

                if (value&128)             //100101000
                {
                sbi(RF_PORT, SDI); 
    }
        else
    {
                cbi(RF_PORT, SDI);
    }
        value<<=1;
    }
}