Matthias Grob & Manuel Stalder / Mbed 2 deprecated DecaWave

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
manumaet
Date:
Tue Nov 18 14:06:48 2014 +0000
Parent:
6:d5864a1b9e17
Child:
8:7a9c61242e2f
Commit message:
first interrupt handled receiving (draft with callback, receive not fully API yet)

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	Tue Nov 18 13:39:50 2014 +0000
+++ b/DW1000/DW1000.cpp	Tue Nov 18 14:06:48 2014 +0000
@@ -1,10 +1,12 @@
 #include "DW1000.h"
 
-DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS) : spi(MOSI, MISO, SCLK), cs(CS)
+DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : spi(MOSI, MISO, SCLK), cs(CS), irq(IRQ)
 {
     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)
+    
+    irq.rise(this, &DW1000::ISR);
 }
 
 uint32_t DW1000::getDeviceID() {
@@ -23,23 +25,6 @@
     writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8);
 }
 
-void DW1000::sendFrame(char* message, int length) {
-    writeRegister(DW1000_TX_BUFFER, 0, (uint8_t*)message, length);  // fill buffer
-    
-    uint16_t framelength = length+2;                                // put length of frame including 2 CRC Bytes
-    writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&framelength, 1);
-    
-    uint8_t txstart = 0x02;                                         // trigger sending process
-    writeRegister(DW1000_SYS_CTRL, 0, &txstart, 1);
-}
-
-void DW1000::resetRX() {
-    uint8_t resetrx = 0xE0;     //set rx reset
-    writeRegister(DW1000_PMSC, 3, &resetrx, 1);
-    resetrx = 0xf0;             //clear RX reset
-    writeRegister(DW1000_PMSC, 3, &resetrx, 1);
-}
-
 float DW1000::getVoltage() {
     uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form DW1000 User Manual p57
     writeRegister(DW1000_RF_CONF, 0x11, buffer, 2);
@@ -52,6 +37,26 @@
     return Voltage;
 }
 
+void DW1000::sendFrame(char* message, int length) {
+    writeRegister(DW1000_TX_BUFFER, 0, (uint8_t*)message, length);  // fill buffer
+    
+    uint16_t framelength = length+2;                                // put length of frame including 2 CRC Bytes
+    writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&framelength, 1);
+    
+    uint8_t txstart = 0x02;                                         // trigger sending process
+    writeRegister(DW1000_SYS_CTRL, 0, &txstart, 1);
+}
+
+void DW1000::ISR() {
+    callbackRX();
+}
+
+void DW1000::resetRX() {
+    uint8_t resetrx = 0xE0;     //set rx reset
+    writeRegister(DW1000_PMSC, 3, &resetrx, 1);
+    resetrx = 0xf0;             //clear RX reset
+    writeRegister(DW1000_PMSC, 3, &resetrx, 1);
+}
 
 // SPI Interface ------------------------------------------------------------------------------------
 void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length)
--- a/DW1000/DW1000.h	Tue Nov 18 13:39:50 2014 +0000
+++ b/DW1000/DW1000.h	Tue Nov 18 14:06:48 2014 +0000
@@ -54,7 +54,7 @@
 class DW1000
 {
     public:            
-        DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS);                           // constructor, uses SPI class
+        DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ);                           // constructor, uses SPI class
 
         // Device API
         uint32_t getDeviceID();
@@ -63,13 +63,18 @@
         float getVoltage();
         
         void sendFrame(char* message, int length);
-        void resetRX();
         
     //private:
+        // Interrupt
+        void (*callbackRX) ();
+        void ISR();
+    
+        void resetRX();
         
         // SPI Inteface
         SPI spi;                                                                                // SPI Bus
         DigitalOut cs;                                                                          // Slave selector for SPI-Bus (here explicitly needed to start and end SPI transactions also usable to wake up DW1000)
+        InterruptIn irq;                                                                        // Pin used to handle Events from DW1000 by an Interrupthandler
         
         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);
@@ -78,34 +83,4 @@
         void deselect();                                                                        // deselects the only slave after transaction
 };
 
-#endif
-
-// -------------------------------------------------------------------------------------------------------------------
-// structure from original Driver to hold device data (to be REMOVED!)
-typedef struct
-{
-    uint32_t      deviceID;
-    uint32_t      partID ;
-    uint32_t      lotID ;
-    uint8_t       chan;               // added chan here - used in the reading of acc
-    uint8_t       longFrames ;        // flag in non-standard long frame mode
-    uint32_t      txFCTRL ;           // keep TX_FCTRL register config
-    uint16_t      rfrxDly;            // rf delay (delay though the RF blocks before the signal comes out of the antenna i.e. "antenna delay")
-    uint16_t      rftxDly;            // rf delay (delay though the RF blocks before the signal comes out of the antenna i.e. "antenna delay")
-    uint32_t      antennaDly;         // antenna delay read from OTP 64 PRF value is in high 16 bits and 16M PRF in low 16 bits
-    uint8_t       xtrim;              // xtrim value read from OTP
-    uint32_t      sysCFGreg ;         // local copy of system config register
-    uint32_t      txPowCfg[12];       // stores the Tx power configuration read from OTP (6 channels consecutively with PRF16 then 64, e.g. Ch 1 PRF16 is index 0 and 64 index 1)
-    uint8_t       dblbuffon;          // double rx buffer mode flag
-                                    // the dwt_isr() will only process the events that "enabled" (i.e. the ones that generate interrupt)
-    //dwt_callback_data_t cdata;      // callback data structure
-
-    uint32_t      states[3] ;         //MP workaround debug states register
-    uint8_t       statescount ;
-    uint8_t       wait4resp ;         //wait 4 response was set with last TX start command
-    int         prfIndex ;
-
-    //void (*dwt_txcallback)(const dwt_callback_data_t *txd);
-    //void (*dwt_rxcallback)(const dwt_callback_data_t *rxd);
-
-} dwt_local_data_t ;
\ No newline at end of file
+#endif
\ No newline at end of file
--- a/main.cpp	Tue Nov 18 13:39:50 2014 +0000
+++ b/main.cpp	Tue Nov 18 14:06:48 2014 +0000
@@ -3,7 +3,7 @@
 #include "DW1000.h"
 
 PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
-DW1000      dw(D11, D12, D13, D10);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS)
+DW1000      dw(D11, D12, D13, D10, D14);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
 
 /*uint32_t read32Bit(uint8_t reg, uint16_t subaddress) {
     uint32_t result = 0;
@@ -14,6 +14,16 @@
 
 void Interrupthandler() {
     pc.printf("Interrupt!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
+    uint8_t frameready = 0;
+    dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
+    pc.printf("Status: %X\r\n", frameready);
+    // get data from buffer
+    uint8_t receive[20] = "NOTHING IN!!";
+    dw.readRegister(DW1000_RX_BUFFER, 0, receive, 20);
+    pc.printf("Message received: %s -------------------------\r\n", receive);
+    dw.resetRX();
+    uint8_t rxenable = 0x01;                // start listening
+    dw.writeRegister(DW1000_SYS_CTRL, 1, &rxenable, 1);
 }
 
 int main() {
@@ -30,13 +40,16 @@
     dw.readRegister(DW1000_SYS_CFG, 0, (uint8_t*)&conf, 4);
     pc.printf("%d System Configuration: %X\r\n", i, conf);
     
-    wait(5);
+    wait(1);
     
 #ifndef SENDR
     uint8_t dataframereadyinterrupt = 0x20; // only good frame would be 0x40
     dw.writeRegister(DW1000_SYS_MASK, 1, &dataframereadyinterrupt, 1);
-    InterruptIn button(USER_BUTTON);
-    button.rise(&Interrupthandler);
+    dw.callbackRX = &Interrupthandler;
+    
+    // Receive something
+    uint8_t rxenable = 0x01;                // start listening
+    dw.writeRegister(DW1000_SYS_CTRL, 1, &rxenable, 1);
 #endif
     
     while(1) {
@@ -51,38 +64,7 @@
         uint8_t messagecheck[20];
         dw.readRegister(DW1000_TX_BUFFER, 0, messagecheck, 20);
         pc.printf("Message in buffer: %s\r\n", messagecheck);
-        wait(2);
-# else
-        
-        // Receive something
-        uint8_t rxenable = 0x01;                // start listening
-        dw.writeRegister(DW1000_SYS_CTRL, 1, &rxenable, 1);
-        
-        uint8_t frameready = 0;                 // check if frame received
-        while (frameready == 0 /*& 0x20) != 0x20*/) {   // while no frame ready
-            i++;
-            
-            pc.printf("%d Waiting for frame... ", i);
-            dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
-            pc.printf("Status: %X ", frameready);
-            uint8_t receive[20] = "NOTHING IN!!";   // get data from buffer
-            dw.readRegister(DW1000_RX_BUFFER, 0, receive, 20);
-            pc.printf("Message would be: %s\r\n", receive);
-            wait(0.3);
-        }
-        
-        // get data from buffer
-        uint8_t receive[20] = "NOTHING IN!!";
-        dw.readRegister(DW1000_RX_BUFFER, 0, receive, 20);
-        pc.printf("Message received: %s -------------------------\r\n", receive);
-        
-        dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
-        pc.printf("Status: %X\r\n", frameready);
-        
-        dw.resetRX();
-        wait(0.5);
-        continue;
-        
+        wait(2);   
 # endif
         
     }