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