Hi Felix,
I used your library this week-end, it work like a charm, thank you for your Job.
Unfortunatly on ArduiPi board the chip select pin for the RF module is not on the same port than D10 and I spent some time to get it working.
First I saw the SetCS method, basically I tested with SetCS(4) before and after initialization but had no luck so I decided to investigate digging into the code.
First thing SetCS does not refers on a pin number but the pin bit position defined into rfm12B.h
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 2 // for PORTB: 2 = d.10, 1 = d.9, 0 = d.8
This was my first mistake and just saw that in this case I would not be able to use libray with digital pin 4 (because it is not located on DDRB.
So I decided to dig into the code to achieve my goal and found somes problems:
- When we call the Initialize method, the cs_pin is affected to constant SS_BIT so if you called SetCS before you're stuck.
- If you call SetCS after the call to Initialize method, the SPI has been setup with the default pin, so we're stuck again
And these problems does not help to resolve my goal of using a pin not on DDRB so I changed the code that now work with any arduino pin
Here what I have done
all changes are on RFM12B.cpp and now SetCS refers to a Arduino Pin name (ex 4 for Digital pin 4, A1 for analog pin 1, ...)
// changed cs_pin declaration to set default value in case you don't call SetCS()
uint8_t RFM12B::cs_pin=SPI_SS; // default CS pin for SPI (D10)
SetCS now affect cs_pin
// function to set chip select
void RFM12B::SetCS(uint8_t arduinoPin)
{
cs_pin = arduinoPin;
}
Initialize the correct pin into SPIInit
void RFM12B::SPIInit() {
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, 1);
pinMode(SPI_SS, OUTPUT); // avoid Arduino to be SPI slave
pinMode(SPI_MOSI, OUTPUT);
...
...
Comment the 1st line in Initialize method
//cs_pin = SS_BIT;
...
...
Now if XFERSlow and XFER function replace
bitClear(SS_PORT, cs_pin) by digitalWrite(cs_pin, 0)
and
bitSet(SS_PORT, cs_pin) by digitalWrite(cs_pin, 1)
Just tested and works fine, if you want to change the default CS pin, you need to call SetCS before initialize
my init is as follow
// Init RFM12B module
radio.SetCS(4);
radio.Initialize(my_node_id, config.band, config.groupID, config.power, config.speed );
If Felix wants to put in the repo it could be cool.
Attached is the new file RFM12B.cpp
Hi Charly,
You're right, others complained about this before. I meant to completely rewrite this lib and synch the naming with the RFM69 lib so it's easier to interchange between examples as well. But just didn't have the time and there's 2 other reasons - first the code is propagated to lots of people and rewriting now might cause more confusion and support, second RFM12B might be dropped by HopeRF any day, so I don't know how much that effort is worth.
But great job on fixing this. I will look into merging this patch. Thanks for the contribution!
The attached RFM12B.cpp file in the initial posting is empty, would be nice if this changes could be in the updated lib files! I'd like to use your RFM12B lib with a Seeeduino Stalker and an externel RFM12B module. D10 is hardwired to the Stalker's SD card so I'm constrained to use an other CS pin.
Find attached a file with Charly's changes as described in the initial posting.
Hi all,
I've forked the RFM12B library to add some features such as SetCS, but check the dedicated blog post entry to see all new features such as accurate ARSSI reading http://hallard.me/rfm12b_arssi-library
Let me know if you have any comments