This is the DW1000 driver and our self developed distance measurement application based on it. We do this as a semester thesis at ETH Zürich under the Automatic Control Laboratory in the Department of electrical engineering.

Dependencies:   mbed

Revision:
17:8afa5f9122da
Parent:
15:e1fea7e2aff1
Child:
18:bbc7ca7d3a95
--- a/DW1000/DW1000.cpp	Mon Nov 24 12:48:40 2014 +0000
+++ b/DW1000/DW1000.cpp	Mon Nov 24 14:09:49 2014 +0000
@@ -1,11 +1,13 @@
 #include "DW1000.h"
 
 DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : spi(MOSI, MISO, SCLK), cs(CS), irq(IRQ) {
+    receiving = 0;
+    
     deselect();                         // Chip must be deselected first
     spi.format(8,0);                    // Setup the spi for standard 8 bit data and SPI-Mode 0 (GPIO5, GPIO6 open circuit or ground on DW1000)
     spi.frequency(1000000);             // with a 1MHz clock rate (worked up to 49MHz in our Test)
     
-    //resetAll();                       // we can do a soft reset if we want to (only needed for debugging)
+    resetAll();                       // we can do a soft reset if we want to (only needed for debugging)
     loadLDE();                          // important everytime DW1000 initialises/awakes otherwise the LDE algorithm must be turned of or there's receiving malfunction see User Manual LDELOAD on p22 & p158
     writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error
     writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames TODO: doesn't work!!
@@ -55,23 +57,29 @@
 }
 
 void DW1000::sendFrame(uint8_t* message, uint16_t length) {
-    writeRegister8(DW1000_SYS_CTRL, 0, 0x40);                       // disable tranceiver go back to idle mode TODO: only if receiving!!
+    if (length >= 1021) length = 1021;
     writeRegister(DW1000_TX_BUFFER, 0, message, length);            // fill buffer
     
     uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1);             // put length of frame including 2 CRC Bytes
     length += 2;
     length = ((backup & 0xFC) << 8) | (length & 0x03FF);
-    
-    writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2);   // TODO: make that bigger frames than 256 can be sent
+    writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2);
     
+    if (receiving) stopTRX();                                       // stop receiving if we are
     writeRegister8(DW1000_SYS_CTRL, 0, 0x02);                       // trigger sending process by setting the TXSTRT bit
-    receiveFrame();                                                 // TODO: only if receiving!!
+    if (receiving) startRX();                                       // enable receiver again if we need it                      TODO: safe to do this directly ??? only after sending ended
 }
 
-void DW1000::receiveFrame() {
+void DW1000::startRX() {
+    receiving = true;
     writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01);                       // start listening for preamble by setting the RXENAB bit
 }
 
+void DW1000::stopRX() {
+    receiving = false;
+    
+}
+
 void DW1000::ISR() {
     uint64_t status;                                                // get the entire system status
     readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
@@ -90,6 +98,10 @@
     writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[2], 2);        // recover to PLL clock
 }
 
+void DW1000::stopTRX() {
+    writeRegister8(DW1000_SYS_CTRL, 0, 0x40);                       // disable tranceiver go back to idle mode
+}
+
 void DW1000::resetRX() {    
     writeRegister8(DW1000_PMSC, 3, 0xE0);   // set RX reset
     writeRegister8(DW1000_PMSC, 3, 0xF0);   // clear RX reset