
Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
Diff: DW1000/DW1000.cpp
- Revision:
- 20:257d56530ae1
- Parent:
- 19:e94bc88c1eb0
- Child:
- 21:23bf4399020d
--- a/DW1000/DW1000.cpp Tue Nov 25 14:48:51 2014 +0000 +++ b/DW1000/DW1000.cpp Tue Nov 25 15:22:53 2014 +0000 @@ -1,7 +1,8 @@ #include "DW1000.h" -DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : spi(MOSI, MISO, SCLK), cs(CS), irq(IRQ) { - receiving = 0; // state in the beginning is not listening for frames +DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : irq(IRQ), spi(MOSI, MISO, SCLK), cs(CS) { + receiving = false; // state in the beginning is not listening for frames + sending = false; // state in the beginning is not sending anything 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) @@ -41,7 +42,7 @@ 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 + //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; } @@ -54,16 +55,14 @@ } char* DW1000::receiveString() { - uint16_t framelength = 0; // get framelength - readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2); - framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes - char* receive = new char[framelength]; // get data from buffer - readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)receive, framelength); + uint16_t framelength = getFramelength(); + char* receive = new char[framelength]; // prepare memory for the string on the heap + readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)receive, framelength); // get data from buffer return receive; } void DW1000::sendFrame(uint8_t* message, uint16_t length) { - if (length >= 1021) length = 1021; + if (length >= 1021) length = 1021; // check for maximim length a frame can have TODO: 127 Byte mode? writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame including 2 CRC Bytes @@ -72,13 +71,14 @@ writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2); if (receiving) stopTRX(); // stop receiving if we are in this state + sending = true; writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit - //if (receiving) startRX(); // enable receiver again if we need to preserve state TODO: safe to do this directly ??? only after sending ended } void DW1000::startRX() { + while(sending); // block until sending finished TODO: the right way?? receiving = true; - writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit + writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit } void DW1000::stopRX() { @@ -86,27 +86,10 @@ stopTRX(); } -void DW1000::ISR() { - uint64_t status = getStatus(); - if (status & 0x4000) { - callbackRX(getFramelength()); - } - if (status & 0x80) { - if (receiving) startRX(); // enable receiver again if we need to preserve state TODO: have to do it here?? - callbackTX(); - } -} - -uint16_t DW1000::getFramelength() { - uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength TODO: get this from isr - framelength &= 0x03FF; - framelength -= 2; - return framelength; -} - +// PRIVATE Methods ------------------------------------------------------------------------------------ void DW1000::loadLDE() { // initialise LDE algorithm LDELOAD User Manual p22 writeRegister16(DW1000_PMSC, 0, 0x0301); // set clock to XTAL so OTP is reliable - writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP + writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP wait_us(150); writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock } @@ -127,6 +110,24 @@ writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset } +void DW1000::ISR() { + uint64_t status = getStatus(); + if (status & 0x4000) { + callbackRX(getFramelength()); + } + if (status & 0x80) { + sending = false; + if (receiving) startRX(); // enable receiver again if we need to preserve state TODO: have to do it here?? + callbackTX(); + } +} + +uint16_t DW1000::getFramelength() { + uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength + framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes + return framelength; +} + // SPI Interface ------------------------------------------------------------------------------------ uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) { uint8_t result;