Skip to content

Commit

Permalink
Fix for #8
Browse files Browse the repository at this point in the history
- Changed from using reinterpret cast to memcpy for storing pipe0
address
  • Loading branch information
TMRh20 committed May 19, 2014
1 parent db1c051 commit 57521fb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
24 changes: 12 additions & 12 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)

while (qty--)
{
uint8_t buffer[5];
uint8_t buffer[addr_width];
read_register(reg++,buffer,sizeof buffer);

printf_P(PSTR(" 0x"));
Expand All @@ -330,7 +330,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)

RF24::RF24(uint8_t _cepin, uint8_t _cspin):
ce_pin(_cepin), csn_pin(_cspin), wide_band(false), p_variant(false),
payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0)
payload_size(32), dynamic_payloads_enabled(false), addr_width(5)//,pipe0_reading_address(0)
{
}

Expand Down Expand Up @@ -501,7 +501,6 @@ void RF24::begin(void)
// PTX should use only 22uA of power
write_register(CONFIG, ( read_register(CONFIG) ) & ~_BV(PRIM_RX) );


}

/****************************************************************************/
Expand All @@ -513,7 +512,7 @@ void RF24::startListening(void)
write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );

// Restore the pipe0 adddress, if exists
if (pipe0_reading_address){
if (pipe0_reading_address[0] > 0){
write_register(RX_ADDR_P0, pipe0_reading_address, addr_width);
}

Expand Down Expand Up @@ -811,8 +810,8 @@ void RF24::openWritingPipe(uint64_t value)
// Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
// expects it LSB first too, so we're good.

write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5);
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width);

//const uint8_t max_payload_size = 32;
//write_register(RX_PW_P0,min(payload_size,max_payload_size));
Expand Down Expand Up @@ -852,14 +851,15 @@ void RF24::openReadingPipe(uint8_t child, uint64_t address)
// If this is pipe 0, cache the address. This is needed because
// openWritingPipe() will overwrite the pipe 0 address, so
// startListening() will have to restore it.
if (child == 0)
pipe0_reading_address = reinterpret_cast<uint8_t*>(&address);
if (child == 0){
memcpy(pipe0_reading_address,&address,addr_width);
}

if (child <= 6)
{
// For pipes 2-5, only write the LSB
if ( child < 2 )
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 5);
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), addr_width);
else
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);

Expand Down Expand Up @@ -889,9 +889,9 @@ void RF24::openReadingPipe(uint8_t child, const uint8_t *address)
// If this is pipe 0, cache the address. This is needed because
// openWritingPipe() will overwrite the pipe 0 address, so
// startListening() will have to restore it.
if (child == 0)
pipe0_reading_address = address;

if (child == 0){
memcpy(pipe0_reading_address,address,addr_width);
}
if (child <= 6)
{
// For pipes 2-5, only write the LSB
Expand Down
2 changes: 1 addition & 1 deletion RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RF24
bool p_variant; /* False for RF24L01 and true for RF24L01P */
uint8_t payload_size; /**< Fixed size of payloads */
bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */
const uint8_t *pipe0_reading_address; /**< Last address set on pipe 0 for reading. */
uint8_t pipe0_reading_address[5]; /**< Last address set on pipe 0 for reading. */
uint8_t addr_width;

public:
Expand Down
27 changes: 14 additions & 13 deletions RPi/RF24/RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)

while (qty--)
{
uint8_t buffer[5];
uint8_t buffer[addr_width];
read_register(reg++,buffer,sizeof buffer);

printf(" 0x");
Expand All @@ -265,7 +265,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)

RF24::RF24(uint8_t _cepin, uint8_t _cspin, uint32_t _spi_speed):
ce_pin(_cepin), csn_pin(_cspin), spi_speed(_spi_speed), wide_band(true), p_variant(false),
payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0)
payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0)
{
}

Expand Down Expand Up @@ -508,8 +508,8 @@ void RF24::startListening(void)
write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );

// Restore the pipe0 adddress, if exists
if (pipe0_reading_address){
write_register(RX_ADDR_P0, pipe0_reading_address, addr_width);
if (pipe0_reading_address[0] > 0){
write_register(RX_ADDR_P0, pipe0_reading_address,addr_width);
}
// Flush buffers
flush_rx();
Expand Down Expand Up @@ -797,8 +797,8 @@ void RF24::openWritingPipe(uint64_t value)
// Note that AVR 8-bit uC's store this LSB first, and the NRF24L01(+)
// expects it LSB first too, so we're good.

write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), 5);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), 5);
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width);


write_register(RX_PW_P0,payload_size);
Expand Down Expand Up @@ -838,14 +838,15 @@ void RF24::openReadingPipe(uint8_t child, uint64_t address)
// If this is pipe 0, cache the address. This is needed because
// openWritingPipe() will overwrite the pipe 0 address, so
// startListening() will have to restore it.
if (child == 0)
pipe0_reading_address = reinterpret_cast<uint8_t*>(&address);

if (child == 0){
memcpy(pipe0_reading_address,&address,addr_width);
}

if (child <= 6)
{
// For pipes 2-5, only write the LSB
if ( child < 2 )
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 5);
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), addr_width);
else
write_register(pgm_read_byte(&child_pipe[child]), reinterpret_cast<const uint8_t*>(&address), 1);

Expand Down Expand Up @@ -876,9 +877,9 @@ void RF24::openReadingPipe(uint8_t child, const uint8_t *address)
// If this is pipe 0, cache the address. This is needed because
// openWritingPipe() will overwrite the pipe 0 address, so
// startListening() will have to restore it.
if (child == 0)
pipe0_reading_address = address;

if (child == 0){
memcpy(pipe0_reading_address,address,addr_width);
}
if (child <= 6)
{
// For pipes 2-5, only write the LSB
Expand Down
2 changes: 1 addition & 1 deletion RPi/RF24/RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RF24
uint8_t payload_size; /**< Fixed size of payloads */

bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */
const uint8_t *pipe0_reading_address; /**< Last address set on pipe 0 for reading. */
uint8_t pipe0_reading_address[5]; /**< Last address set on pipe 0 for reading. */
uint8_t addr_width;

uint8_t debug ; /* Debug flag */
Expand Down

0 comments on commit 57521fb

Please sign in to comment.