Matthias Grob & Manuel Stalder / Mbed 2 deprecated DecaWave

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
manumaet
Date:
Thu Nov 27 19:19:35 2014 +0000
Parent:
25:d58b0595b300
Child:
27:71178fdb78e1
Commit message:
problems with last publish solved, first simple really working driver :)

Changed in this revision

DW1000/DW1000.cpp Show annotated file Show diff for this revision Revisions of this file
DW1000/DW1000.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DW1000/DW1000.cpp	Thu Nov 27 17:51:54 2014 +0000
+++ b/DW1000/DW1000.cpp	Thu Nov 27 19:19:35 2014 +0000
@@ -1,8 +1,7 @@
 #include "DW1000.h"
 
 DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : irq(IRQ), spi(MOSI, MISO, SCLK), cs(CS) {
-    callbackRX = NULL;        // TODO: setter
-    callbackTX = NULL;
+    setCallbacks(NULL, NULL);
     
     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)
@@ -13,11 +12,16 @@
     
     // Configuration TODO: make method for that
     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!!
+    writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames
     
     irq.rise(this, &DW1000::ISR);       // attach Interrupt handler to rising edge
 }
 
+void DW1000::setCallbacks(void (*callbackRX)(int framelength), void (*callbackTX)()) {
+    DW1000::callbackRX = callbackRX;
+    DW1000::callbackTX = callbackTX;
+}
+
 uint32_t DW1000::getDeviceID() {
     uint32_t result;
     readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4);
@@ -50,6 +54,18 @@
     return readRegister40(DW1000_SYS_STATUS, 0);
 }
 
+void DW1000::setInterrupt(bool RX, bool TX) {
+    writeRegister16(DW1000_SYS_MASK, 0, RX*0x4000 | TX*0x0080);  // RX good frame 0x4000, TX done 0x0080
+}
+
+uint64_t DW1000::getRXTimestamp() {
+    return readRegister40(DW1000_RX_TIME, 0);
+}
+
+uint64_t DW1000::getTXTimestamp() {
+    return readRegister40(DW1000_TX_TIME, 0);
+}
+
 void DW1000::sendString(char* message) {
     sendFrame((uint8_t*)message, strlen(message)+1);
 }
@@ -74,7 +90,6 @@
 }
 
 void DW1000::startRX() {
-    //while(sending);                                                 // block until sending finished                         TODO: the right way??
     writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01);                    // start listening for preamble by setting the RXENAB bit
 }
 
--- a/DW1000/DW1000.h	Thu Nov 27 17:51:54 2014 +0000
+++ b/DW1000/DW1000.h	Thu Nov 27 19:19:35 2014 +0000
@@ -54,6 +54,7 @@
 class DW1000 {
     public:            
         DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ);              // constructor, uses SPI class
+        void setCallbacks(void (*callbackRX)(int framelength), void (*callbackTX)());           // setter for callback function pointer fields
 
         // Device API
         uint32_t getDeviceID();                                                                 // gets the Device ID which should be 0xDECA0130 (good for testing SPI!)
@@ -61,6 +62,9 @@
         void setEUI(uint64_t EUI);                                                              // sets 64 bit Extended Unique Identifier according to IEEE standard
         float getVoltage();                                                                     // gets the current chip voltage measurement form the A/D converter
         uint64_t getStatus();                                                                   // get the 40 bit device status
+        void setInterrupt(bool RX, bool TX);                                                    // set Interrupt for received a good frame (CRC ok) or transmission done
+        uint64_t getRXTimestamp();
+        uint64_t getTXTimestamp();
         
         void sendString(char* message);                                                         // to send String with arbitrary length
         void receiveString(char* message);                                                      // to receive char string (length of the buffer must be 1021 to be safe)
@@ -68,15 +72,15 @@
         void startRX();                                                                         // start listening for frames
         void stopTRX();                                                                         // disable tranceiver go back to idle mode
         
-    //private:
+    private:
         void loadLDE();                                                                         // load the leading edge detection algorithm to RAM, [IMPORTANT because receiving malfunction may occur] see User Manual LDELOAD on p22 & p158
         void resetRX();                                                                         // soft reset only the tranciever part of DW1000
         void resetAll();                                                                        // soft reset the entire DW1000 (some registers stay as they were see User Manual)
 
         // Interrupt
         InterruptIn irq;                                                                        // Pin used to handle Events from DW1000 by an Interrupthandler
-        void (*callbackRX) (int framelength);                                                   // function pointer to callback which is called when successfull RX took place
-        void (*callbackTX) ();                                                                  // function pointer to callback which is called when successfull TX took place
+        void (*callbackRX)(int framelength);                                                    // function pointer to callback which is called when successfull RX took place
+        void (*callbackTX)();                                                                   // function pointer to callback which is called when successfull TX took place
         void ISR();                                                                             // interrupt handling method (also calls according callback methods)
         uint16_t getFramelength();                                                              // to get the framelength of the received frame from the PHY header
         
@@ -90,9 +94,9 @@
         void writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer);
         void writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer);
         
-        void readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length);
-        void writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length);
-        void setupTransaction(uint8_t reg, uint16_t subaddress, bool write);                    // writes bytes to SPI to setup a write or read transaction the register address and subaddress
+        void readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length);       // reads the selected part of a slave register into the buffer memory
+        void writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length);      // writes the buffer memory to the selected slave register
+        void setupTransaction(uint8_t reg, uint16_t subaddress, bool write);                    // sets up an SPI read or write transaction with correct register address and offset
         void select();                                                                          // selects the only slave for a transaction
         void deselect();                                                                        // deselects the only slave after transaction
 };
--- a/main.cpp	Thu Nov 27 17:51:54 2014 +0000
+++ b/main.cpp	Thu Nov 27 19:19:35 2014 +0000
@@ -1,34 +1,33 @@
 // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015
 #include "mbed.h"
-#include "PC.h"                         // Serial Port via USB for debugging with Terminal
+#include "PC.h"                                     // Serial Port via USB for debugging with Terminal
 #include "DW1000.h"
 
-PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
-DW1000      dw(PA_7, PA_6, PA_5, PB_6, PB_9);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
+//#define RECEIVER
+
+PC          pc(USBTX, USBRX, 921600);               // USB UART Terminal
+DW1000      dw(PA_7, PA_6, PA_5, PB_6, PB_9);       // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
 
 const float timeunit = 1/(128*499.2);
 int i=0;
 char message[1021] = "";
 char messageRX[1021] = "";
-
 uint64_t TX_timestamp;
 uint64_t RX_timestamp;
-
 int event_i = 0;
 char event[10][20];
 uint64_t eventtimes[10];
 
 void callbackRX(int framelength) {
-    TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
-    RX_timestamp = dw.readRegister40(DW1000_RX_TIME, 0);
-    dw.receiveString(messageRX);                             // receive a string
-#if 1
-    message[0] = 'A';
+    RX_timestamp = dw.getRXTimestamp();
+    dw.receiveString(messageRX);
+#ifdef RECEIVER
+    message[0] = 'A';                               // acknowledge messages
     for(int i = 0; i < 10; i++)
         message[i+1] = messageRX[i];
     dw.sendString(message);
 #endif
-    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: gives some wrong values because of timer overflow  
+    eventtimes[event_i] = RX_timestamp - TX_timestamp;                      // TODO: can give some wrong values because of timer reset after 17 seconds
     event[event_i][0] = '!';
     event[event_i][1] = 'R';
     event[event_i][2] = ' ';
@@ -42,9 +41,7 @@
 }
 
 void callbackTX() {
-    //TX_timestamp = dw.readRegister40(DW1000_TX_TIME, 0);
-    //dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, 1021);
-    //pc.printf("! %d Sent: \"%s\" %d\r\n", i, message, strlen(message)+1);
+    TX_timestamp = dw.getTXTimestamp();
     eventtimes[event_i] = 0;
     event[event_i][0] = '!';
     event[event_i][1] = 'S';
@@ -64,33 +61,21 @@
     pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
     pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
     
-    dw.callbackRX = &callbackRX;        // TODO: must not jump to NULL & setter
-    dw.callbackTX = &callbackTX;
-    
-    // Receiver initialisation
-    dw.writeRegister16(DW1000_SYS_MASK, 0, 0x4080); //| 0x0080); // TODO: RX only good frame 0x4000, RX all frames 0x2000, TX done 0x0080
+    dw.setCallbacks(&callbackRX, &callbackTX);
+    dw.setInterrupt(true, true);
     dw.startRX();
     
     while(1) {
-        for(int i = 0; i < 10; i++)
-            if(event[i][0] == '!') {
-                pc.printf("%s Time: %fus\r\n", event[i], eventtimes[i]*timeunit);
-                event[i][0] = 'X';
+        for(int j = 0; j < 10; j++)
+            if(event[j][0] == '!') {
+                pc.printf("%s Time: %fus\r\n", event[j], eventtimes[j]*timeunit);
+                event[j][0] = 'X';
             }    
-#if 0
-        sprintf(message, "%d", i);
-        //if ((i % 10) > 5) {
-            dw.sendString(message);
-            //pc.printf("%d Sent: \"%s\" %d \r\n", i, message, strlen(message)+1);
-        //}
+#ifndef RECEIVER
+        sprintf(message, "%d", i);                  // send numbers to acknowledge
+        dw.sendString(message);
+        wait(1);
 #endif
-#if 0
-        pc.printf("%d Waiting... %d %d ", i, dw.receiving, dw.sending);
-        wait(5);
-#endif
-        wait(1);
-        //pc.printf("Status: %010llX\r\n", dw.getStatus());
-        //pc.printf("State: %010llX\r\n", dw.readRegister40(DW1000_SYS_STATE, 0));
         i++;
     }
 }
\ No newline at end of file