Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
Diff: DW1000/DW1000.cpp
- Revision:
- 0:f50e671ffff7
- Child:
- 4:6240b9c7a033
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DW1000/DW1000.cpp Sun Nov 16 09:53:25 2014 +0000 @@ -0,0 +1,78 @@ +#include "DW1000.h" + +DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS) : spi(MOSI, MISO, SCLK), cs(CS) +{ + deselect(); // Chip must be deselected first + spi.format(8,0); // Setup the spi for standard 8 bit data and SPI-Mode 0 (GPIO5, GPIO6 open circuit or ground on DW1000) + spi.frequency(1000000); // with a 1MHz clock rate (worked up to 49MHz in our Test) +} + +uint32_t DW1000::getDeviceID() { + uint32_t result; + readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4); + return result; +} + +uint64_t DW1000::getEUI() { + uint64_t result; + readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8); + return result; +} + +void DW1000::setEUI(uint64_t EUI) { + writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8); +} + +float DW1000::getVoltage() { + uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // form DW1000 User Manual p57 + writeRegister(DW1000_RF_CONF, 0x11, buffer, 2); + writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1); + writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1); + writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1); + readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature + float Voltage = buffer[5] * 0.0057 + 2.3; + float Temperature = buffer[6] * 1.13 - 113.0; // TODO: getTemperature was always ~35 degree with better formula/calibration see instance_common.c row 391 + return Voltage; +} + + +// SPI Interface ------------------------------------------------------------------------------------ +void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) +{ + setupTransaction(reg, subaddress, false); + + // get data + for(int i=0; i<length; i++) + buffer[i] = spi.write(0x00); + deselect(); +} + +void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) +{ + setupTransaction(reg, subaddress, true); + + // put data + for(int i=0; i<length; i++) + spi.write(buffer[i]); + deselect(); +} + +void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) +{ + reg |= (write * DW1000_WRITE_FLAG); + select(); + if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte + spi.write(reg | DW1000_SUBADDRESS_FLAG); + if (subaddress > 127) { // sub address too long, we need to set flag and send third header byte + spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); + spi.write((uint8_t)(subaddress >> 7)); + } else { + spi.write((uint8_t)subaddress); + } + } else { + spi.write(reg); + } +} + +void DW1000::select() { cs = 0; } //Set CS low to start transmission +void DW1000::deselect() { cs = 1; } //Set CS high to stop transmission \ No newline at end of file