/* Default communication configuration. We use here EVK1000's default mode (mode 3). */
static dwt_config_t config = {
2, /* Channel number. */
DWT_PRF_64M, /* Pulse repetition frequency. */
DWT_PLEN_1024, /* Preamble length. */
DWT_PAC32, /* 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_110K, /* Data rate. */
DWT_PHRMODE_STD, /* PHY header mode. */
(1025 + 64 - 32) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
};
/* Default antenna delay values for 64 MHz PRF. See NOTE 1 below. */
#define TX_ANT_DLY 16436
#define RX_ANT_DLY 16436
/* As "TX then wait for a response" example sends a blink message encoded as per the ISO/IEC 24730-62:2013 standard which includes a bit signalling
* that a response is listened for, this example will respond with a valid frame (that will be ignored anyway) following the same standard. The
* response is a 21-byte frame composed of the following fields:
* - byte 0/1: frame control (0x8C41 to indicate a data frame using 16-bit source addressing and 64-bit destination addressing).
* - byte 2: sequence number, incremented for each new frame.
* - byte 3/4: application ID (0x609A for data frames in this standard).
* - byte 5 -> 12: 64-bit destination address.
* - byte 13/14: 16-bit source address, hard coded in this example to keep it simple.
* - byte 15: function code (0x10 to indicate this is an activity control message).
* - byte 16: activity code (0x00 to indicate activity is finished).
* - byte 17/18: new tag blink rate.
* - byte 19/20: frame check-sum, automatically set by DW1000.
*/
/// *作为“TX然后等待响应”示例发送按照ISO / IEC 24730-62:2013标准编码的闪烁消息,其包括位信令
//?*响应被监听,这个例子将按照相同的标准响应一个有效的帧(无论如何将被忽略)。该
//?*响应是由以下字段组成的21个字节的帧:
//?* - 字节0/1:帧控制(0x8C41表示使用16位源寻址和64位目标寻址的数据帧)。
//?* - 字节2: ID号,
//?* - 字节3/4:应用程序ID(本标准中的数据帧为0x609A)。
//?* - 字节5 - > 12:64位目标地址。
//?* - 字节13/14:16位源地址,在本例中用硬编码保持简单。
//?* - 字节15:功能码(0x10表示这是一个活动控制信息)。
//?* - 字节16:活动代码(0x00表示活动已完成)。
//?* - 字节17/18:新的标签闪烁频率。
//?* - 字节19/20:帧校验和,由DW1000自动设置。 * /
/* Length of the common part of the message (up to and including the function code, see NOTE 2 below).
消息的公共部分的长度(直到并包括功能代码,参见下面的注2)*/
#define ALL_MSG_COMMON_LEN 10
/* Index to access some of the fields in the frames involved in the process.
索引来访问过程中涉及的帧中的一些字段 */
#define ALL_MSG_SN_IDX 2
#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
/* Frame sequence number, incremented after each transmission..帧序列号,在每次传输后递增 */
//static uint8 frame_seq_nb = 0;
/* Buffer to store received messages.
* Its size is adjusted to longest frame that this example code is supposed to handle.
缓冲区来存储收到的消息。 它的大小被调整到这个示例代码应该处理的最长帧 */
#define RX_BUF_LEN 20
static uint8 rx_buffer[RX_BUF_LEN];
/* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint.
在此处保存状态寄存器状态的副本,以便在调试断点处进行检查*/
static uint32 status_reg = 0;
/* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) conversion factor.
* 1 uus = 512 / 499.2 祍 and 1 祍 = 499.2 * 128 dtu.
UWB微秒(uus)到器件时间单位(dtu,大约15.65 ps)转换因子 */
#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 3100
/* Time-stamps of frames transmission/reception, expressed in device time units.
* As they are 40-bit wide, we need to define a 64-bit int type to handle them. */
static uint64_t poll_tx_ts;
static uint64_t resp_rx_ts;
static uint64_t final_tx_ts;
/* 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);
}
void bsp_distanceCode(void)
{
/* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
// tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0);
dwt_writetxfctrl(sizeof(tx_poll_msg), 0);
/* 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. */
dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED); //立即发送,并在一段时间后打开接收
/* 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)))
{ }
/* Increment frame sequence number after transmission of the poll message (modulo 256). */
// frame_seq_nb++;
if (status_reg & SYS_STATUS_RXFCG)
{
uint32 frame_len;
/* Clear good RX frame event and TX frame sent in the DW1000 status register. */
dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);
/* 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 "DS 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;
// Tag_Number_ID_bak = rx_buffer[ALL_MSG_SN_IDX];
if(rx_buffer[ALL_MSG_SN_IDX] == TAG_USER_ID) // id 号等于本机id继续发送响应信息,否则退出重新从头开始发送poll 包
{
if (memcmp(rx_buffer, rx_resp_msg, ALL_MSG_COMMON_LEN) == 0)
{
uint32 final_tx_time;
/* Final TX timestamp is the transmission time we programmed plus the TX antenna delay. */
final_tx_ts = (((uint64_t)(final_tx_time & 0xFFFFFFFE)) << 8) + TX_ANT_DLY;
/* Write all timestamps in the final message. See NOTE 10 below. 在最后的留言写所有的时间戳。见下文注10 */
final_msg_set_ts(&tx_final_msg[FINAL_MSG_POLL_TX_TS_IDX], poll_tx_ts);
final_msg_set_ts(&tx_final_msg[FINAL_MSG_RESP_RX_TS_IDX], resp_rx_ts);
final_msg_set_ts(&tx_final_msg[FINAL_MSG_FINAL_TX_TS_IDX], final_tx_ts);
/* Write and send final message. See NOTE 7 below. */
tx_final_msg[ALL_MSG_SN_IDX] = TAG_USER_ID; //frame_seq_nb;
dwt_writetxdata(sizeof(tx_final_msg), tx_final_msg, 0);
dwt_writetxfctrl(sizeof(tx_final_msg), 0);
dwt_starttx(DWT_START_TX_DELAYED);
/* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */
while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))
{ };
/* Default communication configuration. We use here EVK1000's default mode (mode 3). */
static dwt_config_t config = {
2, /* Channel number. */
DWT_PRF_64M, /* Pulse repetition frequency. */
DWT_PLEN_1024, /* Preamble length. */
DWT_PAC32, /* 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_110K, /* Data rate. */
DWT_PHRMODE_STD, /* PHY header mode. */
(1025 + 64 - 32) /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
};
/* Default antenna delay values for 64 MHz PRF. See NOTE 1 below. */
#define TX_ANT_DLY 16436
#define RX_ANT_DLY 16436
/* As "TX then wait for a response" example sends a blink message encoded as per the ISO/IEC 24730-62:2013 standard which includes a bit signalling
* that a response is listened for, this example will respond with a valid frame (that will be ignored anyway) following the same standard. The
* response is a 21-byte frame composed of the following fields:
* - byte 0/1: frame control (0x8C41 to indicate a data