Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
DW1000/DW1000.cpp@15:e1fea7e2aff1, 2014-11-23 (annotated)
- Committer:
- manumaet
- Date:
- Sun Nov 23 20:45:14 2014 +0000
- Revision:
- 15:e1fea7e2aff1
- Parent:
- 13:b4d27bf7062a
- Child:
- 17:8afa5f9122da
sending of 1020 chars in one frame worked
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 | 0:f50e671ffff7 | 4 | deselect(); // Chip must be deselected first |
manumaet | 0:f50e671ffff7 | 5 | 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 | 6 | spi.frequency(1000000); // with a 1MHz clock rate (worked up to 49MHz in our Test) |
manumaet | 7:e634eeafc4d2 | 7 | |
manumaet | 12:985aa9843c3c | 8 | //resetAll(); // we can do a soft reset if we want to (only needed for debugging) |
manumaet | 12:985aa9843c3c | 9 | 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 | 12:985aa9843c3c | 10 | writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error |
manumaet | 15:e1fea7e2aff1 | 11 | writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames TODO: doesn't work!! |
manumaet | 11:c87d37db2c6f | 12 | |
manumaet | 8:7a9c61242e2f | 13 | irq.rise(this, &DW1000::ISR); // attach Interrupt handler to rising edge |
manumaet | 0:f50e671ffff7 | 14 | } |
manumaet | 0:f50e671ffff7 | 15 | |
manumaet | 0:f50e671ffff7 | 16 | uint32_t DW1000::getDeviceID() { |
manumaet | 0:f50e671ffff7 | 17 | uint32_t result; |
manumaet | 0:f50e671ffff7 | 18 | readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); |
manumaet | 0:f50e671ffff7 | 19 | return result; |
manumaet | 0:f50e671ffff7 | 20 | } |
manumaet | 0:f50e671ffff7 | 21 | |
manumaet | 0:f50e671ffff7 | 22 | uint64_t DW1000::getEUI() { |
manumaet | 0:f50e671ffff7 | 23 | uint64_t result; |
manumaet | 0:f50e671ffff7 | 24 | readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); |
manumaet | 0:f50e671ffff7 | 25 | return result; |
manumaet | 0:f50e671ffff7 | 26 | } |
manumaet | 0:f50e671ffff7 | 27 | |
manumaet | 0:f50e671ffff7 | 28 | void DW1000::setEUI(uint64_t EUI) { |
manumaet | 0:f50e671ffff7 | 29 | writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); |
manumaet | 0:f50e671ffff7 | 30 | } |
manumaet | 0:f50e671ffff7 | 31 | |
manumaet | 0:f50e671ffff7 | 32 | float DW1000::getVoltage() { |
manumaet | 12:985aa9843c3c | 33 | uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form User Manual p57 |
manumaet | 0:f50e671ffff7 | 34 | writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); |
manumaet | 0:f50e671ffff7 | 35 | writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); |
manumaet | 0:f50e671ffff7 | 36 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); |
manumaet | 0:f50e671ffff7 | 37 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); |
manumaet | 8:7a9c61242e2f | 38 | readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature |
manumaet | 0:f50e671ffff7 | 39 | float Voltage = buffer[5] * 0.0057 + 2.3; |
manumaet | 8:7a9c61242e2f | 40 | 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 | 41 | return Voltage; |
manumaet | 0:f50e671ffff7 | 42 | } |
manumaet | 0:f50e671ffff7 | 43 | |
manumaet | 10:d077bb12d259 | 44 | void DW1000::sendString(char* message) { |
manumaet | 10:d077bb12d259 | 45 | sendFrame((uint8_t*)message, strlen(message)+1); |
manumaet | 10:d077bb12d259 | 46 | } |
manumaet | 10:d077bb12d259 | 47 | |
manumaet | 10:d077bb12d259 | 48 | char* DW1000::receiveString() { |
manumaet | 10:d077bb12d259 | 49 | uint16_t framelength = 0; // get framelength |
manumaet | 10:d077bb12d259 | 50 | readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2); |
manumaet | 10:d077bb12d259 | 51 | framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes |
manumaet | 10:d077bb12d259 | 52 | char* receive = new char[framelength]; // get data from buffer |
manumaet | 10:d077bb12d259 | 53 | readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)receive, framelength); |
manumaet | 10:d077bb12d259 | 54 | return receive; |
manumaet | 10:d077bb12d259 | 55 | } |
manumaet | 10:d077bb12d259 | 56 | |
manumaet | 11:c87d37db2c6f | 57 | void DW1000::sendFrame(uint8_t* message, uint16_t length) { |
manumaet | 13:b4d27bf7062a | 58 | writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode TODO: only if receiving!! |
manumaet | 13:b4d27bf7062a | 59 | writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer |
manumaet | 7:e634eeafc4d2 | 60 | |
manumaet | 13:b4d27bf7062a | 61 | uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame including 2 CRC Bytes |
manumaet | 13:b4d27bf7062a | 62 | length += 2; |
manumaet | 11:c87d37db2c6f | 63 | length = ((backup & 0xFC) << 8) | (length & 0x03FF); |
manumaet | 7:e634eeafc4d2 | 64 | |
manumaet | 11:c87d37db2c6f | 65 | writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2); // TODO: make that bigger frames than 256 can be sent |
manumaet | 11:c87d37db2c6f | 66 | |
manumaet | 11:c87d37db2c6f | 67 | writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit |
manumaet | 13:b4d27bf7062a | 68 | receiveFrame(); // TODO: only if receiving!! |
manumaet | 8:7a9c61242e2f | 69 | } |
manumaet | 8:7a9c61242e2f | 70 | |
manumaet | 8:7a9c61242e2f | 71 | void DW1000::receiveFrame() { |
manumaet | 13:b4d27bf7062a | 72 | writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit |
manumaet | 7:e634eeafc4d2 | 73 | } |
manumaet | 7:e634eeafc4d2 | 74 | |
manumaet | 7:e634eeafc4d2 | 75 | void DW1000::ISR() { |
manumaet | 12:985aa9843c3c | 76 | uint64_t status; // get the entire system status |
manumaet | 12:985aa9843c3c | 77 | readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5); |
manumaet | 12:985aa9843c3c | 78 | status &= 0xFFFFFFFFFF; // only 40-Bit |
manumaet | 12:985aa9843c3c | 79 | if (status & 0x4000) |
manumaet | 12:985aa9843c3c | 80 | callbackRX(); |
manumaet | 12:985aa9843c3c | 81 | if (status & 0x80) |
manumaet | 12:985aa9843c3c | 82 | ;//callbackTX(); // TODO: mask TX done interrupt make TX handler |
manumaet | 7:e634eeafc4d2 | 83 | } |
manumaet | 7:e634eeafc4d2 | 84 | |
manumaet | 12:985aa9843c3c | 85 | void DW1000::loadLDE() { |
manumaet | 12:985aa9843c3c | 86 | uint16_t ldeload[] = {0x0301, 0x8000, 0x0200}; // initialise LDE algorithm LDELOAD User Manual p22 |
manumaet | 12:985aa9843c3c | 87 | writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[0], 2); // set clock to XTAL so OTP is reliable |
manumaet | 12:985aa9843c3c | 88 | writeRegister(DW1000_OTP_IF, 0x06, (uint8_t*)&ldeload[1], 2); // set LDELOAD bit in OTP |
manumaet | 12:985aa9843c3c | 89 | wait_us(150); |
manumaet | 12:985aa9843c3c | 90 | writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[2], 2); // recover to PLL clock |
manumaet | 12:985aa9843c3c | 91 | } |
manumaet | 12:985aa9843c3c | 92 | |
manumaet | 12:985aa9843c3c | 93 | void DW1000::resetRX() { |
manumaet | 12:985aa9843c3c | 94 | writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset |
manumaet | 12:985aa9843c3c | 95 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset |
manumaet | 12:985aa9843c3c | 96 | } |
manumaet | 12:985aa9843c3c | 97 | |
manumaet | 12:985aa9843c3c | 98 | void DW1000::resetAll() { |
manumaet | 12:985aa9843c3c | 99 | writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL |
manumaet | 12:985aa9843c3c | 100 | writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset |
manumaet | 12:985aa9843c3c | 101 | wait_us(10); // wait for PLL to lock |
manumaet | 12:985aa9843c3c | 102 | writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset |
manumaet | 7:e634eeafc4d2 | 103 | } |
manumaet | 0:f50e671ffff7 | 104 | |
manumaet | 0:f50e671ffff7 | 105 | // SPI Interface ------------------------------------------------------------------------------------ |
manumaet | 10:d077bb12d259 | 106 | uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) { |
manumaet | 10:d077bb12d259 | 107 | uint8_t result; |
manumaet | 10:d077bb12d259 | 108 | readRegister(reg, subaddress, &result, 1); |
manumaet | 10:d077bb12d259 | 109 | return result; |
manumaet | 10:d077bb12d259 | 110 | } |
manumaet | 10:d077bb12d259 | 111 | |
manumaet | 8:7a9c61242e2f | 112 | void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) { |
manumaet | 8:7a9c61242e2f | 113 | writeRegister(reg, subaddress, &buffer, 1); |
manumaet | 8:7a9c61242e2f | 114 | } |
manumaet | 8:7a9c61242e2f | 115 | |
manumaet | 8:7a9c61242e2f | 116 | void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 117 | setupTransaction(reg, subaddress, false); |
manumaet | 8:7a9c61242e2f | 118 | for(int i=0; i<length; i++) // get data |
manumaet | 0:f50e671ffff7 | 119 | buffer[i] = spi.write(0x00); |
manumaet | 0:f50e671ffff7 | 120 | deselect(); |
manumaet | 0:f50e671ffff7 | 121 | } |
manumaet | 0:f50e671ffff7 | 122 | |
manumaet | 8:7a9c61242e2f | 123 | void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { |
manumaet | 0:f50e671ffff7 | 124 | setupTransaction(reg, subaddress, true); |
manumaet | 8:7a9c61242e2f | 125 | for(int i=0; i<length; i++) // put data |
manumaet | 0:f50e671ffff7 | 126 | spi.write(buffer[i]); |
manumaet | 0:f50e671ffff7 | 127 | deselect(); |
manumaet | 0:f50e671ffff7 | 128 | } |
manumaet | 0:f50e671ffff7 | 129 | |
manumaet | 8:7a9c61242e2f | 130 | void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) { |
manumaet | 0:f50e671ffff7 | 131 | reg |= (write * DW1000_WRITE_FLAG); |
manumaet | 0:f50e671ffff7 | 132 | select(); |
manumaet | 0:f50e671ffff7 | 133 | if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte |
manumaet | 0:f50e671ffff7 | 134 | spi.write(reg | DW1000_SUBADDRESS_FLAG); |
manumaet | 0:f50e671ffff7 | 135 | if (subaddress > 127) { // sub address too long, we need to set flag and send third header byte |
manumaet | 0:f50e671ffff7 | 136 | spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); |
manumaet | 0:f50e671ffff7 | 137 | spi.write((uint8_t)(subaddress >> 7)); |
manumaet | 0:f50e671ffff7 | 138 | } else { |
manumaet | 0:f50e671ffff7 | 139 | spi.write((uint8_t)subaddress); |
manumaet | 0:f50e671ffff7 | 140 | } |
manumaet | 0:f50e671ffff7 | 141 | } else { |
manumaet | 0:f50e671ffff7 | 142 | spi.write(reg); |
manumaet | 0:f50e671ffff7 | 143 | } |
manumaet | 0:f50e671ffff7 | 144 | } |
manumaet | 0:f50e671ffff7 | 145 | |
manumaet | 12:985aa9843c3c | 146 | void DW1000::select() { cs = 0; } // set CS low to start transmission |
manumaet | 12:985aa9843c3c | 147 | void DW1000::deselect() { cs = 1; } // set CS high to stop transmission |