Skip to content

Commit

Permalink
RX/TX and system timestamp handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thotro committed May 24, 2015
1 parent 3bc7994 commit b2c9234
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

// DEBUG packet sent status and count
volatile boolean sent = false;
volatile int numSent = 0;
int sentNum = 0;
unsigned long sentTime = 0;
// reset line to the chip
int RST = 9;
// chip driver instances with chip select and reset
Expand Down Expand Up @@ -56,23 +57,32 @@ void serviceIRQ() {
}
// "NOP" ISR
sent = true;
numSent++;
}

void loop() {
// transmit some data
Serial.print("Transmitting packet ... #"); Serial.println(numSent+1);
dw.newTransmit();
dw.setDefaults();
String msg = "Hello DW1000";
dw.setData(msg);
dw.startTransmit();
// enter on confirmation of ISR status change (successfully sent)
if(sent) {
Serial.print("Processed packet ... #"); Serial.println(numSent);
// process confirmation of ISR status change (successfully sent)
sent = false;
if(!dw.isTransmitDone()) {
return;
}
// update and print some information about the sent message
unsigned long newSentTime = dw.getTransmitTimestamp();
Serial.print("Processed packet ... #"); Serial.println(sentNum);
Serial.print("Sent timestamp ... "); Serial.println(newSentTime);
// NOTE: delta is just for simple demo as not correct on system time counter wrap-around
Serial.print("Delta send time [s] ... "); Serial.println((newSentTime - sentTime) * 1e-9 * 8.01282);
sentTime = newSentTime;
sentNum++;
} else {
// transmit some data
Serial.print("Transmitting packet ... #"); Serial.println(sentNum);
dw.newTransmit();
dw.setDefaults();
String msg = "Hello DW1000";
dw.setData(msg);
dw.startTransmit();
// wait a bit
delay(500);
}
// wait a bit
delay(500);
//Serial.println(dw.getPrettyBytes(SYS_STATUS, NO_SUB, LEN_SYS_STATUS));
}
41 changes: 36 additions & 5 deletions DW1000/DW1000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ DW1000::~DW1000() {

void DW1000::initialize() {
// reset chip
digitalWrite(_rst, LOW);
delay(10);
digitalWrite(_rst, HIGH);
delay(10);
reset();
// default network and node id
writeValueToBytes(_networkAndAddress, 0xFF, LEN_PANADR);
writeNetworkIdAndDeviceAddress();
Expand All @@ -71,6 +68,13 @@ void DW1000::initialize() {
delay(10);
}

void DW1000::reset() {
digitalWrite(_rst, LOW);
delay(10);
digitalWrite(_rst, HIGH);
delay(10);
}

void DW1000::tune() {
// re-tune chip for channel 5 (default)
byte agctune1[LEN_AGC_TUNE1];
Expand Down Expand Up @@ -261,7 +265,7 @@ boolean DW1000::isSuppressFrameCheck() {
return getBit(_sysctrl, LEN_SYS_CTRL, SFCST_BIT);
}

void DW1000::delayedTransceive(unsigned int delayNanos) {
void DW1000::delayedTransceive(unsigned long delayNanos) {
if(_deviceMode == TX_MODE) {
setBit(_sysctrl, LEN_SYS_CTRL, TXDLYS_BIT, true);
} else if(_deviceMode == RX_MODE) {
Expand All @@ -270,6 +274,7 @@ void DW1000::delayedTransceive(unsigned int delayNanos) {
// in idle, ignore
return;
}
byte delayBytes[5];
// TODO impl 40 bit in DX_TIME register, 9 lower sig. bits are ignored
}

Expand Down Expand Up @@ -431,6 +436,32 @@ void DW1000::getData(String& data) {
free(dataBytes);
}

unsigned long DW1000::getTransmitTimestamp() {
byte txTimeBytes[LEN_TX_STAMP];
readBytes(TX_TIME, TX_STAMP_SUB, txTimeBytes, LEN_TX_STAMP);
return getTimestampAsLong(txTimeBytes+1);
}

unsigned long DW1000::getReceiveTimestamp() {
byte rxTimeBytes[LEN_RX_STAMP];
readBytes(RX_TIME, RX_STAMP_SUB, rxTimeBytes, LEN_RX_STAMP);
return getTimestampAsLong(rxTimeBytes+1);
}

unsigned long DW1000::getSystemTimestamp() {
byte sysTimeBytes[LEN_SYS_TIME];
readBytes(SYS_TIME, NO_SUB, sysTimeBytes, LEN_SYS_TIME);
return getTimestampAsLong(sysTimeBytes+1);
}

unsigned long DW1000::getTimestampAsLong(byte ts[]) {
unsigned long tsValue = ((unsigned long)ts[0] >> 1);
tsValue |= ((unsigned long)ts[1] << 7);
tsValue |= ((unsigned long)ts[2] << 15);
tsValue |= ((unsigned long)ts[3] << 23);
return tsValue;
}

// system event register
boolean DW1000::isTransmitDone() {
// read whole register and check bit
Expand Down
24 changes: 22 additions & 2 deletions DW1000/DW1000.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,21 @@
#define SYS_MASK 0x0E
#define LEN_SYS_MASK 4

// system time counter
#define SYS_TIME 0x06
#define LEN_SYS_TIME 5

// RX timestamp register
#define RX_TIME 0x15
#define LEN_RX_TIME 14
#define RX_STAMP_SUB 0
#define LEN_RX_STAMP_SUB 5
#define LEN_RX_STAMP 5

// TX timestamp register
#define TX_TIME 0x17
#define LEN_TX_TIME 10
#define TX_STAMP_SUB 0
#define LEN_TX_STAMP 5

// timing register (for delayed RX/TX)
#define DX_TIME 0x0A
Expand Down Expand Up @@ -191,6 +201,7 @@ class DW1000 {
DW1000(int ss, int rst);
~DW1000();
void initialize();
void reset();
void tune();

// device id, address, etc.
Expand All @@ -210,7 +221,7 @@ class DW1000 {

// SYS_CTRL, TX/RX_FCTRL, transmit and receive configuration
void suppressFrameCheck(boolean val);
void delayedTransceive(unsigned int delayNanos); // TODO impl
void delayedTransceive(unsigned long delayNanos); // TODO impl
void dataRate(byte rate);
void pulseFrequency(byte freq);
void preambleLength(byte prealen);
Expand All @@ -221,6 +232,9 @@ class DW1000 {
void getData(byte data[], int n);
void getData(String& data);
int getDataLength();
unsigned long getTransmitTimestamp();
unsigned long getReceiveTimestamp();
unsigned long getSystemTimestamp();
boolean isSuppressFrameCheck();

// RX/TX default settings
Expand Down Expand Up @@ -335,6 +349,9 @@ class DW1000 {
/* writing numeric values to bytes. */
void writeValueToBytes(byte data[], int val, int n);

/* writing DW1000 timestamp values to unsigned long. */
unsigned long getTimestampAsLong(byte ts[]);

/* internal helper for bit operations on multi-bytes. */
boolean getBit(byte data[], int n, int bit);
void setBit(byte data[], int n, int bit, boolean val);
Expand All @@ -345,6 +362,9 @@ class DW1000 {
static const byte WRITE_SUB = 0xC0; // write with sub address
static const byte READ = 0x00; // regular read
static const byte READ_SUB = 0x40; // read with sub address

/* Time resolution [ns] of time based registers/values. */
static const float TIME_RES = 8.012820513;
};

#endif
Binary file added dwm1000_datasheet_1.0.pdf
Binary file not shown.

0 comments on commit b2c9234

Please sign in to comment.