Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

Committer:
manumaet
Date:
Sun Nov 23 20:45:14 2014 +0000
Revision:
15:e1fea7e2aff1
Parent:
14:8041c9b68406
Child:
16:96879e1c99f2
sending of 1020 chars in one frame worked

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manumaet 0:f50e671ffff7 1 #include "mbed.h"
manumaet 0:f50e671ffff7 2 #include "PC.h" // Serial Port via USB for debugging with Terminal
manumaet 0:f50e671ffff7 3 #include "DW1000.h"
manumaet 0:f50e671ffff7 4
manumaet 0:f50e671ffff7 5 PC pc(USBTX, USBRX, 921600); // USB UART Terminal
manumaet 7:e634eeafc4d2 6 DW1000 dw(D11, D12, D13, D10, D14); // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)
manumaet 0:f50e671ffff7 7
manumaet 15:e1fea7e2aff1 8 const float timeunit = 1/(128*499.2e6);
manumaet 14:8041c9b68406 9 int i=0;
manumaet 14:8041c9b68406 10 bool starter = 1;
manumaet 14:8041c9b68406 11 bool ping = 0;
manumaet 14:8041c9b68406 12 char message[1024] = "";
manumaet 11:c87d37db2c6f 13 uint64_t timestamp_old = 0;
manumaet 14:8041c9b68406 14
manumaet 15:e1fea7e2aff1 15 //#define PINGPONG
manumaet 5:111f11c95d27 16
manumaet 6:d5864a1b9e17 17 void Interrupthandler() {
manumaet 10:d077bb12d259 18 /*uint8_t frameready = 0;
manumaet 7:e634eeafc4d2 19 dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
manumaet 10:d077bb12d259 20 pc.printf("Interrupt status: %X\r\n", frameready);*/
manumaet 10:d077bb12d259 21
manumaet 12:985aa9843c3c 22 uint16_t framelength = 0; // get framelength TODO: just for debugging of string
manumaet 10:d077bb12d259 23 dw.readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2);
manumaet 10:d077bb12d259 24 framelength &= 0x03FF;
manumaet 10:d077bb12d259 25 framelength -= 2;
manumaet 10:d077bb12d259 26
manumaet 12:985aa9843c3c 27 char* receive = dw.receiveString(); // receive a string
manumaet 15:e1fea7e2aff1 28 pc.printf("Received: \"%s\" %d ", receive, framelength);
manumaet 10:d077bb12d259 29 delete[] receive;
manumaet 10:d077bb12d259 30
manumaet 15:e1fea7e2aff1 31 # ifdef PINGPONG
manumaet 11:c87d37db2c6f 32 uint64_t status;
manumaet 11:c87d37db2c6f 33 dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
manumaet 11:c87d37db2c6f 34 status &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 35 pc.printf("Status: %010llX ", status);
manumaet 11:c87d37db2c6f 36
manumaet 11:c87d37db2c6f 37 uint64_t timestamp;
manumaet 11:c87d37db2c6f 38 dw.readRegister(DW1000_RX_TIME, 0, (uint8_t*)&timestamp, 5);
manumaet 11:c87d37db2c6f 39 timestamp &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 40 uint64_t difference = timestamp - timestamp_old;
manumaet 11:c87d37db2c6f 41 timestamp_old = timestamp;
manumaet 15:e1fea7e2aff1 42 //pc.printf("Timestamp: %lld\r\n", difference);
manumaet 15:e1fea7e2aff1 43 pc.printf("Timestamp: %fs", difference*timeunit); // TODO: gives some wrong values because of timer overflow
manumaet 11:c87d37db2c6f 44
manumaet 15:e1fea7e2aff1 45 starter = 0;
manumaet 14:8041c9b68406 46 wait(1); // TODO: ugly, never wait in interrupthandler!
manumaet 14:8041c9b68406 47 if (ping) {
manumaet 14:8041c9b68406 48 sprintf((char*)message, "PING! %d", i);
manumaet 14:8041c9b68406 49 dw.sendString(message);
manumaet 14:8041c9b68406 50 } else {
manumaet 14:8041c9b68406 51 sprintf((char*)message, "PONG! %d", i);
manumaet 14:8041c9b68406 52 dw.sendString(message);
manumaet 14:8041c9b68406 53 }
manumaet 15:e1fea7e2aff1 54 #endif
manumaet 15:e1fea7e2aff1 55 pc.printf("\r\n");
manumaet 8:7a9c61242e2f 56 dw.receiveFrame();
manumaet 6:d5864a1b9e17 57 }
manumaet 6:d5864a1b9e17 58
manumaet 0:f50e671ffff7 59 int main() {
manumaet 0:f50e671ffff7 60 pc.printf("DecaWave 0.1\r\nup and running!\r\n");
manumaet 0:f50e671ffff7 61
manumaet 14:8041c9b68406 62 dw.setEUI(0xFAEDCD01FAEDCD01); // basic methods called to check if we have a slave
manumaet 4:6240b9c7a033 63 pc.printf("%d DEVICE_ID register: 0x%X\r\n", i, dw.getDeviceID());
manumaet 4:6240b9c7a033 64 pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
manumaet 4:6240b9c7a033 65 pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
manumaet 0:f50e671ffff7 66
manumaet 12:985aa9843c3c 67 uint32_t conf = 0; // read System Configuration
manumaet 4:6240b9c7a033 68 dw.readRegister(DW1000_SYS_CFG, 0, (uint8_t*)&conf, 4);
manumaet 4:6240b9c7a033 69 pc.printf("%d System Configuration: %X\r\n", i, conf);
manumaet 4:6240b9c7a033 70
manumaet 8:7a9c61242e2f 71 dw.callbackRX = &Interrupthandler;
manumaet 14:8041c9b68406 72
manumaet 14:8041c9b68406 73 // Receiver initialisation
manumaet 10:d077bb12d259 74 uint8_t dataframereadyinterrupt = 0x40; // only good frame 0x40 all frames 0x20
manumaet 5:111f11c95d27 75 dw.writeRegister(DW1000_SYS_MASK, 1, &dataframereadyinterrupt, 1);
manumaet 7:e634eeafc4d2 76
manumaet 8:7a9c61242e2f 77 dw.receiveFrame();
manumaet 15:e1fea7e2aff1 78 # ifdef PINGPONG
manumaet 14:8041c9b68406 79 wait(3);
manumaet 15:e1fea7e2aff1 80 # endif
manumaet 5:111f11c95d27 81
manumaet 0:f50e671ffff7 82 while(1) {
manumaet 15:e1fea7e2aff1 83 # ifndef PINGPONG
manumaet 15:e1fea7e2aff1 84 message[i] = 48+ (i%10);
manumaet 15:e1fea7e2aff1 85 message[i+1] = '\0';
manumaet 15:e1fea7e2aff1 86
manumaet 15:e1fea7e2aff1 87 pc.printf("%d Message: \"%s\" %d\r\n", i, message, strlen(message)+1);
manumaet 15:e1fea7e2aff1 88 wait(0.1);
manumaet 15:e1fea7e2aff1 89 char messagecheck[1024];
manumaet 10:d077bb12d259 90 dw.sendString(message);
manumaet 15:e1fea7e2aff1 91 wait(0.1);
manumaet 10:d077bb12d259 92 dw.readRegister(DW1000_TX_BUFFER, 0, (uint8_t*)messagecheck, strlen(message)+1);
manumaet 15:e1fea7e2aff1 93 pc.printf("%d nBuffer: \"%s\" %d\r\n\r\n", i, messagecheck, strlen(messagecheck)+1);
manumaet 15:e1fea7e2aff1 94 wait(0.2);
manumaet 15:e1fea7e2aff1 95
manumaet 15:e1fea7e2aff1 96 //return 0;
manumaet 9:c8839de428ac 97 /*for(int i=0; i<10; i++) { // to control Voltage
manumaet 9:c8839de428ac 98 pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
manumaet 9:c8839de428ac 99 wait(0.2);
manumaet 9:c8839de428ac 100 }*/
manumaet 15:e1fea7e2aff1 101 #else
manumaet 14:8041c9b68406 102 if (starter) {
manumaet 14:8041c9b68406 103 ping = 1;
manumaet 14:8041c9b68406 104 while (starter) {
manumaet 14:8041c9b68406 105 sprintf((char*)message, "PING! %d", i);
manumaet 14:8041c9b68406 106 dw.sendString(message);
manumaet 14:8041c9b68406 107 wait(1);
manumaet 14:8041c9b68406 108 }
manumaet 14:8041c9b68406 109 }
manumaet 14:8041c9b68406 110
manumaet 15:e1fea7e2aff1 111 wait(1);
manumaet 15:e1fea7e2aff1 112 # endif
manumaet 15:e1fea7e2aff1 113 #if 0
manumaet 15:e1fea7e2aff1 114 pc.printf("%d Waiting... ", i);
manumaet 11:c87d37db2c6f 115 uint64_t status;
manumaet 11:c87d37db2c6f 116 dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
manumaet 11:c87d37db2c6f 117 status &= 0xFFFFFFFFFF; // only 40-Bit
manumaet 11:c87d37db2c6f 118 pc.printf("Status: %010llX\r\n", status);
manumaet 15:e1fea7e2aff1 119 wait(5);
manumaet 15:e1fea7e2aff1 120 #endif
manumaet 15:e1fea7e2aff1 121 i++;
manumaet 0:f50e671ffff7 122 }
manumaet 0:f50e671ffff7 123 }