Tobi's ubw test branch
Dependencies: mavlink_bridge mbed
Fork of AIT_UWB_Range by
Diff: DW1000/DW1000.cpp
- Revision:
- 48:5999e510f154
- Parent:
- 47:b6120c152ad1
- Child:
- 50:50b8aea54a51
--- a/DW1000/DW1000.cpp Thu Mar 19 12:54:28 2015 +0000 +++ b/DW1000/DW1000.cpp Tue Nov 24 16:41:23 2015 +0000 @@ -1,12 +1,12 @@ #include "DW1000.h" -DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : irq(IRQ), spi(MOSI, MISO, SCLK), cs(CS) { +//#include "PC.h" +//static PC pc(USBTX, USBRX, 115200); // USB UART Terminal + +DW1000::DW1000(SPI& spi, InterruptMultiplexer& irq_mp, PinName CS, PinName RESET) : spi(spi), cs(CS), irq_mp(irq_mp), reset(RESET) { setCallbacks(NULL, NULL); - + 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(5000000); // with a 1MHz clock rate (worked up to 49MHz in our Test) - resetAll(); // we do a soft reset of the DW1000 everytime the driver starts // Configuration TODO: make method for that @@ -58,7 +58,7 @@ writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error - irq.rise(this, &DW1000::ISR); // attach interrupt handler to rising edge of interrupt pin from DW1000 + irq_index = irq_mp.addISR(this, &DW1000::ISR); } void DW1000::setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void)) { @@ -115,6 +115,44 @@ return readRegister40(DW1000_TX_TIME, 0); } +uint16_t DW1000::getStdNoise() { + return readRegister16(DW1000_RX_FQUAL, 0x00); +} + +uint16_t DW1000::getPACC() { + uint32_t v = readRegister32(DW1000_RX_FINFO, 0x00); + v >>= 20; + return static_cast<uint16_t>(v); +} + +uint16_t DW1000::getFPINDEX() { + return readRegister16(DW1000_RX_TIME, 0x05); +} + +uint16_t DW1000::getFPAMPL1() { + return readRegister16(DW1000_RX_TIME, 0x07); +} + +uint16_t DW1000::getFPAMPL2() { + return readRegister16(DW1000_RX_FQUAL, 0x02); +} + +uint16_t DW1000::getFPAMPL3() { + return readRegister16(DW1000_RX_FQUAL, 0x04); +} + +uint16_t DW1000::getCIRPWR() { + return readRegister16(DW1000_RX_FQUAL, 0x06); +} + +uint8_t DW1000::getPRF() { + uint16_t prf_mask = (0x1 << 19) | (0x1 << 18); + uint16_t prf = readRegister16(DW1000_CHAN_CTRL, 0x00); + prf >> 18; + prf &= 0x03; + return static_cast<uint8_t>(prf); +} + void DW1000::sendString(char* message) { sendFrame((uint8_t*)message, strlen(message)+1); } @@ -176,7 +214,26 @@ writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset } +void DW1000::hardwareReset(PinName reset_pin) { + DigitalOut reset(reset_pin); + reset = 1; + wait_ms(100); + reset = 0; + wait_ms(100); + reset = 1; + wait_ms(100); +} + void DW1000::resetAll() { + if (reset.is_connected()) { + reset = 1; + wait_ms(100); + reset = 0; + wait_ms(100); + reset = 1; + wait_ms(100); + } + writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset wait_us(10); // wait for PLL to lock @@ -189,6 +246,7 @@ } void DW1000::ISR() { +// pc.printf("ISR\r\n"); uint64_t status = getStatus(); if (status & 0x4000) { // a frame was received callbackRX.call(); @@ -219,6 +277,12 @@ return result; } +uint32_t DW1000::readRegister32(uint8_t reg, uint16_t subaddress) { + uint32_t result; + readRegister(reg, subaddress, (uint8_t*)&result, 4); + return result; +} + uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) { uint64_t result; readRegister(reg, subaddress, (uint8_t*)&result, 5); @@ -273,10 +337,18 @@ } void DW1000::select() { // always called to start an SPI transmission - irq.disable_irq(); // disable interrupts from DW1000 during SPI becaus this leads to crashes! TODO: if you have other interrupt handlers attached on the micro controller, they could also interfere. + irq_mp.disable(irq_index); cs = 0; // set Cable Select pin low to start transmission } void DW1000::deselect() { // always called to end an SPI transmission cs = 1; // set Cable Select pin high to stop transmission - irq.enable_irq(); // reenable the interrupt handler + irq_mp.enable(irq_index); } + +void DW1000::enable_irq() { // always called to start an SPI transmission + //pc.printf("Enabling irq %d\r\n", irq_index); + irq_mp.enable(irq_index); +} +void DW1000::disable_irq() { // always called to end an SPI transmission + irq_mp.disable(irq_index); +}