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.
Revision 17:8afa5f9122da, committed 2014-11-24
- Comitter:
- manumaet
- Date:
- Mon Nov 24 14:09:49 2014 +0000
- Parent:
- 16:96879e1c99f2
- Child:
- 18:bbc7ca7d3a95
- Commit message:
- 1021 string now works with API
Changed in this revision
--- a/DW1000/DW1000.cpp Mon Nov 24 12:48:40 2014 +0000
+++ b/DW1000/DW1000.cpp Mon Nov 24 14:09:49 2014 +0000
@@ -1,11 +1,13 @@
#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;
+
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
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!!
@@ -55,23 +57,29 @@
}
void DW1000::sendFrame(uint8_t* message, uint16_t length) {
- writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode TODO: only if receiving!!
+ if (length >= 1021) length = 1021;
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
length += 2;
length = ((backup & 0xFC) << 8) | (length & 0x03FF);
-
- writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2); // TODO: make that bigger frames than 256 can be sent
+ writeRegister(DW1000_TX_FCTRL, 0, (uint8_t*)&length, 2);
+ if (receiving) stopTRX(); // stop receiving if we are
writeRegister8(DW1000_SYS_CTRL, 0, 0x02); // trigger sending process by setting the TXSTRT bit
- receiveFrame(); // TODO: only if receiving!!
+ if (receiving) startRX(); // enable receiver again if we need it TODO: safe to do this directly ??? only after sending ended
}
-void DW1000::receiveFrame() {
+void DW1000::startRX() {
+ receiving = true;
writeRegister8(DW1000_SYS_CTRL, 0x01, 0x01); // start listening for preamble by setting the RXENAB bit
}
+void DW1000::stopRX() {
+ receiving = false;
+
+}
+
void DW1000::ISR() {
uint64_t status; // get the entire system status
readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
@@ -90,6 +98,10 @@
writeRegister(DW1000_PMSC, 0, (uint8_t*)&ldeload[2], 2); // recover to PLL clock
}
+void DW1000::stopTRX() {
+ writeRegister8(DW1000_SYS_CTRL, 0, 0x40); // disable tranceiver go back to idle mode
+}
+
void DW1000::resetRX() {
writeRegister8(DW1000_PMSC, 3, 0xE0); // set RX reset
writeRegister8(DW1000_PMSC, 3, 0xF0); // clear RX reset
--- a/DW1000/DW1000.h Mon Nov 24 12:48:40 2014 +0000
+++ b/DW1000/DW1000.h Mon Nov 24 14:09:49 2014 +0000
@@ -6,46 +6,46 @@
#include "mbed.h"
// register addresses
-// Mnemonic Address Byte Description
-#define DW1000_DEV_ID 0x00 // 4 Device Identifier – includes device type and revision information
-#define DW1000_EUI 0x01 // 8 Extended Unique Identifier
-#define DW1000_PANADR 0x03 // 4 PAN Identifier and Short Address
-#define DW1000_SYS_CFG 0x04 // 4 System Configuration bitmap
-#define DW1000_SYS_TIME 0x06 // 5 System Time Counter (40-bit)
-#define DW1000_TX_FCTRL 0x08 // 5 Transmit Frame Control
-#define DW1000_TX_BUFFER 0x09 // 1024 Transmit Data Buffer
-#define DW1000_DX_TIME 0x0A // 5 Delayed Send or Receive Time (40-bit)
-#define DW1000_RX_FWTO 0x0C // 2 Receive Frame Wait Timeout Period
-#define DW1000_SYS_CTRL 0x0D // 4 System Control Register
-#define DW1000_SYS_MASK 0x0E // 4 System Event Mask Register
-#define DW1000_SYS_STATUS 0x0F // 5 System Event Status Register
-#define DW1000_RX_FINFO 0x10 // 4 RX Frame Information (in double buffer set)
-#define DW1000_RX_BUFFER 0x11 // 1024 Receive Data Buffer (in double buffer set)
-#define DW1000_RX_FQUAL 0x12 // 8 Rx Frame Quality information (in double buffer set)
-#define DW1000_RX_TTCKI 0x13 // 4 Receiver Time Tracking Interval (in double buffer set)
-#define DW1000_RX_TTCKO 0x14 // 5 Receiver Time Tracking Offset (in double buffer set)
-#define DW1000_RX_TIME 0x15 // 14 Receive Message Time of Arrival (in double buffer set)
-#define DW1000_TX_TIME 0x17 // 10 Transmit Message Time of Sending (in double buffer set)
-#define DW1000_TX_ANTD 0x18 // 2 16-bit Delay from Transmit to Antenna
-#define DW1000_SYS_STATE 0x19 // 5 System State information
-#define DW1000_ACK_RESP_T 0x1A // 4 Acknowledgement Time and Response Time
-#define DW1000_RX_SNIFF 0x1D // 4 Pulsed Preamble Reception Configuration
-#define DW1000_TX_POWER 0x1E // 4 TX Power Control
-#define DW1000_CHAN_CTRL 0x1F // 4 Channel Control
-#define DW1000_USR_SFD 0x21 // 41 User-specified short/long TX/RX SFD sequences
-#define DW1000_AGC_CTRL 0x23 // 32 Automatic Gain Control configuration
-#define DW1000_EXT_SYNC 0x24 // 12 External synchronisation control.
-#define DW1000_ACC_MEM 0x25 // 4064 Read access to accumulator data
-#define DW1000_GPIO_CTRL 0x26 // 44 Peripheral register bus 1 access - GPIO control
-#define DW1000_DRX_CONF 0x27 // 44 Digital Receiver configuration
-#define DW1000_RF_CONF 0x28 // 58 Analog RF Configuration
-#define DW1000_TX_CAL 0x2A // 52 Transmitter calibration block
-#define DW1000_FS_CTRL 0x2B // 21 Frequency synthesiser control block
-#define DW1000_AON 0x2C // 12 Always-On register set
-#define DW1000_OTP_IF 0x2D // 18 One Time Programmable Memory Interface
-#define DW1000_LDE_CTRL 0x2E // - Leading edge detection control block
-#define DW1000_DIG_DIAG 0x2F // 41 Digital Diagnostics Interface
-#define DW1000_PMSC 0x36 // 48 Power Management System Control Block
+// Mnemonic Address Bytes Description
+#define DW1000_DEV_ID 0x00 // 4 Device Identifier – includes device type and revision information
+#define DW1000_EUI 0x01 // 8 Extended Unique Identifier
+#define DW1000_PANADR 0x03 // 4 PAN Identifier and Short Address
+#define DW1000_SYS_CFG 0x04 // 4 System Configuration bitmap
+#define DW1000_SYS_TIME 0x06 // 5 System Time Counter (40-bit)
+#define DW1000_TX_FCTRL 0x08 // 5 Transmit Frame Control
+#define DW1000_TX_BUFFER 0x09 // 1024 Transmit Data Buffer
+#define DW1000_DX_TIME 0x0A // 5 Delayed Send or Receive Time (40-bit)
+#define DW1000_RX_FWTO 0x0C // 2 Receive Frame Wait Timeout Period
+#define DW1000_SYS_CTRL 0x0D // 4 System Control Register
+#define DW1000_SYS_MASK 0x0E // 4 System Event Mask Register
+#define DW1000_SYS_STATUS 0x0F // 5 System Event Status Register
+#define DW1000_RX_FINFO 0x10 // 4 RX Frame Information (in double buffer set)
+#define DW1000_RX_BUFFER 0x11 // 1024 Receive Data Buffer (in double buffer set)
+#define DW1000_RX_FQUAL 0x12 // 8 Rx Frame Quality information (in double buffer set)
+#define DW1000_RX_TTCKI 0x13 // 4 Receiver Time Tracking Interval (in double buffer set)
+#define DW1000_RX_TTCKO 0x14 // 5 Receiver Time Tracking Offset (in double buffer set)
+#define DW1000_RX_TIME 0x15 // 14 Receive Message Time of Arrival (in double buffer set)
+#define DW1000_TX_TIME 0x17 // 10 Transmit Message Time of Sending (in double buffer set)
+#define DW1000_TX_ANTD 0x18 // 2 16-bit Delay from Transmit to Antenna
+#define DW1000_SYS_STATE 0x19 // 5 System State information
+#define DW1000_ACK_RESP_T 0x1A // 4 Acknowledgement Time and Response Time
+#define DW1000_RX_SNIFF 0x1D // 4 Pulsed Preamble Reception Configuration
+#define DW1000_TX_POWER 0x1E // 4 TX Power Control
+#define DW1000_CHAN_CTRL 0x1F // 4 Channel Control
+#define DW1000_USR_SFD 0x21 // 41 User-specified short/long TX/RX SFD sequences
+#define DW1000_AGC_CTRL 0x23 // 32 Automatic Gain Control configuration
+#define DW1000_EXT_SYNC 0x24 // 12 External synchronisation control.
+#define DW1000_ACC_MEM 0x25 // 4064 Read access to accumulator data
+#define DW1000_GPIO_CTRL 0x26 // 44 Peripheral register bus 1 access - GPIO control
+#define DW1000_DRX_CONF 0x27 // 44 Digital Receiver configuration
+#define DW1000_RF_CONF 0x28 // 58 Analog RF Configuration
+#define DW1000_TX_CAL 0x2A // 52 Transmitter calibration block
+#define DW1000_FS_CTRL 0x2B // 21 Frequency synthesiser control block
+#define DW1000_AON 0x2C // 12 Always-On register set
+#define DW1000_OTP_IF 0x2D // 18 One Time Programmable Memory Interface
+#define DW1000_LDE_CTRL 0x2E // - Leading edge detection control block
+#define DW1000_DIG_DIAG 0x2F // 41 Digital Diagnostics Interface
+#define DW1000_PMSC 0x36 // 48 Power Management System Control Block
#define DW1000_WRITE_FLAG 0x80 // First Bit of the address has to be 1 to indicate we want to write
#define DW1000_SUBADDRESS_FLAG 0x40 // if we have a sub address second Bit has to be 1
@@ -56,29 +56,33 @@
DW1000(PinName MOSI, PinName MISO, PinName SCLK, PinName CS, PinName IRQ); // constructor, uses SPI class
// Device API
- uint32_t getDeviceID();
- uint64_t getEUI();
- void setEUI(uint64_t EUI);
- float getVoltage();
+ uint32_t getDeviceID(); // gets the Device ID which should be 0xDECA0130 (good for testing SPI!)
+ uint64_t getEUI(); // gets 64 bit Extended Unique Identifier according to IEEE standard
+ void setEUI(uint64_t EUI); // sets 64 bit Extended Unique Identifier according to IEEE standard
+ float getVoltage(); // gets the current chip voltage measurement form the A/D converter
void sendString(char* message); // to send String with arbitrary length
- char* receiveString(); // to receive char string with arbitrary length (ATTENTION! you have to delete the returned memory location as "client delete[] receive;")
- void sendFrame(uint8_t* message, uint16_t length);
- void receiveFrame();
+ char* receiveString(); // to receive char string with arbitrary length (ATTENTION! you have to delete the returned memory location like "client delete[] receivedMessage;")
+ void sendFrame(uint8_t* message, uint16_t length); // send a raw frame (length in bytes)
+ void startRX(); // start listening for frames
+ void stopRX(); // stop listening for frames
//private:
- // Interrupt
- void (*callbackRX) ();
- void ISR();
+ bool receiving; // holds if we need to preserve a receiving state after sending
- void loadLDE(); // load the leading edge detection algorithm to RAM, [IMPORTANT because receiving malfunction may ocur] see User Manual LDELOAD on p22 & p158
- void resetRX();
- void resetAll();
+ void loadLDE(); // load the leading edge detection algorithm to RAM, [IMPORTANT because receiving malfunction may occur] see User Manual LDELOAD on p22 & p158
+ void stopTRX(); // disable tranceiver go back to idle mode
+ void resetRX(); // soft reset only the tranciever part of DW1000
+ void resetAll(); // soft reset the entire DW1000 (some registers stay as they were see User Manual)
+
+ // Interrupt
+ InterruptIn irq; // Pin used to handle Events from DW1000 by an Interrupthandler
+ void ISR(); // interrupt handling method (also calls according callback methods)
+ void (*callbackRX) (); // pointer to callback which is called when successfull RX took place
// SPI Inteface
SPI spi; // SPI Bus
DigitalOut cs; // Slave selector for SPI-Bus (here explicitly needed to start and end SPI transactions also usable to wake up DW1000)
- InterruptIn irq; // Pin used to handle Events from DW1000 by an Interrupthandler
uint8_t readRegister8(uint8_t reg, uint16_t subaddress); // expressive to read just one byte
void writeRegister8(uint8_t reg, uint16_t subaddress, uint8_t buffer); // expressive to write just one byte
--- a/main.cpp Mon Nov 24 12:48:40 2014 +0000
+++ b/main.cpp Mon Nov 24 14:09:49 2014 +0000
@@ -7,9 +7,7 @@
const float timeunit = 1/(128*499.2e6);
int i=0;
-bool starter = 1;
-bool ping = 0;
-char message[1024] = "";
+char message[1200] = "";
uint64_t timestamp_old = 0;
//#define PINGPONG
@@ -31,7 +29,6 @@
} else
pc.printf("Received! %d ", framelength);
-# ifdef PINGPONG
uint64_t status;
dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
status &= 0xFFFFFFFFFF; // only 40-Bit
@@ -44,19 +41,8 @@
timestamp_old = timestamp;
//pc.printf("Timestamp: %lld\r\n", difference);
pc.printf("Timestamp: %fs", difference*timeunit); // TODO: gives some wrong values because of timer overflow
-
- starter = 0;
- wait(1); // TODO: ugly, never wait in interrupthandler!
- if (ping) {
- sprintf((char*)message, "PING! %d", i);
- dw.sendString(message);
- } else {
- sprintf((char*)message, "PONG! %d", i);
- dw.sendString(message);
- }
-#endif
pc.printf("\r\n");
- dw.receiveFrame();
+ dw.startRX();
}
int main() {
@@ -76,44 +62,26 @@
// Receiver initialisation
uint8_t dataframereadyinterrupt = 0x40; // only good frame 0x40 all frames 0x20
dw.writeRegister(DW1000_SYS_MASK, 1, &dataframereadyinterrupt, 1);
-
- dw.receiveFrame();
-# ifdef PINGPONG
- wait(3);
-# endif
+ dw.startRX();
while(1) {
-# ifndef PINGPONG
- message[i] = 48+ (i%10);
- message[i+1] = '\0';
-
- pc.printf("%d Message: \"%s\" %d\r\n", i, message, strlen(message)+1);
+#if 1
+ if(i < 1200) {
+ message[i] = 48+ (i%10);
+ message[i+1] = '\0';
+ }
//wait(0.1);
- char messagecheck[1024];
+ char messagecheck[1200];
dw.sendString(message);
- wait(0.1);
+ wait_us(10000);
dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, strlen(message)+1);
- pc.printf("%d nBuffer: \"%s\" %d\r\n\r\n", i, messagecheck, strlen(messagecheck)+1);
- //wait(0.1);
+ if (i < 200)
+ pc.printf("%d Sent: \"%s\" %d\r\n", i, messagecheck, strlen(message)+1);
+ else
+ pc.printf("%d Sent! %d\r\n", i, strlen(message)+1);
- //return 0;
- /*for(int i=0; i<10; i++) { // to control Voltage
- pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
- wait(0.2);
- }*/
-#else
- if (starter) {
- ping = 1;
- while (starter) {
- sprintf((char*)message, "PING! %d", i);
- dw.sendString(message);
- wait(1);
- }
- }
-
- wait(1);
-# endif
+#endif
#if 0
pc.printf("%d Waiting... ", i);
uint64_t status;