void DWM1000_Anchor::init() { INFO("HSPI"); //_________________________________________________INIT SPI ESP8266 resetChip(); initSpi(); enableIsr(); uint64_t eui = 0xF1F2F3F4F5F6F7F; dwt_seteui((uint8_t*) &eui); dwt_geteui((uint8_t*) &eui); LOG<< HEX << "EUID : "<< eui <<FLUSH; dwt_seteui((uint8_t*) &eui); dwt_geteui((uint8_t*) &eui); LOG<< HEX << "EUID : "<< eui <<FLUSH; // dwt_softreset(); deca_sleep(100); if (dwt_initialise(DWT_LOADUCODE)) { LOG<< " dwt_initialise failed " << FLUSH; } else LOG<< " dwt_initialise done." << FLUSH; if (dwt_configure(&config)) { LOG<< " dwt_configure failed " << FLUSH; } else LOG<< " dwt_configure done." << FLUSH; uint32_t device_id = dwt_readdevid(); uint32_t part_id = dwt_getpartid(); uint32_t lot_id = dwt_getlotid(); LOG<< HEX << " device id : " << device_id << ", part id : " << part_id << ", lot_id :" << lot_id <<FLUSH; /* Apply default antenna delay value. See NOTE 1 below. */ dwt_setrxantennadelay(RX_ANT_DLY); dwt_settxantennadelay(TX_ANT_DLY); /* Set expected response's delay and timeout. See NOTE 4 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); dwt_initialise(DWT_LOADUCODE); // Configure the callbacks from the dwt library dwt_setcallbacks(txcallback, rxcallback); _count = 0; }
portTASK_FUNCTION(task_decawave, pvParameters) { UNUSED(pvParameters); peripherals_init(); spi = spi_peripheral_init(); if (spi != NULL) { printf("FreeRTOS SPI master init success.\r\n"); } else { printf("FreeRTOS SPI master init ERROR.\r\n"); for (;;) { } } Sleep(1000); // initialize DW if (dwt_initialise(DWT_LOADNONE) == DWT_SUCCESS) { printf("dwt_initialize success.\r\n"); } else { printf("dwt_initialize ERROR.\r\n"); for (;;) { } } // get eui64 dwt_seteui(eui64); Sleep(1); dwt_geteui(eui64); printf("eui: "); printf("%02X ", eui64[0]); printf("%02X ", eui64[1]); printf("%02X ", eui64[2]); printf("%02X ", eui64[3]); printf("%02X ", eui64[4]); printf("%02X ", eui64[5]); printf("%02X ", eui64[6]); printf("%02X ", eui64[7]); printf("\r\n"); // enter polling loop for (;;) { } }
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; }
// ------------------------------------------------------------------------------------------------------------------- // function to initialise instance structures // // Returns 0 on success and -1 on error uint8_t interrupt_init_s(uint8_t mode) { int instance = 0 ; int result; //uint16 temp = 0; instance_data[instance].mode = ANCHOR; // assume listener, instance_data[instance].instToSleep = 0; instance_data[instance].sentSN = 0; instance_data[instance].tofindex = 0; instance_data[instance].tofcount = 0; // Reset the IC (might be needed if not getting here from POWER ON) // ARM code: Remove soft reset here as using hard reset in the inittestapplication() in the main.c file //dwt_softreset(); #if (DEEP_SLEEP_AUTOWAKEUP == 1) uint16 blinktime = 0xf; //e.g. blink time to be used for the Tag auto wake up { double t; uint16 lp_osc_cal = dwt_calibratesleepcnt(); //calibrate low power oscillator //the lp_osc_cal value is number of XTAL/2 cycles in one cycle of LP OSC //to convert into seconds (38.4MHz/2 = 19.2MHz (XTAL/2) => 1/19.2MHz ns) //so to get a sleep time of 5s we need to program 5 / period and then >> 12 as the register holds upper 16-bits of 28-bit counter t = ((double)5/((double)lp_osc_cal/19.2e6)); blinktime = (int) t; blinktime >>= 12; dwt_configuresleepcnt(blinktime);//configure sleep time } #endif //we can enable any configuration loding from OTP/ROM on initialisation result = dwt_initialise(DWT_LOADUCODE | DWT_LOADLDO | DWT_LOADTXCONFIG | DWT_LOADANTDLY| DWT_LOADXTALTRIM) ; //temp = dwt_readtempvbat(); // temp formula is: 1.13 * reading - 113.0 // Temperature (°C )= (SAR_LTEMP - (OTP_READ(Vtemp @ 23 °C )) x 1.14) + 23 // volt formula is: 0.0057 * reading + 2.3 // Voltage (volts) = (SAR_LVBAT- (OTP_READ(Vmeas @ 3.3 V )) /173) + 3.3 //printf("Vbat = %d (0x%02x) %1.2fV\tVtemp = %d (0x%02x) %2.2fC\n",temp&0xFF,temp&0xFF, ((0.0057 * (temp&0xFF)) + 2.3), (temp>>8)&0xFF,(temp>>8)&0xFF, ((1.13 * ((temp>>8)&0xFF)) - 113.0)); //this is platform dependant - only program if DW EVK/EVB dwt_setleds(3) ; //configure the GPIOs which control the leds on EVBs if (DWT_SUCCESS != result) { return (-1) ; // device initialise has failed } instanceclearcounts() ; instance_data[instance].panid = 0xdeca ; instance_data[instance].newReportSent = 0; //clear the flag instance_data[instance].wait4ack = 0; instance_data[instance].ackexpected = 0; instance_data[instance].stoptimer = 0; instance_data[instance].instancetimer_en = 0; instance_clearevents(); instance_data[instance].rxautoreenable = 0; dwt_geteui(instance_data[instance].eui64); instance_localdata[instance].testapprun_fn = NULL; instance_data[instance].canprintinfo = 0; return 0 ; }
int app_dw1000_init ( int HACK_role, int HACK_EUI, void (*txcallback)(const dwt_callback_data_t *), void (*rxcallback)(const dwt_callback_data_t *) ) { uint32_t devID; int err; // Start off DW1000 comms slow REG(SSI0_BASE + SSI_CR1) = 0; REG(SSI0_BASE + SSI_CPSR) = 8; REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE; // Reset the DW1000...for some reason dw1000_reset(); // Make sure we can talk to the DW1000 devID = dwt_readdevid(); if (devID != DWT_DEVICE_ID) { #ifdef DW_DEBUG printf("Could not read Device ID from the DW1000\r\n"); printf("Possible the chip is asleep...\r\n"); #endif return -1; } // Select which of the three antennas on the board to use dw1000_choose_antenna(0); // Init the dw1000 hardware err = dwt_initialise(DWT_LOADUCODE | DWT_LOADLDO | DWT_LOADTXCONFIG | DWT_LOADXTALTRIM); if (err != DWT_SUCCESS) { return -1; } // Setup interrupts // Note: using auto rx re-enable so don't need to trigger on error frames dwt_setinterrupt(DWT_INT_TFRS | DWT_INT_RFCG | DWT_INT_SFDT | DWT_INT_RFTO | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFSL | DWT_INT_RXPTO | DWT_INT_SFDT, 1); // Configure the callbacks from the dwt library dwt_setcallbacks(txcallback, rxcallback); // Set the parameters of ranging and channel and whatnot global_ranging_config.chan = 2; global_ranging_config.prf = DWT_PRF_64M; global_ranging_config.txPreambLength = DWT_PLEN_64;//DWT_PLEN_4096 // global_ranging_config.txPreambLength = DWT_PLEN_256; global_ranging_config.rxPAC = DWT_PAC8; global_ranging_config.txCode = 9; // preamble code global_ranging_config.rxCode = 9; // preamble code global_ranging_config.nsSFD = 0; global_ranging_config.dataRate = DWT_BR_6M8; global_ranging_config.phrMode = DWT_PHRMODE_EXT; //Enable extended PHR mode (up to 1024-byte packets) global_ranging_config.smartPowerEn = 1; global_ranging_config.sfdTO = 64+8+1;//(1025 + 64 - 32); dwt_configure(&global_ranging_config, 0);//(DWT_LOADANTDLY | DWT_LOADXTALTRIM)); dwt_setsmarttxpower(global_ranging_config.smartPowerEn); // Configure TX power { global_tx_config.PGdly = pgDelay[global_ranging_config.chan]; global_tx_config.power = txPower[global_ranging_config.chan]; dwt_configuretxrf(&global_tx_config); } /* All constants same anyway if(DW1000_ROLE_TYPE == TAG) dwt_xtaltrim(xtaltrim[0]); else dwt_xtaltrim(xtaltrim[ANCHOR_EUI]); */ dwt_xtaltrim(xtaltrim[0]); ////TEST 1: XTAL trim calibration //dwt_configcwmode(global_ranging_config.chan); //dwt_xtaltrim(8); //while(1); //{ // //TEST 2: TX Power level calibration // uint8_t msg[127] = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the l"; // dwt_configcontinuousframemode(0x1000); // dwt_writetxdata(127, (uint8 *) msg, 0) ; // dwt_writetxfctrl(127, 0); // dwt_starttx(DWT_START_TX_IMMEDIATE); // while(1); //} // Configure the antenna delay settings { uint16_t antenna_delay; //Antenna delay not really necessary if we're doing an end-to-end calibration antenna_delay = 0; dwt_setrxantennadelay(antenna_delay); dwt_settxantennadelay(antenna_delay); //global_tx_antenna_delay = antenna_delay; //// Shift this over a bit for some reason. Who knows. //// instance_common.c:508 //antenna_delay = dwt_readantennadelay(global_ranging_config.prf) >> 1; //if (antenna_delay == 0) { // printf("resetting antenna delay\r\n"); // // If it's not in the OTP, use a magic value from instance_calib.c // antenna_delay = ((DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS); // dwt_setrxantennadelay(antenna_delay); // dwt_settxantennadelay(antenna_delay); //} //global_tx_antenna_delay = antenna_delay; //printf("tx antenna delay: %u\r\n", antenna_delay); } // // Set the sleep delay. Not sure what this does actually. // instancesettagsleepdelay(POLL_SLEEP_DELAY, BLINK_SLEEP_DELAY); // Configure as either a tag or anchor if (HACK_role == ANCHOR) { uint8_t eui_array[8]; // Enable frame filtering dwt_enableframefilter(DWT_FF_DATA_EN | DWT_FF_ACK_EN); dw1000_populate_eui(eui_array, HACK_EUI); dwt_seteui(eui_array); dwt_setpanid(DW1000_PANID); // We do want to enable auto RX dwt_setautorxreenable(1); // Let's do double buffering dwt_setdblrxbuffmode(0); // Disable RX timeout by setting to 0 dwt_setrxtimeout(0); // Go for receiving dwt_rxenable(0); } else if (HACK_role == TAG) { uint8_t eui_array[8]; // Allow data and ack frames dwt_enableframefilter(DWT_FF_DATA_EN | DWT_FF_ACK_EN); dw1000_populate_eui(eui_array, HACK_EUI); dwt_seteui(eui_array); dwt_setpanid(DW1000_PANID); // Do this for the tag too dwt_setautorxreenable(1); dwt_setdblrxbuffmode(1); dwt_enableautoack(5 /*ACK_RESPONSE_TIME*/); // Configure sleep { int mode = DWT_LOADUCODE | DWT_PRESRV_SLEEP | DWT_CONFIG | DWT_TANDV; if (dwt_getldotune() != 0) { // If we need to use LDO tune value from OTP kick it after sleep mode |= DWT_LOADLDO; } // NOTE: on the EVK1000 the DEEPSLEEP is not actually putting the // DW1000 into full DEEPSLEEP mode as XTAL is kept on dwt_configuresleep(mode, DWT_WAKE_CS | DWT_SLP_EN); } } // Make it fast REG(SSI0_BASE + SSI_CR1) = 0; REG(SSI0_BASE + SSI_CPSR) = 2; REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE; return 0; }
// First (generic) init of the DW1000 dw1000_err_e dw1000_init () { // Do the STM setup that initializes pin and peripherals and whatnot. if (!_stm_dw1000_interface_setup) { setup(); } // Reset the dw1000...for some reason dw1000_reset(); uDelay(100); // Make sure we can talk to the DW1000 uint32_t devID; devID = dwt_readdevid(); if (devID != DWT_DEVICE_ID) { //if we can't talk to dw1000, return with an error uDelay(1000); return DW1000_COMM_ERR; } GPIO_WriteBit(STM_GPIO3_PORT, STM_GPIO3_PIN, Bit_SET); uDelay(1000); GPIO_WriteBit(STM_GPIO3_PORT, STM_GPIO3_PIN, Bit_RESET); // Choose antenna 0 as a default dw1000_choose_antenna(0); // Initialize the dw1000 hardware uint32_t err; err = dwt_initialise(DWT_LOADUCODE | DWT_LOADLDO | DWT_LOADTXCONFIG | DWT_LOADXTALTRIM); if (err != DWT_SUCCESS) { return DW1000_COMM_ERR; } // Configure interrupts and callbacks dwt_setinterrupt(0xFFFFFFFF, 0); dwt_setinterrupt(DWT_INT_TFRS | DWT_INT_RFCG | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFSL | DWT_INT_RFTO | DWT_INT_RXPTO | DWT_INT_SFDT | DWT_INT_ARFE, 1); dwt_setcallbacks(txcallback, rxcallback); // Set the parameters of ranging and channel and whatnot global_ranging_config.chan = 2; global_ranging_config.prf = DWT_PRF_64M; global_ranging_config.txPreambLength = DWT_PLEN_64; global_ranging_config.rxPAC = DWT_PAC8; global_ranging_config.txCode = 9; // preamble code global_ranging_config.rxCode = 9; // preamble code global_ranging_config.nsSFD = 0; global_ranging_config.dataRate = DWT_BR_6M8; global_ranging_config.phrMode = DWT_PHRMODE_EXT; //Enable extended PHR mode (up to 1024-byte packets) global_ranging_config.smartPowerEn = 1; global_ranging_config.sfdTO = 64+8+1;//(1025 + 64 - 32); #if DW1000_USE_OTP dwt_configure(&global_ranging_config, (DWT_LOADANTDLY | DWT_LOADXTALTRIM)); #else dwt_configure(&global_ranging_config, 0); #endif dwt_setsmarttxpower(global_ranging_config.smartPowerEn); // Configure TX power based on the channel used global_tx_config.PGdly = pgDelay[global_ranging_config.chan]; global_tx_config.power = txPower[global_ranging_config.chan]; dwt_configuretxrf(&global_tx_config); // Need to set some radio properties. Ideally these would come from the // OTP memory on the DW1000 #if DW1000_USE_OTP == 0 // This defaults to 8. Don't know why. dwt_xtaltrim(8); // Antenna delay we don't really care about so we just use 0 dwt_setrxantennadelay(DW1000_ANTENNA_DELAY_RX); dwt_settxantennadelay(DW1000_ANTENNA_DELAY_TX); #endif return DW1000_NO_ERR; }
/** * 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(); } }
/** * 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); } } }
/* ********************************************************************************************************* * * loca_status dwt_config(eCHAN channel, ePRF prf, ePLEN PreambleLength, eCODE code, uint8_t nsSFD, DataRate db) * * 描 述 : 配置DW1000 * * 输入参数 : * channel:DW1000通道 prf: PreambleLength: code: nsSFD: db: * * 输出参数 : DW1000状态 ********************************************************************************************************* */ static loca_status dwt_config(eCHAN channel, ePRF prf, ePLEN PreambleLength, eCODE code, uint8_t nsSFD, DataRate db) { dwt_config_t config; dwt_txconfig_t txconfig; uint8_t buf[2]; uint64_t eui64; uint16_t panid; eui64 = LOC_CFG_EUI; panid = LOC_CFG_PANID; config.chan = channel; config.prf = prf; config.txPreambLength = PreambleLength; switch (PreambleLength) { case PLEN_64: case PLEN_128: config.rxPAC = DWT_PAC8; break; case PLEN_256: case PLEN_512: config.rxPAC = DWT_PAC16; break; case PLEN_1024: config.rxPAC = DWT_PAC32; break; default: config.rxPAC = DWT_PAC64; break; } config.rxCode = code; config.txCode = code; config.nsSFD = nsSFD; config.dataRate = db; config.smartPowerEn = 0; config.phrMode = 0; txconfig.PGdly = dwt_pgdelay[channel]; txconfig.power = dwt_manualpwr[prf - 1][channel]; BSP_ChangeSPIRate(SPI_SLOW); dwt_softreset(); if (dwt_initialise(DWT_LOADNONE) != DWT_SUCCESS) { BSP_ChangeSPIRate(SPI_FAST); return LOCA_FAULT; } BSP_ChangeSPIRate(SPI_FAST); if (dwt_readdevid() != DWT_DEVICE_ID) { return LOCA_FAULT; } buf[0] = 0x10; dwt_writetodevice(GPIO_CTRL_ID,0x02,1,&buf[0]); buf[0] = 0xf0; buf[1] = 0xf0; dwt_writetodevice(GPIO_CTRL_ID,0x08,2,&buf[0]); dwt_configure(&config, 0); dwt_xtaltrim(LOC_CFG_XTALRIM); dwt_setrxantennadelay(0); dwt_settxantennadelay(0); dwt_setsmarttxpower(0); dwt_configuretxrf(&txconfig); dwt_enableframefilter(DWT_FF_NOTYPE_EN); dwt_seteui((uint8_t *)&eui64); dwt_setpanid(panid); //开启硬件中断 //BSP_DWTIntEnable(); // dwt_setinterrupt( DWT_INT_TFRS, 1); dwt_setrxmode(DWT_RX_NORMAL,0,0); return LOCA_OK; }
/*! ------------------------------------------------------------------------------------------------------------------ * @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++; } }