Rev Author Line No. Line
2112 kakl 1 /*********************************************
2 *
2126 kakl 3 * RFM02 simple library
4 *
2129 kakl 5 * $HeadURL: file:///data/svnData/MLAB/Designs/duckweed_collector/SW/library/RF02/RF02.cpp $
2128 kakl 6 * $Id: RF02.cpp 2130 2011-08-18 19:35:37Z kakl $
2112 kakl 7 *
2130 kakl 8 * code based on the code of "benedikt k." and "contrechoc"
2112 kakl 9 *
2130 kakl 10 * tested on arduino 22
2112 kakl 11 *
12 *********************************************/
13  
14 #include <avr/io.h>
15 #include <stdlib.h>
16  
17  
18 #include "rf02.h"
19 #include <util/delay.h>
20  
21 #define F_CPU 16000000UL
22  
23 #define RF_PORT PORTB
24 #define RF_DDR DDRB
25 #define RF_PIN PINB
26  
27 #define SDI 0 // SDI, -> RF02 Atmega PB0 Arduino 8
28 #define SCK 1 // SCK, -> RF02 Atmega PB1 Arduino 9
29 #define CS 2 // nSEL, -> RF02 Atmega PB2 Arduino 10
30 #define IRQ 4 // nIRQ, <- RF02 Atmega PB4 Arduino 12
31 //------------------// FSK: Pullupto VCC
32  
33 #ifndef cbi
34 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
35 #endif
36 #ifndef sbi
37 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
38 #endif
39  
2130 kakl 40 unsigned char _tx_buffer[32];
2112 kakl 41  
2125 kakl 42 void rf02_prepAll()
43 {
44 RF_PORT=(1<<CS);
45 RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
2112 kakl 46  
2125 kakl 47 for (unsigned char i=0; i<15; i++) _delay_ms(10); // wait until POR done
2112 kakl 48  
2130 kakl 49 rf02_trans(0xCC00); // cargo cult settings
2112 kakl 50  
2130 kakl 51 rf02_trans(0x8000|0x1000|0x70|0x02);
52 rf02_trans(0xA000|0x640);
2112 kakl 53  
2125 kakl 54 // rf02_trans(0xD2C0); // 33% PLL current
55 // rf02_trans(0xC823); // 9600 Bd
56 rf02_trans(0xD040); // RATE/2
57 rf02_trans(0xC811); // 19200 => 9600 Bd
2112 kakl 58  
2125 kakl 59 rf02_trans(0xC0E0); // power settings
60  
61 rf02_trans(0xC220); //0xC2A0
2112 kakl 62 }
63  
64 void rf02_trans(unsigned short value)
2125 kakl 65 {
66 uint8_t i;
2112 kakl 67  
68 cbi(RF_PORT, CS);
69  
70 for (i=0; i<16; i++)
71 { if (value&0x8000) //0x8000
72 sbi(RF_PORT, SDI);
73 else
74 cbi(RF_PORT, SDI);
75  
76 sbi(RF_PORT, SCK);
77 value<<=1;
78 _delay_us(0.3);
79 cbi(RF_PORT, SCK);
80 }
81 sbi(RF_PORT, CS);
82 }
83  
84  
85 void rf02_txdata( unsigned char * data, uint8_t number)
86 {
87 uint8_t i,value;
88 value=0xC6; //1100 0110
89 cbi(RF_PORT, CS); //nSel
90  
91 for (i=0; i<8; i++)
92 { if (value&0x80) //1000 0000 = 80
93 sbi(RF_PORT, SDI);
94 else
95 cbi(RF_PORT, SDI);
96  
97 sbi(RF_PORT, SCK);
2130 kakl 98 sbi(RF_PORT, SCK);
99 sbi(RF_PORT, SCK);
2112 kakl 100 value<<=1;
101 cbi(RF_PORT, SCK);
102 }
103  
2130 kakl 104 rf02_shiftout(0xAA);//10101010 // RX AGC initiation
2112 kakl 105 rf02_shiftout(0xAA);
106 rf02_shiftout(0xAA);
2130 kakl 107 rf02_shiftout(0x2D);//00101101 // preamble
2112 kakl 108 rf02_shiftout(0xD4);//11010100
109  
2130 kakl 110 for (i=0; i<number; i++) // payload
2112 kakl 111 rf02_shiftout(*data++);
112  
2130 kakl 113 rf02_shiftout(0xAA); // dummy byte
114  
2112 kakl 115 sbi(RF_PORT, CS);
116 while(RF_PIN&(1<<IRQ)); // wait until transfer done
117 rf02_trans(0xC464); // TX off after 10us
118 }
119  
120 void rf02_shiftout(unsigned char value)
121 { uint8_t j;
122 for (j=0; j<8; j++)
123 { while(RF_PIN&(1<<IRQ));
124 while(!(RF_PIN&(1<<IRQ)));
125  
126 if (value&128) //100101000
2125 kakl 127 {
128 sbi(RF_PORT, SDI);
129 }
2112 kakl 130 else
2125 kakl 131 {
2112 kakl 132 cbi(RF_PORT, SDI);
2125 kakl 133 }
2112 kakl 134 value<<=1;
135 }
136 }
137