1 | #include "rfm12b.h"
|
2 | // send and receive a 16bit-word via spi
|
3 | uint16_t RFM12B_spi(uint16_t cmd)
|
4 | {
|
5 | uint8_t i;
|
6 | uint16_t recv = 0;
|
7 | // start communication
|
8 | RFM12B_SCK_LOW();
|
9 | RFM12B_SS_LOW();
|
10 | // read 16 bits
|
11 | for(i=0; i<16; i++)
|
12 | {
|
13 | // send bit
|
14 | if(cmd & 0x8000)
|
15 | RFM12B_MOSI_HIGH();
|
16 | else
|
17 | RFM12B_MOSI_LOW();
|
18 | // clock
|
19 | RFM12B_SCK_HIGH();
|
20 | recv<<=1;
|
21 | // receive bit
|
22 | if(RFM12B_MISO_HIGH())
|
23 | {
|
24 | recv|=0x0001;
|
25 | }
|
26 | // clock
|
27 | RFM12B_SCK_LOW();
|
28 | cmd<<=1;
|
29 | }
|
30 | // end communication
|
31 | RFM12B_SS_HIGH();
|
32 | return recv;
|
33 | }
|
34 | // initialize hardware (atmel) (CHECKED)
|
35 | void RFM12B_init_hard()
|
36 | {
|
37 | // set default values
|
38 | RFM12B_SS_HIGH();
|
39 | RFM12B_MOSI_HIGH();
|
40 | RFM12B_SCK_LOW();
|
41 | // init ports
|
42 | RFM12B_SCK_INIT();
|
43 | RFM12B_MOSI_INIT();
|
44 | RFM12B_MISO_INIT();
|
45 | RFM12B_SS_INIT();
|
46 | }
|
47 | // initialize software (rfm12b) (CHECKED)
|
48 | void RFM12B_init_soft()
|
49 | {
|
50 | RFM12B_spi(0x80D7);//EL,EF,433band,12.0pF
|
51 | RFM12B_spi(0x8239);//!er,!ebb,ET,ES,EX,!eb,!ew,DC
|
52 | /* RFM12B_spi(0xA640);//434MHz*/
|
53 | RFM12B_spi(0xA74E);//869,35 MHz
|
54 | RFM12B_spi(0xC647);//4.8kbps
|
55 | RFM12B_spi(0x94A0);//VDI,FAST,134kHz,0dBm,-103dBm
|
56 | RFM12B_spi(0xC2AC);//AL,!ml,DIG,DQD4
|
57 | RFM12B_spi(0xCA81);//FIFO8,SYNC,!ff,DR
|
58 | RFM12B_spi(0xCED4);//SYNC=2DD4;
|
59 | RFM12B_spi(0xC483);//@PWR,NO RSTRIC,!st,!fi,OE,EN
|
60 | /* RFM12B_spi(0x9850);//!mp,90kHz,MAX OUT*/
|
61 | RFM12B_spi(0x9820);//!mp,45kHz,MAX OUT*/
|
62 | RFM12B_spi(0xCC77);//OB1,OB0, LPX,!ddy,DDIT,BW0
|
63 | RFM12B_spi(0xE000);//NOT USE
|
64 | RFM12B_spi(0xC800);//NOT USE
|
65 | RFM12B_spi(0xC040);//1.66MHz,2.2V
|
66 | }
|
67 | // send a byte (CHECKED)
|
68 | // uint8_t data data to send
|
69 | void RFM12B_put(uint8_t data)
|
70 | {
|
71 | RFM12B_WAIT_RGIT();
|
72 | RFM12B_spi(0xB800 + data);
|
73 | }
|
74 | // receive a byte (CHECKED)
|
75 | uint8_t RFM12B_get()
|
76 | {
|
77 | RFM12B_WAIT_FFIT();
|
78 | return RFM12B_spi(0xB000) & 0x00FF;
|
79 | }
|
80 | // transmit multiple bytes (CHECKED)
|
81 | // uint8_t data[] data to transmit
|
82 | // uint8_t length length of the data to transmit
|
83 | void RFM12B_transmit(uint8_t data[], uint8_t length)
|
84 | {
|
85 | uint8_t i;
|
86 | // enable TX
|
87 | RFM12B_spi(0x8238);
|
88 | RFM12B_put(0xAA);
|
89 | RFM12B_put(0xAA);
|
90 | RFM12B_put(0xAA);
|
91 | RFM12B_put(0x2D);
|
92 | RFM12B_put(0xD4);
|
93 | for(i = 0; i < length; i++)
|
94 | {
|
95 | RFM12B_put(data[i]);
|
96 | }
|
97 | RFM12B_WAIT_RGIT();
|
98 | // disable TX
|
99 | RFM12B_spi(0x8208);
|
100 | }
|
101 | // receive multiple bytes (CHECKED)
|
102 | // uint8_t data[] buffer for received data
|
103 | // uint8_t length length of data to receive
|
104 | void RFM12B_receive(uint8_t *data, uint8_t length)
|
105 | {
|
106 | uint8_t i;
|
107 | // enable RX
|
108 | RFM12B_spi(0x82C8);
|
109 | // set FIFO mode
|
110 | RFM12B_spi(0xCA81);
|
111 | // enable FIFO
|
112 | RFM12B_spi(0xCA83);
|
113 | for(i = 0; i < length; i++)
|
114 | {
|
115 | data[i] = RFM12B_get();
|
116 | }
|
117 | RFM12B_WAIT_FFIT();
|
118 | // disable RX
|
119 | RFM12B_spi(0x8208);
|
120 | }
|