-
Notifications
You must be signed in to change notification settings - Fork 215
/
RF69_avr.h
182 lines (148 loc) · 4.37 KB
/
RF69_avr.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <avr/interrupt.h>
#include <util/crc16.h>
// prog_uint8_t appears to be deprecated in avr libc, this resolves it for now
#define __PROG_TYPES_COMPAT__
#include <avr/pgmspace.h>
#define ROM_UINT8 const prog_uint8_t
#define ROM_READ_UINT8 pgm_read_byte
#define ROM_DATA PROGMEM
#define IRQ_ENABLE sei()
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
#define RFM_IRQ 2
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 0
#define SPI_SS 53 // PB0, pin 19
#define SPI_MOSI 51 // PB2, pin 21
#define SPI_MISO 50 // PB3, pin 22
#define SPI_SCK 52 // PB1, pin 20
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#elif defined(__AVR_ATmega644P__)
#define RFM_IRQ 10
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 4
#define SPI_SS 4
#define SPI_MOSI 5
#define SPI_MISO 6
#define SPI_SCK 7
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#elif defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny44__)
#define RFM_IRQ 2
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 1
#define SPI_SS 1 // PB1, pin 3
#define SPI_MISO 6 // PA6, pin 7
#define SPI_MOSI 5 // PA5, pin 8
#define SPI_SCK 4 // PA4, pin 9
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS);
PORTA |= _BV(SPI_SS);
DDRA |= _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#elif defined(__AVR_ATmega32U4__) //Arduino Leonardo
#define RFM_IRQ 0 // PD0, INT0, Digital3
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 6 // Dig10, PB6
#define SPI_SS 10 // PB6, pin 30, Digital10
#define SPI_MISO 14 // PB3, pin 11, Digital14
#define SPI_MOSI 16 // PB2, pin 10, Digital16
#define SPI_SCK 15 // PB1, pin 9, Digital15
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#elif defined(__AVR_ATmega1284P__) //Moteino mega
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 4 // PB4 D4
#define SPI_SS 4 // PB4 D4
#define SPI_MOSI 5 // PB5 D5
#define SPI_MISO 6 // PB6 D6
#define SPI_SCK 7 // PB7 D7
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#else // ATmega168, ATmega328, etc.
// #define RFM_IRQ 2
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 2 // for PORTB: 2 = d.10, 1 = d.9, 0 = d.8
#define SPI_SS 2 // PB0
#define SPI_MOSI 3 // PB1
#define SPI_MISO 4 // PB2
#define SPI_SCK 5 // PB3
static void spiConfigPins () {
SS_PORT |= _BV(SS_BIT);
SS_DDR |= _BV(SS_BIT);
PORTB |= _BV(SPI_SS);
DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}
#endif
#ifndef EIMSK
#define EIMSK GIMSK // ATtiny
#endif
struct PreventInterrupt {
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
PreventInterrupt () { EIMSK &= ~ _BV(INT0 << 4); }
~PreventInterrupt () { EIMSK |= _BV(INT0 << 4); }
#else
PreventInterrupt () { EIMSK &= ~ _BV(INT0); }
~PreventInterrupt () { EIMSK |= _BV(INT0); }
#endif
};
static void spiInit (void) {
spiConfigPins();
#ifdef SPCR
SPCR = _BV(SPE) | _BV(MSTR);
SPSR |= _BV(SPI2X);
#else
USICR = _BV(USIWM0); // ATtiny
#endif
// pinMode(RFM_IRQ, INPUT);
// digitalWrite(RFM_IRQ, 1); // pull-up
}
static uint8_t spiTransferByte (uint8_t out) {
#ifdef SPDR
SPDR = out;
while (!(SPSR & _BV(SPIF)))
;
return SPDR;
#else
USIDR = out; // ATtiny
uint8_t v1 = _BV(USIWM0) | _BV(USITC);
uint8_t v2 = _BV(USIWM0) | _BV(USITC) | _BV(USICLK);
for (uint8_t i = 0; i < 8; ++i) {
USICR = v1;
USICR = v2;
}
return USIDR;
#endif
}
static uint8_t spiTransfer (uint8_t cmd, uint8_t val) {
SS_PORT &= ~ _BV(SS_BIT);
spiTransferByte(cmd);
uint8_t in = spiTransferByte(val);
SS_PORT |= _BV(SS_BIT);
return in;
}