SPI communication with arduino

Hello there,

I am testing the DWM1001-DEV SPI API to communicate with an Arduino Nano. First, I connect the MOSI MISO CLK GND CS and 5V pins to the nano.

I am not sure about the spi setting of DWM1001 so I set the speed to 8000000, the bit order to MSBFIRST(Most Significant bit first), and the SPI_MODE0
[color=#4f4e4e][size=medium][font=TyponineSans Text 16]Mode [color=#4f4e4e][size=medium]Clock Polarity (CPOL) [color=#4f4e4e][size=medium]Clock Phase (CPHA) [color=#4f4e4e][size=medium]Output Edge [color=#4f4e4e][size=medium]Data Capture[/size][/color][/size][/color][/size][/color][/size][/color][/font]
SPI_MODE0 0 0 Falling Rising[/size][/color]

I wanted to get the position data. In each loop, I transfer 0x02 and 0x00 at first, and keep sending 0xFF to dwm1001 until I get the return Size byte != 0x00, and then I keep transfer Size bytes of 0xFF to get the data back. I followed exactly what the SPI Example diagram shows in the dwm1001-api-guide.

However, the data I got back doesn’t look good. I supposed I would get two 0xFF bytes back as I was sending the get_position TLV request (0x02, 0x00), I got random bytes back instead. What’s more, I keep getting 0xFF back from DWM1001. I will post my code below and the Serial monitor result to show what I got back from DWM1001.

Can anyone give me some help? Really appreciate it!

Thanks in advance
Richard

code:

[code]#include <SPI.h>
SPISettings settingsA(8000000,MSBFIRST,SPI_MODE0);
void setup() {
// pinMode(SS,OUTPUT);
Serial.begin(115200);
SPI.begin();
}

void loop() {
Serial.println("request for pos start ");
SPI.beginTransaction(settingsA);
digitalWrite(SS,LOW);
Serial.print("Sending command, getting back these bytes: “);
Serial.print(SPI.transfer(0x02));
Serial.print(” “);
Serial.print(SPI.transfer(0x00));
Serial.println(” ");

// get the SIZE
uint8_t rxSize = 0x00;
while (rxSize == 0x00) {
rxSize = SPI.transfer(0xFF);
}
Serial.print(“Size:”);
Serial.print(rxSize,DEC);
Serial.println("");

Serial.print(“here are the data DWM1001 sent back:”);
for (byte i = 0; i < rxSize; i++) {
Serial.print(SPI.transfer(0xFF),HEX);
Serial.print(" “);
}
Serial.println(”");
Serial.println("------------------");
SPI.endTransaction();
digitalWrite(SS,HIGH);
delay(5000);
}

[/code]

RESULTS:

request for pos start
Sending command, getting back these bytes: 18 1
Size:255
here are the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 64 1
Size:65
here are the data DWM1001 sent back:D 26 FF FF FF 50 1 0 0 1B FF FF FF 0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 255 255
Size:255
here are the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 18 1
Size:255
here are the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 64 1
Size:65
here are the data DWM1001 sent back:D 7C FC FF FF 42 1 0 0 BC FF FF FF 0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 255 255
Size:255
here are the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 18 1
Size:255
here are the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

request for pos start
Sending command, getting back these bytes: 64 1
Size:65
here are the data DWM1001 sent back:D 4A 2 0 0 A3 FF FF FF 94 FF FF FF 0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

Hi,

Not sure if you solved it already but I made some edits to your code and it seemed to work for me.

I basically just added slave select pulls when you write some packet of bytes.

//https://decawave.com/decaforum/showthread.php?tid=901

#include <SPI.h>
SPISettings settingsA(8000000,MSBFIRST,SPI_MODE0);
//SPISettings settingsA(100000,MSBFIRST,SPI_MODE0);

void setup() {
pinMode(SS,OUTPUT);
Serial.begin(115200);
SPI.begin();
}

void loop() {
Serial.println("request for pos start ");
SPI.beginTransaction(settingsA);
digitalWrite(SS,LOW);
Serial.print("Sending command, getting back these bytes: ");

Serial.print(SPI.transfer(0x02),HEX);
Serial.print(" “);
Serial.print(SPI.transfer(0x00),HEX);
Serial.println(” ");
digitalWrite(SS,HIGH);

// get the SIZE
uint8_t rxSize = 0x00;
while (rxSize == 0x00) {
digitalWrite(SS,LOW);
rxSize = SPI.transfer(0xFF);
digitalWrite(SS,HIGH);
}
Serial.print(“Size:”);
Serial.print(rxSize,HEX);
Serial.println("");

Serial.print(“here are the data DWM1001 sent back:”);
digitalWrite(SS,LOW);
for (byte i = 0; i < rxSize; i++) {
Serial.print(SPI.transfer(0xFF),HEX);
Serial.print(" ");
}
digitalWrite(SS,HIGH);

Serial.println("");
Serial.println("------------------");
SPI.endTransaction();

delay(100);
}

Hi
I tried this code but did not get the expected results. However I noticed that right after I send the commands, dwm1001 sends the reply. But correct reply once in three times. Probably because of it is missing the sampling. I know this is not the way the datasheet has mentioned. But no idea why this works only sometimes. Any insights?

example:

SPISettings dwm_1001(8000000, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(dwm_1001);

tx_data[tx_len++] = DWM1001_TLV_TYPE_CMD_BLE_ADDR_GET;
tx_data[tx_len++] = 0;

digitalWrite(_cs, LOW);
SPI.transfer(tx_data[0]);
SPI.transfer(tx_data[1]);

for(int i=0; i<9; ++i){
rx_data[i] = SPI.transfer(0xFF);
}
digitalWrite(_cs, HIGH);

Did you get it to work? If yes can you please share the complete code here, Thanks!

1 Like

Yes. Try the following after the initial SPI setup.

SPI.transfer(DWM1001_TLV_TYPE_CMD_BLE_ADDR_GET);

for(int i=0; i < 11; ++i){
rx_data[i] = SPI.transfer(0);
}

Thanks, can you please tell me what you have defined as DWM1001_TLV_TYPE_CMD_BLE_ADDR_GET, the value for it, or if its part of API documentation can you please guide me where to find it. Thanks!

Hi, Its 0x10.

And yes it is part of the documentation. I have attached the API guide for you. DWM1001-API-Guide.pdf (1.9 MB)

1 Like

Thanks @kasun1992, i am also facing the same issue, getting correct data every third time, were you able to resolve this?

I couldn’t get the location with this codes, someone have a good response?

I also had problems getting the SPI connection to work.

As I found out the main reason why my code was not working was that I did not released the chip select while waiting for the response.
As in the code example above:

// get the SIZE
uint8_t rxSize = 0x00;
while (rxSize == 0x00) {
digitalWrite(SS,LOW);
rxSize = SPI.transfer(0xFF);
digitalWrite(SS,HIGH);
}

The chip select must be released when the response is not ready.

This is a bit confusing since I could not find any thing about this in the documentation. I would expect that using a single SPI master and slave the chip select should be allowed to be permanently set.

Hi. I am using an Arduino Due to connect to the DWM-1001 with SPI. I have tried the code pasted in the first post and I am getting this as a result:

13:23:57.662 -> request for pos start
13:23:57.662 -> Sending command, getting back these bytes: FF FF
13:23:57.662 -> Size:FF
13:23:57.662 -> Here is the data DWM1001 sent back:FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
13:23:57.729 -> ------------------

I am trying to figure out all the changes that have been mentioned throughout the thread, does anyone have some final code that will return something other than FF’s? Does it require 2 boards to communicate with each other or can I get a return with just one board.

Thanks in advance for your help.

My first guess would be a hardware problem with the connections. If all you ever get back is FF then it’s as if the DWM1001 isn’t connected, you’re not getting a reply from it.

1 Like

Hi. Thanks for your reply. I have checked my wiring and it is all correct. I have also tried a different board but that didnt seem to work either. I have attached a basic diagram of my circuit. Do I have to have a second DWM-1001 to get communication between the Arduino and the DWM? Any ideas as to what might be the problem. Here is the code I have been using.

#include <SPI.h>
SPISettings settingsA(8000000, MSBFIRST, SPI_MODE0);
//SPISettings settingsA(100000,MSBFIRST,SPI_MODE0);

int rx_data[11];

void setup() {
pinMode(SS, OUTPUT);
Serial.begin(115200);
SPI.begin();

SPI.transfer(0x10);

for (int i = 0; i < 11; ++i) {
rx_data[i] = SPI.transfer(0);
}
}

void loop() {
Serial.print("SS pin = ");
Serial.println(SS);

Serial.println("request for pos start ");
SPI.beginTransaction(settingsA);
digitalWrite(SS, LOW);
Serial.print("Sending command, getting back these bytes: ");

Serial.print(SPI.transfer(0x02), HEX);
Serial.print(" “);
Serial.print(SPI.transfer(0x00), HEX);
Serial.println(” ");
digitalWrite(SS, HIGH);

// get the SIZE
uint8_t rxSize = 0x00;
while (rxSize == 0x00) {
digitalWrite(SS, LOW);
rxSize = SPI.transfer(0xFF);
digitalWrite(SS, HIGH);
}
Serial.print(“Size:”);
Serial.print(rxSize, HEX);
Serial.println("");

Serial.print(“Here is the data DWM1001 sent back:”);
digitalWrite(SS, LOW);
for (byte i = 0; i < rxSize; i++) {
Serial.print(SPI.transfer(0xFF), HEX);
Serial.print(" ");
}
digitalWrite(SS, HIGH);

Serial.println("");
Serial.println("------------------");
SPI.endTransaction();

delay(1000);
}

Cheers,
Macgregor

Circuit diagram SPI1

Hi Macgregor,

Did you made any progress? I’m struggling with the same issue… driving me nuts:(
Could you please share your code?

Thanks in advance

Hey

I have made progress however, I am now using the J-link to communicate with the board instead of SPI. The reason my boards weren’t responding was because they were not pre-flashed as I was told they would be. I have fixed this and I am now able to communicate with the boards via J-link. I have not yet been able to get the boards to talk to each other but once I get this sorted I will post my solutions on Help getting started with DWM1001-DEV where I have already shown the process of flashing the boards. Once I am able to create a network just via J-link then I will move onto UART and/or SPI.

One question I do have for anyone who might know, what units is the accelerometer reading acceleration in? I am getting readings such as 16000 which is either means the units are not ms^-2 or the boards are faulty or some other unknown reason.

Let me know how you go and I this helps, cheers,

Macgregor

1 Like