1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include "global.h"
|
4 | #include "rf01.h"
|
5 |
|
6 | #define F_CPU 16000000UL
|
7 | #include <util/delay.h>
|
8 |
|
9 | #define RF_PORT PORTB
|
10 | #define RF_DDR DDRB
|
11 | #define RF_PIN PINB
|
12 |
|
13 | #define SDI 5 // PB5 SDI, -> RF01
|
14 | #define SCK 3 // PB3 SCK, -> RF01
|
15 | #define CS 2 // PB2 nSEL, -> RF01
|
16 | #define SDO 0 // PB0 SDO, <- RF01
|
17 |
|
18 | // nFFS: 1-10k Pullup an Vcc !!!
|
19 |
|
20 | static unsigned char sdrssi, sgain;
|
21 |
|
22 | void rf01_trans(unsigned short wert)
|
23 | { unsigned char i;
|
24 |
|
25 | cbi(RF_PORT, CS);
|
26 | for (i=0; i<16; i++)
|
27 | { if (wert&32768)
|
28 | sbi(RF_PORT, SDI);
|
29 | else
|
30 | cbi(RF_PORT, SDI);
|
31 | sbi(RF_PORT, SCK);
|
32 | wert<<=1;
|
33 | _delay_us(0.2);
|
34 | cbi(RF_PORT, SCK);
|
35 | }
|
36 | sbi(RF_PORT, CS);
|
37 | }
|
38 |
|
39 | void rf01_init(void)
|
40 | { unsigned char i;
|
41 | RF_PORT=(1<<CS);
|
42 | RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
|
43 |
|
44 | for (i=0; i<16; i++)
|
45 | _delay_ms(10); // wait until POR done
|
46 |
|
47 | rf01_trans(0xC2E0); // AVR CLK: 10MHz
|
48 | rf01_trans(0xC42B); // Data Filter: internal
|
49 | rf01_trans(0xCE88); // FIFO mode
|
50 | rf01_trans(0xC6F7); // AFC settings: autotuning: -10kHz...+7,5kHz
|
51 | rf01_trans(0xE000); // disable wakeuptimer
|
52 | rf01_trans(0xCC00); // disable low duty cycle
|
53 | }
|
54 |
|
55 | void rf01_setbandwidth(unsigned char bandwidth)
|
56 | {
|
57 | rf01_trans(0x9170|((bandwidth&7)<<1)); //auf 868 Mhz umgestellt, früher stand da: rf01_trans(0x8970|((bandwidth&7)<<1));
|
58 | }
|
59 |
|
60 | void rf01_setreceiver(unsigned char gain, unsigned char drssi)
|
61 | {
|
62 | sdrssi=drssi;
|
63 | sgain=gain;
|
64 | }
|
65 |
|
66 | void rf01_setfreq(unsigned short freq)
|
67 | { if (freq<96) // 860,24 MHz; früher stand hier: 430,2400MHz
|
68 | freq=96;
|
69 | else if (freq>3903) // 869,7575 MHz; früher stand hier: 439,7575MHz
|
70 | freq=3903;
|
71 | rf01_trans(0xA000|freq);
|
72 | }
|
73 |
|
74 | void rf01_setbaud(unsigned short baud)
|
75 | {
|
76 | if (baud<336)
|
77 | return;
|
78 | if (baud<5400) // Baudrate= 344827,58621/(R+1)/(1+CS*7)
|
79 | rf01_trans(0xC880|((43104/baud)-1));
|
80 | else
|
81 | rf01_trans(0xC800|((344828UL/baud)-1));
|
82 | }
|
83 |
|
84 | void rf01_rxdata(unsigned char *data, unsigned char number)
|
85 | { unsigned char i,j,c;
|
86 |
|
87 | rf01_trans(0xC0C1|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX on
|
88 | rf01_trans(0xCE89); // set FIFO mode
|
89 | rf01_trans(0xCE8B); // enable FIFO
|
90 | cbi(RF_PORT, SDI);
|
91 | for (i=0; i<number; i++)
|
92 | { cbi(RF_PORT, CS);
|
93 | while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO
|
94 | for (j=0; j<16; j++) // read and discard status register
|
95 | { sbi(RF_PORT, SCK);
|
96 | asm("nop");
|
97 | cbi(RF_PORT, SCK);
|
98 | }
|
99 | c=0;
|
100 | for (j=0; j<8; j++)
|
101 | { c<<=1;
|
102 | if (RF_PIN&(1<<SDO))
|
103 | c|=1;
|
104 | sbi(RF_PORT, SCK);
|
105 | _delay_us(0.2);
|
106 | cbi(RF_PORT, SCK);
|
107 | }
|
108 | *data++=c;
|
109 | sbi(RF_PORT, CS);
|
110 | }
|
111 | rf01_trans(0xC0C0|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX off
|
112 | }
|