Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

main.cpp

Committer:
manumaet
Date:
2014-11-24
Revision:
16:96879e1c99f2
Parent:
15:e1fea7e2aff1
Child:
17:8afa5f9122da

File content as of revision 16:96879e1c99f2:

#include "mbed.h"
#include "PC.h"                         // Serial Port via USB for debugging with Terminal
#include "DW1000.h"

PC          pc(USBTX, USBRX, 921600);   // USB UART Terminal
DW1000      dw(D11, D12, D13, D10, D14);     // SPI1 on Nucleo Board (MOSI, MISO, SCLK, CS, IRQ)

const float timeunit = 1/(128*499.2e6);
int i=0;
bool starter = 1;
bool ping = 0;
char message[1024] = "";
uint64_t timestamp_old = 0;

//#define PINGPONG

void Interrupthandler() {
    /*uint8_t frameready = 0;
    dw.readRegister(DW1000_SYS_STATUS, 1, &frameready, 1);
    pc.printf("Interrupt status: %X\r\n", frameready);*/
    
    uint16_t framelength = 0;                                       // get framelength TODO: just for debugging of string
    dw.readRegister(DW1000_RX_FINFO, 0, (uint8_t*)&framelength, 2);
    framelength &= 0x03FF;
    framelength -= 2;
    
    if(framelength<200) {
        char* receive = dw.receiveString();                             // receive a string
        pc.printf("Received: \"%s\" %d ", receive, framelength);
        delete[] receive;
    } 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
    pc.printf("Status: %010llX  ", status);
    
    uint64_t timestamp;
    dw.readRegister(DW1000_RX_TIME, 0, (uint8_t*)&timestamp, 5);
    timestamp &= 0xFFFFFFFFFF;                                      // only 40-Bit
    uint64_t difference = timestamp - timestamp_old;
    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();
}

int main() {
    pc.printf("DecaWave 0.1\r\nup and running!\r\n");
    
    dw.setEUI(0xFAEDCD01FAEDCD01);                  // basic methods called to check if we have a slave
    pc.printf("%d DEVICE_ID register: 0x%X\r\n", i, dw.getDeviceID());
    pc.printf("%d EUI register: %016llX\r\n", i, dw.getEUI());
    pc.printf("%d Voltage: %f\r\n", i, dw.getVoltage());
    
    uint32_t conf = 0;     // read System Configuration
    dw.readRegister(DW1000_SYS_CFG, 0, (uint8_t*)&conf, 4);
    pc.printf("%d System Configuration: %X\r\n", i, conf);
    
    dw.callbackRX = &Interrupthandler;
    
    // 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
    
    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);
        
        //wait(0.1);
        char messagecheck[1024];
        dw.sendString(message);
        wait(0.1);
        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);
        
        //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
#if 0
        pc.printf("%d Waiting...  ", i);
        uint64_t status;
        dw.readRegister(DW1000_SYS_STATUS, 0, (uint8_t*)&status, 5);
        status &= 0xFFFFFFFFFF;                                      // only 40-Bit
        pc.printf("Status: %010llX\r\n", status);
        wait(5);
#endif
        i++;
    }
}