Simple driver for DWM1000 modules.
DW1000.h@0:2c8820705cdd, 2016-01-29 (annotated)
- Committer:
- bhepp
- Date:
- Fri Jan 29 10:47:13 2016 +0000
- Revision:
- 0:2c8820705cdd
- Child:
- 1:19b5bef7ecf4
Simple DW1000 driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhepp | 0:2c8820705cdd | 1 | // by Matthias Grob & Manuel Stalder - ETH Zürich - 2015 |
bhepp | 0:2c8820705cdd | 2 | |
bhepp | 0:2c8820705cdd | 3 | #ifndef DW1000_H |
bhepp | 0:2c8820705cdd | 4 | #define DW1000_H |
bhepp | 0:2c8820705cdd | 5 | |
bhepp | 0:2c8820705cdd | 6 | #include "mbed.h" |
bhepp | 0:2c8820705cdd | 7 | |
bhepp | 0:2c8820705cdd | 8 | |
bhepp | 0:2c8820705cdd | 9 | // register addresses |
bhepp | 0:2c8820705cdd | 10 | // Mnemonic Address Bytes Description |
bhepp | 0:2c8820705cdd | 11 | #define DW1000_DEV_ID 0x00 // 4 Device Identifier – includes device type and revision information |
bhepp | 0:2c8820705cdd | 12 | #define DW1000_EUI 0x01 // 8 Extended Unique Identifier |
bhepp | 0:2c8820705cdd | 13 | #define DW1000_PANADR 0x03 // 4 PAN Identifier and Short Address |
bhepp | 0:2c8820705cdd | 14 | #define DW1000_SYS_CFG 0x04 // 4 System Configuration bitmap |
bhepp | 0:2c8820705cdd | 15 | #define DW1000_SYS_TIME 0x06 // 5 System Time Counter (40-bit) |
bhepp | 0:2c8820705cdd | 16 | #define DW1000_TX_FCTRL 0x08 // 5 Transmit Frame Control |
bhepp | 0:2c8820705cdd | 17 | #define DW1000_TX_BUFFER 0x09 // 1024 Transmit Data Buffer |
bhepp | 0:2c8820705cdd | 18 | #define DW1000_DX_TIME 0x0A // 5 Delayed Send or Receive Time (40-bit) |
bhepp | 0:2c8820705cdd | 19 | #define DW1000_RX_FWTO 0x0C // 2 Receive Frame Wait Timeout Period |
bhepp | 0:2c8820705cdd | 20 | #define DW1000_SYS_CTRL 0x0D // 4 System Control Register |
bhepp | 0:2c8820705cdd | 21 | #define DW1000_SYS_MASK 0x0E // 4 System Event Mask Register |
bhepp | 0:2c8820705cdd | 22 | #define DW1000_SYS_STATUS 0x0F // 5 System Event Status Register |
bhepp | 0:2c8820705cdd | 23 | #define DW1000_RX_FINFO 0x10 // 4 RX Frame Information (in double buffer set) |
bhepp | 0:2c8820705cdd | 24 | #define DW1000_RX_BUFFER 0x11 // 1024 Receive Data Buffer (in double buffer set) |
bhepp | 0:2c8820705cdd | 25 | #define DW1000_RX_FQUAL 0x12 // 8 Rx Frame Quality information (in double buffer set) |
bhepp | 0:2c8820705cdd | 26 | #define DW1000_RX_TTCKI 0x13 // 4 Receiver Time Tracking Interval (in double buffer set) |
bhepp | 0:2c8820705cdd | 27 | #define DW1000_RX_TTCKO 0x14 // 5 Receiver Time Tracking Offset (in double buffer set) |
bhepp | 0:2c8820705cdd | 28 | #define DW1000_RX_TIME 0x15 // 14 Receive Message Time of Arrival (in double buffer set) |
bhepp | 0:2c8820705cdd | 29 | #define DW1000_TX_TIME 0x17 // 10 Transmit Message Time of Sending (in double buffer set) |
bhepp | 0:2c8820705cdd | 30 | #define DW1000_TX_ANTD 0x18 // 2 16-bit Delay from Transmit to Antenna |
bhepp | 0:2c8820705cdd | 31 | #define DW1000_SYS_STATE 0x19 // 5 System State information |
bhepp | 0:2c8820705cdd | 32 | #define DW1000_ACK_RESP_T 0x1A // 4 Acknowledgement Time and Response Time |
bhepp | 0:2c8820705cdd | 33 | #define DW1000_RX_SNIFF 0x1D // 4 Pulsed Preamble Reception Configuration |
bhepp | 0:2c8820705cdd | 34 | #define DW1000_TX_POWER 0x1E // 4 TX Power Control |
bhepp | 0:2c8820705cdd | 35 | #define DW1000_CHAN_CTRL 0x1F // 4 Channel Control |
bhepp | 0:2c8820705cdd | 36 | #define DW1000_USR_SFD 0x21 // 41 User-specified short/long TX/RX SFD sequences |
bhepp | 0:2c8820705cdd | 37 | #define DW1000_AGC_CTRL 0x23 // 32 Automatic Gain Control configuration |
bhepp | 0:2c8820705cdd | 38 | #define DW1000_EXT_SYNC 0x24 // 12 External synchronisation control. |
bhepp | 0:2c8820705cdd | 39 | #define DW1000_ACC_MEM 0x25 // 4064 Read access to accumulator data |
bhepp | 0:2c8820705cdd | 40 | #define DW1000_GPIO_CTRL 0x26 // 44 Peripheral register bus 1 access - GPIO control |
bhepp | 0:2c8820705cdd | 41 | #define DW1000_DRX_CONF 0x27 // 44 Digital Receiver configuration |
bhepp | 0:2c8820705cdd | 42 | #define DW1000_RF_CONF 0x28 // 58 Analog RF Configuration |
bhepp | 0:2c8820705cdd | 43 | #define DW1000_TX_CAL 0x2A // 52 Transmitter calibration block |
bhepp | 0:2c8820705cdd | 44 | #define DW1000_FS_CTRL 0x2B // 21 Frequency synthesiser control block |
bhepp | 0:2c8820705cdd | 45 | #define DW1000_AON 0x2C // 12 Always-On register set |
bhepp | 0:2c8820705cdd | 46 | #define DW1000_OTP_IF 0x2D // 18 One Time Programmable Memory Interface |
bhepp | 0:2c8820705cdd | 47 | #define DW1000_LDE_CTRL 0x2E // - Leading edge detection control block |
bhepp | 0:2c8820705cdd | 48 | #define DW1000_DIG_DIAG 0x2F // 41 Digital Diagnostics Interface |
bhepp | 0:2c8820705cdd | 49 | #define DW1000_PMSC 0x36 // 48 Power Management System Control Block |
bhepp | 0:2c8820705cdd | 50 | |
bhepp | 0:2c8820705cdd | 51 | #define DW1000_WRITE_FLAG 0x80 // First Bit of the address has to be 1 to indicate we want to write |
bhepp | 0:2c8820705cdd | 52 | #define DW1000_SUBADDRESS_FLAG 0x40 // if we have a sub address second Bit has to be 1 |
bhepp | 0:2c8820705cdd | 53 | #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 |
bhepp | 0:2c8820705cdd | 54 | |
bhepp | 0:2c8820705cdd | 55 | class DW1000 { |
bhepp | 0:2c8820705cdd | 56 | public: |
bhepp | 0:2c8820705cdd | 57 | DW1000(SPI& spi, InterruptIn& irq, PinName CS, PinName RESET = NC); // constructor, uses SPI class |
bhepp | 0:2c8820705cdd | 58 | |
bhepp | 0:2c8820705cdd | 59 | void setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void)); // setter for callback functions, automatically enables interrupt, if NULL is passed the coresponding interrupt gets disabled |
bhepp | 0:2c8820705cdd | 60 | template<typename T> |
bhepp | 0:2c8820705cdd | 61 | void setCallbacks(T* tptr, void (T::*mptrRX)(void), void (T::*mptrTX)(void)) { // overloaded setter to treat member function pointers of objects |
bhepp | 0:2c8820705cdd | 62 | callbackRX.attach(tptr, mptrRX); // possible client code: dw.setCallbacks(this, &A::callbackRX, &A::callbackTX); |
bhepp | 0:2c8820705cdd | 63 | 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 |
bhepp | 0:2c8820705cdd | 64 | setInterrupt(true,true); |
bhepp | 0:2c8820705cdd | 65 | } |
bhepp | 0:2c8820705cdd | 66 | |
bhepp | 0:2c8820705cdd | 67 | // Device API |
bhepp | 0:2c8820705cdd | 68 | uint32_t getDeviceID(); // gets the Device ID which should be 0xDECA0130 (good for testing SPI!) |
bhepp | 0:2c8820705cdd | 69 | uint64_t getEUI(); // gets 64 bit Extended Unique Identifier according to IEEE standard |
bhepp | 0:2c8820705cdd | 70 | void setEUI(uint64_t EUI); // sets 64 bit Extended Unique Identifier according to IEEE standard |
bhepp | 0:2c8820705cdd | 71 | float getVoltage(); // gets the current chip voltage measurement form the A/D converter |
bhepp | 0:2c8820705cdd | 72 | uint64_t getStatus(); // get the 40 bit device status |
bhepp | 0:2c8820705cdd | 73 | bool hasSentFrame(); |
bhepp | 0:2c8820705cdd | 74 | bool hasReceivedFrame(); |
bhepp | 0:2c8820705cdd | 75 | void clearReceivedFlag(); |
bhepp | 0:2c8820705cdd | 76 | void clearSentFlag(); |
bhepp | 0:2c8820705cdd | 77 | uint64_t getRXTimestamp(); |
bhepp | 0:2c8820705cdd | 78 | uint64_t getTXTimestamp(); |
bhepp | 0:2c8820705cdd | 79 | |
bhepp | 0:2c8820705cdd | 80 | uint16_t DW1000::getStdNoise(); |
bhepp | 0:2c8820705cdd | 81 | uint16_t DW1000::getPACC(); |
bhepp | 0:2c8820705cdd | 82 | uint16_t DW1000::getFPINDEX(); |
bhepp | 0:2c8820705cdd | 83 | uint16_t DW1000::getFPAMPL1(); |
bhepp | 0:2c8820705cdd | 84 | uint16_t DW1000::getFPAMPL2(); |
bhepp | 0:2c8820705cdd | 85 | uint16_t DW1000::getFPAMPL3(); |
bhepp | 0:2c8820705cdd | 86 | uint16_t DW1000::getCIRPWR(); |
bhepp | 0:2c8820705cdd | 87 | uint8_t DW1000::getPRF(); |
bhepp | 0:2c8820705cdd | 88 | |
bhepp | 0:2c8820705cdd | 89 | void sendString(char* message); // to send String with arbitrary length |
bhepp | 0:2c8820705cdd | 90 | void receiveString(char* message); // to receive char string (length of the buffer must be 1021 to be safe) |
bhepp | 0:2c8820705cdd | 91 | void sendFrame(uint8_t* message, uint16_t length); // send a raw frame (length in bytes) |
bhepp | 0:2c8820705cdd | 92 | void sendDelayedFrame(uint8_t* message, uint16_t length, uint64_t TxTimestamp); |
bhepp | 0:2c8820705cdd | 93 | void startRX(); // start listening for frames |
bhepp | 0:2c8820705cdd | 94 | void stopTRX(); // disable tranceiver go back to idle mode |
bhepp | 0:2c8820705cdd | 95 | |
bhepp | 0:2c8820705cdd | 96 | //private: |
bhepp | 0:2c8820705cdd | 97 | void loadLDE(); // load the leading edge detection algorithm to RAM, [IMPORTANT because receiving malfunction may occur] see User Manual LDELOAD on p22 & p158 |
bhepp | 0:2c8820705cdd | 98 | void resetRX(); // soft reset only the tranciever part of DW1000 |
bhepp | 0:2c8820705cdd | 99 | static void hardwareReset(PinName reset_pin); |
bhepp | 0:2c8820705cdd | 100 | void resetAll(); // soft reset the entire DW1000 (some registers stay as they were see User Manual) |
bhepp | 0:2c8820705cdd | 101 | |
bhepp | 0:2c8820705cdd | 102 | // Interrupt |
bhepp | 0:2c8820705cdd | 103 | InterruptIn& irq; |
bhepp | 0:2c8820705cdd | 104 | FunctionPointer callbackRX; // function pointer to callback which is called when successfull RX took place |
bhepp | 0:2c8820705cdd | 105 | FunctionPointer callbackTX; // function pointer to callback which is called when successfull TX took place |
bhepp | 0:2c8820705cdd | 106 | void setInterrupt(bool RX, bool TX); // set Interrupt for received a good frame (CRC ok) or transmission done |
bhepp | 0:2c8820705cdd | 107 | void ISR(); // interrupt handling method (also calls according callback methods) |
bhepp | 0:2c8820705cdd | 108 | uint16_t getFramelength(); // to get the framelength of the received frame from the PHY header |
bhepp | 0:2c8820705cdd | 109 | |
bhepp | 0:2c8820705cdd | 110 | // SPI Inteface |
bhepp | 0:2c8820705cdd | 111 | SPI& spi; // SPI Bus |
bhepp | 0:2c8820705cdd | 112 | DigitalOut cs; // Slave selector for SPI-Bus (here explicitly needed to start and end SPI transactions also usable to wake up DW1000) |
bhepp | 0:2c8820705cdd | 113 | DigitalOut reset; |
bhepp | 0:2c8820705cdd | 114 | |
bhepp | 0:2c8820705cdd | 115 | uint8_t readRegister8(uint8_t reg, uint16_t subaddress); // expressive methods to read or write the number of bits written in the name |
bhepp | 0:2c8820705cdd | 116 | uint16_t readRegister16(uint8_t reg, uint16_t subaddress); |
bhepp | 0:2c8820705cdd | 117 | uint32_t readRegister32(uint8_t reg, uint16_t subaddress); |
bhepp | 0:2c8820705cdd | 118 | uint64_t readRegister40(uint8_t reg, uint16_t subaddress); |
bhepp | 0:2c8820705cdd | 119 | void writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer); |
bhepp | 0:2c8820705cdd | 120 | void writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer); |
bhepp | 0:2c8820705cdd | 121 | void writeRegister32(uint8_t reg, uint16_t subaddress, uint32_t buffer); |
bhepp | 0:2c8820705cdd | 122 | void writeRegister40(uint8_t reg, uint16_t subaddress, uint64_t buffer); |
bhepp | 0:2c8820705cdd | 123 | |
bhepp | 0:2c8820705cdd | 124 | 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 |
bhepp | 0:2c8820705cdd | 125 | void writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length); // writes the buffer memory to the selected slave register |
bhepp | 0:2c8820705cdd | 126 | void setupTransaction(uint8_t reg, uint16_t subaddress, bool write); // sets up an SPI read or write transaction with correct register address and offset |
bhepp | 0:2c8820705cdd | 127 | void select(); // selects the only slave for a transaction |
bhepp | 0:2c8820705cdd | 128 | void deselect(); |
bhepp | 0:2c8820705cdd | 129 | }; |
bhepp | 0:2c8820705cdd | 130 | |
bhepp | 0:2c8820705cdd | 131 | #endif |