0

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
KC Engel
  • 43
  • 1
  • 8
  • The use of `static` is a pessimization – M.M May 27 '19 at 02:10
  • Does the platform provide code examples for this task? Often that can serve as clearer documentation than the actual documentation. – M.M May 27 '19 at 02:13
  • Does `start_job` finish the job and return? (If it initiates asynchronous comms then we have a problem) – M.M May 27 '19 at 02:15
  • @M.M I haven't found any examples using direct register addressing. start_job returns as expected. The program seems to run just fine, other than the data received is far from expected. – KC Engel May 27 '19 at 02:52

0 Answers0