Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
DW1000/DW1000.cpp@4:6240b9c7a033, 2014-11-18 (annotated)
- Committer:
- manumaet
- Date:
- Tue Nov 18 12:44:22 2014 +0000
- Revision:
- 4:6240b9c7a033
- Parent:
- 0:f50e671ffff7
- Child:
- 7:e634eeafc4d2
managed to get multiple frames via resetRX()
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 | 0:f50e671ffff7 | 3 | DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS) : spi(MOSI, MISO, SCLK), cs(CS) |
manumaet | 0:f50e671ffff7 | 4 | { |
manumaet | 0:f50e671ffff7 | 5 | deselect(); // Chip must be deselected first |
manumaet | 0:f50e671ffff7 | 6 | 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 | 7 | spi.frequency(1000000); // with a 1MHz clock rate (worked up to 49MHz in our Test) |
manumaet | 0:f50e671ffff7 | 8 | } |
manumaet | 0:f50e671ffff7 | 9 | |
manumaet | 0:f50e671ffff7 | 10 | uint32_t DW1000::getDeviceID() { |
manumaet | 0:f50e671ffff7 | 11 | uint32_t result; |
manumaet | 0:f50e671ffff7 | 12 | readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); |
manumaet | 0:f50e671ffff7 | 13 | return result; |
manumaet | 0:f50e671ffff7 | 14 | } |
manumaet | 0:f50e671ffff7 | 15 | |
manumaet | 0:f50e671ffff7 | 16 | uint64_t DW1000::getEUI() { |
manumaet | 0:f50e671ffff7 | 17 | uint64_t result; |
manumaet | 0:f50e671ffff7 | 18 | readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); |
manumaet | 0:f50e671ffff7 | 19 | return result; |
manumaet | 0:f50e671ffff7 | 20 | } |
manumaet | 0:f50e671ffff7 | 21 | |
manumaet | 0:f50e671ffff7 | 22 | void DW1000::setEUI(uint64_t EUI) { |
manumaet | 0:f50e671ffff7 | 23 | writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); |
manumaet | 0:f50e671ffff7 | 24 | } |
manumaet | 0:f50e671ffff7 | 25 | |
manumaet | 4:6240b9c7a033 | 26 | void DW1000::sendFrame(char* message, int length) { |
manumaet | 4:6240b9c7a033 | 27 | writeRegister(DW1000_TX_BUFFER, 0, (uint8_t*)message, length); // fill buffer |
manumaet | 4:6240b9c7a033 | 28 | |
manumaet | 4:6240b9c7a033 | 29 | uint16_t framelength = length+2; // put length of frame including 2 CRC Bytes |
manumaet | 4:6240b9c7a033 | 30 | writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&framelength, 1); |
manumaet | 4:6240b9c7a033 | 31 | |
manumaet | 4:6240b9c7a033 | 32 | uint8_t txstart = 0x02; // trigger sending process |
manumaet | 4:6240b9c7a033 | 33 | writeRegister(DW1000_SYS_CTRL, 0, &txstart, 1); |
manumaet | 4:6240b9c7a033 | 34 | } |
manumaet | 4:6240b9c7a033 | 35 | |
manumaet | 4:6240b9c7a033 | 36 | void DW1000::resetRX() { |
manumaet | 4:6240b9c7a033 | 37 | uint8_t resetrx = 0xE0; //set rx reset |
manumaet | 4:6240b9c7a033 | 38 | writeRegister(DW1000_PMSC, 3, &resetrx, 1); |
manumaet | 4:6240b9c7a033 | 39 | resetrx = 0xf0; //clear RX reset |
manumaet | 4:6240b9c7a033 | 40 | writeRegister(DW1000_PMSC, 3, &resetrx, 1); |
manumaet | 4:6240b9c7a033 | 41 | } |
manumaet | 4:6240b9c7a033 | 42 | |
manumaet | 0:f50e671ffff7 | 43 | float DW1000::getVoltage() { |
manumaet | 4:6240b9c7a033 | 44 | uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form DW1000 User Manual p57 |
manumaet | 0:f50e671ffff7 | 45 | writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); |
manumaet | 0:f50e671ffff7 | 46 | writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); |
manumaet | 0:f50e671ffff7 | 47 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); |
manumaet | 0:f50e671ffff7 | 48 | writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); |
manumaet | 0:f50e671ffff7 | 49 | readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature |
manumaet | 0:f50e671ffff7 | 50 | float Voltage = buffer[5] * 0.0057 + 2.3; |
manumaet | 4:6240b9c7a033 | 51 | 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 | 52 | return Voltage; |
manumaet | 0:f50e671ffff7 | 53 | } |
manumaet | 0:f50e671ffff7 | 54 | |
manumaet | 0:f50e671ffff7 | 55 | |
manumaet | 0:f50e671ffff7 | 56 | // SPI Interface ------------------------------------------------------------------------------------ |
manumaet | 0:f50e671ffff7 | 57 | void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) |
manumaet | 0:f50e671ffff7 | 58 | { |
manumaet | 0:f50e671ffff7 | 59 | setupTransaction(reg, subaddress, false); |
manumaet | 0:f50e671ffff7 | 60 | |
manumaet | 0:f50e671ffff7 | 61 | // get data |
manumaet | 0:f50e671ffff7 | 62 | for(int i=0; i<length; i++) |
manumaet | 0:f50e671ffff7 | 63 | buffer[i] = spi.write(0x00); |
manumaet | 0:f50e671ffff7 | 64 | deselect(); |
manumaet | 0:f50e671ffff7 | 65 | } |
manumaet | 0:f50e671ffff7 | 66 | |
manumaet | 0:f50e671ffff7 | 67 | void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) |
manumaet | 0:f50e671ffff7 | 68 | { |
manumaet | 0:f50e671ffff7 | 69 | setupTransaction(reg, subaddress, true); |
manumaet | 0:f50e671ffff7 | 70 | |
manumaet | 0:f50e671ffff7 | 71 | // put data |
manumaet | 0:f50e671ffff7 | 72 | for(int i=0; i<length; i++) |
manumaet | 0:f50e671ffff7 | 73 | spi.write(buffer[i]); |
manumaet | 0:f50e671ffff7 | 74 | deselect(); |
manumaet | 0:f50e671ffff7 | 75 | } |
manumaet | 0:f50e671ffff7 | 76 | |
manumaet | 0:f50e671ffff7 | 77 | void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) |
manumaet | 0:f50e671ffff7 | 78 | { |
manumaet | 0:f50e671ffff7 | 79 | reg |= (write * DW1000_WRITE_FLAG); |
manumaet | 0:f50e671ffff7 | 80 | select(); |
manumaet | 0:f50e671ffff7 | 81 | if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte |
manumaet | 0:f50e671ffff7 | 82 | spi.write(reg | DW1000_SUBADDRESS_FLAG); |
manumaet | 0:f50e671ffff7 | 83 | if (subaddress > 127) { // sub address too long, we need to set flag and send third header byte |
manumaet | 0:f50e671ffff7 | 84 | spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); |
manumaet | 0:f50e671ffff7 | 85 | spi.write((uint8_t)(subaddress >> 7)); |
manumaet | 0:f50e671ffff7 | 86 | } else { |
manumaet | 0:f50e671ffff7 | 87 | spi.write((uint8_t)subaddress); |
manumaet | 0:f50e671ffff7 | 88 | } |
manumaet | 0:f50e671ffff7 | 89 | } else { |
manumaet | 0:f50e671ffff7 | 90 | spi.write(reg); |
manumaet | 0:f50e671ffff7 | 91 | } |
manumaet | 0:f50e671ffff7 | 92 | } |
manumaet | 0:f50e671ffff7 | 93 | |
manumaet | 0:f50e671ffff7 | 94 | void DW1000::select() { cs = 0; } //Set CS low to start transmission |
manumaet | 0:f50e671ffff7 | 95 | void DW1000::deselect() { cs = 1; } //Set CS high to stop transmission |