Simple driver for DWM1000 modules.

Committer:
bhepp
Date:
Mon Apr 04 11:19:26 2016 +0000
Revision:
6:028891b5f03b
Parent:
3:6a9372463510
Removed automatic retransmission and added clearing sent flag before delayed transmission.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bhepp 3:6a9372463510 1 #pragma once
bhepp 3:6a9372463510 2
bhepp 3:6a9372463510 3 #include <mbed.h>
bhepp 2:12a2907957b8 4 #include "DW1000.h"
bhepp 2:12a2907957b8 5
bhepp 2:12a2907957b8 6 class DW1000Utils
bhepp 2:12a2907957b8 7 {
bhepp 2:12a2907957b8 8 public:
bhepp 3:6a9372463510 9 // Recommended settings:
bhepp 3:6a9372463510 10 // Data rate 110 kbps - Preamble length 2048 or 4096.
bhepp 3:6a9372463510 11 // Data rate 850 kbps - Preamble length 256, 512 or 1024.
bhepp 3:6a9372463510 12 // Data rate 6.8 Mbps - Preamble length 64.
bhepp 3:6a9372463510 13
bhepp 3:6a9372463510 14 const static uint32_t PREAMBLE_LENGTH_64 = (1 << 18);
bhepp 3:6a9372463510 15 const static uint32_t PREAMBLE_LENGTH_128 = (1 << 18) | (1 << 20);
bhepp 3:6a9372463510 16 const static uint32_t PREAMBLE_LENGTH_256 = (1 << 18) | (1 << 21);
bhepp 3:6a9372463510 17 const static uint32_t PREAMBLE_LENGTH_512 = (1 << 18) | (1 << 20) | (1 << 21);
bhepp 3:6a9372463510 18 const static uint32_t PREAMBLE_LENGTH_1024 = (1 << 19);
bhepp 3:6a9372463510 19 const static uint32_t PREAMBLE_LENGTH_2048 = (1 << 19) | (1 << 21);
bhepp 3:6a9372463510 20 const static uint32_t PREAMBLE_LENGTH_4096 = (1 << 18) | (1 << 19);
bhepp 3:6a9372463510 21
bhepp 3:6a9372463510 22 enum PrfSetting
bhepp 3:6a9372463510 23 {
bhepp 3:6a9372463510 24 PRF_16_MHz = 1,
bhepp 3:6a9372463510 25 PRF_64_MHz,
bhepp 3:6a9372463510 26 };
bhepp 3:6a9372463510 27
bhepp 3:6a9372463510 28 enum DataRateSetting
bhepp 3:6a9372463510 29 {
bhepp 3:6a9372463510 30 RATE_110_kbps = 1,
bhepp 3:6a9372463510 31 RATE_850_kbps,
bhepp 3:6a9372463510 32 RATE_6_8_Mbps,
bhepp 3:6a9372463510 33 };
bhepp 3:6a9372463510 34
bhepp 3:6a9372463510 35 // Set pulse repetition frequency
bhepp 3:6a9372463510 36 static void setPulseRepetitionFrequency(DW1000* dw_ptr, PrfSetting prf_setting)
bhepp 2:12a2907957b8 37 {
bhepp 3:6a9372463510 38 // Transmit PRF setting (see page 75 of user manual)
bhepp 3:6a9372463510 39 uint32_t prf_value;
bhepp 3:6a9372463510 40 if (prf_setting == PRF_16_MHz)
bhepp 3:6a9372463510 41 {
bhepp 3:6a9372463510 42 prf_value = (1 << 16);
bhepp 3:6a9372463510 43 }
bhepp 3:6a9372463510 44 else
bhepp 3:6a9372463510 45 {
bhepp 3:6a9372463510 46 prf_value = (1 << 17);
bhepp 3:6a9372463510 47 }
bhepp 3:6a9372463510 48 uint32_t prf_mask = (1 << 16) | (1 << 17);
bhepp 3:6a9372463510 49 uint32_t tx_ctrl = dw_ptr->readRegister32(DW1000_TX_FCTRL, 0x00);
bhepp 3:6a9372463510 50 tx_ctrl &= ~prf_mask;
bhepp 3:6a9372463510 51 tx_ctrl |= (prf_value & prf_mask);
bhepp 3:6a9372463510 52 dw_ptr->writeRegister32(DW1000_TX_FCTRL, 0x00, tx_ctrl);
bhepp 3:6a9372463510 53
bhepp 3:6a9372463510 54 // Receive PRF setting (see page 109 and of user manual)
bhepp 3:6a9372463510 55 if (prf_setting == PRF_16_MHz)
bhepp 3:6a9372463510 56 {
bhepp 3:6a9372463510 57 prf_value = (1 << 18);
bhepp 3:6a9372463510 58 }
bhepp 3:6a9372463510 59 else
bhepp 3:6a9372463510 60 {
bhepp 3:6a9372463510 61 prf_value = (1 << 19);
bhepp 3:6a9372463510 62 }
bhepp 3:6a9372463510 63 prf_mask = (1 << 18) | (1 << 19);
bhepp 3:6a9372463510 64 uint32_t chan_ctrl = dw_ptr->readRegister32(DW1000_CHAN_CTRL, 0x00);
bhepp 3:6a9372463510 65 chan_ctrl &= ~prf_mask;
bhepp 3:6a9372463510 66 chan_ctrl |= (prf_value & prf_mask);
bhepp 3:6a9372463510 67 dw_ptr->writeRegister32(DW1000_CHAN_CTRL, 0x00, chan_ctrl);
bhepp 3:6a9372463510 68 }
bhepp 3:6a9372463510 69
bhepp 3:6a9372463510 70 // Set preamble length (see page 76 of user manual)
bhepp 3:6a9372463510 71 static void setPreambleLength(DW1000* dw_ptr, uint32_t preamble_setting)
bhepp 3:6a9372463510 72 {
bhepp 3:6a9372463510 73 uint32_t preamble_mask = (1 << 18) | (1 << 19) | (1 << 20) | (1 << 21);
bhepp 3:6a9372463510 74 uint32_t tx_ctrl = dw_ptr->readRegister32(DW1000_TX_FCTRL, 0x00);
bhepp 3:6a9372463510 75 tx_ctrl &= ~preamble_mask;
bhepp 3:6a9372463510 76 tx_ctrl |= (preamble_setting & preamble_mask);
bhepp 2:12a2907957b8 77 dw_ptr->writeRegister32(DW1000_TX_FCTRL, 0x00, tx_ctrl);
bhepp 2:12a2907957b8 78 }
bhepp 3:6a9372463510 79
bhepp 3:6a9372463510 80 // Set data rate
bhepp 3:6a9372463510 81 static void setDataRate(DW1000* dw_ptr, DataRateSetting rate_setting)
bhepp 3:6a9372463510 82 {
bhepp 3:6a9372463510 83 // Transmit data rate (see page 73 of user manual)
bhepp 3:6a9372463510 84 uint32_t rate_value;
bhepp 3:6a9372463510 85 if (rate_setting == RATE_110_kbps)
bhepp 3:6a9372463510 86 {
bhepp 3:6a9372463510 87 rate_value = 0;
bhepp 3:6a9372463510 88 }
bhepp 3:6a9372463510 89 else if (rate_setting == RATE_850_kbps)
bhepp 3:6a9372463510 90 {
bhepp 3:6a9372463510 91 rate_value = (1 << 13);
bhepp 3:6a9372463510 92 }
bhepp 3:6a9372463510 93 else
bhepp 3:6a9372463510 94 {
bhepp 3:6a9372463510 95 rate_value = (1 << 14);
bhepp 3:6a9372463510 96 }
bhepp 3:6a9372463510 97 uint32_t rate_mask = (1 << 13) | (1 << 14);
bhepp 3:6a9372463510 98 uint32_t tx_ctrl = dw_ptr->readRegister32(DW1000_TX_FCTRL, 0x00);
bhepp 3:6a9372463510 99 tx_ctrl &= ~rate_mask;
bhepp 3:6a9372463510 100 tx_ctrl |= (rate_value & rate_mask);
bhepp 3:6a9372463510 101 dw_ptr->writeRegister32(DW1000_TX_FCTRL, 0x00, tx_ctrl);
bhepp 3:6a9372463510 102
bhepp 3:6a9372463510 103 // Receive data rate (see page 72 of user manual)
bhepp 3:6a9372463510 104 if (rate_setting == RATE_110_kbps)
bhepp 3:6a9372463510 105 {
bhepp 3:6a9372463510 106 rate_value = (1 << 18);
bhepp 3:6a9372463510 107 rate_value = (1 << 22);
bhepp 3:6a9372463510 108 }
bhepp 3:6a9372463510 109 else if (rate_setting == RATE_850_kbps)
bhepp 3:6a9372463510 110 {
bhepp 3:6a9372463510 111 rate_value = (1 << 18);
bhepp 3:6a9372463510 112 }
bhepp 3:6a9372463510 113 else
bhepp 3:6a9372463510 114 {
bhepp 3:6a9372463510 115 rate_value = 0;
bhepp 3:6a9372463510 116 }
bhepp 3:6a9372463510 117 rate_mask = (1 << 18) | (1 << 22);
bhepp 3:6a9372463510 118 uint32_t sys_cfg = dw_ptr->readRegister32(DW1000_SYS_CFG, 0x00);
bhepp 3:6a9372463510 119 sys_cfg &= ~rate_mask;
bhepp 3:6a9372463510 120 sys_cfg |= (rate_value & rate_mask);
bhepp 3:6a9372463510 121 dw_ptr->writeRegister32(DW1000_SYS_CFG, 0x00, sys_cfg);
bhepp 3:6a9372463510 122
bhepp 3:6a9372463510 123 if (rate_setting == RATE_110_kbps)
bhepp 3:6a9372463510 124 {
bhepp 3:6a9372463510 125 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x02, 0x000A); // DRX_TUNE0b for 110 kbps
bhepp 3:6a9372463510 126 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x06, 0x0064); // DRX_TUNE1b for 110 kbps & > 1024 symbols
bhepp 3:6a9372463510 127 }
bhepp 3:6a9372463510 128 else if (rate_setting == RATE_850_kbps)
bhepp 3:6a9372463510 129 {
bhepp 3:6a9372463510 130 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x02, 0x0001); // DRX_TUNE0b for 850 kbps
bhepp 3:6a9372463510 131 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x06, 0x0020); // DRX_TUNE1b for 850 kbps & 128 - 1024 symbols
bhepp 3:6a9372463510 132 }
bhepp 3:6a9372463510 133 else
bhepp 3:6a9372463510 134 {
bhepp 3:6a9372463510 135 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x02, 0x0001); // DRX_TUNE0b for 6.8 Mbps
bhepp 3:6a9372463510 136 dw_ptr->writeRegister16(DW1000_DRX_CONF, 0x06, 0x0010); // DRX_TUNE1b for 6.8 Mbps & 64 symbols
bhepp 3:6a9372463510 137 }
bhepp 3:6a9372463510 138 }
bhepp 3:6a9372463510 139
bhepp 3:6a9372463510 140 // Improved settings for direct path detection in non-line-of-sight environments.
bhepp 3:6a9372463510 141 // See DecaWave Application Note APS006.
bhepp 3:6a9372463510 142 static void setNLOSSettings(DW1000* dw_ptr, DataRateSetting rate_setting = RATE_850_kbps, PrfSetting prf_setting = PRF_16_MHz, uint32_t preamble_setting = PREAMBLE_LENGTH_1024)
bhepp 3:6a9372463510 143 {
bhepp 3:6a9372463510 144 setDataRate(dw_ptr, rate_setting);
bhepp 3:6a9372463510 145 setPulseRepetitionFrequency(dw_ptr, prf_setting);
bhepp 3:6a9372463510 146 setPreambleLength(dw_ptr, preamble_setting);
bhepp 3:6a9372463510 147
bhepp 3:6a9372463510 148 // Setting for Noise Threshold Multiplier 1
bhepp 3:6a9372463510 149 dw_ptr->writeRegister8(DW1000_LDE_CTRL, 0x0806, 0x07); // LDE_CFG1
bhepp 3:6a9372463510 150 // Setting for Noise Threshold Multiplier 2
bhepp 3:6a9372463510 151 if (prf_setting == PRF_16_MHz)
bhepp 3:6a9372463510 152 {
bhepp 3:6a9372463510 153 dw_ptr->writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x0003); // LDE_CFG2 for 16 MHz PRF
bhepp 3:6a9372463510 154 }
bhepp 3:6a9372463510 155 else
bhepp 3:6a9372463510 156 {
bhepp 3:6a9372463510 157 dw_ptr->writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1603); // LDE_CFG2 for 64 MHz PRF
bhepp 3:6a9372463510 158 }
bhepp 3:6a9372463510 159 }
bhepp 3:6a9372463510 160
bhepp 3:6a9372463510 161 // Default settings for line-of-sight environments
bhepp 3:6a9372463510 162 static void setLOSSettings(DW1000* dw_ptr, DataRateSetting rate_setting = RATE_850_kbps, PrfSetting prf_setting = PRF_16_MHz, uint32_t preamble_setting = PREAMBLE_LENGTH_1024)
bhepp 3:6a9372463510 163 {
bhepp 3:6a9372463510 164 setDataRate(dw_ptr, rate_setting);
bhepp 3:6a9372463510 165 setPulseRepetitionFrequency(dw_ptr, prf_setting);
bhepp 3:6a9372463510 166 setPreambleLength(dw_ptr, preamble_setting);
bhepp 3:6a9372463510 167
bhepp 3:6a9372463510 168 // Setting for Noise Threshold Multiplier 1
bhepp 3:6a9372463510 169 dw_ptr->writeRegister8(DW1000_LDE_CTRL, 0x0806, 0x0c); // LDE_CFG1
bhepp 3:6a9372463510 170 // dw_ptr->writeRegister8(DW1000_LDE_CTRL, 0x0806, 0x0d); // LDE_CFG1
bhepp 3:6a9372463510 171 // Setting for Noise Threshold Multiplier 2
bhepp 3:6a9372463510 172 if (prf_setting == PRF_16_MHz)
bhepp 3:6a9372463510 173 {
bhepp 3:6a9372463510 174 dw_ptr->writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1607); // LDE_CFG2 for 16 MHz PRF
bhepp 3:6a9372463510 175 }
bhepp 3:6a9372463510 176 else
bhepp 3:6a9372463510 177 {
bhepp 3:6a9372463510 178 dw_ptr->writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x0607); // LDE_CFG2 for 64 MHz PRF
bhepp 3:6a9372463510 179 }
bhepp 3:6a9372463510 180 }
bhepp 2:12a2907957b8 181 };