Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Thu Mar 19 12:54:28 2015 +0000
Revision:
47:b6120c152ad1
Parent:
46:6398237672a0
Child:
48:5999e510f154
final release with the code used in the verification test case

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 0:f50e671ffff7 1 #include "DW1000.h"
manumaet 0:f50e671ffff7 2
manumaet 20:257d56530ae1 3 DW1000::DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ) : irq(IRQ), spi(MOSI, MISO, SCLK), cs(CS) {
manumaet 26:a65c6f26c458 4 setCallbacks(NULL, NULL);
manumaet 17:8afa5f9122da 5
manumaet 0:f50e671ffff7 6 deselect(); // Chip must be deselected first
manumaet 0:f50e671ffff7 7 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 45:01a33363bc21 8 spi.frequency(5000000); // with a 1MHz clock rate (worked up to 49MHz in our Test)
manumaet 7:e634eeafc4d2 9
manumaet 37:40f94c634c3e 10 resetAll(); // we do a soft reset of the DW1000 everytime the driver starts
manumaet 44:2e0045042a59 11
manumaet 18:bbc7ca7d3a95 12 // Configuration TODO: make method for that
manumaet 45:01a33363bc21 13 // User Manual "2.5.5 Default Configurations that should be modified" p. 22
manumaet 45:01a33363bc21 14 //Those values are for the standard mode (6.8Mbps, 5, 16Mhz, 32 Symbols) and are INCOMPLETE!
manumaet 45:01a33363bc21 15 // writeRegister16(DW1000_AGC_CTRL, 0x04, 0x8870);
manumaet 45:01a33363bc21 16 // writeRegister32(DW1000_AGC_CTRL, 0x0C, 0x2502A907);
manumaet 45:01a33363bc21 17 // writeRegister32(DW1000_DRX_CONF, 0x08, 0x311A002D);
manumaet 45:01a33363bc21 18 // writeRegister8 (DW1000_LDE_CTRL, 0x0806, 0xD);
manumaet 45:01a33363bc21 19 // writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1607);
manumaet 45:01a33363bc21 20 // writeRegister32(DW1000_TX_POWER, 0, 0x0E082848);
manumaet 45:01a33363bc21 21 // writeRegister32(DW1000_RF_CONF, 0x0C, 0x001E3FE0);
manumaet 45:01a33363bc21 22 // writeRegister8 (DW1000_TX_CAL, 0x0B, 0xC0);
manumaet 45:01a33363bc21 23 // writeRegister8 (DW1000_FS_CTRL, 0x0B, 0xA6);
manumaet 44:2e0045042a59 24
manumaet 45:01a33363bc21 25
manumaet 45:01a33363bc21 26 //Those values are for the 110kbps mode (5, 16MHz, 1024 Symbols) and are quite complete
manumaet 45:01a33363bc21 27 writeRegister16(DW1000_AGC_CTRL, 0x04, 0x8870); //AGC_TUNE1 for 16MHz PRF
manumaet 45:01a33363bc21 28 writeRegister32(DW1000_AGC_CTRL, 0x0C, 0x2502A907); //AGC_TUNE2 (Universal)
manumaet 45:01a33363bc21 29 writeRegister16(DW1000_AGC_CTRL, 0x12, 0x0055); //AGC_TUNE3 (Universal)
manumaet 45:01a33363bc21 30
manumaet 45:01a33363bc21 31 writeRegister16(DW1000_DRX_CONF, 0x02, 0x000A); //DRX_TUNE0b for 110kbps
manumaet 45:01a33363bc21 32 writeRegister16(DW1000_DRX_CONF, 0x04, 0x0087); //DRX_TUNE1a for 16MHz PRF
manumaet 45:01a33363bc21 33 writeRegister16(DW1000_DRX_CONF, 0x06, 0x0064); //DRX_TUNE1b for 110kbps & > 1024 symbols
manumaet 45:01a33363bc21 34 writeRegister32(DW1000_DRX_CONF, 0x08, 0x351A009A); //PAC size for 1024 symbols preamble & 16MHz PRF
manumaet 45:01a33363bc21 35 //writeRegister32(DW1000_DRX_CONF, 0x08, 0x371A011D); //PAC size for 2048 symbols preamble
manumaet 45:01a33363bc21 36
manumaet 45:01a33363bc21 37 writeRegister8 (DW1000_LDE_CTRL, 0x0806, 0xD); //LDE_CFG1
manumaet 45:01a33363bc21 38 writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1607); //LDE_CFG2 for 16MHz PRF
manumaet 45:01a33363bc21 39
manumaet 46:6398237672a0 40 writeRegister32(DW1000_TX_POWER, 0, 0x28282828); //Power for channel 5
manumaet 45:01a33363bc21 41
manumaet 45:01a33363bc21 42 writeRegister8(DW1000_RF_CONF, 0x0B, 0xD8); //RF_RXCTRLH for channel 5
manumaet 45:01a33363bc21 43 writeRegister32(DW1000_RF_CONF, 0x0C, 0x001E3FE0); //RF_TXCTRL for channel 5
manumaet 45:01a33363bc21 44
manumaet 45:01a33363bc21 45 writeRegister8 (DW1000_TX_CAL, 0x0B, 0xC0); //TC_PGDELAY for channel 5
manumaet 45:01a33363bc21 46
manumaet 45:01a33363bc21 47 writeRegister32 (DW1000_FS_CTRL, 0x07, 0x0800041D); //FS_PLLCFG for channel 5
manumaet 45:01a33363bc21 48 writeRegister8 (DW1000_FS_CTRL, 0x0B, 0xA6); //FS_PLLTUNE for channel 5
manumaet 45:01a33363bc21 49
manumaet 46:6398237672a0 50 loadLDE(); // important everytime DW1000 initialises/awakes otherwise the LDE algorithm must be turned off or there's receiving malfunction see User Manual LDELOAD on p22 & p158
manumaet 42:83931678c4de 51
manumaet 42:83931678c4de 52 // 110kbps CAUTION: a lot of other registers have to be set for an optimized operation on 110kbps
manumaet 45:01a33363bc21 53 writeRegister16(DW1000_TX_FCTRL, 1, 0x0800 | 0x0100 | 0x0080); // use 1024 symbols preamble (0x0800) (previously 2048 - 0x2800), 16MHz pulse repetition frequency (0x0100), 110kbps bit rate (0x0080) see p.69 of DW1000 User Manual
manumaet 45:01a33363bc21 54 writeRegister8(DW1000_SYS_CFG, 2, 0x44); // enable special receiving option for 110kbps (disable smartTxPower)!! (0x44) see p.64 of DW1000 User Manual [DO NOT enable 1024 byte frames (0x03) becuase it generates disturbance of ranging don't know why...]
manumaet 44:2e0045042a59 55
manumaet 42:83931678c4de 56 writeRegister16(DW1000_TX_ANTD, 0, 16384); // set TX and RX Antenna delay to neutral because we calibrate afterwards
manumaet 46:6398237672a0 57 writeRegister16(DW1000_LDE_CTRL, 0x1804, 16384); // = 2^14 a quarter of the range of the 16-Bit register which corresponds to zero calibration in a round trip (TX1+RX2+TX2+RX1)
manumaet 44:2e0045042a59 58
manumaet 42:83931678c4de 59 writeRegister8(DW1000_SYS_CFG, 3, 0x20); // enable auto reenabling receiver after error
manumaet 42:83931678c4de 60
manumaet 39:bb57aa77b015 61 irq.rise(this, &DW1000::ISR); // attach interrupt handler to rising edge of interrupt pin from DW1000
manumaet 0:f50e671ffff7 62 }
manumaet 0:f50e671ffff7 63
manumaet 29:019ff388ed76 64 void DW1000::setCallbacks(void (*callbackRX)(void), void (*callbackTX)(void)) {
manumaet 29:019ff388ed76 65 bool RX = false;
manumaet 29:019ff388ed76 66 bool TX = false;
manumaet 29:019ff388ed76 67 if (callbackRX) {
manumaet 29:019ff388ed76 68 DW1000::callbackRX.attach(callbackRX);
manumaet 29:019ff388ed76 69 RX = true;
manumaet 29:019ff388ed76 70 }
manumaet 29:019ff388ed76 71 if (callbackTX) {
manumaet 29:019ff388ed76 72 DW1000::callbackTX.attach(callbackTX);
manumaet 29:019ff388ed76 73 TX = true;
manumaet 29:019ff388ed76 74 }
manumaet 29:019ff388ed76 75 setInterrupt(RX,TX);
manumaet 26:a65c6f26c458 76 }
manumaet 26:a65c6f26c458 77
manumaet 0:f50e671ffff7 78 uint32_t DW1000::getDeviceID() {
manumaet 0:f50e671ffff7 79 uint32_t result;
manumaet 0:f50e671ffff7 80 readRegister(DW1000_DEV_ID, 0, (uint8_t*)&result, 4);
manumaet 0:f50e671ffff7 81 return result;
manumaet 0:f50e671ffff7 82 }
manumaet 0:f50e671ffff7 83
manumaet 0:f50e671ffff7 84 uint64_t DW1000::getEUI() {
manumaet 0:f50e671ffff7 85 uint64_t result;
manumaet 0:f50e671ffff7 86 readRegister(DW1000_EUI, 0, (uint8_t*)&result, 8);
manumaet 0:f50e671ffff7 87 return result;
manumaet 0:f50e671ffff7 88 }
manumaet 0:f50e671ffff7 89
manumaet 0:f50e671ffff7 90 void DW1000::setEUI(uint64_t EUI) {
manumaet 0:f50e671ffff7 91 writeRegister(DW1000_EUI, 0, (uint8_t*)&EUI, 8);
manumaet 0:f50e671ffff7 92 }
manumaet 0:f50e671ffff7 93
manumaet 0:f50e671ffff7 94 float DW1000::getVoltage() {
manumaet 12:985aa9843c3c 95 uint8_t buffer[7] = {0x80, 0x0A, 0x0F, 0x01, 0x00}; // algorithm form User Manual p57
manumaet 0:f50e671ffff7 96 writeRegister(DW1000_RF_CONF, 0x11, buffer, 2);
manumaet 0:f50e671ffff7 97 writeRegister(DW1000_RF_CONF, 0x12, &buffer[2], 1);
manumaet 0:f50e671ffff7 98 writeRegister(DW1000_TX_CAL, 0x00, &buffer[3], 1);
manumaet 0:f50e671ffff7 99 writeRegister(DW1000_TX_CAL, 0x00, &buffer[4], 1);
manumaet 8:7a9c61242e2f 100 readRegister(DW1000_TX_CAL, 0x03, &buffer[5], 2); // get the 8-Bit readings for Voltage and Temperature
manumaet 0:f50e671ffff7 101 float Voltage = buffer[5] * 0.0057 + 2.3;
manumaet 47:b6120c152ad1 102 //float Temperature = buffer[6] * 1.13 - 113.0; // TODO: getTemperature was always ~35 degree with better formula/calibration
manumaet 0:f50e671ffff7 103 return Voltage;
manumaet 0:f50e671ffff7 104 }
manumaet 0:f50e671ffff7 105
manumaet 18:bbc7ca7d3a95 106 uint64_t DW1000::getStatus() {
manumaet 18:bbc7ca7d3a95 107 return readRegister40(DW1000_SYS_STATUS, 0);
manumaet 18:bbc7ca7d3a95 108 }
manumaet 18:bbc7ca7d3a95 109
manumaet 26:a65c6f26c458 110 uint64_t DW1000::getRXTimestamp() {
manumaet 26:a65c6f26c458 111 return readRegister40(DW1000_RX_TIME, 0);
manumaet 26:a65c6f26c458 112 }
manumaet 26:a65c6f26c458 113
manumaet 26:a65c6f26c458 114 uint64_t DW1000::getTXTimestamp() {
manumaet 26:a65c6f26c458 115 return readRegister40(DW1000_TX_TIME, 0);
manumaet 26:a65c6f26c458 116 }
manumaet 26:a65c6f26c458 117
manumaet 10:d077bb12d259 118 void DW1000::sendString(char* message) {
manumaet 10:d077bb12d259 119 sendFrame((uint8_t*)message, strlen(message)+1);
manumaet 10:d077bb12d259 120 }
manumaet 10:d077bb12d259 121
manumaet 24:6f25ba679490 122 void DW1000::receiveString(char* message) {
manumaet 31:6f76f3d518ac 123 readRegister(DW1000_RX_BUFFER, 0, (uint8_t*)message, getFramelength()); // get data from buffer
manumaet 10:d077bb12d259 124 }
manumaet 10:d077bb12d259 125
manumaet 11:c87d37db2c6f 126 void DW1000::sendFrame(uint8_t* message, uint16_t length) {
manumaet 38:8ef3b8d8b908 127 //if (length >= 1021) length = 1021; // check for maximim length a frame can have with 1024 Byte frames [not used, see constructor]
manumaet 38:8ef3b8d8b908 128 if (length >= 125) length = 125; // check for maximim length a frame can have with 127 Byte frames
manumaet 13:b4d27bf7062a 129 writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer
manumaet 7:e634eeafc4d2 130
manumaet 39:bb57aa77b015 131 uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame
manumaet 39:bb57aa77b015 132 length += 2; // including 2 CRC Bytes
manumaet 39:bb57aa77b015 133 length = ((backup & 0xFC) << 8) | (length & 0x03FF);
manumaet 39:bb57aa77b015 134 writeRegister16(DW1000_TX_FCTRL, 0, length);
manumaet 11:c87d37db2c6f 135
manumaet 25:d58b0595b300 136 stopTRX(); // stop receiving
manumaet 23:661a79e56208 137 writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit
manumaet 25:d58b0595b300 138 startRX(); // enable receiver again
manumaet 8:7a9c61242e2f 139 }
manumaet 8:7a9c61242e2f 140
manumaet 44:2e0045042a59 141 void DW1000::sendDelayedFrame(uint8_t* message, uint16_t length, uint64_t TxTimestamp) {
manumaet 44:2e0045042a59 142 //if (length >= 1021) length = 1021; // check for maximim length a frame can have with 1024 Byte frames [not used, see constructor]
manumaet 44:2e0045042a59 143 if (length >= 125) length = 125; // check for maximim length a frame can have with 127 Byte frames
manumaet 44:2e0045042a59 144 writeRegister(DW1000_TX_BUFFER, 0, message, length); // fill buffer
manumaet 44:2e0045042a59 145
manumaet 44:2e0045042a59 146 uint8_t backup = readRegister8(DW1000_TX_FCTRL, 1); // put length of frame
manumaet 44:2e0045042a59 147 length += 2; // including 2 CRC Bytes
manumaet 44:2e0045042a59 148 length = ((backup & 0xFC) << 8) | (length & 0x03FF);
manumaet 44:2e0045042a59 149 writeRegister16(DW1000_TX_FCTRL, 0, length);
manumaet 44:2e0045042a59 150
manumaet 45:01a33363bc21 151 writeRegister40(DW1000_DX_TIME, 0, TxTimestamp); //write the timestamp on which to send the message
manumaet 44:2e0045042a59 152
manumaet 44:2e0045042a59 153 stopTRX(); // stop receiving
manumaet 44:2e0045042a59 154 writeRegister8(DW1000_SYS_CTRL, 0, 0x02 | 0x04); // trigger sending process by setting the TXSTRT and TXDLYS bit
manumaet 44:2e0045042a59 155 startRX(); // enable receiver again
manumaet 44:2e0045042a59 156 }
manumaet 44:2e0045042a59 157
manumaet 17:8afa5f9122da 158 void DW1000::startRX() {
manumaet 20:257d56530ae1 159 writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit
manumaet 7:e634eeafc4d2 160 }
manumaet 7:e634eeafc4d2 161
manumaet 25:d58b0595b300 162 void DW1000::stopTRX() {
manumaet 25:d58b0595b300 163 writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode
manumaet 17:8afa5f9122da 164 }
manumaet 17:8afa5f9122da 165
manumaet 20:257d56530ae1 166 // PRIVATE Methods ------------------------------------------------------------------------------------
manumaet 18:bbc7ca7d3a95 167 void DW1000::loadLDE() { // initialise LDE algorithm LDELOAD User Manual p22
manumaet 18:bbc7ca7d3a95 168 writeRegister16(DW1000_PMSC, 0, 0x0301); // set clock to XTAL so OTP is reliable
manumaet 20:257d56530ae1 169 writeRegister16(DW1000_OTP_IF, 0x06, 0x8000); // set LDELOAD bit in OTP
manumaet 12:985aa9843c3c 170 wait_us(150);
manumaet 18:bbc7ca7d3a95 171 writeRegister16(DW1000_PMSC, 0, 0x0200); // recover to PLL clock
manumaet 12:985aa9843c3c 172 }
manumaet 12:985aa9843c3c 173
manumaet 12:985aa9843c3c 174 void DW1000::resetRX() {
manumaet 12:985aa9843c3c 175 writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset
manumaet 12:985aa9843c3c 176 writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset
manumaet 12:985aa9843c3c 177 }
manumaet 12:985aa9843c3c 178
manumaet 12:985aa9843c3c 179 void DW1000::resetAll() {
manumaet 12:985aa9843c3c 180 writeRegister8(DW1000_PMSC, 0, 0x01); // set clock to XTAL
manumaet 12:985aa9843c3c 181 writeRegister8(DW1000_PMSC, 3, 0x00); // set All reset
manumaet 12:985aa9843c3c 182 wait_us(10); // wait for PLL to lock
manumaet 12:985aa9843c3c 183 writeRegister8(DW1000_PMSC, 3, 0xF0); // clear All reset
manumaet 7:e634eeafc4d2 184 }
manumaet 0:f50e671ffff7 185
manumaet 29:019ff388ed76 186
manumaet 29:019ff388ed76 187 void DW1000::setInterrupt(bool RX, bool TX) {
manumaet 29:019ff388ed76 188 writeRegister16(DW1000_SYS_MASK, 0, RX*0x4000 | TX*0x0080); // RX good frame 0x4000, TX done 0x0080
manumaet 29:019ff388ed76 189 }
manumaet 29:019ff388ed76 190
manumaet 20:257d56530ae1 191 void DW1000::ISR() {
manumaet 20:257d56530ae1 192 uint64_t status = getStatus();
manumaet 22:576ee999b004 193 if (status & 0x4000) { // a frame was received
manumaet 29:019ff388ed76 194 callbackRX.call();
manumaet 22:576ee999b004 195 writeRegister16(DW1000_SYS_STATUS, 0, 0x6F00); // clearing of receiving status bits
manumaet 20:257d56530ae1 196 }
manumaet 22:576ee999b004 197 if (status & 0x80) { // sending complete
manumaet 29:019ff388ed76 198 callbackTX.call();
manumaet 22:576ee999b004 199 writeRegister8(DW1000_SYS_STATUS, 0, 0xF8); // clearing of sending status bits
manumaet 20:257d56530ae1 200 }
manumaet 20:257d56530ae1 201 }
manumaet 20:257d56530ae1 202
manumaet 20:257d56530ae1 203 uint16_t DW1000::getFramelength() {
manumaet 20:257d56530ae1 204 uint16_t framelength = readRegister16(DW1000_RX_FINFO, 0); // get framelength
manumaet 20:257d56530ae1 205 framelength = (framelength & 0x03FF) - 2; // take only the right bits and subtract the 2 CRC Bytes
manumaet 20:257d56530ae1 206 return framelength;
manumaet 20:257d56530ae1 207 }
manumaet 20:257d56530ae1 208
manumaet 0:f50e671ffff7 209 // SPI Interface ------------------------------------------------------------------------------------
manumaet 10:d077bb12d259 210 uint8_t DW1000::readRegister8(uint8_t reg, uint16_t subaddress) {
manumaet 10:d077bb12d259 211 uint8_t result;
manumaet 10:d077bb12d259 212 readRegister(reg, subaddress, &result, 1);
manumaet 10:d077bb12d259 213 return result;
manumaet 10:d077bb12d259 214 }
manumaet 10:d077bb12d259 215
manumaet 18:bbc7ca7d3a95 216 uint16_t DW1000::readRegister16(uint8_t reg, uint16_t subaddress) {
manumaet 18:bbc7ca7d3a95 217 uint16_t result;
manumaet 18:bbc7ca7d3a95 218 readRegister(reg, subaddress, (uint8_t*)&result, 2);
manumaet 18:bbc7ca7d3a95 219 return result;
manumaet 18:bbc7ca7d3a95 220 }
manumaet 18:bbc7ca7d3a95 221
manumaet 18:bbc7ca7d3a95 222 uint64_t DW1000::readRegister40(uint8_t reg, uint16_t subaddress) {
manumaet 18:bbc7ca7d3a95 223 uint64_t result;
manumaet 18:bbc7ca7d3a95 224 readRegister(reg, subaddress, (uint8_t*)&result, 5);
manumaet 18:bbc7ca7d3a95 225 result &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 18:bbc7ca7d3a95 226 return result;
manumaet 18:bbc7ca7d3a95 227 }
manumaet 18:bbc7ca7d3a95 228
manumaet 8:7a9c61242e2f 229 void DW1000::writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer) {
manumaet 8:7a9c61242e2f 230 writeRegister(reg, subaddress, &buffer, 1);
manumaet 8:7a9c61242e2f 231 }
manumaet 8:7a9c61242e2f 232
manumaet 18:bbc7ca7d3a95 233 void DW1000::writeRegister16(uint8_t reg, uint16_t subaddress, uint16_t buffer) {
manumaet 18:bbc7ca7d3a95 234 writeRegister(reg, subaddress, (uint8_t*)&buffer, 2);
manumaet 18:bbc7ca7d3a95 235 }
manumaet 18:bbc7ca7d3a95 236
manumaet 42:83931678c4de 237 void DW1000::writeRegister32(uint8_t reg, uint16_t subaddress, uint32_t buffer) {
manumaet 42:83931678c4de 238 writeRegister(reg, subaddress, (uint8_t*)&buffer, 4);
manumaet 42:83931678c4de 239 }
manumaet 42:83931678c4de 240
manumaet 44:2e0045042a59 241 void DW1000::writeRegister40(uint8_t reg, uint16_t subaddress, uint64_t buffer) {
manumaet 44:2e0045042a59 242 writeRegister(reg, subaddress, (uint8_t*)&buffer, 5);
manumaet 44:2e0045042a59 243 }
manumaet 44:2e0045042a59 244
manumaet 8:7a9c61242e2f 245 void DW1000::readRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
manumaet 0:f50e671ffff7 246 setupTransaction(reg, subaddress, false);
manumaet 18:bbc7ca7d3a95 247 for(int i=0; i<length; i++) // get data
manumaet 0:f50e671ffff7 248 buffer[i] = spi.write(0x00);
manumaet 0:f50e671ffff7 249 deselect();
manumaet 0:f50e671ffff7 250 }
manumaet 0:f50e671ffff7 251
manumaet 8:7a9c61242e2f 252 void DW1000::writeRegister(uint8_t reg, uint16_t subaddress, uint8_t *buffer, int length) {
manumaet 0:f50e671ffff7 253 setupTransaction(reg, subaddress, true);
manumaet 18:bbc7ca7d3a95 254 for(int i=0; i<length; i++) // put data
manumaet 0:f50e671ffff7 255 spi.write(buffer[i]);
manumaet 0:f50e671ffff7 256 deselect();
manumaet 0:f50e671ffff7 257 }
manumaet 0:f50e671ffff7 258
manumaet 8:7a9c61242e2f 259 void DW1000::setupTransaction(uint8_t reg, uint16_t subaddress, bool write) {
manumaet 18:bbc7ca7d3a95 260 reg |= (write * DW1000_WRITE_FLAG); // set read/write flag
manumaet 0:f50e671ffff7 261 select();
manumaet 0:f50e671ffff7 262 if (subaddress > 0) { // there's a subadress, we need to set flag and send second header byte
manumaet 0:f50e671ffff7 263 spi.write(reg | DW1000_SUBADDRESS_FLAG);
manumaet 18:bbc7ca7d3a95 264 if (subaddress > 0x7F) { // sub address too long, we need to set flag and send third header byte
manumaet 18:bbc7ca7d3a95 265 spi.write((uint8_t)(subaddress & 0x7F) | DW1000_2_SUBADDRESS_FLAG); // and
manumaet 0:f50e671ffff7 266 spi.write((uint8_t)(subaddress >> 7));
manumaet 0:f50e671ffff7 267 } else {
manumaet 0:f50e671ffff7 268 spi.write((uint8_t)subaddress);
manumaet 0:f50e671ffff7 269 }
manumaet 0:f50e671ffff7 270 } else {
manumaet 18:bbc7ca7d3a95 271 spi.write(reg); // say which register address we want to access
manumaet 0:f50e671ffff7 272 }
manumaet 0:f50e671ffff7 273 }
manumaet 0:f50e671ffff7 274
manumaet 39:bb57aa77b015 275 void DW1000::select() { // always called to start an SPI transmission
manumaet 39:bb57aa77b015 276 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.
manumaet 39:bb57aa77b015 277 cs = 0; // set Cable Select pin low to start transmission
manumaet 39:bb57aa77b015 278 }
manumaet 39:bb57aa77b015 279 void DW1000::deselect() { // always called to end an SPI transmission
manumaet 39:bb57aa77b015 280 cs = 1; // set Cable Select pin high to stop transmission
manumaet 39:bb57aa77b015 281 irq.enable_irq(); // reenable the interrupt handler
manumaet 45:01a33363bc21 282 }