init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DW1000.h Source File

DW1000.h

00001 // Adapted from Matthias Grob & Manuel Stalder - ETH Zürich - 2015
00002  
00003 #ifndef DW1000_H
00004 #define DW1000_H
00005  
00006 #include "mbed.h"
00007  
00008 // register addresses
00009 //      Mnemonic                    Address Bytes Description
00010 #define DW1000_DEV_ID               0x00 //     4 Device Identifier – includes device type and revision information
00011 #define DW1000_EUI                  0x01 //     8 Extended Unique Identifier
00012 #define DW1000_PANADR               0x03 //     4 PAN Identifier and Short Address
00013 #define DW1000_SYS_CFG              0x04 //     4 System Configuration bitmap
00014 #define DW1000_SYS_TIME             0x06 //     5 System Time Counter (40-bit)
00015 #define DW1000_TX_FCTRL             0x08 //     5 Transmit Frame Control
00016 #define DW1000_TX_BUFFER            0x09 //  1024 Transmit Data Buffer
00017 #define DW1000_DX_TIME              0x0A //     5 Delayed Send or Receive Time (40-bit)
00018 #define DW1000_RX_FWTO              0x0C //     2 Receive Frame Wait Timeout Period
00019 #define DW1000_SYS_CTRL             0x0D //     4 System Control Register
00020 #define DW1000_SYS_MASK             0x0E //     4 System Event Mask Register
00021 #define DW1000_SYS_STATUS           0x0F //     5 System Event Status Register
00022 #define DW1000_RX_FINFO             0x10 //     4 RX Frame Information                (in double buffer set)
00023 #define DW1000_RX_BUFFER            0x11 //  1024 Receive Data Buffer                 (in double buffer set)
00024 #define DW1000_RX_FQUAL             0x12 //     8 Rx Frame Quality information        (in double buffer set)
00025 #define DW1000_RX_TTCKI             0x13 //     4 Receiver Time Tracking Interval     (in double buffer set)
00026 #define DW1000_RX_TTCKO             0x14 //     5 Receiver Time Tracking Offset       (in double buffer set)
00027 #define DW1000_RX_TIME              0x15 //    14 Receive Message Time of Arrival     (in double buffer set)
00028 #define DW1000_TX_TIME              0x17 //    10 Transmit Message Time of Sending    (in double buffer set)
00029 #define DW1000_TX_ANTD              0x18 //     2 16-bit Delay from Transmit to Antenna
00030 #define DW1000_SYS_STATE            0x19 //     5 System State information
00031 #define DW1000_ACK_RESP_T           0x1A //     4 Acknowledgement Time and Response Time
00032 #define DW1000_RX_SNIFF             0x1D //     4 Pulsed Preamble Reception Configuration
00033 #define DW1000_TX_POWER             0x1E //     4 TX Power Control
00034 #define DW1000_CHAN_CTRL            0x1F //     4 Channel Control
00035 #define DW1000_USR_SFD              0x21 //    41 User-specified short/long TX/RX SFD sequences
00036 #define DW1000_AGC_CTRL             0x23 //    32 Automatic Gain Control configuration
00037 #define DW1000_EXT_SYNC             0x24 //    12 External synchronisation control.
00038 #define DW1000_ACC_MEM              0x25 //  4064 Read access to accumulator data
00039 #define DW1000_GPIO_CTRL            0x26 //    44 Peripheral register bus 1 access - GPIO control
00040 #define DW1000_DRX_CONF             0x27 //    44 Digital Receiver configuration
00041 #define DW1000_RF_CONF              0x28 //    58 Analog RF Configuration
00042 #define DW1000_TX_CAL               0x2A //    52 Transmitter calibration block
00043 #define DW1000_FS_CTRL              0x2B //    21 Frequency synthesiser control block
00044 #define DW1000_AON                  0x2C //    12 Always-On register set
00045 #define DW1000_OTP_IF               0x2D //    18 One Time Programmable Memory Interface
00046 #define DW1000_LDE_CTRL             0x2E //     - Leading edge detection control block
00047 #define DW1000_DIG_DIAG             0x2F //    41 Digital Diagnostics Interface
00048 #define DW1000_PMSC                 0x36 //    48 Power Management System Control Block
00049  
00050 #define DW1000_WRITE_FLAG           0x80 // First Bit of the address has to be 1 to indicate we want to write
00051 #define DW1000_SUBADDRESS_FLAG      0x40 // if we have a sub address second Bit has to be 1
00052 #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
00053  
00054  
00055  
00056 //#define SYS_STATUS_ALL_RX_ERR  (SYS_STATUS_RXPHE | SYS_STATUS_RXFCE | SYS_STATUS_RXRFSL | SYS_STATUS_RXSFDTO | SYS_STATUS_AFFREJ | SYS_STATUS_LDEERR)
00057                                 
00058  
00059 class DW1000
00060 {
00061     public:
00062         const static float TIMEUNITS_TO_US = (1/(128*499.2f));  // conversion between the decawave timeunits (ca 15.65ps) to microseconds.
00063         const static float US_TO_TIMEUNITS = (128*499.2f);      // conversion between microseconds to the decawave timeunits (ca 15.65ps).
00064         const static uint64_t CONST_2POWER40 = 1099511627776;  // Time register in DW1000 is 40 bit so this is needed to detect overflows.
00065  
00066         DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ, PinName RESET);              // constructor, uses SPI class             // constructor, uses SPI class
00067  
00068         void setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void));                  // setter for callback functions, automatically enables interrupt, if NULL is passed the coresponding interrupt gets disabled
00069         template<typename T>
00070         void setCallbacks(T* tptr, void (T::*mptrRX)(void), void (T::*mptrTX)(void))
00071         {      // overloaded setter to treat member function pointers of objects
00072             callbackRX.attach(tptr, mptrRX);                                                    // possible client code: dw.setCallbacks(this, &A::callbackRX, &A::callbackTX);
00073             callbackTX.attach(tptr, mptrTX);                                                    // concept seen in line 100 of http://developer.mbed.org/users/mbed_official/code/mbed/docs/4fc01daae5a5/InterruptIn_8h_source.html
00074             setInterrupt(true,true);
00075         }
00076  
00077         // Device API
00078         uint32_t getDeviceID();                                                                 // gets the Device ID which should be 0xDECA0130 (good for testing SPI!)
00079         uint64_t getEUI();                                                                      // gets 64 bit Extended Unique Identifier according to IEEE standard
00080         void setEUI(uint64_t EUI);                                                              // sets 64 bit Extended Unique Identifier according to IEEE standard
00081         float getVoltage();                                                                     // gets the current chip voltage measurement form the A/D converter
00082         uint64_t getStatus();                                                                   // get the 40 bit device status
00083         bool hasTransmissionStarted();                                                          // check if frame transmission has started
00084         bool hasSentPreamble();                                                                 // check if preamble has been sent
00085         bool hasSentPHYHeader();                                                                // check if PHY header has been sent
00086         bool hasSentFrame();                                                                    // check if frame has been sent completely
00087         bool hasReceivedFrame();
00088         void clearReceivedFlag();
00089         void clearSentFlag();
00090         uint64_t getSYSTimestamp();
00091         uint64_t getRXTimestamp();
00092         uint64_t getTXTimestamp();
00093         float getSYSTimestampUS();
00094         float getRXTimestampUS();
00095         float getTXTimestampUS();
00096  
00097         uint16_t getStdNoise();
00098         uint16_t getPACC();
00099         uint16_t getFPINDEX();
00100         uint16_t getFPAMPL1();
00101         uint16_t getFPAMPL2();
00102         uint16_t getFPAMPL3();
00103         uint16_t getCIRPWR();
00104         uint8_t getPRF();
00105         
00106         void sendString(char* message);                                                         // to send String with arbitrary length
00107         void receiveString(char* message);                                                      // to receive char string (length of the buffer must be 1021 to be safe)
00108         void sendFrame(uint8_t* message, uint16_t length);                                      // send a raw frame (length in bytes)
00109         void sendDelayedFrame(uint8_t* message, uint16_t length, uint64_t TxTimestamp);
00110         uint16_t getFramelength();                                                              // to get the framelength of the received frame from the PHY header
00111         void startRX();                                                                         // start listening for frames
00112         void stopTRX();                                                                         // disable tranceiver go back to idle mode
00113  
00114         static void hardwareReset(PinName reset_pin);
00115         static void hardwareReset(DigitalInOut& reset_pin);
00116         void softwareReset();
00117  
00118         uint8_t readRegister8(uint8_t reg, uint16_t subaddress);                                // expressive methods to read or write the number of bits written in the name
00119         uint16_t readRegister16(uint8_t reg, uint16_t subaddress);
00120         uint32_t readRegister32(uint8_t reg, uint16_t subaddress);
00121         uint64_t readRegister40(uint8_t reg, uint16_t subaddress);
00122         void writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer);
00123         void writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer);
00124         void writeRegister32(uint8_t reg, uint16_t subaddress, uint32_t buffer);
00125         void writeRegister40(uint8_t reg, uint16_t subaddress, uint64_t buffer);
00126         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
00127         void writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length);      // writes the buffer memory to the selected slave register
00128  
00129     private:
00130         void loadLDE();                                                                         // load the leading edge detection algorithm to RAM, [IMPORTANT because receiving malfunction may occur] see User Manual LDELOAD on p22 & p158
00131         void resetRX();                                                                         // soft reset only the tranciever part of DW1000
00132         void resetAll();                                                                        // soft reset the entire DW1000 (some registers stay as they were see User Manual)
00133  
00134         // Interrupt
00135         InterruptIn irq;
00136         FunctionPointer callbackRX;                                                             // function pointer to callback which is called when successfull RX took place
00137         FunctionPointer callbackTX;                                                             // function pointer to callback which is called when successfull TX took place
00138         void setInterrupt(bool RX, bool TX);                                                    // set Interrupt for received a good frame (CRC ok) or transmission done
00139         void ISR();                                                                             // interrupt handling method (also calls according callback methods)
00140         
00141         // SPI Inteface
00142         SPI spi;                                                                                // SPI Bus
00143         DigitalOut cs;                                                                          // Slave selector for SPI-Bus (here explicitly needed to start and end SPI transactions also usable to wake up DW1000)
00144         DigitalInOut reset;
00145  
00146         void setupTransaction(uint8_t reg, uint16_t subaddress, bool write);                    // sets up an SPI read or write transaction with correct register address and offset
00147         void select();                                                                          // selects the only slave for a transaction
00148         void deselect();   
00149 };
00150  
00151 #endif