#include "deca_interface.h"

uint8_t ull_geticrefvolt(dwchip_t *dw);
uint8_t ull_geticreftemp(dwchip_t *dw);
uint32_t ull_getpartid(dwchip_t *dw);
uint64_t ull_getlotid(dwchip_t *dw);
uint32_t dwt_read32bitoffsetreg(dwchip_t *dw, uint32_t regFileID, uint16_t regOffset);
void dwt_write32bitoffsetreg(dwchip_t *dw, uint32_t regFileID, uint16_t regOffset, uint32_t regval);
uint8_t ull_otprevision(dwchip_t *dw);
void ull_setpllcaltemperature(dwchip_t *dw, int8_t temperature); // DW3720 only
int8_t ull_getpllcaltemperature(dwchip_t *dw);// DW3720 only
void ull_setfinegraintxseq(dwchip_t *dw, int32_t enable);
void ull_setlnapamode(dwchip_t *dw, int32_t lna_pa);
void ull_setgpiomode(dwchip_t *dw, uint32_t gpio_mask, uint32_t gpio_modes);
void ull_setgpiodir(dwchip_t *dw, uint16_t in_out);
void ull_setgpiovalue(dwchip_t *dw, uint16_t gpio_mask, int32_t value);
uint16_t ull_readgpiovalue(dwchip_t *dw);
int ull_setdwstate(dwchip_t *dw, int32_t state);
void ull_enablegpioclocks(dwchip_t *dw);
void ull_restoreconfig(dwchip_t *dw, int32_t full_restore);
void ull_configurestsmode(dwchip_t *dw, uint8_t stsMode);
void ull_settxpower(dwchip_t *dw, uint32_t power);
void ull_configurestsloadiv(dwchip_t *dw);
void ull_configmrxlut(dwchip_t *dw, int32_t channel);
void ull_configurestskey(dwchip_t *dw, dwt_sts_cp_key_t *pStsKey);
void ull_configurestsiv(dwchip_t *dw, dwt_sts_cp_iv_t *pStsIv);
void ull_setrxantennadelay(dwchip_t *dw, uint16_t rxDelay);
uint16_t ull_getrxantennadelay(dwchip_t* dw);
void ull_settxantennadelay(dwchip_t *dw, uint16_t txDelay);
uint16_t ull_gettxantennadelay(dwchip_t* dw);
void ull_setplenfine(dwchip_t *dw, uint8_t preambleLength);
int32_t ull_starttx(dwchip_t *dw, uint8_t mode);
void ull_setreferencetrxtime(dwchip_t *dw, uint32_t reftime);
void ull_setdelayedtrxtime(dwchip_t *dw, uint32_t starttime);
uint8_t ull_get_dgcdecision(dwchip_t *dw);
void ull_readtxtimestamp(dwchip_t *dw, uint8_t *timestamp);
uint32_t ull_readtxtimestamphi32(dwchip_t *dw);
uint32_t ull_readtxtimestamplo32(dwchip_t *dw);
int16_t ull_readpdoa(dwchip_t *dw);
void ull_readtdoa(dwchip_t *dw, uint8_t *tdoa);
void ull_readrxtimestampunadj(dwchip_t *dw, uint8_t *timestamp);
void ull_readrxtimestamp_ipatov(dwchip_t *dw, uint8_t *timestamp);
void ull_readrxtimestamp_sts(dwchip_t *dw, uint8_t *timestamp);
uint32_t ull_readrxtimestamphi32(dwchip_t *dw);
uint32_t ull_readrxtimestamplo32(dwchip_t *dw);
uint32_t ull_readsystimehi32(dwchip_t *dw);
void ull_readsystime(dwchip_t *dw, uint8_t *timestamp);
void ull_forcetrxoff(dwchip_t *dw);
void ull_setsniffmode(dwchip_t *dw, int32_t enable, uint8_t timeOn, uint8_t timeOff);
void ull_setdblrxbuffmode(dwchip_t *dw, dwt_dbl_buff_state_e dbl_buff_state, dwt_dbl_buff_mode_e dbl_buff_mode);
void ull_signal_rx_buff_free(dwchip_t *dw);
void ull_setrxtimeout(dwchip_t *dw, uint32_t on_time);
void ull_setpreambledetecttimeout(dwchip_t *dw, uint16_t timeout);
uint16_t ull_calibratesleepcnt(dwchip_t *dw);
void ull_configuresleepcnt(dwchip_t *dw, uint16_t sleepcnt);
void ull_configuresleep(dwchip_t *dw, uint16_t mode, uint8_t wake);
void ull_clearaonconfig(dwchip_t *dw);
void ull_entersleep(dwchip_t *dw, int32_t idle_rc);
void ull_entersleepaftertx(dwchip_t *dw, int32_t enable);
void ull_entersleepafter(dwchip_t *dw, int32_t event_mask);
uint8_t ull_checkirq(dwchip_t *dw);
uint8_t ull_checkidlerc(dwchip_t *dw);
void ull_setpanid(dwchip_t *dw, uint16_t panID);
void ull_setaddress16(dwchip_t *dw, uint16_t shortAddress);
void ull_seteui(dwchip_t *dw, uint8_t *eui64);
void ull_geteui(dwchip_t *dw, uint8_t *eui64);
uint8_t ull_aon_read(dwchip_t *dw, uint16_t aon_address);
void ull_aon_write(dwchip_t *dw, uint16_t aon_address, uint8_t aon_write_data);
void ull_configureframefilter(dwchip_t *dw, uint16_t enabletype, uint16_t filtermode);
void ull_enablespicrccheck(dwchip_t *dw, dwt_spi_crc_mode_e crc_mode, dwt_spierrcb_t spireaderr_cb);
void ull_enableautoack(dwchip_t *dw, uint8_t responseDelayTime, int32_t enable);
void ull_setrxaftertxdelay(dwchip_t *dw, uint32_t rxDelayTime);
void ull_softreset(dwchip_t *dw, int32_t reset_semaphore);
void ull_write_rx_scratch_data(dwchip_t *dw, uint8_t *buffer, uint16_t length, uint16_t bufferOffset);
void ull_read_rx_scratch_data(dwchip_t *dw, uint8_t *buffer, uint16_t length, uint16_t rxBufferOffset);
int16_t ull_readclockoffset(dwchip_t *dw);
int32_t ull_readcarrierintegrator(dwchip_t *dw);
void ull_configciadiag(dwchip_t *dw, uint8_t enable_mask);
int32_t ull_readstsquality(dwchip_t *dw, int16_t *rxStsQualityIndex);
int32_t ull_readstsstatus(dwchip_t *dw, uint16_t *stsStatus, int32_t sts_num);
void ull_readdiagnostics(dwchip_t *dw, dwt_rxdiag_t *diagnostics);
int ull_readdiagnostics_acc(dwchip_t *dw, dwt_cirdiags_t *cir_diag, dwt_acc_idx_e acc_idx);
void ull_configeventcounters(dwchip_t *dw, int32_t enable);
void ull_readeventcounters(dwchip_t *dw, dwt_deviceentcnts_t *counters);
void ull_otpread(dwchip_t *dw, uint16_t address, uint32_t *array, uint8_t length);
int32_t ull_otpwriteandverify(dwchip_t *dw, uint32_t value, uint16_t address);
int32_t ull_otpwrite(dwchip_t *dw, uint32_t value, uint16_t address);
void ull_setleds(dwchip_t *dw, uint8_t mode);
void ull_setxtaltrim(dwchip_t *dw, uint8_t value);
uint8_t ull_getxtaltrim(dwchip_t *dw);
void ull_stop_repeated_frames(dwchip_t *dw);
void ull_repeated_frames(dwchip_t *dw, uint32_t framerepetitionrate);
void ull_send_test_preamble(dwchip_t *dw, uint16_t delay, uint32_t test_txpower);
void ull_repeated_cw(dwchip_t *dw, int32_t cw_enable, int32_t cw_mode_config);
void ull_configcwmode(dwchip_t *dw);
void ull_configcontinuousframemode(dwchip_t *dw, uint32_t framerepetitionrate);
void ull_disablecontinuousframemode(dwchip_t *dw);
void ull_disablecontinuouswavemode(dwchip_t *dw);
uint16_t ull_readtempvbat(dwchip_t *dw);
float ull_convertrawtemperature(dwchip_t *dw, uint8_t raw_temp);
float ull_convertrawvoltage(dwchip_t *dw, uint8_t raw_voltage);
uint8_t ull_readwakeuptemp(dwchip_t *dw);
uint8_t ull_readwakeupvbat(dwchip_t *dw);
uint8_t ull_readpgdelay(dwchip_t *dw);
uint8_t ull_calcbandwidthadj(dwchip_t *dw, uint16_t target_count);
uint16_t ull_calcpgcount(dwchip_t *dw, uint8_t pgdly);
void ull_set_keyreg_128(dwchip_t *dw, const dwt_aes_key_t *key);
void ull_configure_aes(dwchip_t *dw, const dwt_aes_config_t *pCfg);
dwt_mic_size_e ull_mic_size_from_bytes(dwchip_t *dw, uint8_t mic_size_in_bytes);
int8_t ull_do_aes(dwchip_t *dw, dwt_aes_job_t *job, dwt_aes_core_type_e core_type);
int32_t ull_check_dev_id(dwchip_t *dw);
int32_t ull_run_pgfcal(dwchip_t *dw);
int32_t ull_pgf_cal(dwchip_t *dw, int32_t ldoen);
uint32_t ull_readpllstatus(dwchip_t *dw);
int32_t ull_pll_cal(dwchip_t *dw);
void ull_configure_rf_port(dwchip_t *dw, dwt_rf_port_ctrl_e port_control);
void ull_configure_le_address(dwchip_t *dw, uint16_t addr, int32_t leIndex);
void ull_configuresfdtype(dwchip_t *dw, uint32_t sfdType);
void ull_writesysstatuslo(dwchip_t *dw, uint32_t mask);
void ull_writesysstatushi(dwchip_t *dw, uint32_t mask);
uint32_t ull_readsysstatuslo(dwchip_t *dw);
uint16_t ull_readsysstatushi(dwchip_t *dw);
void ull_writerdbstatus(dwchip_t *dw, uint8_t mask);
uint8_t ull_readrdbstatus(dwchip_t *dw);
uint16_t ull_getframelength(dwchip_t *dw, uint8_t *rng_bit);
void ull_setpdoaoffset(dwchip_t *dw, uint16_t pdoa_offset);
void ull_configure_and_set_antenna_selection_gpio(dwchip_t *dw, uint8_t antenna_config);
void ull_wifi_coex_set(dwchip_t *dw, dwt_wifi_coex_e enable, int32_t coex_io_swap);
void ull_reset_system_counter(dwchip_t *dw);
void ull_config_ostr_mode(dwchip_t *dw, uint8_t enable, uint16_t wait_time);
uint32_t ull_readCIAversion(dwchip_t* dw);
uint8_t ull_nlos_alldiag(dwchip_t *dw, dwt_nlos_alldiag_t *all_diag);
void ull_nlos_ipdiag(dwchip_t *dw, dwt_nlos_ipdiag_t *index);
int32_t ull_adjust_tx_power(uint16_t boost, uint32_t ref_tx_power, uint8_t channel, uint32_t* adj_tx_power, uint16_t* applied_boost );
int32_t ull_calculate_linear_tx_setting(struct dwchip_s *dw, int32_t channel, power_indexes_t *p_indexes, tx_adj_res_t *p_res);
int ull_convert_tx_power_to_index(int channel, uint8_t tx_power, uint8_t *tx_power_idx);
void ull_set_pll_config(struct dwchip_s *dw, uint32_t pll_common);
void ull_setstslength(dwchip_t *dw, dwt_sts_lengths_e sts_len);
void ull_setphr(struct dwchip_s *dw, uint8_t phrMode, uint8_t phrRate);
void ull_setdatarate(struct dwchip_s *dw, dwt_uwb_bit_rate_e bitRate);
void ull_setrxpac(struct dwchip_s *dw, dwt_pac_size_e rxPAC);
void ull_setsfdtimeout(struct dwchip_s *dw, uint16_t sfdTO);
void ull_dis_otp_ips(dwchip_t *dw, int32_t mode);
uint8_t ull_pll_chx_auto_cal(struct dwchip_s *dw, int32_t chan, uint32_t coarse_code, uint16_t sleep, uint8_t steps, int8_t temp);
void ull_configtxrxfcs(struct dwchip_s *dw, dwt_fcs_mode_e enable);
int ull_calculate_rssi(struct dwchip_s *dw, const dwt_cirdiags_t *diag, dwt_acc_idx_e acc_idx, int16_t *signal_strength);
int ull_calculate_first_path_power(struct dwchip_s *dw, const dwt_cirdiags_t *diag, dwt_acc_idx_e acc_idx, int16_t *signal_strength);
int32_t ull_setpdoamode(dwchip_t *dw, dwt_pdoa_mode_e pdoaMode);
int32_t ull_writetxdata(dwchip_t *dw, uint16_t txDataLength, uint8_t *txDataBytes, uint16_t txBufferOffset);
void ull_writetxfctrl(dwchip_t *dw, uint16_t txFrameLength, uint16_t txBufferOffset, uint8_t ranging);
void ull_readrxtimestamp(dwchip_t *dw, uint8_t *timestamp);
int32_t ull_rxenable(dwchip_t *dw, int32_t mode);
void ull_readrxdata(dwchip_t *dw, uint8_t *buffer, uint16_t length, uint16_t rxBufferOffset);

// DW3720 only
void ull_setinterrupt_db(dwchip_t *dw, uint8_t bitmask, dwt_INT_options_e INT_options);
void ull_ds_sema_request(dwchip_t *dw);
void ull_ds_sema_release(dwchip_t *dw);
void ull_ds_sema_force(dwchip_t *dw);
uint8_t ull_ds_sema_status(dwchip_t *dw);
uint8_t ull_ds_sema_status_hi(dwchip_t* dw);
void ull_ds_en_sleep(dwchip_t *dw, dwt_host_sleep_en_e host_sleep_en);
int32_t ull_ds_setinterrupt_SPIxavailable(dwchip_t* dw, dwt_spi_host_e spi_num, dwt_INT_options_e int_set);
void ull_enable_disable_eq(dwchip_t *dw, uint8_t en);
void ull_timers_reset(dwchip_t *dw);
uint16_t ull_timers_read_and_clear_events(dwchip_t *dw);
void ull_configure_timer(dwchip_t* dw, dwt_timer_cfg_t *tim_cfg);
void ull_configure_wificoex_gpio(dwchip_t *dw, uint8_t timer_coexout, uint8_t coex_swap);
void ull_set_timer_expiration(dwchip_t *dw, dwt_timers_e timer_name, uint32_t expire);
void ull_timer_enable(dwchip_t *dw, dwt_timers_e timer_name);
void ull_set_fixedsts(dwchip_t *dw, uint8_t set);
void ull_set_alternate_pulse_shape(dwchip_t *dw, uint8_t set_alternate);
int32_t ull_xtal_temperature_compensation(dwchip_t *dw, dwt_xtal_trim_t *params, uint8_t *xtaltrim);
void ull_capture_adc_samples(dwchip_t *dw, dwt_capture_adc_t *capture_adc);
void ull_read_adc_samples(dwchip_t *dw, dwt_capture_adc_t *capture_adc);
