/*! ---------------------------------------------------------------------------- * @file main.c * @brief Double-sided two-way ranging (DS TWR) initiator example code * * * * @attention * * Copyright 2015 (c) Decawave Ltd, Dublin, Ireland. * * All rights reserved. * * @author Decawave */ #include #include #include "dw_app.h" #include "deca_device_api.h" #include "deca_regs.h" #include "dw_driver.h" #include "Spi.h" #include "led.h" #include "serial_at_cmd_app.h" #include "Usart.h" #include "global_param.h" #include "filters.h" #include #include "beep.h" #include "modbus.h" #include "CRC.h" //#define USART_INTEGRATE_OUTPUT /*------------------------------------ Marcos ------------------------------------------*/ /* Inter-ranging delay period, in milliseconds. */ #define RNG_DELAY_MS 100 /* Default antenna delay values for 64 MHz PRF. See NOTE 1 below. */ #define TX_ANT_DLY 0 #define RX_ANT_DLY 32899 /* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) conversion factor. * 1 uus = 512 / 499.2 µs and 1 µs = 499.2 * 128 dtu. */ #define UUS_TO_DWT_TIME 65536 ///* Delay between frames, in UWB microseconds. See NOTE 4 below. */ ///* This is the delay from the end of the frame transmission to the enable of the receiver, as programmed for the DW1000's wait for response feature. */ //#define POLL_TX_TO_RESP_RX_DLY_UUS 150 ///* This is the delay from Frame RX timestamp to TX reply timestamp used for calculating/setting the DW1000's delayed TX function. This includes the // * frame length of approximately 2.66 ms with above configuration. */ //#define RESP_RX_TO_FINAL_TX_DLY_UUS 410 ///* Receive response timeout. See NOTE 5 below. */ //#define RESP_RX_TIMEOUT_UUS 600 //#define DELAY_BETWEEN_TWO_FRAME_UUS 400 ////#define POLL_RX_TO_RESP_TX_DLY_UUS 470 ///* This is the delay from the end of the frame transmission to the enable of the receiver, as programmed for the DW1000's wait for response feature. */ //#define RESP_TX_TO_FINAL_RX_DLY_UUS 200 ///* Receive final timeout. See NOTE 5 below. */ //#define FINAL_RX_TIMEOUT_UUS 4300 #define SPEED_OF_LIGHT 299702547 /* Indexes to access some of the fields in the frames defined above. */ #define FINAL_MSG_POLL_TX_TS_IDX 10 #define FINAL_MSG_RESP_RX_TS_IDX 14 #define FINAL_MSG_FINAL_TX_TS_IDX 18 #define FINAL_MSG_TS_LEN 4 //#define _UWB_4G static dwt_config_t config = { #ifdef _UWB_4G 2, /* Channel number. */ #else 5, #endif DWT_PRF_64M, /* Pulse repetition frequency. */ DWT_PLEN_64, /* Preamble length. */ DWT_PAC8, /* Preamble acquisition chunk size. Used in RX only. */ 9, /* TX preamble code. Used in TX only. */ 9, /* RX preamble code. Used in RX only. */ 1, /* Use non-standard SFD (Boolean) */ DWT_BR_6M8, /* Data rate. */ DWT_PHRMODE_STD, /* PHY header mode. */ (65 + 8 - 8) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */ }; uint32_t uwbid=0; void Dw1000_Init(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();//ÖØÆôDW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */ Spi_ChangePrescaler(SPIx_PRESCALER_SLOW); //ÉèÖÃΪ¿ìËÙģʽ dwt_initialise(DWT_LOADUCODE);//³õʼ»¯DW1000 Spi_ChangePrescaler(SPIx_PRESCALER_FAST); //ÉèÖÃΪ¿ìËÙģʽ /* Configure DW1000. See NOTE 6 below. */ dwt_configure(&config);//ÅäÖÃDW1000 // dwt_setinterrupt( 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); /* Apply default antenna delay value. See NOTE 1 below. */ dwt_setrxantennadelay(RX_ANT_DLY); //ÉèÖýÓÊÕÌìÏßÑÓ³Ù dwt_settxantennadelay(TX_ANT_DLY); //ÉèÖ÷¢ÉäÌìÏßÑÓ³Ù // dwt_setrxtimeout(1000);//É趨½ÓÊÕ³¬Ê±Ê±¼ä£¬0λûÓг¬Ê±Ê±¼ä // dwt_rxenable(0);//´ò¿ª½ÓÊÕ // uwbid=dwt_readdevid(); /* 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. */ //ÉèÖýÓÊÕ³¬Ê±Ê±¼ä } uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) { uint16_t sum = 0; uint32_t i; for(i=0; iBRR = SPIx_CS; delay_us(600); SPIx_CS_GPIO->BSRR = SPIx_CS; id = dwt_readdevid() ; while (0xDECA0130!=id) { id = dwt_readdevid() ; if(iderror_count++>100) { iderror_count=0; printf("UWBоƬID´íÎó\r\n"); break; } } } uint8_t uwb_errorflag; uint8_t uwbrxflag; void UWB_RXON() { if(!uwb_errorflag) { uwbrxflag=1; // Dw1000_Init(); UWB_Wkup(); // dwt_forcetrxoff(); // dwt_setinterrupt( DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0); // delay_ms(1); dwt_setinterrupt( 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); dwt_setrxtimeout(0);//É趨½ÓÊÕ³¬Ê±Ê±¼ä£¬0λûÓг¬Ê±Ê±¼ä dwt_rxenable(0); // HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); } } void UWB_RXOFF() { if(!uwb_errorflag) { uwbrxflag=0; dwt_forcetrxoff(); dwt_setinterrupt( DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0); delay_ms(1); dwt_entersleep(); // HAL_NVIC_DisableIRQ(EXTI4_15_IRQn); } }