void cph_deca_force_wakeup() { reset_DW1000(); spi_set_rate_low(); uint32_t id = dwt_readdevid(); if (id == 0xFFFFFFFF) { TRACE("DW asleep..waking\r\n"); // asleep, wakeup pio_set_pin_high(DW_WAKEUP_PIO_IDX); cph_millis_delay(1); pio_set_pin_low(DW_WAKEUP_PIO_IDX); cph_millis_delay(1); } }
void cph_deca_init_device() { dwt_txconfig_t txconfig; // Setup DECAWAVE reset_DW1000(); spi_set_rate_low(); dwt_initialise(DWT_LOADUCODE); spi_set_rate_high(); dwt_configure(&config); // txconfig.PGdly = 0xC2; // for channel 2 // txconfig.power = 0x07274767; // smart power, channel 2, 64MHz // dwt_setsmarttxpower(1); // dwt_configuretxrf(&txconfig); dwt_setrxantennadelay(RX_ANT_DLY); dwt_settxantennadelay(TX_ANT_DLY); }
/** * Application entry point. */ int simpleTx(void) { /* Reset and initialise DW1000. * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum * performance. */ reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */ //spi_set_rate_low(); dwt_initialise(DWT_LOADNONE); //spi_set_rate_high(); /* Configure DW1000. See NOTE 2 below. */ dwt_configure(&config); /* Loop forever sending frames periodically. */ while(1) { /* Write frame data to DW1000 and prepare transmission. See NOTE 3 below.*/ dwt_writetxdata(sizeof(tx_msg), tx_msg, 0); dwt_writetxfctrl(sizeof(tx_msg), 0); /* Start transmission. */ dwt_starttx(DWT_START_TX_IMMEDIATE); /* Poll DW1000 until TX frame sent event set. See NOTE 4 below. * STATUS register is 5 bytes long but, as the event we are looking at is in the first byte of the register, we can use this simplest API * function to access it.*/ while (!(status_reg = dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) { }; printf("Status reg now 0x%x\r\n",status_reg); /* Clear TX frame sent event. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); /* Execute a delay between transmissions. */ deca_sleep(TX_DELAY_MS); // toggle led ledToggle(); /* Increment the blink frame sequence number (modulo 256). */ tx_msg[BLINK_FRAME_SN_IDX]++; } }
void cph_deca_init_device() { dwt_txconfig_t txconfig; TRACE("ant dly: %d %d\r\n", RX_ANT_DLY, TX_ANT_DLY); // Setup DECAWAVE reset_DW1000(); spi_set_rate_low(); dwt_initialise(DWT_LOADUCODE); spi_set_rate_high(); dwt_configure(&cph_config->dwt_config); dwt_setrxantennadelay(RX_ANT_DLY); dwt_settxantennadelay(TX_ANT_DLY); TRACE("ant dly: %d %d\r\n", RX_ANT_DLY, TX_ANT_DLY); // Clear CLKPLL_LL dwt_write32bitreg(SYS_STATUS_ID, 0x02000000); }
int main() { Xil_ICacheEnable(); Xil_DCacheEnable(); print("---Entering main---\n\r"); { printf("LEDs and switches\r\n"); XGpio_Initialize(&inGpio,XPAR_AXI_GPIO_1_DEVICE_ID); XGpio_Initialize(&outGpio,XPAR_AXI_GPIO_0_DEVICE_ID); XGpio_DiscreteWrite(&outGpio,1,0); // leds off } { printf("Deca SPI test\r\n"); if (0 != openspi()){ printf("Init SPI failed\r\n"); } else { // get switches int sw = XGpio_DiscreteRead(&inGpio,1); XGpio_DiscreteWrite(&outGpio,1,sw); //printf("LED: %x\r\n",XGpio_DiscreteRead(&outGpio,1)); switch (sw & 0x7){ case 1: printf("SS TWR INIT\r\n"); ssTwrInit(); break; case 2: printf("SS TWR RESP\r\n"); ssTwrResp(); break; case 3: printf("Simple TX\r\n"); simpleTx(); break; case 4: printf("Simple RX\r\n"); simpleRx(); break; case 5: printf("TX Wait\r\n"); txWait(); break; case 6: printf("RX Wait\r\n"); rxWait(); break; default: /* Reset and initialise DW1000. */ reset_DW1000(); dwt_initialise(DWT_LOADNONE); /* Configure DW1000. */ printf("UBW configuration sequence\r\n"); dwt_configure(&config); dwt_configuretxrf(&txconfig); /* Activate continuous wave mode. */ dwt_configcwmode(config.chan); /* Wait for the wanted duration of the continuous wave transmission. */ printf("Waiting for UBW continuous wave transmission delay: %ds\r\n",CONT_WAVE_DURATION_MS/1000); deca_sleep(CONT_WAVE_DURATION_MS); /* Software reset of the DW1000 to deactivate continuous wave mode and go back to default state. Initialisation and configuration should be run * again if one wants to get the DW1000 back to normal operation. */ dwt_softreset(); } printf("Deca test done. press any key\r\n"); getchar(); } } print("---Exiting main---\n\r"); Xil_DCacheDisable(); Xil_ICacheDisable(); return 0; }
uint32 inittestapplication(void) { uint32 devID; instanceConfig_t instConfig; int i, result; SPI_ConfigFastRate(SPI_BaudRatePrescaler_16); //max SPI before PLLs configured is ~4M i = 10; //this is called here to wake up the device (i.e. if it was in sleep mode before the restart) devID = instancereaddeviceid(); printf("devID %08X\r\n", devID); //if the read of devide ID fails, the DW1000 could be asleep if (DWT_DEVICE_ID != devID) { // port_SPIx_clear_chip_select(); //CS low // Sleep(1); //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise // port_SPIx_set_chip_select(); //CS high // Sleep(7); printf("asleep...wakeup!\r\n"); pio_set_pin_high(DW_WAKEUP_PIO_IDX); Sleep(1); pio_set_pin_low(DW_WAKEUP_PIO_IDX); Sleep(7); devID = instancereaddeviceid(); printf("devID %08X\r\n", devID); // SPI not working or Unsupported Device ID if (DWT_DEVICE_ID != devID) { return (-1); } //clear the sleep bit - so that after the hard reset below the DW does not go into sleep dwt_softreset(); } //reset the DW1000 by driving the RSTn line low reset_DW1000(); result = instance_init(); if (0 > result) return (-2); // Some failure has occurred SPI_ConfigFastRate(SPI_BaudRatePrescaler_4); //increase SPI to max devID = instancereaddeviceid(); printf("devID %08X\r\n", devID); if (DWT_DEVICE_ID != devID) // Means it is NOT MP device { // SPI not working or Unsupported Device ID return (-3); } if (is_tag) { instance_mode = TAG; led_on(LED_PC7); } else { instance_mode = ANCHOR; #if (DR_DISCOVERY == 1) led_on(LED_PC6); #else if(instance_anchaddr & 0x1) led_on(LED_PC6); if(instance_anchaddr & 0x2) led_on(LED_PC7); #endif } instancesetrole(instance_mode); // Set this instance role if (use_fast2wr) //if fast ranging then initialise instance for fast ranging application { instance_init_f(instance_mode); //initialise Fast 2WR specific data //when using fast ranging the channel config is either mode 2 or mode 6 //default is mode 2 dr_mode = decarangingmode(); if ((dr_mode & 0x1) == 0) dr_mode = 1; } else { instance_init_s(instance_mode); dr_mode = decarangingmode(); } instConfig.channelNumber = chConfig[dr_mode].channel; instConfig.preambleCode = chConfig[dr_mode].preambleCode; instConfig.pulseRepFreq = chConfig[dr_mode].prf; instConfig.pacSize = chConfig[dr_mode].pacSize; instConfig.nsSFD = chConfig[dr_mode].nsSFD; instConfig.sfdTO = chConfig[dr_mode].sfdTO; instConfig.dataRate = chConfig[dr_mode].datarate; instConfig.preambleLen = chConfig[dr_mode].preambleLength; instance_config(&instConfig); // Set operating channel etc #if (DR_DISCOVERY == 0) addressconfigure(); // set up initial payload configuration #endif instancesettagsleepdelay(POLL_SLEEP_DELAY, BLINK_SLEEP_DELAY); //set the Tag sleep time //if TA_SW1_2 is on use fast ranging (fast 2wr) if (use_fast2wr) { //Fast 2WR specific config //configure the delays/timeouts instance_config_f(); } else //use default ranging modes { // NOTE: this is the delay between receiving the blink and sending the ranging init message // The anchor ranging init response delay has to match the delay the tag expects // the tag will then use the ranging response delay as specified in the ranging init message // use this to set the long blink response delay (e.g. when ranging with a PC anchor that wants to use the long response times != 150ms) if (use_long_blink_delay) { instancesetblinkreplydelay(FIXED_LONG_BLINK_RESPONSE_DELAY); } else //this is for ARM to ARM tag/anchor (using normal response times 150ms) { instancesetblinkreplydelay(FIXED_REPLY_DELAY); } //set the default response delays instancesetreplydelay(FIXED_REPLY_DELAY, 0); } // return devID; return 0; }
/** * Application entry point. */ int main(void) { int prijem=0; uint32 device_id; /* Start with board specific hardware init. */ peripherals_init(); spi_peripheral_init(); Sleep(1000); //wait for LCD to power on initLCD(); /* Display application name on LCD. */ setLCDline1( 234); // Sleep(1000); usb_init(); //Sleep(1000); // lcd_display_str("connected"); /* Reset and initialise DW1000. * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum * performance. */ reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */ SPI_ChangeRate(SPI_BaudRatePrescaler_32); // spi_set_rate_low(); // uint32 temp = dwt_read32bitoffsetreg(AON_ID,AON_WCFG_OFFSET); dwt_initialise(DWT_LOADUCODE); // dwt_configuresleepcnt(sleep16); //needed for LPL // dwt_configuresleep(DWT_LOADUCODE | DWT_LOADOPSET | DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_WK | DWT_SLP_EN); //needed for LPL SPI_ChangeRate(SPI_BaudRatePrescaler_4); //spi_set_rate_high(); // dwt_configure(&config); /* Loop forever receiving frames. */ /* while (1) { led_on(LED_ALL); Sleep(100); led_off(LED_ALL); Sleep(100); push_over_usb("nikola",6); setLCDline1( 123); }*/ s1switch = is_button_low(0) << 1 // is_switch_on(TA_SW1_2) << 2 | is_switch_on(TA_SW1_3) << 2 | is_switch_on(TA_SW1_4) << 3 | is_switch_on(TA_SW1_5) << 4 | is_switch_on(TA_SW1_6) << 5 | is_switch_on(TA_SW1_7) << 6 | is_switch_on(TA_SW1_8) << 7; port_EnableEXT_IRQ(); while(1){ setLCDline1(123); deca_sleep(1000); device_id= inittestapplication(s1switch); setLCDline1(instance_data[0].mode); if(instance_mode == TAG){ setLCDline1(1); } else if(instance_mode==ANCHOR){ setLCDline1(2); } else { setLCDline1(3); } deca_sleep(1000); // instance_run(); //setLCDline1(message); instance_run(); } }
uint32 inittestapplication(uint8 s1switch) { uint32 devID ; instanceConfig_t instConfig; int result; SPI_ConfigFastRate(SPI_BaudRatePrescaler_32); //max SPI before PLLs configured is ~4M //this is called here to wake up the device (i.e. if it was in sleep mode before the restart) devID = instancereaddeviceid() ; if(DWT_DEVICE_ID != devID) //if the read of device ID fails, the DW1000 could be asleep { port_SPIx_clear_chip_select(); //CS low Sleep(1); //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise port_SPIx_set_chip_select(); //CS high Sleep(7); devID = instancereaddeviceid() ; // SPI not working or Unsupported Device ID if(DWT_DEVICE_ID != devID) return(-1) ; //clear the sleep bit - so that after the hard reset below the DW does not go into sleep dwt_softreset(); } //reset the DW1000 by driving the RSTn line low reset_DW1000(); result = instance_init() ; if (0 > result) return(-1) ; // Some failure has occurred SPI_ConfigFastRate(SPI_BaudRatePrescaler_4); //increase SPI to max devID = instancereaddeviceid() ; if (DWT_DEVICE_ID != devID) // Means it is NOT MP device { // SPI not working or Unsupported Device ID return(-1) ; } if(s1switch & SWS1_ANC_MODE) { instance_mode = ANCHOR; led_on(LED_PC6); } else { instance_mode = TAG; led_on(LED_PC7); } instancesetrole(instance_mode) ; // Set this instance role instance_init_s(instance_mode); dr_mode = decarangingmode(s1switch); instConfig.channelNumber = chConfig[dr_mode].channel ; instConfig.preambleCode = chConfig[dr_mode].preambleCode ; instConfig.pulseRepFreq = chConfig[dr_mode].prf ; instConfig.pacSize = chConfig[dr_mode].pacSize ; instConfig.nsSFD = chConfig[dr_mode].nsSFD ; instConfig.sfdTO = chConfig[dr_mode].sfdTO ; instConfig.dataRate = chConfig[dr_mode].datarate ; instConfig.preambleLen = chConfig[dr_mode].preambleLength ; instance_config(&instConfig) ; // Set operating channel etc instancesettagsleepdelay(POLL_SLEEP_DELAY, BLINK_SLEEP_DELAY); //set the Tag sleep time instance_init_timings(); return devID; }
/** * Application entry point. */ int rxWait(void) { /* Reset and initialise DW1000. * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum * performance. */ int i; reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */ //spi_set_rate_low(); dwt_initialise(DWT_LOADNONE); //spi_set_rate_high(); /* Configure DW1000. See NOTE 2 below. */ dwt_configure(&config); /* Loop forever sending and receiving frames periodically. */ while (1) { /* Activate reception immediately. See NOTE 3 below. */ dwt_rxenable(0); /* Poll until a frame is properly received or an error occurs. See NOTE 4 below. * STATUS register is 5 bytes long but, as the events we are looking at are in the lower bytes of the register, we can use this simplest API * function to access it. */ while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { }; printf("Status reg now 0x%x\r\n",status_reg); if (status_reg & SYS_STATUS_RXFCG) { /* A frame has been received, read it into the local buffer. */ frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023; if (frame_len <= FRAME_LEN_MAX) { dwt_readrxdata(rx_buffer, frame_len, 0); } /* Clear good RX frame event in the DW1000 status register. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); for (i=0;i<frame_len;i++) { printf("%x ",rx_buffer[i]); } printf("\r\n"); /* Validate the frame is the one expected as sent by "TX then wait for a response" example. */ if ((frame_len == 14) && (rx_buffer[0] == 0xC5) && (rx_buffer[10] == 0x43) && (rx_buffer[11] == 0x2)) { int i; /* Copy source address of blink in response destination address. */ for (i = 0; i < 8; i++) { tx_msg[DATA_FRAME_DEST_IDX + i] = rx_buffer[BLINK_FRAME_SRC_IDX + i]; } /* Write response frame data to DW1000 and prepare transmission. See NOTE 5 below.*/ dwt_writetxdata(sizeof(tx_msg), tx_msg, 0); dwt_writetxfctrl(sizeof(tx_msg), 0); /* Send the response. */ dwt_starttx(DWT_START_TX_IMMEDIATE); /* Poll DW1000 until TX frame sent event set. */ while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) { }; /* Clear TX frame sent event. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); /* Increment the data frame sequence number (modulo 256). */ tx_msg[DATA_FRAME_SN_IDX]++; } } else { /* Clear RX error events in the DW1000 status register. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); printf("Some RX errors ...\r\n"); } } }
void listener_run(void) { uint32_t announce_coord_ts = 0; uint32_t elapsed = 0; uint32_t last_ts = 0; uint32_t count = 0; irq_init(); pio_disable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK); // Setup DW1000 dwt_txconfig_t txconfig; // Setup DECAWAVE reset_DW1000(); spi_set_rate_low(); dwt_initialise(DWT_LOADUCODE); spi_set_rate_high(); dwt_configure(&cph_config->dwt_config); dwt_setpanid(0x4350); dwt_setaddress16(0x1234); // Clear CLKPLL_LL dwt_write32bitreg(SYS_STATUS_ID, 0x02000000); uint32_t id = dwt_readdevid(); printf("Device ID: %08X\r\n", id); #if 1 dwt_setcallbacks(0, rxcallback); dwt_setinterrupt( DWT_INT_TFRS | DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO /*| DWT_INT_RXPTO*/), 1); pio_enable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK); dwt_rxenable(0); while (1) { elapsed = cph_get_millis() - last_ts; if (elapsed > 5000) { printf("alive %d\r\n", count++); last_ts = cph_get_millis(); } if (trx_signal == SIGNAL_RCV) { printf("[RCV] %d - ", frame_len); for (int i = 0; i < frame_len; i++) { printf("%02X ", rx_buffer[i]); } printf("\r\n"); trx_signal = SIGNAL_EMPTY; dwt_rxenable(0); } else if(trx_signal == SIGNAL_ERR) { printf("ERROR: %08X\r\n", error_status_reg); trx_signal = SIGNAL_EMPTY; dwt_rxenable(0); } else if(trx_signal == SIGNAL_ERR_LEN) { printf("ERROR LENGTH: %08X\r\n", error_status_reg); trx_signal = SIGNAL_EMPTY; dwt_rxenable(0); } } #else while (1) { /* Activate reception immediately. */ dwt_rxenable(0); while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { }; if (status_reg & SYS_STATUS_RXFCG) { uint32 frame_len; dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023; if (frame_len <= MAXRXSIXZE) { dwt_readrxdata(rx_buffer, frame_len, 0); } else { frame_len = 0; } if (frame_len > 0) { printf("[RCV] "); for (int i = 0; i < frame_len; i++) { printf("%02X ", rx_buffer[i]); } printf("\r\n"); } else { printf("ERROR: frame_len == %d\r\n", frame_len); } } else { printf("ERROR: dwt_rxenable has status of %08X\r\n", status_reg); /* Clear RX error events in the DW1000 status register. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR | SYS_STATUS_CLKPLL_LL); } } #endif }
/*! ------------------------------------------------------------------------------------------------------------------ * @fn main() * * @brief Application entry point. * * @param none * * @return none */ int ssTwrInit(void) { /* Reset and initialise DW1000. * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum * performance. */ int status; reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */ //spi_set_rate_low(); status = dwt_initialise(DWT_LOADUCODE); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); //spi_set_rate_high(); /* Configure DW1000. See NOTE 6 below. */ status = dwt_configure(&config); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); // read otp uint32_t otpVal[0x20]; dwt_otpread(0,otpVal,0x20); printf("OTP 6: 0x%x\r\n",otpVal[6]); printf("OTP 7: 0x%x\r\n",otpVal[7]); printf("OTP x16: 0x%x\r\n",otpVal[0x16]); printf("OTP x17: 0x%x\r\n",otpVal[0x17]); /* Apply default antenna delay value. See NOTE 2 below. */ printf("antenna delays: default TX: %d, default RX: %d, evk 16m: %d, evk 64m: %d\r\n",TX_ANT_DLY,RX_ANT_DLY,DWT_RF_DELAY_16M,DWT_RF_DELAY_64M); tx_delay = TX_ANT_DLY; rx_delay = RX_ANT_DLY; dwt_setrxantennadelay(rx_delay); dwt_settxantennadelay(tx_delay); /* Set expected response's delay and timeout. See NOTE 1 and 5 below. * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */ dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS); dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS); btn = buttons(); printf("%s entering main loop\r\n",__FUNCTION__); /* Loop forever initiating ranging exchanges. */ while (1) { /* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */ tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb; status = dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); status = dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); status = dwt_writetxfctrl(sizeof(tx_poll_msg), 0); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); /* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay * set by dwt_setrxaftertxdelay() has elapsed. */ status = dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); /* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 8 below. */ while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { } ; // printf("Waiting. status reg 0x%x\r\n",status_reg); }; //printf("Status reg now 0x%x\r\n",status_reg); if (SYS_STATUS_RXRFTO & status_reg) printf("RX timeout\r\n"); /* Increment frame sequence number after transmission of the poll message (modulo 256). */ frame_seq_nb++; if (status_reg & SYS_STATUS_RXFCG) { uint32 frame_len; //printf("Check RX\r\n"); /* Clear good RX frame event in the DW1000 status register. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); /* A frame has been received, read it into the local buffer. */ frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK; if (frame_len <= RX_BUF_LEN) { dwt_readrxdata(rx_buffer, frame_len, 0); } /* Check that the frame is the expected response from the companion "SS TWR responder" example. * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */ rx_buffer[ALL_MSG_SN_IDX] = 0; if (memcmp(rx_buffer, rx_resp_msg, ALL_MSG_COMMON_LEN) == 0) { uint32 poll_tx_ts, resp_rx_ts, poll_rx_ts, resp_tx_ts; int32 rtd_init, rtd_resp; /* Retrieve poll transmission and response reception timestamps. See NOTE 9 below. */ poll_tx_ts = dwt_readtxtimestamplo32(); resp_rx_ts = dwt_readrxtimestamplo32(); /* Get timestamps embedded in response message. */ resp_msg_get_ts(&rx_buffer[RESP_MSG_POLL_RX_TS_IDX], &poll_rx_ts); resp_msg_get_ts(&rx_buffer[RESP_MSG_RESP_TX_TS_IDX], &resp_tx_ts); /* Compute time of flight and distance. */ rtd_init = resp_rx_ts - poll_tx_ts; rtd_resp = resp_tx_ts - poll_rx_ts; tof = ((rtd_init - rtd_resp) / 2.0) * DWT_TIME_UNITS; distance = tof * SPEED_OF_LIGHT; /* Display computed distance on LCD. */ //sprintf(dist_str, "DIST: %3.2f m", distance); sprintf(dist_str, "%3.2f", distance); printf("%s\r\n",dist_str); //lcd_display_str(dist_str); } } else { /* Clear RX error events in the DW1000 status register. */ printf("Errors occured. Clearing up\r\n"); dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); } /* Execute a delay between ranging exchanges. */ //printf("Delay %dms\r\n",RNG_DELAY_MS); deca_sleep(RNG_DELAY_MS); // toggle led ledToggle(); // update antenna if (buttons() & 1) { tx_delay -= 10; // >>= 1; rx_delay -= 10; //>>= 1; dwt_setrxantennadelay(rx_delay); dwt_settxantennadelay(tx_delay); printf("Decreased antenna delay to 0x%x\r\n",tx_delay); } if (buttons() & 2) { tx_delay += 10; rx_delay += 10; dwt_setrxantennadelay(rx_delay); dwt_settxantennadelay(tx_delay); printf("Increased antenna delay to 0x%x\r\n",tx_delay); } } }
/*! ------------------------------------------------------------------------------------------------------------------ * @fn main() * * @brief Application entry point. * * @param none * * @return none */ int ssTwrResp(void) { /* Reset and initialise DW1000. * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum * performance. */ int i; int status; reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */ //spi_set_rate_low(); dwt_initialise(DWT_LOADUCODE); //spi_set_rate_high(); /* Configure DW1000. See NOTE 5 below. */ dwt_configure(&config); uint32_t otpVal[0x20]; dwt_otpread(0,otpVal,0x20); printf("OTP 6: 0x%x\r\n",otpVal[6]); printf("OTP 7: 0x%x\r\n",otpVal[7]); printf("OTP x16: 0x%x\r\n",otpVal[0x16]); printf("OTP x17: 0x%x\r\n",otpVal[0x17]); /* Apply default antenna delay value. See NOTE 2 below. */ printf("antenna delays: default TX: %d, default RX: %d, evk 16m: %d, evk 64m: %d\r\n",TX_ANT_DLY,RX_ANT_DLY,DWT_RF_DELAY_16M,DWT_RF_DELAY_64M); tx_delay = TX_ANT_DLY; rx_delay = RX_ANT_DLY; dwt_setrxantennadelay(rx_delay); dwt_settxantennadelay(tx_delay); btn = buttons(); printf("%s entering main loop\r\n",__FUNCTION__); /* Loop forever responding to ranging requests. */ while (1) { /* Activate reception immediately. */ dwt_rxenable(0); /* Poll for reception of a frame or error/timeout. See NOTE 6 below. */ while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { } ; //printf("Waiting. status reg 0x%x\r\n",status_reg); }; //printf("Status reg now 0x%x\r\n",status_reg); if (status_reg & SYS_STATUS_RXFCG) { uint32 frame_len; //printf("Check RX\r\n"); /* Clear good RX frame event in the DW1000 status register. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); /* A frame has been received, read it into the local buffer. */ frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023; //printf("Frame length %d\r\n",frame_len); if (frame_len <= RX_BUFFER_LEN) { dwt_readrxdata(rx_buffer, frame_len, 0); } /* for (i=0;i<frame_len;i++) { printf("%x ",rx_buffer[i]); } printf("\r\n"); */ /* Check that the frame is a poll sent by "SS TWR initiator" example. * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */ rx_buffer[ALL_MSG_SN_IDX] = 0; if (memcmp(rx_buffer, rx_poll_msg, ALL_MSG_COMMON_LEN) == 0) { uint32 resp_tx_time; //printf("Poll MSG\r\n"); /* Retrieve poll reception timestamp. */ poll_rx_ts = get_rx_timestamp_u64(); //printf("RX timestamp: %lld\r\n",poll_rx_ts); /* Compute final message transmission time. See NOTE 7 below. */ resp_tx_time = (poll_rx_ts + (POLL_RX_TO_RESP_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8; dwt_setdelayedtrxtime(resp_tx_time); //printf("TX time: %d\r\n",resp_tx_time); /* Response TX timestamp is the transmission time we programmed plus the antenna delay. */ resp_tx_ts = (((uint64)(resp_tx_time & 0xFFFFFFFE)) << 8) + TX_ANT_DLY; //printf("TX timestamp: %lld\r\n",resp_tx_ts); /* Write all timestamps in the final message. See NOTE 8 below. */ resp_msg_set_ts(&tx_resp_msg[RESP_MSG_POLL_RX_TS_IDX], poll_rx_ts); resp_msg_set_ts(&tx_resp_msg[RESP_MSG_RESP_TX_TS_IDX], resp_tx_ts); /* Write and send the response message. See NOTE 9 below. */ tx_resp_msg[ALL_MSG_SN_IDX] = frame_seq_nb; status = dwt_writetxdata(sizeof(tx_resp_msg), tx_resp_msg, 0); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); status = dwt_writetxfctrl(sizeof(tx_resp_msg), 0); if (DWT_SUCCESS != status) printf("API error line %d\r\n",__LINE__); status = dwt_starttx(DWT_START_TX_DELAYED); if (DWT_SUCCESS != status) { printf("API error line %d\r\n",__LINE__); printf("RX timestamp: %llu\r\n",poll_rx_ts); printf("TX time: %llu\r\n",((uint64)resp_tx_time) << 8); printf("TX timestamp: %llu\r\n",resp_tx_ts); } // poll only if starttx was OK if (DWT_SUCCESS == status) { /* Poll DW1000 until TX frame sent event set. See NOTE 6 below. */ u32 tx_stat; while (!(tx_stat = dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) { }; //printf("Waiting. status reg 0x%x\r\n",tx_stat); } //printf("After Poll: status reg now 0x%x\r\n",tx_stat); } /* Clear TXFRS event. */ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); /* Increment frame sequence number after transmission of the poll message (modulo 256). */ frame_seq_nb++; } }