Buffers, pointers and SPI call

I am attempting to convert some software written for the DWM3000 to arduino
and am getting a little lost in the program and the necessary SPI commands to talk to the unit.

This is a stripped down call sequence in the program I am converting.

First Question
The links between the subroutines using pointers to return data.

dwt_read32bitoffsetreg sets uint8_t buffer[4];
dwt_readfromdevice links this as uint8_t *buffer (a pointer)
dwt_xfer3000 also links this as uint8_t *buffer (a pointer)
readfromspi links this as uint8_t *readBuffer (a pointer)

Can someone please explain this, as the readbuffer has to eventually end up as 4 bytes in buffer

Second question
dwt_xfer3000 sets uint8_t header[2];
readfromspi links this as uint8_t *headerBuffer (a pointer)
Does this command send both header bytes?
SPI.transfer((uint8_t *)headerBuffer); // Send header

Third question
How to I get the results from an SPI read into buffer, back through the pointer system

Any assistance gratefully received. This software is doing my head in

uint32_t dwt_readdevid(void)        
{
return dwt_read32bitoffsetreg(DEV_ID_ID,0)            
}

uint32_t dwt_read32bitoffsetreg (int regFileID, int regOffset)                                  
{
uint8_t buffer[4];
dwt_readfromdevice(regFileID,regOffset,4,buffer);    // Read 4 bytes (32-bits) register into buffer
}

void dwt_readfromdevice (uint32_t regFileID, uint16_t index, uint16_t lengthA, uint8_t *buffer)   
{
dwt_xfer3000(regFileID, index, lengthA, buffer, DW3000_SPI_RD_BIT);
}

void dwt_xfer3000 (const uint32_t regFileID,const uint16_t indx,const uint16_t lengthA, uint8_t *buffer, const spi_modes_e mode)
  {
   uint8_t  header[2];           // Buffer to compose header in
   uint16_t cnt = 0;             // Counter for length of a header
   ////cnt and header populated
   readfromspi(cnt, header, lengthA, buffer);
  }

int readfromspi(uint16_t headerLength,uint8_t *headerBuffer,uint16_t readlength,uint8_t *readBuffer)
{
  SPI.transfer((uint8_t *)headerBuffer);                 // Send header
  // how do I get the read from SPI
return 0;
}

That code does not make sense. SPI::transfer() sends 1 byte, not a pointer to data. I would suggest looking at the SPI examples that come with the library. They show how to read/write registers on a device. (File->examples->SPI->.....)

Nope, if you use the correct overload, you can send a whole buffer. From SPI.h for Uno board package:

  inline static void transfer(void *buf, size_t count) {
    if (count == 0) return;
    uint8_t *p = (uint8_t *)buf;
    SPDR = *p;
    while (--count > 0) {
      uint8_t out = *(p + 1);
      while (!(SPSR & _BV(SPIF))) ;
      uint8_t in = SPDR;
      SPDR = out;
      *p++ = in;
    }
    while (!(SPSR & _BV(SPIF))) ;
    *p = SPDR;
  }

That not withstanding, OP's question is unintelligible.

But the OP did not specify a length, so that function signature does not match. I see no default for the length being 1, so.....

still does not make sense.

Okay, then what should the SPI send be?

Look at the examples that come with the SPI library as I pointed out in reply #2

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.