/*******************************************************************************
  * @filename          : dw1000.h
  * @brief             : dw1000芯片测距
  * @author            : zhanli
  * @date              : 2024/08/22
  *****************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __DW1000_H__
#define __DW1000_H__

#include "stm32f1xx_hal.h"

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#define APP_NAME                        "KWS1000 UWB TWR v1.0"
/* Inter-ranging delay period, in milliseconds. */
#define RNG_DELAY_MS                    1000

/* 天线延迟参数: DW1000由于天线的延迟会导致测量距离存在一定的线性误差，可以调整这个
   参数调节测距误差，直到和实际距离值符合*/
#define TX_ANT_DLY                      16550         // 天线发送延迟参数
#define RX_ANT_DLY                      16550         // 天线接收延迟参数

/* Length of the common part of the message (up to and including the function code */
#define ALL_MSG_COMMON_LEN              10
/* Indexes to access some of the fields in the frames defined above. */
#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 Hand_MSG_1_IDX                  10            // 手持标签配置参数
#define Hand_MSG_2_IDX                  12            // 手持标签配置参数
#define Hand_MSG_3_IDX                  14            // 手持标签配置参数
#define FINAL_MSG_TS_LEN                4
#define Hand_MSG_LEN                    2             // 手持标签消息长度
#define LOCAL_MSG_IDX                   10
/* Buffer to store received response message.Its size is adjusted to longest 
 * frame that this example code is supposed to handle. */
#define RX_BUF_LEN                      24            // 接收缓存Buff的长度
#define TAG_IDX                         8             // 标签索引在Buffer中的位号
/* 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
/* 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_RX_TIMEOUT_UUS             3000
#define BLINK_TX_TO_POLL_RX_DLY         500
/* 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.46 ms with above configuration. */
#define BLINK_TX_TO_RESP_TX_DLY_UUS     4000 * TAG_NUM
/* 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     500
/* Receive final timeout. */
#define FINAL_RX_TIMEOUT_UUS            3300
/* Preamble timeout, in multiple of PAC size. */
#define PRE_TIMEOUT                     8
#define SPEED_OF_LIGHT                  299702547

#define BLINK_RX_TO_POLL_TX_DLY_UUS     2750


void InitDW1000(void);

uint64_t get_tx_timestamp_u64(void);
uint64_t get_rx_timestamp_u64(void);
void final_msg_get_ts(const uint8_t *ts_field, uint32_t *ts);
void final_msg_set_ts(uint8_t *ts_field, uint64_t ts);
void hand_msg_set(uint8_t *dis_field, uint16_t dis);
void hand_msg_get(const uint8_t *dis_field, uint16_t *dis);


#ifdef __cplusplus
}
#endif
#endif
