Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
DW1000/DW1000.cpp@18:bbc7ca7d3a95, 2014-11-24 (annotated)
- Committer:
- manumaet
- Date:
- Mon Nov 24 16:48:03 2014 +0000
- Revision:
- 18:bbc7ca7d3a95
- Parent:
- 17:8afa5f9122da
- Child:
- 19:e94bc88c1eb0
many changes, sadly TX interrupt broken
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 | 8:7a9c61242e2f | 3 | DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : spi(MOSI, MISO, SCLK), cs(CS), irq(IRQ) { |
manumaet | 18:bbc7ca7d3a95 | 4 | receiving = 0; // state in the beginning is not listening for frames |
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 | 18:bbc7ca7d3a95 | 10 | //resetAll(); // we can do a soft reset if we want to (only needed for debugging) |
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 | 15:e1fea7e2aff1 | 15 | writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames TODO: doesn't work!! |
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 | 0:f50e671ffff7 | 20 | uint32_t DW1000::getDeviceID() { |
manumaet | 0:f50e671ffff7 | 21 | uint32_t result; |
manumaet | 0:f50e671ffff7 | 22 | readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); |
manumaet | 0:f50e671ffff7 | 23 | return result; |
manumaet | 0:f50e671ffff7 | 24 | } |
manumaet | 0:f50e671ffff7 | 25 | |
manumaet | 0:f50e671ffff7 | 26 | uint64_t DW1000::getEUI() { |
manumaet | 0:f50e671ffff7 | 27 | uint64_t result; |
manumaet | 0:f50e671ffff7 | 28 | readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); |
manumaet | 0:f50e671ffff7 | 29 | return result; |
manumaet | 0:f50e671ffff7 | 30 | } |
manumaet | 0:f50e671ffff7 | 31 | |
manumaet | 0:f50e671ffff7 | 32 | void DW1000::setEUI(uint64_t EUI) { |
manumaet | 0:f50e671ffff7 | 33 | writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); |
manumaet | 0:f50e671ffff7 | 34 | } |
manumaet | 0:f50e671ffff7 | 35 | |
manumaet | 0:f50e671ffff7 | 36 | float DW1000::getVoltage() { |
manumaet | 12:985aa9843c3c | 37 | uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form User Manual p57 |
manumaet | 0:f50e671ffff7 | 38 | writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); |
manumaet | 0:f50e671ffff7 | 39 | writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); |
manumaet | 0:f50e671ffff7 | 40 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); |
manumaet | 0:f50e671ffff7 | 41 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); |
manumaet | 8:7a9c61242e2f | 42 | readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature |
manumaet | 0:f50e671ffff7 | 43 | float Voltage = buffer[5] * 0.0057 + 2.3; |
manumaet | 8:7a9c61242e2f | 44 | 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 | 45 | return Voltage; |
manumaet | 0:f50e671ffff7 | 46 | } |
manumaet | 0:f50e671ffff7 | 47 | |
manumaet | 18:bbc7ca7d3a95 | 48 | uint64_t DW1000::getStatus() { |
manumaet | 18:bbc7ca7d3a95 | 49 | return readRegister40(DW1000_SYS_STATUS, 0); |
manumaet | 18:bbc7ca7d3a95 | 50 | } |
manumaet | 18:bbc7ca7d3a95 | 51 | |
manumaet | 10:d077bb12d259 | 52 | void DW1000::sendString(char* message) { |
manumaet | 10:d077bb12d259 | 53 | sendFrame((uint8_t*)message, strlen(message)+1); |
manumaet | 10:d077bb12d259 | 54 | } |
manumaet | 10:d077bb12d259 | 55 | |
manumaet | 10:d077bb12d259 | 56 | char* DW1000::receiveString() { |
manumaet | 10:d077bb12d259 | 57 | uint16_t framelength = 0; // get framelength |
manumaet | 10:d077bb12d259 | 58 | readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2); |
manumaet | 10:d077bb12d259 | 59 | framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes |
manumaet | 10:d077bb12d259 | 60 | char* receive = new char[framelength]; // get data from buffer |
manumaet | 10:d077bb12d259 | 61 | readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)receive, framelength); |
manumaet | 10:d077bb12d259 | 62 | return receive; |
manumaet | 10:d077bb12d259 | 63 | } |
manumaet | 10:d077bb12d259 | 64 | |
manumaet | 11:c87d37db2c6f | 65 | void DW1000::sendFrame(uint8_t* message, uint16_t length) { |
manumaet | 17:8afa5f9122da | 66 | if (length >= 1021) length = 1021; |
manumaet | 13:b4d27bf7062a | 67 | writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer |
manumaet | 7:e634eeafc4d2 | 68 | |
manumaet | 13:b4d27bf7062a | 69 | uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame including 2 CRC Bytes |
manumaet | 13:b4d27bf7062a | 70 | length += 2; |
manumaet | 11:c87d37db2c6f | 71 | length = ((backup & 0xFC) << 8) | (length & 0x03FF); |
manumaet | 17:8afa5f9122da | 72 | writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2); |
manumaet | 11:c87d37db2c6f | 73 | |
manumaet | 18:bbc7ca7d3a95 | 74 | if (receiving) stopTRX(); // stop receiving if we are in this state |
manumaet | 11:c87d37db2c6f | 75 | writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit |
manumaet | 18:bbc7ca7d3a95 | 76 | //if (receiving) startRX(); // enable receiver again if we need to preserve state TODO: safe to do this directly ??? only after sending ended |
manumaet | 8:7a9c61242e2f | 77 | } |
manumaet | 8:7a9c61242e2f | 78 | |
manumaet | 17:8afa5f9122da | 79 | void DW1000::startRX() { |
manumaet | 17:8afa5f9122da | 80 | receiving = true; |
manumaet | 13:b4d27bf7062a | 81 | writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit |
manumaet | 7:e634eeafc4d2 | 82 | } |
manumaet | 7:e634eeafc4d2 | 83 | |
manumaet | 17:8afa5f9122da | 84 | void DW1000::stopRX() { |
manumaet | 17:8afa5f9122da | 85 | receiving = false; |
manumaet | 17:8afa5f9122da | 86 | |
manumaet | 17:8afa5f9122da | 87 | } |
manumaet | 17:8afa5f9122da | 88 | |
manumaet | 7:e634eeafc4d2 | 89 | void DW1000::ISR() { |
manumaet | 18:bbc7ca7d3a95 | 90 | uint64_t status = getStatus(); |
manumaet | 18:bbc7ca7d3a95 | 91 | if (status & 0x4000) { |
manumaet | 18:bbc7ca7d3a95 | 92 | callbackRX(getFramelength()); |
manumaet | 18:bbc7ca7d3a95 | 93 | } |
manumaet | 18:bbc7ca7d3a95 | 94 | if (status & 0x80) { |
manumaet | 18:bbc7ca7d3a95 | 95 | if (receiving) startRX(); // enable receiver again if we need to preserve state TODO: have to do it here?? |
manumaet | 18:bbc7ca7d3a95 | 96 | callbackTX(); |
manumaet | 18:bbc7ca7d3a95 | 97 | } |
manumaet | 7:e634eeafc4d2 | 98 | } |
manumaet | 7:e634eeafc4d2 | 99 | |
manumaet | 18:bbc7ca7d3a95 | 100 | uint16_t DW1000::getFramelength() { |
manumaet | 18:bbc7ca7d3a95 | 101 | uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength TODO: get this from isr |
manumaet | 18:bbc7ca7d3a95 | 102 | framelength &= 0x03FF; |
manumaet | 18:bbc7ca7d3a95 | 103 | framelength -= 2; |
manumaet | 18:bbc7ca7d3a95 | 104 | return framelength; |
manumaet | 18:bbc7ca7d3a95 | 105 | } |
manumaet | 18:bbc7ca7d3a95 | 106 | |
manumaet | 18:bbc7ca7d3a95 | 107 | void DW1000::loadLDE() { // initialise LDE algorithm LDELOAD User Manual p22 |
manumaet | 18:bbc7ca7d3a95 | 108 | writeRegister16(DW1000_PMSC, 0, 0x0301); // set clock to XTAL so OTP is reliable |
manumaet | 18:bbc7ca7d3a95 | 109 | writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP |
manumaet | 12:985aa9843c3c | 110 | wait_us(150); |
manumaet | 18:bbc7ca7d3a95 | 111 | writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock |
manumaet | 12:985aa9843c3c | 112 | } |
manumaet | 12:985aa9843c3c | 113 | |
manumaet | 17:8afa5f9122da | 114 | void DW1000::stopTRX() { |
manumaet | 17:8afa5f9122da | 115 | writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode |
manumaet | 17:8afa5f9122da | 116 | } |
manumaet | 17:8afa5f9122da | 117 | |
manumaet | 12:985aa9843c3c | 118 | void DW1000::resetRX() { |
manumaet | 12:985aa9843c3c | 119 | writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset |
manumaet | 12:985aa9843c3c | 120 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset |
manumaet | 12:985aa9843c3c | 121 | } |
manumaet | 12:985aa9843c3c | 122 | |
manumaet | 12:985aa9843c3c | 123 | void DW1000::resetAll() { |
manumaet | 12:985aa9843c3c | 124 | writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL |
manumaet | 12:985aa9843c3c | 125 | writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset |
manumaet | 12:985aa9843c3c | 126 | wait_us(10); // wait for PLL to lock |
manumaet | 12:985aa9843c3c | 127 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset |
manumaet | 7:e634eeafc4d2 | 128 | } |
manumaet | 0:f50e671ffff7 | 129 | |
manumaet | 0:f50e671ffff7 | 130 | // SPI Interface ------------------------------------------------------------------------------------ |
manumaet | 10:d077bb12d259 | 131 | uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) { |
manumaet | 10:d077bb12d259 | 132 | uint8_t result; |
manumaet | 10:d077bb12d259 | 133 | readRegister(reg, subaddress, &result, 1); |
manumaet | 10:d077bb12d259 | 134 | return result; |
manumaet | 10:d077bb12d259 | 135 | } |
manumaet | 10:d077bb12d259 | 136 | |
manumaet | 18:bbc7ca7d3a95 | 137 | uint16_t DW1000::readRegister16(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 138 | uint16_t result; |
manumaet | 18:bbc7ca7d3a95 | 139 | readRegister(reg, subaddress, (uint8_t*)&result, 2); |
manumaet | 18:bbc7ca7d3a95 | 140 | return result; |
manumaet | 18:bbc7ca7d3a95 | 141 | } |
manumaet | 18:bbc7ca7d3a95 | 142 | |
manumaet | 18:bbc7ca7d3a95 | 143 | uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) { |
manumaet | 18:bbc7ca7d3a95 | 144 | uint64_t result; |
manumaet | 18:bbc7ca7d3a95 | 145 | readRegister(reg, subaddress, (uint8_t*)&result, 5); |
manumaet | 18:bbc7ca7d3a95 | 146 | result &= 0xFFFFFFFFFF; // only 40-Bit |
manumaet | 18:bbc7ca7d3a95 | 147 | return result; |
manumaet | 18:bbc7ca7d3a95 | 148 | } |
manumaet | 18:bbc7ca7d3a95 | 149 | |
manumaet | 8:7a9c61242e2f | 150 | void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) { |
manumaet | 8:7a9c61242e2f | 151 | writeRegister(reg, subaddress, &buffer, 1); |
manumaet | 8:7a9c61242e2f | 152 | } |
manumaet | 8:7a9c61242e2f | 153 | |
manumaet | 18:bbc7ca7d3a95 | 154 | void DW1000::writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer) { |
manumaet | 18:bbc7ca7d3a95 | 155 | writeRegister(reg, subaddress, (uint8_t*)&buffer, 2); |
manumaet | 18:bbc7ca7d3a95 | 156 | } |
manumaet | 18:bbc7ca7d3a95 | 157 | |
manumaet | 8:7a9c61242e2f | 158 | void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 159 | setupTransaction(reg, subaddress, false); |
manumaet | 18:bbc7ca7d3a95 | 160 | for(int i=0; i<length; i++) // get data |
manumaet | 0:f50e671ffff7 | 161 | buffer[i] = spi.write(0x00); |
manumaet | 0:f50e671ffff7 | 162 | deselect(); |
manumaet | 0:f50e671ffff7 | 163 | } |
manumaet | 0:f50e671ffff7 | 164 | |
manumaet | 8:7a9c61242e2f | 165 | void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 166 | setupTransaction(reg, subaddress, true); |
manumaet | 18:bbc7ca7d3a95 | 167 | for(int i=0; i<length; i++) // put data |
manumaet | 0:f50e671ffff7 | 168 | spi.write(buffer[i]); |
manumaet | 0:f50e671ffff7 | 169 | deselect(); |
manumaet | 0:f50e671ffff7 | 170 | } |
manumaet | 0:f50e671ffff7 | 171 | |
manumaet | 8:7a9c61242e2f | 172 | void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) { |
manumaet | 18:bbc7ca7d3a95 | 173 | reg |= (write * DW1000_WRITE_FLAG); // set read/write flag |
manumaet | 0:f50e671ffff7 | 174 | select(); |
manumaet | 0:f50e671ffff7 | 175 | if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte |
manumaet | 0:f50e671ffff7 | 176 | spi.write(reg | DW1000_SUBADDRESS_FLAG); |
manumaet | 18:bbc7ca7d3a95 | 177 | if (subaddress > 0x7F) { // sub address too long, we need to set flag and send third header byte |
manumaet | 18:bbc7ca7d3a95 | 178 | spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); // and |
manumaet | 0:f50e671ffff7 | 179 | spi.write((uint8_t)(subaddress >> 7)); |
manumaet | 0:f50e671ffff7 | 180 | } else { |
manumaet | 0:f50e671ffff7 | 181 | spi.write((uint8_t)subaddress); |
manumaet | 0:f50e671ffff7 | 182 | } |
manumaet | 0:f50e671ffff7 | 183 | } else { |
manumaet | 18:bbc7ca7d3a95 | 184 | spi.write(reg); // say which register address we want to access |
manumaet | 0:f50e671ffff7 | 185 | } |
manumaet | 0:f50e671ffff7 | 186 | } |
manumaet | 0:f50e671ffff7 | 187 | |
manumaet | 18:bbc7ca7d3a95 | 188 | void DW1000::select() { cs = 0; } // set CS low to start transmission |
manumaet | 18:bbc7ca7d3a95 | 189 | void DW1000::deselect() { cs = 1; } // set CS high to stop transmission |