Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DecaWave by
Diff: DW1000/DW1000.cpp
- Revision:
- 18:bbc7ca7d3a95
- Parent:
- 17:8afa5f9122da
- Child:
- 19:e94bc88c1eb0
--- a/DW1000/DW1000.cpp Mon Nov 24 14:09:49 2014 +0000 +++ b/DW1000/DW1000.cpp Mon Nov 24 16:48:03 2014 +0000 @@ -1,14 +1,16 @@ #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; + receiving = 0; // state in the beginning is not listening for frames 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) - resetAll(); // we can do a soft reset if we want to (only needed for debugging) + //resetAll(); // we can do a soft reset if we want to (only needed for debugging) 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 + + // Configuration TODO: make method for that writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error writeRegister8(DW1000_SYS_CFG, 2, 0x03); // enable 1024 byte frames TODO: doesn't work!! @@ -43,6 +45,10 @@ return Voltage; } +uint64_t DW1000::getStatus() { + return readRegister40(DW1000_SYS_STATUS, 0); +} + void DW1000::sendString(char* message) { sendFrame((uint8_t*)message, strlen(message)+1); } @@ -65,9 +71,9 @@ length = ((backup & 0xFC) << 8) | (length & 0x03FF); writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2); - if (receiving) stopTRX(); // stop receiving if we are + if (receiving) stopTRX(); // stop receiving if we are in this state writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit - if (receiving) startRX(); // enable receiver again if we need it TODO: safe to do this directly ??? only after sending ended + //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() { @@ -81,21 +87,28 @@ } void DW1000::ISR() { - uint64_t status; // get the entire system status - readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5); - status &= 0xFFFFFFFFFF; // only 40-Bit - if (status & 0x4000) - callbackRX(); - if (status & 0x80) - ;//callbackTX(); // TODO: mask TX done interrupt make TX handler + 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(); + } } -void DW1000::loadLDE() { - uint16_t ldeload[] = {0x0301, 0x8000, 0x0200}; // initialise LDE algorithm LDELOAD User Manual p22 - writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[0], 2); // set clock to XTAL so OTP is reliable - writeRegister(DW1000_OTP_IF, 0x06, (uint8_t*)&ldeload[1], 2); // set LDELOAD bit in OTP +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; +} + +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 wait_us(150); - writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[2], 2); // recover to PLL clock + writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock } void DW1000::stopTRX() { @@ -121,39 +134,56 @@ return result; } +uint16_t DW1000::readRegister16(uint8_t reg, uint16_t subaddress) { + uint16_t result; + readRegister(reg, subaddress, (uint8_t*)&result, 2); + return result; +} + +uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) { + uint64_t result; + readRegister(reg, subaddress, (uint8_t*)&result, 5); + result &= 0xFFFFFFFFFF; // only 40-Bit + return result; +} + void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) { writeRegister(reg, subaddress, &buffer, 1); } +void DW1000::writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer) { + writeRegister(reg, subaddress, (uint8_t*)&buffer, 2); +} + void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { setupTransaction(reg, subaddress, false); - for(int i=0; i<length; i++) // get data + for(int i=0; i<length; i++) // get data buffer[i] = spi.write(0x00); deselect(); } void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) { setupTransaction(reg, subaddress, true); - for(int i=0; i<length; i++) // put data + for(int i=0; i<length; i++) // put data spi.write(buffer[i]); deselect(); } void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) { - reg |= (write * DW1000_WRITE_FLAG); + reg |= (write * DW1000_WRITE_FLAG); // set read/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); + if (subaddress > 0x7F) { // sub address too long, we need to set flag and send third header byte + spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); // and spi.write((uint8_t)(subaddress >> 7)); } else { spi.write((uint8_t)subaddress); } } else { - spi.write(reg); + spi.write(reg); // say which register address we want to access } } -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 +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