Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Fri Nov 21 14:33:23 2014 +0000
Revision:
10:d077bb12d259
Parent:
8:7a9c61242e2f
Child:
11:c87d37db2c6f
in between messing with framelenght of sendFrame to solve issue with >1 byte;; readRegister8 tested

Who changed what in which revision?

UserRevisionLine numberNew 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 8:7a9c61242e2f 8 irq.rise(this, &DW1000::ISR); // attach Interrupt handler to rising edge
manumaet 8:7a9c61242e2f 9 resetRX();
manumaet 0:f50e671ffff7 10 }
manumaet 0:f50e671ffff7 11
manumaet 0:f50e671ffff7 12 uint32_t DW1000::getDeviceID() {
manumaet 0:f50e671ffff7 13 uint32_t result;
manumaet 0:f50e671ffff7 14 readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4);
manumaet 0:f50e671ffff7 15 return result;
manumaet 0:f50e671ffff7 16 }
manumaet 0:f50e671ffff7 17
manumaet 0:f50e671ffff7 18 uint64_t DW1000::getEUI() {
manumaet 0:f50e671ffff7 19 uint64_t result;
manumaet 0:f50e671ffff7 20 readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8);
manumaet 0:f50e671ffff7 21 return result;
manumaet 0:f50e671ffff7 22 }
manumaet 0:f50e671ffff7 23
manumaet 0:f50e671ffff7 24 void DW1000::setEUI(uint64_t EUI) {
manumaet 0:f50e671ffff7 25 writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8);
manumaet 0:f50e671ffff7 26 }
manumaet 0:f50e671ffff7 27
manumaet 0:f50e671ffff7 28 float DW1000::getVoltage() {
manumaet 8:7a9c61242e2f 29 uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form DW1000 User Manual p57
manumaet 0:f50e671ffff7 30 writeRegister(DW1000_RF_CONF, 0x11, buffer, 2);
manumaet 0:f50e671ffff7 31 writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1);
manumaet 0:f50e671ffff7 32 writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1);
manumaet 0:f50e671ffff7 33 writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1);
manumaet 8:7a9c61242e2f 34 readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature
manumaet 0:f50e671ffff7 35 float Voltage = buffer[5] * 0.0057 + 2.3;
manumaet 8:7a9c61242e2f 36 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 37 return Voltage;
manumaet 0:f50e671ffff7 38 }
manumaet 0:f50e671ffff7 39
manumaet 10:d077bb12d259 40 void DW1000::sendString(char* message) {
manumaet 10:d077bb12d259 41 sendFrame((uint8_t*)message, strlen(message)+1);
manumaet 10:d077bb12d259 42 }
manumaet 10:d077bb12d259 43
manumaet 10:d077bb12d259 44 char* DW1000::receiveString() {
manumaet 10:d077bb12d259 45 uint16_t framelength = 0; // get framelength
manumaet 10:d077bb12d259 46 readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2);
manumaet 10:d077bb12d259 47 framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes
manumaet 10:d077bb12d259 48 char* receive = new char[framelength]; // get data from buffer
manumaet 10:d077bb12d259 49 readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)receive, framelength);
manumaet 10:d077bb12d259 50 return receive;
manumaet 10:d077bb12d259 51 }
manumaet 10:d077bb12d259 52
manumaet 10:d077bb12d259 53 void DW1000::sendFrame(uint8_t* message, int length) {
manumaet 10:d077bb12d259 54 writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer
manumaet 7:e634eeafc4d2 55
manumaet 7:e634eeafc4d2 56 uint16_t framelength = length+2; // put length of frame including 2 CRC Bytes
manumaet 10:d077bb12d259 57 writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&framelength, 1); // TODO: make that bigger frames than 256 can be sent
manumaet 7:e634eeafc4d2 58
manumaet 10:d077bb12d259 59 //writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit
manumaet 8:7a9c61242e2f 60 }
manumaet 8:7a9c61242e2f 61
manumaet 8:7a9c61242e2f 62 void DW1000::receiveFrame() {
manumaet 8:7a9c61242e2f 63 writeRegister8(DW1000_SYS_CTRL, 1, 0x01); // start listening for preamble by setting the RXENAB bit
manumaet 7:e634eeafc4d2 64 }
manumaet 7:e634eeafc4d2 65
manumaet 7:e634eeafc4d2 66 void DW1000::ISR() {
manumaet 7:e634eeafc4d2 67 callbackRX();
manumaet 7:e634eeafc4d2 68 }
manumaet 7:e634eeafc4d2 69
manumaet 7:e634eeafc4d2 70 void DW1000::resetRX() {
manumaet 7:e634eeafc4d2 71 uint8_t resetrx = 0xE0; //set rx reset
manumaet 7:e634eeafc4d2 72 writeRegister(DW1000_PMSC, 3, &resetrx, 1);
manumaet 7:e634eeafc4d2 73 resetrx = 0xf0; //clear RX reset
manumaet 7:e634eeafc4d2 74 writeRegister(DW1000_PMSC, 3, &resetrx, 1);
manumaet 7:e634eeafc4d2 75 }
manumaet 0:f50e671ffff7 76
manumaet 0:f50e671ffff7 77 // SPI Interface ------------------------------------------------------------------------------------
manumaet 10:d077bb12d259 78 uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) {
manumaet 10:d077bb12d259 79 uint8_t result;
manumaet 10:d077bb12d259 80 readRegister(reg, subaddress, &result, 1);
manumaet 10:d077bb12d259 81 return result;
manumaet 10:d077bb12d259 82 }
manumaet 10:d077bb12d259 83
manumaet 8:7a9c61242e2f 84 void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) {
manumaet 8:7a9c61242e2f 85 writeRegister(reg, subaddress, &buffer, 1);
manumaet 8:7a9c61242e2f 86 }
manumaet 8:7a9c61242e2f 87
manumaet 8:7a9c61242e2f 88 void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
manumaet 0:f50e671ffff7 89 setupTransaction(reg, subaddress, false);
manumaet 8:7a9c61242e2f 90 for(int i=0; i<length; i++) // get data
manumaet 0:f50e671ffff7 91 buffer[i] = spi.write(0x00);
manumaet 0:f50e671ffff7 92 deselect();
manumaet 0:f50e671ffff7 93 }
manumaet 0:f50e671ffff7 94
manumaet 8:7a9c61242e2f 95 void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
manumaet 0:f50e671ffff7 96 setupTransaction(reg, subaddress, true);
manumaet 8:7a9c61242e2f 97 for(int i=0; i<length; i++) // put data
manumaet 0:f50e671ffff7 98 spi.write(buffer[i]);
manumaet 0:f50e671ffff7 99 deselect();
manumaet 0:f50e671ffff7 100 }
manumaet 0:f50e671ffff7 101
manumaet 8:7a9c61242e2f 102 void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) {
manumaet 0:f50e671ffff7 103 reg |= (write * DW1000_WRITE_FLAG);
manumaet 0:f50e671ffff7 104 select();
manumaet 0:f50e671ffff7 105 if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte
manumaet 0:f50e671ffff7 106 spi.write(reg | DW1000_SUBADDRESS_FLAG);
manumaet 0:f50e671ffff7 107 if (subaddress > 127) { // sub address too long, we need to set flag and send third header byte
manumaet 0:f50e671ffff7 108 spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG);
manumaet 0:f50e671ffff7 109 spi.write((uint8_t)(subaddress >> 7));
manumaet 0:f50e671ffff7 110 } else {
manumaet 0:f50e671ffff7 111 spi.write((uint8_t)subaddress);
manumaet 0:f50e671ffff7 112 }
manumaet 0:f50e671ffff7 113 } else {
manumaet 0:f50e671ffff7 114 spi.write(reg);
manumaet 0:f50e671ffff7 115 }
manumaet 0:f50e671ffff7 116 }
manumaet 0:f50e671ffff7 117
manumaet 0:f50e671ffff7 118 void DW1000::select() { cs = 0; } //Set CS low to start transmission
manumaet 0:f50e671ffff7 119 void DW1000::deselect() { cs = 1; } //Set CS high to stop transmission