/*
 * libdeca - UWB Library for Qorvo/Decawave DW3000
 *
 * Copyright (C) 2016 - 2024 Bruno Randolf (br@einfach.org)
 *
 * This source code is licensed under the GNU Lesser General Public License,
 * Version 3. See the file LICENSE.txt for more details.
 */

#ifndef DECA_PHY_H
#define DECA_PHY_H

#include <stdbool.h>
#include <stdint.h>

#include "dwutil.h"

 /* Default antenna delay values for 64 MHz PRF, usually calibrated
  * (PRF16: 16418) */
  // #define DWPHY_ANTENNA_DELAY 16436
  // #define DWPHY_ANTENNA_DELAY 16496
  // #define DWPHY_ANTENNA_DELAY 16384
  // #define DWPHY_ANTENNA_DELAY 16150
  // #define DWPHY_ANTENNA_DELAY 16405
  // #define DWPHY_ANTENNA_DELAY 16368
#define DWPHY_ANTENNA_DELAY 16435  // 与 permanent_data.tx_antenna_delay 保持一致

/* the phy_get_[preable/sfd/phyhdr/data/packet]_time functions return in units
 * of (picoseconds / 10) = (nanoseconds * 100), so this macro can be used to
 * convert to microseconds */
#define PKTTIME_TO_USEC(ft) CEIL_DIV(ft, 100000)

bool dwphy_config(void);
void dwphy_set_antenna_delay(uint16_t antdelay);

/* phy time calculations */
const char* dwphy_rate_str(uint8_t br);
int dwphy_rate_int(uint8_t br);
int dwphy_plen_int(uint16_t pl);
int dwphy_prf_int(uint8_t p);
int dwphy_pac_int(uint8_t p);
uint32_t dwphy_calc_preamble_time(uint8_t plen_dwt, uint8_t prf_dwt,
	uint8_t rate_dwt);
uint32_t dwphy_calc_sfd_time(uint8_t prf_dwt, uint8_t rate_dwt);
uint32_t dwphy_calc_phyhdr_time(uint8_t rate_dwt);
uint64_t dwphy_calc_data_time(uint8_t rate_dwt, int len);
uint64_t dwphy_calc_packet_time(uint8_t rate_dwt, uint8_t plen_dwt,
	uint8_t prf_dwt, int data_len);
int dwphy_usec_to_pac(uint32_t x);
int dwphy_pac_to_usec(uint16_t pacs);
int dwphy_get_recommended_preambletimeout(void);
void dwphy_print_packet_times(void);

/* clock */
float dwphy_get_rx_clock_offset_ci(int32_t ci);
void dwphy_xtal_trim(void);

/* get / set config */
void dwphy_set_rate(uint8_t br);
uint8_t dwphy_get_rate(void);
void dwphy_set_plen(uint8_t plen);
uint8_t dwphy_get_plen(void);
uint8_t dwphy_get_prf(void);

void dwphy_set_chan(uint8_t chan);
uint8_t dwphy_get_chan(void);
void dwphy_set_rxPAC(uint8_t rxPAC);
uint8_t dwphy_get_rxPAC(void);
void dwphy_set_txCode(uint8_t txCode);
uint8_t dwphy_get_txCode(void);
void dwphy_set_rxCode(uint8_t rxCode);
uint8_t dwphy_get_rxCode(void);
void dwphy_set_sfdType(uint8_t sfdType);
uint8_t dwphy_get_sfdType(void);
void dwphy_set_dataRate(uint8_t dataRate);
uint8_t dwphy_get_dataRate(void);
void dwphy_set_phrMode(uint8_t phrMode);
uint8_t dwphy_get_phrMode(void);
void dwphy_set_phrRate(uint8_t phrRate);
uint8_t dwphy_get_phrRate(void);
void dwphy_set_sfdTO(uint16_t sfdTO);
uint16_t dwphy_get_sfdTO(void);
void dwphy_set_stsMode(uint8_t stsMode);
uint8_t dwphy_get_stsMode(void);
void dwphy_set_stsLength(uint8_t stsLength);
uint8_t dwphy_get_stsLength(void);
void dwphy_set_pdoaMode(uint8_t pdoaMode);
uint8_t dwphy_get_pdoaMode(void);

#endif
