As per the NRF52840 microcontroller datasheet, the packet pointer is the "packet address to be used for the next transmission or reception. When transmitting, the packet pointed to by this address will be transmitted and when receiving, the received packet will be written to this address." Using direct register addressing, I'm attempting to assign the address of a variable to a de-referenced pointer that points to the NRF52's packet pointer register address. The code compiles with no errors and a message is being sent, but the data received is not what I intended to send. As this is the first time I have needed to recast a pointer to an int, I assume I'm doing it incorrectly.
Cplusplus.com says reinterpret_cast "...can also cast pointers to or from integer types." I also found here that the correct type should be observed. Can someone please look at my Radio::read function and verify the correct usage of reinterpret_cast?
#include <cstdint>
#define BASE_ADDRESS_RADIO 0x40001000
#define OFFSET_REGISTER_PACKETPTR 0x504 // Packet pointer
#define OFFSET_REGISTER_STATE 0x550 // Current radio state
unsigned int Radio::read (void){
unsigned int rx_buff;
static std::uintptr_t * packet_pointer = (unsigned int *)
(BASE_ADDRESS_RADIO + OFFSET_REGISTER_PACKETPTR);
static unsigned int * radioState_reg = (unsigned int *)
(BASE_ADDRESS_RADIO + OFFSET_REGISTER_STATE);
while((*radioState_reg != RXIDLE)&&(*radioState_reg != TXIDLE)&&
(*radioState_reg != DISABLED)){}
if (*radioState_reg == TXIDLE){
radio_disable();
}
if(*radioState_reg == DISABLED){
rx_en();
}
*packet_pointer = reinterpret_cast<std::uintptr_t>(&rx_buff);
start_job();
return rx_buff;
}
The receiving radio signals reception almost instantly after initiating the transmission on the other radio; however, sending a single byte of data results in a full 4 bytes read at the other end.