
   #define TX_MODE   1
   #define RX_MODE   2
   
   #define FREQ_434_MHZ   1
   #define FREQ_868_MHZ   2
   #define FREQ_915_MHZ   3   
   
   #define PACKET_LENGTH   2
     
   #define DummyDelay(x,y)    \
                  for (i = 0; i < x; i++) \
                  for (j = 0; j < y; j++);

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

/*
 RFM12_0000_STATUS_READ
 BIT  15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
      0  X  X  X  X  X  X X X X X X X X X X 
      
      This is result of status read
*/
#define RGITFFIT         0x8000      // TX ready for next byte or FIFO received data Status
                                                // depends on mode of transmitter
                                 
#define POR              0x4000      // Power on Reset Status
#define RGURFFOV         0x2000      // TX Register underun or RX FIFO Overflow Status
                                                // depends on mode of transmitter

#define WKUP             0x1000      // Wakeup Timer overflow Status
#define INTEXT           0x0800      // Interrup on external source Status
#define LBD              0x0400      // Low battery detect Status
#define FFEM             0x0200      // FIFO Empty Status
#define ATS              0x0100      // Antenna Tuning Signal Detect Status
#define RSSI             0x0080      // Received Signal Strength Indicator Status
#define DQD              0x0040      // Data Quality Dedector Status
#define CRL              0x0020      // Clock Recovery Locked status
#define ATGL             0x0010      // Toggling in each AFC Cycle
#define OFFS_SIGN        0x0008      // Measured Offset Frequency Sign Value 1='+', 0='-' 
#define OFFS             0x0004      // Measured offset Frequency value (3 bits) 
#define OFFS_MASK        0x0003      // Measured offset mask

#define isRGITFFIT      (1 << RGIT_FFIT)
#define isPOR           (1 << POR)
#define isRGURFFOV      (1 << RGUR_FFOV)
#define isWKUP          (1 << WKUP)
#define isEXT           (1 << INTEXT)
#define isLBD           (1 << LBD)
#define isFFEM          (1 << FFEM)
#define isATS           (1 << ATS)
#define isRSSI          (1 << RSSI)
#define isDQD           (1 << DQD)
#define isCRL           (1 << CRL)
#define isATGL          (1 << ATGL)

//________________________________________________________________________

// from Remote-sensor-2.0
/***********************************************************
   1. Configuration Setting Command
***********************************************************/

#define RF12_CFG_SETTING 0x80
#define RF12_EL _BV(7)      // enables the internal data register for TX Buffered Data Transmission
#define RF12_EF _BV(6)      // enables FIFO mode. If EF=0 DATA and DCLK clock are used for output
#define RF12_315MHz   (0<<4)
#define RF12_433MHz   (1<<4)
#define RF12_868MHz (2<<4)
#define RF12_915MHz (3<<4)
#define RF12_LOAD_C(v) ((char)((v-8.5)*2))   //crystal load capacitance 8.5pF to 16.0pF

/***********************************************************
   2. Power Managment Command
***********************************************************/

//Anmerkung: ich gehe davon aus dass PLL synonym für "RF synthesizer" verwendet wird

#define RF12_POWER_SETTING 0x82
#define RF12_ER  _BV(7)      //Enable the whole receiver chain. (RF synthesizer, RF front end, baseband circuits, oscillator)
#define RF12_EBB _BV(6)      //Enable baseband circuits only
#define RF12_ET  _BV(5)      //Enable the whole transmitter chain (PowerAmplifier, synthesizer, oscillator) and starts TX
#define RF12_ES  _BV(4)      //Enables the PLL/Synthesizer
#define RF12_EX  _BV(3)      //Enables the oscillator
#define RF12_EB  _BV(2)      //Enables the low battery detector
#define RF12_EW  _BV(1)      //Enables the wake-up timer

/***********************************************************
   3. Frequency Setting Command
***********************************************************/
//The 12-bit parameter F (bits f11 to f0) should be in the range of 96 and 3903. When F value sent is out of
//range, the previous value is kept. The synthesizer band center frequency f0 can be calculated as:
//f0 = 10 * C1 * (C2 + F/4000) [MHz]
//The constants C1 and C2 are determined by the selected band as:
//315MHz Band C1=1, C2=31
//433MHz Band C1=1, C2=43
//868MHz Band C1=2, C2=43
//915MHz Band C1=3, C2=30

#define RF12_FREQ_SETTING_HIGH(bits) (0xA0 | (bits>>8))
#define RF12_FREQ_SETTING_LOW(bits) (bits & 0xFF)

#define RF12_FREQ_315Band(v) (uint16_t)((v/10.0-31)*4000)
#define RF12_FREQ_433Band(v) (uint16_t)((v/10.0-43)*4000)
#define RF12_FREQ_868Band(v) (uint16_t)((v/20.0-43)*4000)
#define RF12_FREQ_915Band(v) (uint16_t)((v/30.0-30)*4000)

/***********************************************************
   4. Data Rate Command
***********************************************************/

//R= (10000 / 29 / (1+cs*7) / BR) – 1
//BR = 10000 / 29 / (R+1) / (1+cs*7) [kbps]BR = 10000 / 29 / (R+1) / (1+cs*7) [kbps]

//some predefines
#define RF12_4800 71
#define RF12_9600 35
#define RF12_19200 17
#define RF12_38400 8
#define RF12_57600 5

#define RF12_DATA_RATE 0xC6
#define RF12_DATA_RATE_CS _BV(7)

/***********************************************************
   5. Receiver Control Command
***********************************************************/

#define RF12_RECEIVER_CONTROL(bits) (0x90 | bits)

#define RF12_BW_400 (1<<5)
#define RF12_BW_340 (2<<5)
#define RF12_BW_270 (3<<5)
#define RF12_BW_200 (4<<5)
#define RF12_BW_134 (5<<5)
#define RF12_BW_67  (6<<5)

#define RF12_P20_INT 0
#define RF12_P20_VDI _BV(2)

#define RF12_VDI_FAST   (0)
#define RF12_VDI_MEDIUM (1)
#define RF12_VDI_SLOW   (2)
#define RF12_VDI_ON     (3)

#define RF12_LNA_0dB (0<<3)
#define RF12_LNA_6dB (1<<3)
#define RF12_LNA_14dB (2<<3)
#define RF12_LNA_20dB (3<<3)

#define RF12_RSSI_103dBm  (0)
#define RF12_RSSI_97dBm   (1)
#define RF12_RSSI_91dBm   (2)
#define RF12_RSSI_85dBm   (3)
#define RF12_RSSI_79dBm   (4)
#define RF12_RSSI_73dBm   (5)
#define RF12_RSSI_67dBm   (6)
#define RF12_RSSI_61dBm   (7)

/***********************************************************
   6. Data Filter Command
***********************************************************/

#define RF12_DATA_FILTER 0xC2
#define RF12_AUTOLOCK (_BV(7) | _BV(5) | _BV(3))
#define RF12_FASTMODE (_BV(6) | _BV(5) | _BV(3))
#define RF12_ANALOG_FILTER (_BV(4) | _BV(5) | _BV(3))
#define RF12_DQD_THRESHOLD(bits) (bits | _BV(5) | _BV(3))

/***********************************************************
   7. FIFO and Reset Mode Command
***********************************************************/

#define RF12_FIFO_RESET 0xCA
#define RF12_FIFO_IT_LEVEL(bits) (bits<<4)
#define RF12_FIFO_FILL(bit) (bit<<2)
#define RF12_FF _BV(1)      //FIFO fill after synchron pattern reception
//To restart the synchron pattern recognition, this bit should be cleared and set
#define RF12_DR _BV(0)      //Disables the highly sensitive RESET mode.
//If this bit is cleared, a 200 mV glitch in the power supply may cause a system reset.


/***********************************************************
   8. Receiver FIFO Read Command
***********************************************************/

#define RF12_FIFO_READ 0xB0

/***********************************************************
   9. AFC Command
***********************************************************/

#define RF12_AFC 0xC4
#define RF12_AFC_en _BV(0)   // Enables the calculation of the offset frequency by the AFC circuit
#define RF12_AFC_oe _BV(1)   // Enables the frequency offset register. It allows the addition of the offset register to
// the frequency control word of the PLL
#define RF12_AFC_fi _BV(2)   // Switches the circuit to high accuracy (fine) mode. In this case,
// the processing time is about twice longer, but the measurement uncertainty is about the half
#define RF12_AFC_st _BV(3)   // Stobe edge, when st goes to high, the actual latest calculated frequency error
// is stored into the offset register of the AFC block

#define RF12_AFC_RANGE_LIMIT(v) (v<<4)
// Limits the value of the frequency offset register to the next values:
// 0: No restriction
// 1: +15 fres to -16 fres
// 2: +7 fres to -8 fres
// 3: +3 fres to -4 fres
// fres=2.5 kHz in 315MHz and 433MHz Bands
// fres=5.0 kHz in 868MHz Band
// fres=7.5 kHz in 915MHz Band

#define RF12_AFC_AUTO(v) (v<<6)
// 0: Auto mode off (Strobe is controlled by microcontroller)
// 1: Runs only once after each power-up
// 2: Keep the foffset only during receiving(VDI=high)
// 3: Keep the foffset value independently trom the state of the VDI signal

/***********************************************************
   10. TX Config Control Command
***********************************************************/

#define RF12_TX_CFG 0x98

/***********************************************************
   11. Transmitter Register Write Command
***********************************************************/

#define RF12_TRANSMIT 0xB8

/***********************************************************
   12. Wake-Up Timer Command
      (not fully implemented)
   see page 22 hope-rf rf12.pdf
***********************************************************/
#define RF12_WAKE_UP(R) (0xE0 | R)
//The wake-up time period can be calculated by t_wake_up=M * 2^R [ms]

/***********************************************************
   13. Low Duty.Cycle Command
      (not fully implemented),
   see page 22 hope-rf rf12.pdf
***********************************************************/

#define RF12_DUTY_CYCLE(r0) (0xC8 | r0)

/**********************************************************************
   14. Low Battery Detector and Microcontroller Clock Divider Command
**********************************************************************/

#define RF12_LOW_BAT_AND_CLK_DIV 0xC0
#define RF12_LOW(v)   (v)
//5 bit represents the value, which defines the threshold voltage of the detector
// 0:  2.2V
// 1:  2.3V
// ...
// 31: 5.3V
#define RF12_DIV(v) (v<<5)
// 0: 1    MHz Clock Output Frequency
// 1: 1.25 MHz Clock Output Frequency
// 2: 1.66 MHz Clock Output Frequency
// 3: 2    MHz Clock Output Frequency
// 4: 2.5  MHz Clock Output Frequency
// 5: 3.33 MHz Clock Output Frequency
// 6: 5    MHz Clock Output Frequency
// 7: 10   MHz Clock Output Frequency


//______________________________________________________________________


   /* Initialize all aspects of the radio module */
   void RFM12BInit(int frequency, int mode);
   
   /* SPI initialization */
   void SPIInit();
   
   /* RFM12B Module Initialization */
   void RFM12BModuleInit(int frequency, int mode);
   
   /* Reset RFM12B RX FIFO */
   void RFM12BFIFOReset();
   
   /* Send a command to the RFM12B module */
   unsigned long int RFM12BWriteCommand(unsigned long int command);
   
   /* Send a transmit command to the RFM12B module */
   void RFM12BSendMessage(unsigned int message);
   
   /* Transmit a whole packet */
   void RFM12BSendPacket(unsigned char packet[], unsigned int length);
   
   /* Receive one byte */
   unsigned long int RFM12BReceiveByte();
   
