blob: e8651e269326b6e9bf69da6076d0120391c72a15 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include <avr/io.h>
#include "rfm.h"
#define SS_PIN PB2
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SCK_PIN PB5
#define MOSI_PIN PB3
#define MISO_PIN PB4
#define SPI_DDR DDRB
#define SLEEP_MODE 0x00
#define STDBY_MODE 0x04
#define TX_MODE 0x0C
#define RX_MODE 0x10
static inline void spi_init(void)
{
SS_DDR |= (1 << SS_PIN);
SS_PORT |= (1 << SS_PIN);
SPI_DDR = (1 << MOSI_PIN) | (1 << SCK_PIN);
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
}
static inline uint8_t read_reg(uint8_t reg)
{
uint8_t data;
SS_PORT |= (1 << SS_PIN);
SPDR = addr | 0x7F;
while (!(SPSR & (1 << SPIF)))
;
data = SPDR;
SS_PORT &= ~(1 << SS_PIN);
return data;
}
static inline void write_reg(uint8_t reg, uint8_t val)
{
while (read_reg(reg) != val) {
SS_PORT |= (1 << SS_PIN);
SPDR = addr | 0x80;
while (!(SPSR & (1 << SPIF)))
;
SS_PORT &= ~(1 << SS_PIN);
}
}
static inline void set_mode(uint8_t mode)
{
write_reg(0x01, mode);
while (!read_reg(0x27))
;
}
void rfm_init(uint8_t addr)
{
spi_init();
set_mode(STDBY_MODE);
// rx interrupt on DPIO0
write_reg(0x25, 0x40);
write_reg(0x26, 0x07);
// packet format: 8 bits + whitening + crc + addr filtering
write_reg(0x37, 0x52);
write_reg(0x38, 0x08);
write_reg(0x38, addr);
// disable encryption
write_reg(0x3D, 0x02);
}
void rfm_send(uint8_t addr, uint8_t *data, uint8_t n)
{
uint8_t i;
set_mode(STDBY_MODE);
SS_PORT |= (1 << SS_PIN);
SPDR = 0x7F;
while (!(SPSR & (1 << SPIF)))
;
for (i = 0; i < n; i++) {
SPDR = data[i];
while (!(SPSR & (1 << SPIF)))
;
}
SS_PORT &= ~(1 << SS_PIN);
set_mode(TX_MODE);
while (!((read_reg(0x28) >> 3) & 1))
;
}
|