From f475d69c98e524fea47c692233420fa1b1fac9cb Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 20 May 2015 00:53:40 +0200 Subject: [PATCH] bugfixes, first stable RX/TX (!) :-) --- .../DW1000-arduino-basic-test.ino | 7 +- .../DW1000-arduino-receiver-test.ino | 31 ++++---- .../DW1000-arduino-sender-test.ino | 12 +-- DW1000/DW1000.cpp | 78 +++++++++++++------ DW1000/DW1000.h | 12 ++- 5 files changed, 93 insertions(+), 47 deletions(-) diff --git a/DW1000-arduino-test/DW1000-arduino-basic-test/DW1000-arduino-basic-test.ino b/DW1000-arduino-test/DW1000-arduino-basic-test/DW1000-arduino-basic-test.ino index e0b9843f..c1ec555c 100644 --- a/DW1000-arduino-test/DW1000-arduino-basic-test/DW1000-arduino-basic-test.ino +++ b/DW1000-arduino-test/DW1000-arduino-basic-test/DW1000-arduino-basic-test.ino @@ -34,10 +34,15 @@ void setup() { } void loop() { + // wait a bit + delay(1000); // DEBUG chip info and registers pretty printed Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier()); Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier()); Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress()); + // DEBUG print device tuning results + Serial.println(dw.getPrettyBytes(AGC_TUNE, AGC_TUNE1_SUB, LEN_AGC_TUNE1)); + Serial.println(dw.getPrettyBytes(AGC_TUNE, AGC_TUNE2_SUB, LEN_AGC_TUNE2)); // wait a bit - delay(1000); + delay(10000); } diff --git a/DW1000-arduino-test/DW1000-arduino-receiver-test/DW1000-arduino-receiver-test.ino b/DW1000-arduino-test/DW1000-arduino-receiver-test/DW1000-arduino-receiver-test.ino index 0bf582fa..bf546949 100644 --- a/DW1000-arduino-test/DW1000-arduino-receiver-test/DW1000-arduino-receiver-test.ino +++ b/DW1000-arduino-test/DW1000-arduino-receiver-test/DW1000-arduino-receiver-test.ino @@ -14,6 +14,8 @@ #include #include +// packet data +byte recvBytes[256]; // DEBUG packet sent status and count volatile boolean received = false; volatile int numReceived = 0; @@ -41,9 +43,9 @@ void setup() { Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier()); Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier()); Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress()); - Serial.println(dw.getPrettyBytes(SYS_CFG, LEN_SYS_CFG)); - Serial.println(dw.getPrettyBytes(PANADR, LEN_PANADR)); - Serial.println(dw.getPrettyBytes(SYS_MASK, LEN_SYS_MASK)); + Serial.println(dw.getPrettyBytes(SYS_CFG, NO_SUB, LEN_SYS_CFG)); + Serial.println(dw.getPrettyBytes(PANADR, NO_SUB, LEN_PANADR)); + Serial.println(dw.getPrettyBytes(SYS_MASK, NO_SUB, LEN_SYS_MASK)); // attach interrupt and ISR pinMode(INT0, INPUT); attachInterrupt(0, serviceIRQ, FALLING); @@ -68,20 +70,21 @@ void loop() { // Interrupt version of transmit: Confirmation of ISR status change if(received) { Serial.print("Received packet ... #"); Serial.println(numReceived); - Serial.println(dw.getDataLength()); - received = false; + int n = dw.getDataLength(); + Serial.print("Bytes available ... "); Serial.println(n); + dw.getData(recvBytes, n); + Serial.print("Data is ... "); + for(int i = 0; i < n; i++) { + Serial.print((char)recvBytes[i]); + } + Serial.println(); + received = false; + // restart the receiver dw.newReceive(); dw.setDefaults(); dw.startReceive(); } - // wait a bit - delay(1000); - //Serial.println(dw.getPrettyBytes(SYS_STATUS, LEN_SYS_STATUS)); - //Serial.println(dw.getPrettyBytes(CHAN_CTRL, LEN_CHAN_CTRL)); - - // TODO re-issue receive after error - dw.newReceive(); - dw.setDefaults(); - dw.startReceive(); + //Serial.println(dw.getPrettyBytes(SYS_STATUS, NO_SUB, LEN_SYS_STATUS)); + //Serial.println(dw.getPrettyBytes(CHAN_CTRL, NO_SUB, LEN_CHAN_CTRL)); } diff --git a/DW1000-arduino-test/DW1000-arduino-sender-test/DW1000-arduino-sender-test.ino b/DW1000-arduino-test/DW1000-arduino-sender-test/DW1000-arduino-sender-test.ino index c6102ae6..b1e52cb4 100644 --- a/DW1000-arduino-test/DW1000-arduino-sender-test/DW1000-arduino-sender-test.ino +++ b/DW1000-arduino-test/DW1000-arduino-sender-test/DW1000-arduino-sender-test.ino @@ -41,9 +41,9 @@ void setup() { Serial.print("Device ID: "); Serial.println(dw.getPrintableDeviceIdentifier()); Serial.print("Unique ID: "); Serial.println(dw.getPrintableExtendedUniqueIdentifier()); Serial.print("Network ID & Device Address: "); Serial.println(dw.getPrintableNetworkIdAndShortAddress()); - Serial.println(dw.getPrettyBytes(SYS_CFG, LEN_SYS_CFG)); - Serial.println(dw.getPrettyBytes(PANADR, LEN_PANADR)); - Serial.println(dw.getPrettyBytes(SYS_MASK, LEN_SYS_MASK)); + Serial.println(dw.getPrettyBytes(SYS_CFG, NO_SUB, LEN_SYS_CFG)); + Serial.println(dw.getPrettyBytes(PANADR, NO_SUB, LEN_PANADR)); + Serial.println(dw.getPrettyBytes(SYS_MASK, NO_SUB, LEN_SYS_MASK)); // attach interrupt and ISR pinMode(INT0, INPUT); attachInterrupt(0, serviceIRQ, FALLING); @@ -66,8 +66,8 @@ void loop() { dw.newTransmit(); { dw.setDefaults(); - byte data[4] = {'t', 'e', 's', 't'}; - dw.setData(data, 3); + String msg = "Hello DW1000"; + dw.setData(msg); dw.startTransmit(); } // Interrupt version of transmit: Confirmation of ISR status change @@ -77,5 +77,5 @@ void loop() { } // wait a bit delay(2000); - //Serial.println(dw.getPrettyBytes(SYS_STATUS, LEN_SYS_STATUS)); + //Serial.println(dw.getPrettyBytes(SYS_STATUS, NO_SUB, LEN_SYS_STATUS)); } diff --git a/DW1000/DW1000.cpp b/DW1000/DW1000.cpp index 8f70733f..48a6f0e8 100644 --- a/DW1000/DW1000.cpp +++ b/DW1000/DW1000.cpp @@ -47,7 +47,7 @@ void DW1000::initialize() { digitalWrite(_rst, HIGH); delay(10); // default network and node id - memset(_networkAndAddress, 0xFF, LEN_PANADR); + writeValueToBytes(_networkAndAddress, 0xFF, LEN_PANADR); writeNetworkIdAndDeviceAddress(); // default system configuration memset(_syscfg, 0, LEN_SYS_CFG); @@ -60,12 +60,12 @@ void DW1000::initialize() { // tell the chip to load the LDE microcode byte pmscctrl0[LEN_PMSC_CTRL0]; byte otpctrl[LEN_OTP_CTRL]; - memset(otpctrl, 0x8000, LEN_OTP_CTRL); - memset(pmscctrl0, 0x0301, LEN_PMSC_CTRL0); + writeValueToBytes(otpctrl, 0x8000, LEN_OTP_CTRL); + writeValueToBytes(pmscctrl0, 0x0301, LEN_PMSC_CTRL0); writeBytes(PMSC_CTRL0, NO_SUB, pmscctrl0, LEN_PMSC_CTRL0); writeBytes(OTP_CTRL, OTP_CTRL_SUB, otpctrl, LEN_OTP_CTRL); delay(10); - memset(pmscctrl0, 0x0200, LEN_PMSC_CTRL0); + writeValueToBytes(pmscctrl0, 0x0200, LEN_PMSC_CTRL0); writeBytes(PMSC_CTRL0, NO_SUB, pmscctrl0, LEN_PMSC_CTRL0); tune(); delay(10); @@ -82,15 +82,15 @@ void DW1000::tune() { byte rftxctrl[LEN_RF_TXCTRL]; byte tcpgdelay[LEN_TC_PGDELAY]; byte fsplltune[LEN_FS_PLLTUNE]; - memset(agctune1, 0x8870, LEN_AGC_TUNE1); - memset(agctune2, 0x2502A907, LEN_AGC_TUNE2); - memset(drxtune2, 0x311A002D, LEN_DRX_TUNE2); - memset(ldecfg1, 0x6D, LEN_LDE_CFG1); - memset(ldecfg2, 0x1607, LEN_LDE_CFG2); - memset(txpower, 0x0E082848, LEN_TX_POWER); - memset(rftxctrl, 0x001E3FE0, LEN_RF_TXCTRL); - memset(tcpgdelay, 0xC0, LEN_TC_PGDELAY); - memset(fsplltune, 0xA6, LEN_FS_PLLTUNE); + writeValueToBytes(agctune1, 0x8870, LEN_AGC_TUNE1); + writeValueToBytes(agctune2, 0x2502A907, LEN_AGC_TUNE2); + writeValueToBytes(drxtune2, 0x311A002D, LEN_DRX_TUNE2); + writeValueToBytes(ldecfg1, 0x6D, LEN_LDE_CFG1); + writeValueToBytes(ldecfg2, 0x1607, LEN_LDE_CFG2); + writeValueToBytes(txpower, 0x0E082848, LEN_TX_POWER); + writeValueToBytes(rftxctrl, 0x001E3FE0, LEN_RF_TXCTRL); + writeValueToBytes(tcpgdelay, 0xC0, LEN_TC_PGDELAY); + writeValueToBytes(fsplltune, 0xA6, LEN_FS_PLLTUNE); writeBytes(AGC_TUNE, AGC_TUNE1_SUB, agctune1, LEN_AGC_TUNE1); writeBytes(AGC_TUNE, AGC_TUNE2_SUB, agctune2, LEN_AGC_TUNE2); writeBytes(DRX_TUNE, DRX_TUNE2_SUB, drxtune2, LEN_DRX_TUNE2); @@ -296,9 +296,9 @@ void DW1000::pulseFrequency(byte freq) { // tuning byte agctune1[LEN_AGC_TUNE1]; if(freq == TX_PULSE_FREQ_16MHZ) { - memset(agctune1, 0x8870, LEN_AGC_TUNE1); + writeValueToBytes(agctune1, 0x8870, LEN_AGC_TUNE1); } else if(freq == TX_PULSE_FREQ_64MHZ) { - memset(agctune1, 0x889B, LEN_AGC_TUNE1); + writeValueToBytes(agctune1, 0x889B, LEN_AGC_TUNE1); } else { return; } @@ -360,7 +360,7 @@ void DW1000::startTransmit() { _deviceMode = IDLE_MODE; } -void DW1000::setData(byte data[], unsigned int n) { +void DW1000::setData(byte data[], int n) { if(!isSuppressFrameCheck()) { n+=2; // two bytes CRC-16 } @@ -382,6 +382,15 @@ void DW1000::setData(byte data[], unsigned int n) { writeTransmitFrameControlRegister(); } +void DW1000::setData(String data) { + int n = data.length()+1; + byte* dataBytes = (byte*)malloc(n); + data.getBytes(dataBytes, n); + setData(dataBytes, n); + free(dataBytes); + +} + int DW1000::getDataLength() { if(_deviceMode == TX_MODE) { // 10 bits of TX frame control register @@ -397,13 +406,24 @@ int DW1000::getDataLength() { } } -int DW1000::getData(byte data[]) { - int n = getDataLength(); // number of bytes w/o the two FCS ones +void DW1000::getData(byte data[], int n) { if(n < 0) { - return n; + return; } readBytes(RX_BUFFER, NO_SUB, data, n); - return n; +} + +void DW1000::getData(String data) { + int i; + int n = getDataLength(); // number of bytes w/o the two FCS ones + byte* dataBytes = (byte*)malloc(n); + getData(dataBytes, n); + // clear string + data.remove(0); + // append to string + for(i = 0; i < n; i++) { + data += (char)dataBytes[i]; + } } // system event register @@ -447,6 +467,11 @@ boolean DW1000::isReceiveSuccess() { return false; } +void DW1000::clearAllStatus() { + memset(_sysstatus, 0, LEN_SYS_STATUS); + writeBytes(SYS_STATUS, NO_SUB, _sysstatus, LEN_SYS_STATUS); +} + void DW1000::clearReceiveStatus() { // clear latched RX bits (i.e. write 1 to clear) setBit(_sysstatus, LEN_SYS_STATUS, RXDFR_BIT, true); @@ -524,6 +549,13 @@ boolean DW1000::getBit(byte data[], int n, int bit) { return bitRead(targetByte, shift); } +void DW1000::writeValueToBytes(byte data[], int val, int n) { + int i; + for(i = 0; i < n; i++) { + data[i] = ((val >> (i * 8)) & 0xFF); + } +} + /* * Read bytes from the DW1000. Number of bytes depend on register length. * @param cmd @@ -623,11 +655,11 @@ void DW1000::writeBytes(byte cmd, word offset, byte data[], int n) { #endif } -char* DW1000::getPrettyBytes(unsigned int reg, unsigned int n) { +char* DW1000::getPrettyBytes(byte cmd, word offset, int n) { unsigned int i, j, b; byte* readBuf = (byte*)malloc(n); - readBytes(reg, NO_SUB, readBuf, n); - b = sprintf(_msgBuf, "Reg: 0x%02x, bytes: %d\nB: 7 6 5 4 3 2 1 0\n", reg, n); + readBytes(cmd, offset, readBuf, n); + b = sprintf(_msgBuf, "Reg: 0x%02x, bytes: %d\nB: 7 6 5 4 3 2 1 0\n", cmd, n); for(i = 0; i < n; i++) { byte curByte = readBuf[i]; snprintf(&_msgBuf[b++], 2, "%d", (i + 1)); diff --git a/DW1000/DW1000.h b/DW1000/DW1000.h index cda186e5..75c2b693 100644 --- a/DW1000/DW1000.h +++ b/DW1000/DW1000.h @@ -216,8 +216,10 @@ class DW1000 { void preambleLength(byte prealen); void extendedFrameLength(boolean val); void waitForResponse(boolean val); - void setData(byte data[], unsigned int n); - int getData(byte data[]); + void setData(byte data[], int n); + void setData(String data); + void getData(byte data[], int n); + void getData(String data); int getDataLength(); boolean isSuppressFrameCheck(); @@ -236,6 +238,7 @@ class DW1000 { void interruptOnAutomaticAcknowledgeTrigger(boolean val); void clearInterrupts(); + void clearAllStatus(); void clearReceiveStatus(); void clearTransmitStatus(); // TODO impl @@ -258,7 +261,7 @@ class DW1000 { void startTransmit(); // debug pretty print registers - char* getPrettyBytes(unsigned int reg, unsigned int n); + char* getPrettyBytes(byte cmd, word offset, int n); // transmission/reception bit rate static const byte TRX_RATE_110KBPS = 0x00; @@ -329,6 +332,9 @@ class DW1000 { void readBytes(byte cmd, word offset, byte data[], int n); void writeBytes(byte cmd, word offset, byte data[], int n); + /* writing numeric values to bytes. */ + void writeValueToBytes(byte data[], int val, int n); + /* 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);