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 15:41:33 2014 +0000
Parent:
7:e634eeafc4d2
Child:
9:c8839de428ac
Commit message:
sender doesn't work anymore

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 14:06:48 2014 +0000
+++ b/DW1000/DW1000.cpp	Tue Nov 18 15:41:33 2014 +0000
@@ -1,12 +1,12 @@
 #include "DW1000.h"
 
-DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : spi(MOSI, MISO, SCLK), cs(CS), irq(IRQ)
-{
+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);
+    irq.rise(this, &DW1000::ISR);       // attach Interrupt handler to rising edge
+    resetRX();
 }
 
 uint32_t DW1000::getDeviceID() {
@@ -26,14 +26,14 @@
 }
 
 float DW1000::getVoltage() {
-    uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form DW1000 User Manual p57
+    uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00};             // algorithm form DW1000 User Manual p57
     writeRegister(DW1000_RF_CONF, 0x11, buffer, 2);
     writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1);
     writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1);
     writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1);
-    readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2);   // get the 8-Bit readings for Voltage and Temperature
+    readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2);               // get the 8-Bit readings for Voltage and Temperature
     float Voltage = buffer[5] * 0.0057 + 2.3;
-    float Temperature = buffer[6] * 1.13 - 113.0;       // TODO: getTemperature was always ~35 degree with better formula/calibration see instance_common.c row 391
+    float Temperature = buffer[6] * 1.13 - 113.0;                   // TODO: getTemperature was always ~35 degree with better formula/calibration see instance_common.c row 391
     return Voltage;
 }
 
@@ -43,8 +43,11 @@
     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);
+    writeRegister8(DW1000_SYS_CTRL, 0, 0x02);                       // trigger sending process by setting the TXSTRT bit
+}
+
+void DW1000::receiveFrame() {
+    writeRegister8(DW1000_SYS_CTRL, 1, 0x01);                       // start listening for preamble by setting the RXENAB bit
 }
 
 void DW1000::ISR() {
@@ -59,28 +62,27 @@
 }
 
 // SPI Interface ------------------------------------------------------------------------------------
-void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length)
-{
+void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) {
+    writeRegister(reg, subaddress, &buffer, 1);
+}
+
+void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
     setupTransaction(reg, subaddress, false);
     
-    // get data
-    for(int i=0; i<length; i++)
+    for(int i=0; i<length; i++)     // get data
         buffer[i] = spi.write(0x00);
     deselect();
 }
 
-void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length)
-{
+void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
     setupTransaction(reg, subaddress, true);
     
-    // put data
-    for(int i=0; i<length; i++)
+    for(int i=0; i<length; i++)     // put data
         spi.write(buffer[i]);
     deselect();
 }
 
-void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write)
-{
+void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) {
     reg |=  (write * DW1000_WRITE_FLAG);
     select();
     if (subaddress > 0) {                                                       // there's a subadress, we need to set flag and send second header byte
--- a/DW1000/DW1000.h	Tue Nov 18 14:06:48 2014 +0000
+++ b/DW1000/DW1000.h	Tue Nov 18 15:41:33 2014 +0000
@@ -51,8 +51,7 @@
 #define DW1000_SUBADDRESS_FLAG      0x40 // if we have a sub address second Bit has to be 1
 #define DW1000_2_SUBADDRESS_FLAG    0x80 // if we have a long sub adress (more than 7 Bit) we set this Bit in the first part
 
-class DW1000
-{
+class DW1000 {
     public:            
         DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ);                           // constructor, uses SPI class
 
@@ -63,6 +62,7 @@
         float getVoltage();
         
         void sendFrame(char* message, int length);
+        void receiveFrame();
         
     //private:
         // Interrupt
@@ -76,6 +76,8 @@
         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 writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer);                  // expressive to write just one byte
+        
         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
--- a/main.cpp	Tue Nov 18 14:06:48 2014 +0000
+++ b/main.cpp	Tue Nov 18 15:41:33 2014 +0000
@@ -5,25 +5,18 @@
 PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
 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;
-    
-}*/
-
-//#define SENDER
+#define SENDER
 
 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);
+    pc.printf("Interrupt 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);
+    pc.printf("Message received: %s\r\n", receive);
     dw.resetRX();
-    uint8_t rxenable = 0x01;                // start listening
-    dw.writeRegister(DW1000_SYS_CTRL, 1, &rxenable, 1);
+    dw.receiveFrame();
 }
 
 int main() {
@@ -42,14 +35,13 @@
     
     wait(1);
     
+    dw.callbackRX = &Interrupthandler;
 #ifndef SENDR
     uint8_t dataframereadyinterrupt = 0x20; // only good frame would be 0x40
     dw.writeRegister(DW1000_SYS_MASK, 1, &dataframereadyinterrupt, 1);
-    dw.callbackRX = &Interrupthandler;
     
     // Receive something
-    uint8_t rxenable = 0x01;                // start listening
-    dw.writeRegister(DW1000_SYS_CTRL, 1, &rxenable, 1);
+    dw.receiveFrame();
 #endif
     
     while(1) {
@@ -57,14 +49,17 @@
         
 # ifdef SENDER                      // to make one node sender and one receiver
         // Send something
-        char message[20] = "HELLO WORLD!";
+        char message[20];
         sprintf((char*)message, "HELLO WORLD! %d", i);
         dw.sendFrame(message, 20);
         
-        uint8_t messagecheck[20];
+        uint8_t messagecheck[20] = "HELLO WORLD!";
         dw.readRegister(DW1000_TX_BUFFER, 0, messagecheck, 20);
         pc.printf("Message in buffer: %s\r\n", messagecheck);
-        wait(2);   
+        wait(2);
+#else
+        pc.printf("%d Waiting...\r\n", i);
+        wait(5);
 # endif
         
     }