Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
DW1000/DW1000.cpp@42:83931678c4de, 2015-03-02 (annotated)
- Committer:
- manumaet
- Date:
- Mon Mar 02 09:33:03 2015 +0000
- Revision:
- 42:83931678c4de
- Parent:
- 40:5ce51b7e3118
- Child:
- 44:2e0045042a59
after setting initialization parameters of Manual, moving Antenna delays to driver as standard value
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manumaet | 0:f50e671ffff7 | 1 | #include "DW1000.h" |
manumaet | 0:f50e671ffff7 | 2 | |
manumaet | 20:257d56530ae1 | 3 | DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : irq(IRQ), spi(MOSI, MISO, SCLK), cs(CS) { |
manumaet | 26:a65c6f26c458 | 4 | setCallbacks(NULL, NULL); |
manumaet | 17:8afa5f9122da | 5 | |
manumaet | 0:f50e671ffff7 | 6 | deselect(); // Chip must be deselected first |
manumaet | 0:f50e671ffff7 | 7 | spi.format(8,0); // Setup the spi for standard 8 bit data and SPI-Mode 0 (GPIO5, GPIO6 open circuit or ground on DW1000) |
manumaet | 0:f50e671ffff7 | 8 | spi.frequency(1000000); // with a 1MHz clock rate (worked up to 49MHz in our Test) |
manumaet | 7:e634eeafc4d2 | 9 | |
manumaet | 37:40f94c634c3e | 10 | resetAll(); // we do a soft reset of the DW1000 everytime the driver starts |
manumaet | 18:bbc7ca7d3a95 | 11 | |
manumaet | 18:bbc7ca7d3a95 | 12 | // Configuration TODO: make method for that |
manumaet | 42:83931678c4de | 13 | |
manumaet | 42:83931678c4de | 14 | // User Manual "2.5.5 Default Configurations that should be modified" p. 22 |
manumaet | 42:83931678c4de | 15 | writeRegister16(DW1000_AGC_CTRL, 0x04, 0x8870); |
manumaet | 42:83931678c4de | 16 | writeRegister32(DW1000_AGC_CTRL, 0x0C, 0x2502A907); |
manumaet | 42:83931678c4de | 17 | writeRegister32(DW1000_DRX_CONF, 0x08, 0x311A002D); |
manumaet | 42:83931678c4de | 18 | writeRegister8 (DW1000_LDE_CTRL, 0x0806, 0xD); |
manumaet | 42:83931678c4de | 19 | writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1607); |
manumaet | 42:83931678c4de | 20 | writeRegister32(DW1000_TX_POWER, 0, 0x0E082848); |
manumaet | 42:83931678c4de | 21 | writeRegister32(DW1000_RF_CONF, 0x0C, 0x001E3FE0); |
manumaet | 42:83931678c4de | 22 | writeRegister8 (DW1000_TX_CAL, 0x0B, 0xC0); |
manumaet | 42:83931678c4de | 23 | writeRegister8 (DW1000_FS_CTRL, 0x0B, 0xA6); |
manumaet | 42:83931678c4de | 24 | loadLDE(); // important everytime DW1000 initialises/awakes otherwise the LDE algorithm must be turned of or there's receiving malfunction see User Manual LDELOAD on p22 & p158 |
manumaet | 42:83931678c4de | 25 | |
manumaet | 42:83931678c4de | 26 | // 110kbps CAUTION: a lot of other registers have to be set for an optimized operation on 110kbps |
manumaet | 40:5ce51b7e3118 | 27 | //writeRegister16(DW1000_TX_FCTRL, 1, 0x2800 | 0x0100 | 0x0080); // use 2048 symbols preable (0x2800), 16MHz pulse repetition frequency (0x0100), 110kbps bit rate (0x0080) see p.69 of DW1000 User Manual |
manumaet | 40:5ce51b7e3118 | 28 | //writeRegister8(DW1000_SYS_CFG, 2, 0x40); // enable special receiving option for 110kbps!! (0x40) see p.64 of DW1000 User Manual [DO NOT enable 1024 byte frames (0x03) becuase it generates disturbance of ranging don't know why...] |
manumaet | 11:c87d37db2c6f | 29 | |
manumaet | 42:83931678c4de | 30 | writeRegister16(DW1000_TX_ANTD, 0, 16384); // set TX and RX Antenna delay to neutral because we calibrate afterwards |
manumaet | 42:83931678c4de | 31 | writeRegister16(DW1000_LDE_CTRL, 0x1804, 16384); // = 2^14 a qurter of the range of the 16-Bit register which corresponds to zero calibration in a round trip (TX1+RX2+TX2+RX1) |
manumaet | 42:83931678c4de | 32 | |
manumaet | 42:83931678c4de | 33 | writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error |
manumaet | 42:83931678c4de | 34 | |
manumaet | 39:bb57aa77b015 | 35 | irq.rise(this, &DW1000::ISR); // attach interrupt handler to rising edge of interrupt pin from DW1000 |
manumaet | 0:f50e671ffff7 | 36 | } |
manumaet | 0:f50e671ffff7 | 37 | |
manumaet | 29:019ff388ed76 | 38 | void DW1000::setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void)) { |
manumaet | 29:019ff388ed76 | 39 | bool RX = false; |
manumaet | 29:019ff388ed76 | 40 | bool TX = false; |
manumaet | 29:019ff388ed76 | 41 | if (callbackRX) { |
manumaet | 29:019ff388ed76 | 42 | DW1000::callbackRX.attach(callbackRX); |
manumaet | 29:019ff388ed76 | 43 | RX = true; |
manumaet | 29:019ff388ed76 | 44 | } |
manumaet | 29:019ff388ed76 | 45 | if (callbackTX) { |
manumaet | 29:019ff388ed76 | 46 | DW1000::callbackTX.attach(callbackTX); |
manumaet | 29:019ff388ed76 | 47 | TX = true; |
manumaet | 29:019ff388ed76 | 48 | } |
manumaet | 29:019ff388ed76 | 49 | setInterrupt(RX,TX); |
manumaet | 26:a65c6f26c458 | 50 | } |
manumaet | 26:a65c6f26c458 | 51 | |
manumaet | 0:f50e671ffff7 | 52 | uint32_t DW1000::getDeviceID() { |
manumaet | 0:f50e671ffff7 | 53 | uint32_t result; |
manumaet | 0:f50e671ffff7 | 54 | readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); |
manumaet | 0:f50e671ffff7 | 55 | return result; |
manumaet | 0:f50e671ffff7 | 56 | } |
manumaet | 0:f50e671ffff7 | 57 | |
manumaet | 0:f50e671ffff7 | 58 | uint64_t DW1000::getEUI() { |
manumaet | 0:f50e671ffff7 | 59 | uint64_t result; |
manumaet | 0:f50e671ffff7 | 60 | readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); |
manumaet | 0:f50e671ffff7 | 61 | return result; |
manumaet | 0:f50e671ffff7 | 62 | } |
manumaet | 0:f50e671ffff7 | 63 | |
manumaet | 0:f50e671ffff7 | 64 | void DW1000::setEUI(uint64_t EUI) { |
manumaet | 0:f50e671ffff7 | 65 | writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); |
manumaet | 0:f50e671ffff7 | 66 | } |
manumaet | 0:f50e671ffff7 | 67 | |
manumaet | 0:f50e671ffff7 | 68 | float DW1000::getVoltage() { |
manumaet | 12:985aa9843c3c | 69 | uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form User Manual p57 |
manumaet | 0:f50e671ffff7 | 70 | writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); |
manumaet | 0:f50e671ffff7 | 71 | writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); |
manumaet | 0:f50e671ffff7 | 72 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); |
manumaet | 0:f50e671ffff7 | 73 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); |
manumaet | 8:7a9c61242e2f | 74 | readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature |
manumaet | 0:f50e671ffff7 | 75 | float Voltage = buffer[5] * 0.0057 + 2.3; |
manumaet | 20:257d56530ae1 | 76 | //float Temperature = buffer[6] * 1.13 - 113.0; // TODO: getTemperature was always ~35 degree with better formula/calibration see instance_common.c row 391 |
manumaet | 0:f50e671ffff7 | 77 | return Voltage; |
manumaet | 0:f50e671ffff7 | 78 | } |
manumaet | 0:f50e671ffff7 | 79 | |
manumaet | 18:bbc7ca7d3a95 | 80 | uint64_t DW1000::getStatus() { |
manumaet | 18:bbc7ca7d3a95 | 81 | return readRegister40(DW1000_SYS_STATUS, 0); |
manumaet | 18:bbc7ca7d3a95 | 82 | } |
manumaet | 18:bbc7ca7d3a95 | 83 | |
manumaet | 26:a65c6f26c458 | 84 | uint64_t DW1000::getRXTimestamp() { |
manumaet | 26:a65c6f26c458 | 85 | return readRegister40(DW1000_RX_TIME, 0); |
manumaet | 26:a65c6f26c458 | 86 | } |
manumaet | 26:a65c6f26c458 | 87 | |
manumaet | 26:a65c6f26c458 | 88 | uint64_t DW1000::getTXTimestamp() { |
manumaet | 26:a65c6f26c458 | 89 | return readRegister40(DW1000_TX_TIME, 0); |
manumaet | 26:a65c6f26c458 | 90 | } |
manumaet | 26:a65c6f26c458 | 91 | |
manumaet | 10:d077bb12d259 | 92 | void DW1000::sendString(char* message) { |
manumaet | 10:d077bb12d259 | 93 | sendFrame((uint8_t*)message, strlen(message)+1); |
manumaet | 10:d077bb12d259 | 94 | } |
manumaet | 10:d077bb12d259 | 95 | |
manumaet | 24:6f25ba679490 | 96 | void DW1000::receiveString(char* message) { |
manumaet | 31:6f76f3d518ac | 97 | readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)message, getFramelength()); // get data from buffer |
manumaet | 10:d077bb12d259 | 98 | } |
manumaet | 10:d077bb12d259 | 99 | |
manumaet | 11:c87d37db2c6f | 100 | void DW1000::sendFrame(uint8_t* message, uint16_t length) { |
manumaet | 38:8ef3b8d8b908 | 101 | //if (length >= 1021) length = 1021; // check for maximim length a frame can have with 1024 Byte frames [not used, see constructor] |
manumaet | 38:8ef3b8d8b908 | 102 | if (length >= 125) length = 125; // check for maximim length a frame can have with 127 Byte frames |
manumaet | 13:b4d27bf7062a | 103 | writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer |
manumaet | 7:e634eeafc4d2 | 104 | |
manumaet | 39:bb57aa77b015 | 105 | uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame |
manumaet | 39:bb57aa77b015 | 106 | length += 2; // including 2 CRC Bytes |
manumaet | 39:bb57aa77b015 | 107 | length = ((backup & 0xFC) << 8) | (length & 0x03FF); |
manumaet | 39:bb57aa77b015 | 108 | writeRegister16(DW1000_TX_FCTRL, 0, length); |
manumaet | 11:c87d37db2c6f | 109 | |
manumaet | 25:d58b0595b300 | 110 | stopTRX(); // stop receiving |
manumaet | 23:661a79e56208 | 111 | writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit |
manumaet | 25:d58b0595b300 | 112 | startRX(); // enable receiver again |
manumaet | 8:7a9c61242e2f | 113 | } |
manumaet | 8:7a9c61242e2f | 114 | |
manumaet | 17:8afa5f9122da | 115 | void DW1000::startRX() { |
manumaet | 20:257d56530ae1 | 116 | writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit |
manumaet | 7:e634eeafc4d2 | 117 | } |
manumaet | 7:e634eeafc4d2 | 118 | |
manumaet | 25:d58b0595b300 | 119 | void DW1000::stopTRX() { |
manumaet | 25:d58b0595b300 | 120 | writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode |
manumaet | 17:8afa5f9122da | 121 | } |
manumaet | 17:8afa5f9122da | 122 | |
manumaet | 20:257d56530ae1 | 123 | // PRIVATE Methods ------------------------------------------------------------------------------------ |
manumaet | 18:bbc7ca7d3a95 | 124 | void DW1000::loadLDE() { // initialise LDE algorithm LDELOAD User Manual p22 |
manumaet | 18:bbc7ca7d3a95 | 125 | writeRegister16(DW1000_PMSC, 0, 0x0301); // set clock to XTAL so OTP is reliable |
manumaet | 20:257d56530ae1 | 126 | writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP |
manumaet | 12:985aa9843c3c | 127 | wait_us(150); |
manumaet | 18:bbc7ca7d3a95 | 128 | writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock |
manumaet | 12:985aa9843c3c | 129 | } |
manumaet | 12:985aa9843c3c | 130 | |
manumaet | 12:985aa9843c3c | 131 | void DW1000::resetRX() { |
manumaet | 12:985aa9843c3c | 132 | writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset |
manumaet | 12:985aa9843c3c | 133 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset |
manumaet | 12:985aa9843c3c | 134 | } |
manumaet | 12:985aa9843c3c | 135 | |
manumaet | 12:985aa9843c3c | 136 | void DW1000::resetAll() { |
manumaet | 12:985aa9843c3c | 137 | writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL |
manumaet | 12:985aa9843c3c | 138 | writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset |
manumaet | 12:985aa9843c3c | 139 | wait_us(10); // wait for PLL to lock |
manumaet | 12:985aa9843c3c | 140 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset |
manumaet | 7:e634eeafc4d2 | 141 | } |
manumaet | 0:f50e671ffff7 | 142 | |
manumaet | 29:019ff388ed76 | 143 | |
manumaet | 29:019ff388ed76 | 144 | void DW1000::setInterrupt(bool RX, bool TX) { |
manumaet | 29:019ff388ed76 | 145 | writeRegister16(DW1000_SYS_MASK, 0, RX*0x4000 | TX*0x0080); // RX good frame 0x4000, TX done 0x0080 |
manumaet | 29:019ff388ed76 | 146 | } |
manumaet | 29:019ff388ed76 | 147 | |
manumaet | 20:257d56530ae1 | 148 | void DW1000::ISR() { |
manumaet | 20:257d56530ae1 | 149 | uint64_t status = getStatus(); |
manumaet | 22:576ee999b004 | 150 | if (status & 0x4000) { // a frame was received |
manumaet | 29:019ff388ed76 | 151 | callbackRX.call(); |
manumaet | 22:576ee999b004 | 152 | writeRegister16(DW1000_SYS_STATUS, 0, 0x6F00); // clearing of receiving status bits |
manumaet | 20:257d56530ae1 | 153 | } |
manumaet | 22:576ee999b004 | 154 | if (status & 0x80) { // sending complete |
manumaet | 29:019ff388ed76 | 155 | callbackTX.call(); |
manumaet | 22:576ee999b004 | 156 | writeRegister8(DW1000_SYS_STATUS, 0, 0xF8); // clearing of sending status bits |
manumaet | 20:257d56530ae1 | 157 | } |
manumaet | 20:257d56530ae1 | 158 | } |
manumaet | 20:257d56530ae1 | 159 | |
manumaet | 20:257d56530ae1 | 160 | uint16_t DW1000::getFramelength() { |
manumaet | 20:257d56530ae1 | 161 | uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength |
manumaet | 20:257d56530ae1 | 162 | framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes |
manumaet | 20:257d56530ae1 | 163 | return framelength; |
manumaet | 20:257d56530ae1 | 164 | } |
manumaet | 20:257d56530ae1 | 165 | |
manumaet | 0:f50e671ffff7 | 166 | // SPI Interface ------------------------------------------------------------------------------------ |
manumaet | 10:d077bb12d259 | 167 | uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) { |
manumaet | 10:d077bb12d259 | 168 | uint8_t result; |
manumaet | 10:d077bb12d259 | 169 | readRegister(reg, subaddress, &result, 1); |
manumaet | 10:d077bb12d259 | 170 | return result; |
manumaet | 10:d077bb12d259 | 171 | } |
manumaet | 10:d077bb12d259 | 172 | |
manumaet | 18:bbc7ca7d3a95 | 173 | uint16_t DW1000::readRegister16(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 174 | uint16_t result; |
manumaet | 18:bbc7ca7d3a95 | 175 | readRegister(reg, subaddress, (uint8_t*)&result, 2); |
manumaet | 18:bbc7ca7d3a95 | 176 | return result; |
manumaet | 18:bbc7ca7d3a95 | 177 | } |
manumaet | 18:bbc7ca7d3a95 | 178 | |
manumaet | 18:bbc7ca7d3a95 | 179 | uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 180 | uint64_t result; |
manumaet | 18:bbc7ca7d3a95 | 181 | readRegister(reg, subaddress, (uint8_t*)&result, 5); |
manumaet | 18:bbc7ca7d3a95 | 182 | result &= 0xFFFFFFFFFF; // only 40-Bit |
manumaet | 18:bbc7ca7d3a95 | 183 | return result; |
manumaet | 18:bbc7ca7d3a95 | 184 | } |
manumaet | 18:bbc7ca7d3a95 | 185 | |
manumaet | 8:7a9c61242e2f | 186 | void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) { |
manumaet | 8:7a9c61242e2f | 187 | writeRegister(reg, subaddress, &buffer, 1); |
manumaet | 8:7a9c61242e2f | 188 | } |
manumaet | 8:7a9c61242e2f | 189 | |
manumaet | 18:bbc7ca7d3a95 | 190 | void DW1000::writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer) { |
manumaet | 18:bbc7ca7d3a95 | 191 | writeRegister(reg, subaddress, (uint8_t*)&buffer, 2); |
manumaet | 18:bbc7ca7d3a95 | 192 | } |
manumaet | 18:bbc7ca7d3a95 | 193 | |
manumaet | 42:83931678c4de | 194 | void DW1000::writeRegister32(uint8_t reg, uint16_t subaddress, uint32_t buffer) { |
manumaet | 42:83931678c4de | 195 | writeRegister(reg, subaddress, (uint8_t*)&buffer, 4); |
manumaet | 42:83931678c4de | 196 | } |
manumaet | 42:83931678c4de | 197 | |
manumaet | 8:7a9c61242e2f | 198 | void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 199 | setupTransaction(reg, subaddress, false); |
manumaet | 18:bbc7ca7d3a95 | 200 | for(int i=0; i<length; i++) // get data |
manumaet | 0:f50e671ffff7 | 201 | buffer[i] = spi.write(0x00); |
manumaet | 0:f50e671ffff7 | 202 | deselect(); |
manumaet | 0:f50e671ffff7 | 203 | } |
manumaet | 0:f50e671ffff7 | 204 | |
manumaet | 8:7a9c61242e2f | 205 | void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 206 | setupTransaction(reg, subaddress, true); |
manumaet | 18:bbc7ca7d3a95 | 207 | for(int i=0; i<length; i++) // put data |
manumaet | 0:f50e671ffff7 | 208 | spi.write(buffer[i]); |
manumaet | 0:f50e671ffff7 | 209 | deselect(); |
manumaet | 0:f50e671ffff7 | 210 | } |
manumaet | 0:f50e671ffff7 | 211 | |
manumaet | 8:7a9c61242e2f | 212 | void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) { |
manumaet | 18:bbc7ca7d3a95 | 213 | reg |= (write * DW1000_WRITE_FLAG); // set read/write flag |
manumaet | 0:f50e671ffff7 | 214 | select(); |
manumaet | 0:f50e671ffff7 | 215 | if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte |
manumaet | 0:f50e671ffff7 | 216 | spi.write(reg | DW1000_SUBADDRESS_FLAG); |
manumaet | 18:bbc7ca7d3a95 | 217 | if (subaddress > 0x7F) { // sub address too long, we need to set flag and send third header byte |
manumaet | 18:bbc7ca7d3a95 | 218 | spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); // and |
manumaet | 0:f50e671ffff7 | 219 | spi.write((uint8_t)(subaddress >> 7)); |
manumaet | 0:f50e671ffff7 | 220 | } else { |
manumaet | 0:f50e671ffff7 | 221 | spi.write((uint8_t)subaddress); |
manumaet | 0:f50e671ffff7 | 222 | } |
manumaet | 0:f50e671ffff7 | 223 | } else { |
manumaet | 18:bbc7ca7d3a95 | 224 | spi.write(reg); // say which register address we want to access |
manumaet | 0:f50e671ffff7 | 225 | } |
manumaet | 0:f50e671ffff7 | 226 | } |
manumaet | 0:f50e671ffff7 | 227 | |
manumaet | 39:bb57aa77b015 | 228 | void DW1000::select() { // always called to start an SPI transmission |
manumaet | 39:bb57aa77b015 | 229 | irq.disable_irq(); // disable interrupts from DW1000 during SPI becaus this leads to crashes! TODO: if you have other interrupt handlers attached on the micro controller, they could also interfere. |
manumaet | 39:bb57aa77b015 | 230 | cs = 0; // set Cable Select pin low to start transmission |
manumaet | 39:bb57aa77b015 | 231 | } |
manumaet | 39:bb57aa77b015 | 232 | void DW1000::deselect() { // always called to end an SPI transmission |
manumaet | 39:bb57aa77b015 | 233 | cs = 1; // set Cable Select pin high to stop transmission |
manumaet | 39:bb57aa77b015 | 234 | irq.enable_irq(); // reenable the interrupt handler |
manumaet | 39:bb57aa77b015 | 235 | } |