2112 |
kakl |
1 |
/********************************************* |
|
|
2 |
* |
2131 |
kakl |
3 |
* RFM01 simple library |
|
|
4 |
* |
|
|
5 |
* $HeadURL: svn://www.mlab.cz/home/MLAB/Designs/duckweed_collector/SW/library/RF02/RF02.cpp $ |
|
|
6 |
* $Id: RF02.cpp 2129 2011-08-18 18:40:43Z kakl $ |
2112 |
kakl |
7 |
* |
2131 |
kakl |
8 |
* code based on the code of "benedikt k." and "contrechoc" |
2112 |
kakl |
9 |
* |
2131 |
kakl |
10 |
* tested on arduino 22 |
2112 |
kakl |
11 |
* |
|
|
12 |
*********************************************/ |
|
|
13 |
|
|
|
14 |
#include <avr/io.h> |
|
|
15 |
#include <stdlib.h> |
|
|
16 |
|
|
|
17 |
#include "rf01.h" |
|
|
18 |
|
|
|
19 |
#define F_CPU 16000000UL |
|
|
20 |
#include <util/delay.h> |
|
|
21 |
|
|
|
22 |
#define RF_PORT PORTB |
|
|
23 |
#define RF_DDR DDRB |
|
|
24 |
#define RF_PIN PINB |
|
|
25 |
|
|
|
26 |
#define SDI 5 // RF01 SDI, arduino 13 cannot be changed |
|
|
27 |
#define SCK 4 // RF01 SCK, arduino 12 cannot be changed |
|
|
28 |
#define CS 3 // RF01 nSEL, arduino 11 cannot be changed |
|
|
29 |
#define SDO 2 // RF01 SDO, arduino 10 cannot be changed |
2131 |
kakl |
30 |
//----------------- // RF01 niRQ, arduino 2 cannot be changed |
2112 |
kakl |
31 |
//------------------// RF01 nFFS: 1-10k Pullup too Vcc |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
#ifndef cbi |
|
|
35 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
|
|
36 |
#endif |
|
|
37 |
#ifndef sbi |
|
|
38 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
|
|
39 |
#endif |
|
|
40 |
|
2131 |
kakl |
41 |
unsigned char _rx_buffer[32]; // recv buf |
2112 |
kakl |
42 |
|
|
|
43 |
#include <util/delay.h> |
|
|
44 |
|
2125 |
kakl |
45 |
void rf01_prepAll() |
|
|
46 |
{ |
|
|
47 |
RF_PORT=(1<<CS); |
|
|
48 |
RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS); |
|
|
49 |
|
|
|
50 |
for (unsigned char i=0; i<11; i++) _delay_ms(10); // wait until POR done |
|
|
51 |
|
2139 |
kakl |
52 |
rf01_trans(0x0000); // cargo cult settings |
2125 |
kakl |
53 |
rf01_trans(0x8000|0x1000|0x70|0x02); //band |
2131 |
kakl |
54 |
rf01_trans(0xA640); // freq |
2125 |
kakl |
55 |
rf01_trans(0xC823); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 9600 Bd |
|
|
56 |
rf01_trans(0xC69B); |
|
|
57 |
rf01_trans(0xC42A); |
|
|
58 |
rf01_trans(0xC240); //* |
|
|
59 |
rf01_trans(0xC080); //* |
|
|
60 |
rf01_trans(0xCE88); // FIFO mode |
|
|
61 |
rf01_trans(0xCE8B); //* |
|
|
62 |
rf01_trans(0xC081); //* |
2112 |
kakl |
63 |
} |
|
|
64 |
|
|
|
65 |
void rf01_trans(unsigned short wert) |
|
|
66 |
{ unsigned char i; |
|
|
67 |
|
|
|
68 |
cbi(RF_PORT, CS); |
|
|
69 |
for (i=0; i<16; i++) |
|
|
70 |
{ if (wert&32768) |
|
|
71 |
sbi(RF_PORT, SDI); |
|
|
72 |
else |
|
|
73 |
cbi(RF_PORT, SDI); |
|
|
74 |
sbi(RF_PORT, SCK); |
2131 |
kakl |
75 |
sbi(RF_PORT, SCK); |
|
|
76 |
sbi(RF_PORT, SCK); |
2112 |
kakl |
77 |
wert<<=1; |
|
|
78 |
cbi(RF_PORT, SCK); |
|
|
79 |
} |
|
|
80 |
sbi(RF_PORT, CS); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
void rf01_rxdata(unsigned char *data, unsigned char number) |
|
|
84 |
{ unsigned char i,j,c; |
|
|
85 |
|
|
|
86 |
rf01_trans(0xCE89); // set FIFO mode |
|
|
87 |
rf01_trans(0xCE8B); // enable FIFO |
|
|
88 |
cbi(RF_PORT, SDI); |
2125 |
kakl |
89 |
asm("nop"); |
|
|
90 |
asm("nop"); |
|
|
91 |
asm("nop"); |
2112 |
kakl |
92 |
for (i=0; i<number; i++) |
|
|
93 |
{ cbi(RF_PORT, CS); |
2125 |
kakl |
94 |
asm("nop"); |
|
|
95 |
asm("nop"); |
|
|
96 |
asm("nop"); |
2112 |
kakl |
97 |
while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO |
|
|
98 |
for (j=0; j<16; j++) // read and discard status register |
2125 |
kakl |
99 |
{ |
|
|
100 |
sbi(RF_PORT, SCK); |
2112 |
kakl |
101 |
asm("nop"); |
2125 |
kakl |
102 |
asm("nop"); |
|
|
103 |
asm("nop"); |
2112 |
kakl |
104 |
cbi(RF_PORT, SCK); |
2125 |
kakl |
105 |
asm("nop"); |
|
|
106 |
asm("nop"); |
|
|
107 |
asm("nop"); |
2112 |
kakl |
108 |
} |
|
|
109 |
c=0; |
|
|
110 |
for (j=0; j<8; j++) |
|
|
111 |
{ c<<=1; |
|
|
112 |
if (RF_PIN&(1<<SDO)) |
|
|
113 |
c|=1; |
|
|
114 |
sbi(RF_PORT, SCK); |
2125 |
kakl |
115 |
asm("nop"); |
|
|
116 |
asm("nop"); |
|
|
117 |
asm("nop"); |
2112 |
kakl |
118 |
cbi(RF_PORT, SCK); |
2125 |
kakl |
119 |
asm("nop"); |
|
|
120 |
asm("nop"); |
|
|
121 |
asm("nop"); |
2112 |
kakl |
122 |
} |
|
|
123 |
*data++=c; |
|
|
124 |
sbi(RF_PORT, CS); |
2125 |
kakl |
125 |
asm("nop"); |
|
|
126 |
asm("nop"); |
|
|
127 |
asm("nop"); |
2112 |
kakl |
128 |
} |
|
|
129 |
} |