Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
DW1000/DW1000.cpp@37:40f94c634c3e, 2015-02-20 (annotated)
- Committer:
- manumaet
- Date:
- Fri Feb 20 13:21:06 2015 +0000
- Revision:
- 37:40f94c634c3e
- Parent:
- 36:883de6f9a73b
- Child:
- 38:8ef3b8d8b908
implemented source/destination adresses and filtering
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 | 12:985aa9843c3c | 11 | 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 | 18:bbc7ca7d3a95 | 12 | |
manumaet | 18:bbc7ca7d3a95 | 13 | // Configuration TODO: make method for that |
manumaet | 12:985aa9843c3c | 14 | writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error |
manumaet | 36:883de6f9a73b | 15 | //writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames TODO: is this really what stated in the comment? |
manumaet | 11:c87d37db2c6f | 16 | |
manumaet | 8:7a9c61242e2f | 17 | irq.rise(this, &DW1000::ISR); // attach Interrupt handler to rising edge |
manumaet | 0:f50e671ffff7 | 18 | } |
manumaet | 0:f50e671ffff7 | 19 | |
manumaet | 29:019ff388ed76 | 20 | void DW1000::setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void)) { |
manumaet | 29:019ff388ed76 | 21 | bool RX = false; |
manumaet | 29:019ff388ed76 | 22 | bool TX = false; |
manumaet | 29:019ff388ed76 | 23 | if (callbackRX) { |
manumaet | 29:019ff388ed76 | 24 | DW1000::callbackRX.attach(callbackRX); |
manumaet | 29:019ff388ed76 | 25 | RX = true; |
manumaet | 29:019ff388ed76 | 26 | } |
manumaet | 29:019ff388ed76 | 27 | if (callbackTX) { |
manumaet | 29:019ff388ed76 | 28 | DW1000::callbackTX.attach(callbackTX); |
manumaet | 29:019ff388ed76 | 29 | TX = true; |
manumaet | 29:019ff388ed76 | 30 | } |
manumaet | 29:019ff388ed76 | 31 | setInterrupt(RX,TX); |
manumaet | 26:a65c6f26c458 | 32 | } |
manumaet | 26:a65c6f26c458 | 33 | |
manumaet | 0:f50e671ffff7 | 34 | uint32_t DW1000::getDeviceID() { |
manumaet | 0:f50e671ffff7 | 35 | uint32_t result; |
manumaet | 0:f50e671ffff7 | 36 | readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); |
manumaet | 0:f50e671ffff7 | 37 | return result; |
manumaet | 0:f50e671ffff7 | 38 | } |
manumaet | 0:f50e671ffff7 | 39 | |
manumaet | 0:f50e671ffff7 | 40 | uint64_t DW1000::getEUI() { |
manumaet | 0:f50e671ffff7 | 41 | uint64_t result; |
manumaet | 0:f50e671ffff7 | 42 | readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); |
manumaet | 0:f50e671ffff7 | 43 | return result; |
manumaet | 0:f50e671ffff7 | 44 | } |
manumaet | 0:f50e671ffff7 | 45 | |
manumaet | 0:f50e671ffff7 | 46 | void DW1000::setEUI(uint64_t EUI) { |
manumaet | 0:f50e671ffff7 | 47 | writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); |
manumaet | 0:f50e671ffff7 | 48 | } |
manumaet | 0:f50e671ffff7 | 49 | |
manumaet | 0:f50e671ffff7 | 50 | float DW1000::getVoltage() { |
manumaet | 12:985aa9843c3c | 51 | uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form User Manual p57 |
manumaet | 0:f50e671ffff7 | 52 | writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); |
manumaet | 0:f50e671ffff7 | 53 | writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); |
manumaet | 0:f50e671ffff7 | 54 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); |
manumaet | 0:f50e671ffff7 | 55 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); |
manumaet | 8:7a9c61242e2f | 56 | readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature |
manumaet | 0:f50e671ffff7 | 57 | float Voltage = buffer[5] * 0.0057 + 2.3; |
manumaet | 20:257d56530ae1 | 58 | //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 | 59 | return Voltage; |
manumaet | 0:f50e671ffff7 | 60 | } |
manumaet | 0:f50e671ffff7 | 61 | |
manumaet | 18:bbc7ca7d3a95 | 62 | uint64_t DW1000::getStatus() { |
manumaet | 18:bbc7ca7d3a95 | 63 | return readRegister40(DW1000_SYS_STATUS, 0); |
manumaet | 18:bbc7ca7d3a95 | 64 | } |
manumaet | 18:bbc7ca7d3a95 | 65 | |
manumaet | 26:a65c6f26c458 | 66 | uint64_t DW1000::getRXTimestamp() { |
manumaet | 26:a65c6f26c458 | 67 | return readRegister40(DW1000_RX_TIME, 0); |
manumaet | 26:a65c6f26c458 | 68 | } |
manumaet | 26:a65c6f26c458 | 69 | |
manumaet | 26:a65c6f26c458 | 70 | uint64_t DW1000::getTXTimestamp() { |
manumaet | 26:a65c6f26c458 | 71 | return readRegister40(DW1000_TX_TIME, 0); |
manumaet | 26:a65c6f26c458 | 72 | } |
manumaet | 26:a65c6f26c458 | 73 | |
manumaet | 10:d077bb12d259 | 74 | void DW1000::sendString(char* message) { |
manumaet | 10:d077bb12d259 | 75 | sendFrame((uint8_t*)message, strlen(message)+1); |
manumaet | 10:d077bb12d259 | 76 | } |
manumaet | 10:d077bb12d259 | 77 | |
manumaet | 24:6f25ba679490 | 78 | void DW1000::receiveString(char* message) { |
manumaet | 31:6f76f3d518ac | 79 | readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)message, getFramelength()); // get data from buffer |
manumaet | 10:d077bb12d259 | 80 | } |
manumaet | 10:d077bb12d259 | 81 | |
manumaet | 11:c87d37db2c6f | 82 | void DW1000::sendFrame(uint8_t* message, uint16_t length) { |
manumaet | 20:257d56530ae1 | 83 | if (length >= 1021) length = 1021; // check for maximim length a frame can have TODO: 127 Byte mode? |
manumaet | 13:b4d27bf7062a | 84 | writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer |
manumaet | 7:e634eeafc4d2 | 85 | |
manumaet | 37:40f94c634c3e | 86 | #if 0 // switch draft for slower data rate and original working 6.8Mbps |
manumaet | 37:40f94c634c3e | 87 | uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame |
manumaet | 37:40f94c634c3e | 88 | length += 2; // including 2 CRC Bytes |
manumaet | 37:40f94c634c3e | 89 | //length = ((backup & 0xFC) << 8) | (length & 0x03FF); |
manumaet | 37:40f94c634c3e | 90 | length = ((0xA0 & 0xFC) << 8) | (length & 0x03FF); // for slower data rate and therefore more range TODO: put in a modular configuration not a fixed value |
manumaet | 37:40f94c634c3e | 91 | writeRegister16(DW1000_TX_FCTRL, 0, length); |
manumaet | 37:40f94c634c3e | 92 | |
manumaet | 37:40f94c634c3e | 93 | backup = readRegister8(DW1000_TX_FCTRL, 2); // change preamble length |
manumaet | 37:40f94c634c3e | 94 | uint8_t preamble_reg = (backup & 0xC0) | (0x29 & 0x3F); // for longer preamble to match slower data rate TODO: put in a modular configuration not a fixed value |
manumaet | 37:40f94c634c3e | 95 | writeRegister8(DW1000_TX_FCTRL, 2, preamble_reg); |
manumaet | 37:40f94c634c3e | 96 | #else |
manumaet | 37:40f94c634c3e | 97 | uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame |
manumaet | 37:40f94c634c3e | 98 | length += 2; // including 2 CRC Bytes |
manumaet | 37:40f94c634c3e | 99 | length = ((backup & 0xFC) << 8) | (length & 0x03FF); |
manumaet | 37:40f94c634c3e | 100 | writeRegister16(DW1000_TX_FCTRL, 0, length); |
manumaet | 37:40f94c634c3e | 101 | #endif |
manumaet | 11:c87d37db2c6f | 102 | |
manumaet | 25:d58b0595b300 | 103 | stopTRX(); // stop receiving |
manumaet | 23:661a79e56208 | 104 | writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit |
manumaet | 25:d58b0595b300 | 105 | startRX(); // enable receiver again |
manumaet | 8:7a9c61242e2f | 106 | } |
manumaet | 8:7a9c61242e2f | 107 | |
manumaet | 17:8afa5f9122da | 108 | void DW1000::startRX() { |
manumaet | 20:257d56530ae1 | 109 | writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit |
manumaet | 7:e634eeafc4d2 | 110 | } |
manumaet | 7:e634eeafc4d2 | 111 | |
manumaet | 25:d58b0595b300 | 112 | void DW1000::stopTRX() { |
manumaet | 25:d58b0595b300 | 113 | writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode |
manumaet | 17:8afa5f9122da | 114 | } |
manumaet | 17:8afa5f9122da | 115 | |
manumaet | 20:257d56530ae1 | 116 | // PRIVATE Methods ------------------------------------------------------------------------------------ |
manumaet | 18:bbc7ca7d3a95 | 117 | void DW1000::loadLDE() { // initialise LDE algorithm LDELOAD User Manual p22 |
manumaet | 18:bbc7ca7d3a95 | 118 | writeRegister16(DW1000_PMSC, 0, 0x0301); // set clock to XTAL so OTP is reliable |
manumaet | 20:257d56530ae1 | 119 | writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP |
manumaet | 12:985aa9843c3c | 120 | wait_us(150); |
manumaet | 18:bbc7ca7d3a95 | 121 | writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock |
manumaet | 12:985aa9843c3c | 122 | } |
manumaet | 12:985aa9843c3c | 123 | |
manumaet | 12:985aa9843c3c | 124 | void DW1000::resetRX() { |
manumaet | 12:985aa9843c3c | 125 | writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset |
manumaet | 12:985aa9843c3c | 126 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset |
manumaet | 12:985aa9843c3c | 127 | } |
manumaet | 12:985aa9843c3c | 128 | |
manumaet | 12:985aa9843c3c | 129 | void DW1000::resetAll() { |
manumaet | 12:985aa9843c3c | 130 | writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL |
manumaet | 12:985aa9843c3c | 131 | writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset |
manumaet | 12:985aa9843c3c | 132 | wait_us(10); // wait for PLL to lock |
manumaet | 12:985aa9843c3c | 133 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset |
manumaet | 7:e634eeafc4d2 | 134 | } |
manumaet | 0:f50e671ffff7 | 135 | |
manumaet | 29:019ff388ed76 | 136 | |
manumaet | 29:019ff388ed76 | 137 | void DW1000::setInterrupt(bool RX, bool TX) { |
manumaet | 29:019ff388ed76 | 138 | writeRegister16(DW1000_SYS_MASK, 0, RX*0x4000 | TX*0x0080); // RX good frame 0x4000, TX done 0x0080 |
manumaet | 29:019ff388ed76 | 139 | } |
manumaet | 29:019ff388ed76 | 140 | |
manumaet | 20:257d56530ae1 | 141 | void DW1000::ISR() { |
manumaet | 20:257d56530ae1 | 142 | uint64_t status = getStatus(); |
manumaet | 22:576ee999b004 | 143 | if (status & 0x4000) { // a frame was received |
manumaet | 29:019ff388ed76 | 144 | callbackRX.call(); |
manumaet | 22:576ee999b004 | 145 | writeRegister16(DW1000_SYS_STATUS, 0, 0x6F00); // clearing of receiving status bits |
manumaet | 20:257d56530ae1 | 146 | } |
manumaet | 22:576ee999b004 | 147 | if (status & 0x80) { // sending complete |
manumaet | 29:019ff388ed76 | 148 | callbackTX.call(); |
manumaet | 22:576ee999b004 | 149 | writeRegister8(DW1000_SYS_STATUS, 0, 0xF8); // clearing of sending status bits |
manumaet | 20:257d56530ae1 | 150 | } |
manumaet | 20:257d56530ae1 | 151 | } |
manumaet | 20:257d56530ae1 | 152 | |
manumaet | 20:257d56530ae1 | 153 | uint16_t DW1000::getFramelength() { |
manumaet | 20:257d56530ae1 | 154 | uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength |
manumaet | 20:257d56530ae1 | 155 | framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes |
manumaet | 20:257d56530ae1 | 156 | return framelength; |
manumaet | 20:257d56530ae1 | 157 | } |
manumaet | 20:257d56530ae1 | 158 | |
manumaet | 0:f50e671ffff7 | 159 | // SPI Interface ------------------------------------------------------------------------------------ |
manumaet | 10:d077bb12d259 | 160 | uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) { |
manumaet | 10:d077bb12d259 | 161 | uint8_t result; |
manumaet | 10:d077bb12d259 | 162 | readRegister(reg, subaddress, &result, 1); |
manumaet | 10:d077bb12d259 | 163 | return result; |
manumaet | 10:d077bb12d259 | 164 | } |
manumaet | 10:d077bb12d259 | 165 | |
manumaet | 18:bbc7ca7d3a95 | 166 | uint16_t DW1000::readRegister16(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 167 | uint16_t result; |
manumaet | 18:bbc7ca7d3a95 | 168 | readRegister(reg, subaddress, (uint8_t*)&result, 2); |
manumaet | 18:bbc7ca7d3a95 | 169 | return result; |
manumaet | 18:bbc7ca7d3a95 | 170 | } |
manumaet | 18:bbc7ca7d3a95 | 171 | |
manumaet | 18:bbc7ca7d3a95 | 172 | uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 173 | uint64_t result; |
manumaet | 18:bbc7ca7d3a95 | 174 | readRegister(reg, subaddress, (uint8_t*)&result, 5); |
manumaet | 18:bbc7ca7d3a95 | 175 | result &= 0xFFFFFFFFFF; // only 40-Bit |
manumaet | 18:bbc7ca7d3a95 | 176 | return result; |
manumaet | 18:bbc7ca7d3a95 | 177 | } |
manumaet | 18:bbc7ca7d3a95 | 178 | |
manumaet | 8:7a9c61242e2f | 179 | void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) { |
manumaet | 8:7a9c61242e2f | 180 | writeRegister(reg, subaddress, &buffer, 1); |
manumaet | 8:7a9c61242e2f | 181 | } |
manumaet | 8:7a9c61242e2f | 182 | |
manumaet | 18:bbc7ca7d3a95 | 183 | void DW1000::writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer) { |
manumaet | 18:bbc7ca7d3a95 | 184 | writeRegister(reg, subaddress, (uint8_t*)&buffer, 2); |
manumaet | 18:bbc7ca7d3a95 | 185 | } |
manumaet | 18:bbc7ca7d3a95 | 186 | |
manumaet | 8:7a9c61242e2f | 187 | void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 188 | setupTransaction(reg, subaddress, false); |
manumaet | 18:bbc7ca7d3a95 | 189 | for(int i=0; i<length; i++) // get data |
manumaet | 0:f50e671ffff7 | 190 | buffer[i] = spi.write(0x00); |
manumaet | 0:f50e671ffff7 | 191 | deselect(); |
manumaet | 0:f50e671ffff7 | 192 | } |
manumaet | 0:f50e671ffff7 | 193 | |
manumaet | 8:7a9c61242e2f | 194 | void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 195 | setupTransaction(reg, subaddress, true); |
manumaet | 18:bbc7ca7d3a95 | 196 | for(int i=0; i<length; i++) // put data |
manumaet | 0:f50e671ffff7 | 197 | spi.write(buffer[i]); |
manumaet | 0:f50e671ffff7 | 198 | deselect(); |
manumaet | 0:f50e671ffff7 | 199 | } |
manumaet | 0:f50e671ffff7 | 200 | |
manumaet | 8:7a9c61242e2f | 201 | void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) { |
manumaet | 18:bbc7ca7d3a95 | 202 | reg |= (write * DW1000_WRITE_FLAG); // set read/write flag |
manumaet | 0:f50e671ffff7 | 203 | select(); |
manumaet | 0:f50e671ffff7 | 204 | if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte |
manumaet | 0:f50e671ffff7 | 205 | spi.write(reg | DW1000_SUBADDRESS_FLAG); |
manumaet | 18:bbc7ca7d3a95 | 206 | if (subaddress > 0x7F) { // sub address too long, we need to set flag and send third header byte |
manumaet | 18:bbc7ca7d3a95 | 207 | spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); // and |
manumaet | 0:f50e671ffff7 | 208 | spi.write((uint8_t)(subaddress >> 7)); |
manumaet | 0:f50e671ffff7 | 209 | } else { |
manumaet | 0:f50e671ffff7 | 210 | spi.write((uint8_t)subaddress); |
manumaet | 0:f50e671ffff7 | 211 | } |
manumaet | 0:f50e671ffff7 | 212 | } else { |
manumaet | 18:bbc7ca7d3a95 | 213 | spi.write(reg); // say which register address we want to access |
manumaet | 0:f50e671ffff7 | 214 | } |
manumaet | 0:f50e671ffff7 | 215 | } |
manumaet | 0:f50e671ffff7 | 216 | |
manumaet | 18:bbc7ca7d3a95 | 217 | void DW1000::select() { cs = 0; } // set CS low to start transmission |
manumaet | 18:bbc7ca7d3a95 | 218 | void DW1000::deselect() { cs = 1; } // set CS high to stop transmission |