Skip to content

Commit 90fe6e7

Browse files
author
Trystan
committed
An added function to allow chip select to be set
1 parent f48cee8 commit 90fe6e7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

RF12.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ enum {
120120
TXPRE1, TXPRE2, TXPRE3, TXSYN1, TXSYN2,
121121
};
122122

123+
static uint8_t cs_pin = SS_BIT; // address of this node
124+
123125
static uint8_t nodeid; // address of this node
124126
static uint8_t group; // network group
125127
static volatile uint8_t rxfill; // number of data bytes in rf12_buf
@@ -142,9 +144,17 @@ static uint32_t seqNum; // encrypted send sequence number
142144
static uint32_t cryptKey[4]; // encryption key to use
143145
void (*crypter)(uint8_t); // does en-/decryption (null if disabled)
144146

147+
void rf12_set_cs(uint8_t pin)
148+
{
149+
if (pin==10) cs_pin = 2;
150+
if (pin==9) cs_pin = 1;
151+
if (pin==8) cs_pin = 0;
152+
}
153+
154+
145155
void rf12_spiInit () {
146-
bitSet(SS_PORT, SS_BIT);
147-
bitSet(SS_DDR, SS_BIT);
156+
bitSet(SS_PORT, cs_pin);
157+
bitSet(SS_DDR, cs_pin);
148158
digitalWrite(SPI_SS, 1);
149159
pinMode(SPI_SS, OUTPUT);
150160
pinMode(SPI_MOSI, OUTPUT);
@@ -201,10 +211,10 @@ static uint16_t rf12_xferSlow (uint16_t cmd) {
201211
#if F_CPU > 10000000
202212
bitSet(SPCR, SPR0);
203213
#endif
204-
bitClear(SS_PORT, SS_BIT);
214+
bitClear(SS_PORT, cs_pin);
205215
uint16_t reply = rf12_byte(cmd >> 8) << 8;
206216
reply |= rf12_byte(cmd);
207-
bitSet(SS_PORT, SS_BIT);
217+
bitSet(SS_PORT, cs_pin);
208218
#if F_CPU > 10000000
209219
bitClear(SPCR, SPR0);
210220
#endif
@@ -214,10 +224,10 @@ static uint16_t rf12_xferSlow (uint16_t cmd) {
214224
#if OPTIMIZE_SPI
215225
static void rf12_xfer (uint16_t cmd) {
216226
// writing can take place at full speed, even 8 MHz works
217-
bitClear(SS_PORT, SS_BIT);
227+
bitClear(SS_PORT, cs_pin);
218228
rf12_byte(cmd >> 8) << 8;
219229
rf12_byte(cmd);
220-
bitSet(SS_PORT, SS_BIT);
230+
bitSet(SS_PORT, cs_pin);
221231
}
222232
#else
223233
#define rf12_xfer rf12_xferSlow

RF12.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern volatile uint16_t rf12_crc; // running crc value, should be zero at end
4545
extern volatile uint8_t rf12_buf[]; // recv/xmit buf including hdr & crc bytes
4646
extern long rf12_seq; // seq number of encrypted packet (or -1)
4747

48+
void rf12_set_cs(uint8_t pin);
49+
4850
// only needed if you want to init the SPI bus before rf12_initialize does it
4951
void rf12_spiInit(void);
5052

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#######################################
1919
# Methods and functions (KEYWORD2)
2020
#######################################
21+
rf12_set_cs KEYWORD2
2122
rf12_initialize KEYWORD2
2223
rf12_config KEYWORD2
2324
rf12_recvDone KEYWORD2

0 commit comments

Comments
 (0)