From f93470e8fa153bbb7ea0c9cc062c55d88d457ef3 Mon Sep 17 00:00:00 2001 From: javerik Date: Wed, 22 Jan 2025 22:13:57 +0100 Subject: [PATCH 1/5] Added gitignore for KiCad temporary project files --- .../TUSS4470_shield/.gitignore | 55 ++++++++++++++++++ .../TUSS4470_shield-backups/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 55 insertions(+) create mode 100644 TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/.gitignore delete mode 100644 TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/TUSS4470_shield-backups/.DS_Store diff --git a/TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/.gitignore b/TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/.gitignore new file mode 100644 index 0000000..469a502 --- /dev/null +++ b/TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/.gitignore @@ -0,0 +1,55 @@ +# KiCad project files to include in the repository +!*.kicad_pro +!*.kicad_sch +!*.kicad_pcb +!*.kicad_sym +!*.kicad_mod +!*.kicad_wks + +# Ignore backup and autosave files +*.bak +*.b#* +*.s#* +*.~* +*.kicad_prl +*-backups +_autosave-* + +# Ignore user settings and logs +*.prl +*.net +*.xml +*.dcm +*.emp +fp-info-cache +sym-lib-table +fp-lib-table +*~ + +# Ignore gerber and fabrication outputs +*.gbr +*.drl +*.gbo +*.gtl +*.gts +*.gbs +*.gm1 +*.gm2 +*.fab +*.pos +*.gko + +# Ignore simulation-related files +*.cir +*.spice + +# Ignore intermediate build or temporary files +__pycache__/ +.cache/ +*.pyc + +# Ignore debugging and crash logs +*.log + +# Ignore specific files for user/system configurations +*.config.json diff --git a/TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/TUSS4470_shield-backups/.DS_Store b/TUSS4470_shield_001/TUSS4470_shield_hardware/TUSS4470_shield/TUSS4470_shield-backups/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Wed, 22 Jan 2025 22:19:29 +0100 Subject: [PATCH 2/5] Draft of a C based library for easy access to TUSS4470 SPI register --- .../development/libTUSS4470/libTUSS4470.ino | 70 +++++++ software/development/libTUSS4470/tuss4470.c | 151 ++++++++++++++++ software/development/libTUSS4470/tuss4470.h | 51 ++++++ .../development/libTUSS4470/tuss4470_types.h | 171 ++++++++++++++++++ 4 files changed, 443 insertions(+) create mode 100644 software/development/libTUSS4470/libTUSS4470.ino create mode 100644 software/development/libTUSS4470/tuss4470.c create mode 100644 software/development/libTUSS4470/tuss4470.h create mode 100644 software/development/libTUSS4470/tuss4470_types.h diff --git a/software/development/libTUSS4470/libTUSS4470.ino b/software/development/libTUSS4470/libTUSS4470.ino new file mode 100644 index 0000000..b5726a1 --- /dev/null +++ b/software/development/libTUSS4470/libTUSS4470.ino @@ -0,0 +1,70 @@ +#define PLATFORM_ARDUINO +#include "tuss4470.h" +#include + + +const int SPI_CS = 10; +const int IO1 = 8; +const int IO2 = 9; +const int O3 = 6; +const int O4 = 5; +const int analogIn = A0; + + +int spiTransfer(uint8_t mode, uint8_t *data, uint8_t size) { + static uint8_t buffer[2]; + digitalWrite(SPI_CS, LOW); + data[0] = SPI.transfer(data[0]); + data[1] = SPI.transfer(data[1]); + digitalWrite(SPI_CS, HIGH); + return 0; +} + +void setup() { + Serial.begin(115200); + + SPI.begin(); + SPI.setBitOrder(MSBFIRST); + SPI.setClockDivider(SPI_CLOCK_DIV16); + SPI.setDataMode(SPI_MODE1); // CPOL=0, CPHA=1 + + + pinMode(SPI_CS, OUTPUT); + digitalWrite(SPI_CS, HIGH); + + // Configure GPIOs + pinMode(IO1, OUTPUT); + digitalWrite(IO1, HIGH); + pinMode(IO2, OUTPUT); + pinMode(O3, INPUT); + pinMode(O4, INPUT); + + Serial.println("Initialize TUSS lib"); + tuss4470_t tuss4470; + int err = tuss4470_t_init(&spiTransfer, &tuss4470); + if (err) + { + Serial.println("Failed to init TUSS lib"); + } + + tuss4470_config_t config; + err = tuss4470_read_config(&tuss4470, tuss4470.config); + if (err) { + Serial.println("Failed to read config"); + return; + } + + Serial.println("TUSS4470 Configuration:"); + uint8_t *cfg_data = (uint8_t *)tuss4470.config; + for (int i = 0; i < sizeof(tuss4470_config_t); ++i) { + Serial.print("Register 0x"); + Serial.print(tuss4470_register_map[i], HEX); + Serial.print(": 0x"); + Serial.println(cfg_data[i], HEX); + } +} + +void loop() { + + +} diff --git a/software/development/libTUSS4470/tuss4470.c b/software/development/libTUSS4470/tuss4470.c new file mode 100644 index 0000000..9139db9 --- /dev/null +++ b/software/development/libTUSS4470/tuss4470.c @@ -0,0 +1,151 @@ +// +// Created by javerik on 20.01.25. +// +#include "tuss4470.h" + + +#ifdef PLATFORM_X86 +#include +#elif defined(PLATFORM_ARM) +#include +#elif defined(PLATFORM_ARDUINO) + + +#endif + + +#include +#include +uint8_t parity(const uint8_t *data) { + uint16_t data16 = (data[0] << 8) | data[1]; + uint8_t parity = 0; + for (int i = 0; i < 16; ++i) { + if ((data16 >> i) & 1) { + parity++; + } + } + return (parity + 1) % 2; +} + +int evaluate_status(uint8_t status, tuss4470_t *tuss4470) { + tuss4470->raw_device_state = status; + tuss4470->device_state = (tuss4470_device_state_t)(status & TUSS4470_STAT_DEV_STATE); + tuss4470->flag_vdrv_ready = (status & TUSS4470_STAT_VDRV_READY) >> 5; + tuss4470->flag_pulse_num_flt = (status & TUSS4470_STAT_PULSE_NUM_FLT) >> 4; + tuss4470->flag_drv_pulse_flt = (status & TUSS4470_STAT_DRV_PULSE_FLT) >> 3; + tuss4470->flag_ee_crc_flt = (status & TUSS4470_STAT_EE_CRC_FLT) >> 2; + if ((tuss4470->flag_ee_crc_flt || tuss4470->flag_drv_pulse_flt || tuss4470->flag_drv_pulse_flt )) { + return 1; + } else { + return 0; + } +} + +int tuss4470_t_init(tuss4470_spi_transfer_fptr spiTransfer_fptr, tuss4470_t *tuss4470) { + tuss4470->spiTransfer_fptr = spiTransfer_fptr; + tuss4470->config = (tuss4470_config_t *)malloc(sizeof(tuss4470_config_t)); + if (tuss4470->config == NULL) { + return 1; + } + return 0; +} + +int tuu4470_t_free(tuss4470_t *tuss4470) { + free(tuss4470->config); + return 0; +} + +int tuss4470_read_config(tuss4470_t *tuss4470, tuss4470_config_t *config) { + uint8_t *cfg_data = (uint8_t *)config; + int err = 0; + uint8_t data = 0; + for (size_t i = 0; i < sizeof(tuss4470_config_t); i++) + { + err = tuss4470_read_register(tuss4470, (tuss4470_register_map_t) (tuss4470_register_map[i]), &data); + if (err) return err; + cfg_data[i] = data; + } + return 0; +} + +int tuss4470_read_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t *data) { + if (tuss4470->spiTransfer_fptr == NULL) { + return 1; + } + static uint8_t spi_mode = 0x80; + + uint8_t tx_data[2] = {0}; + tx_data[0] = 0x80 + ((reg & 0x3F) << 1); + tx_data[1] = 0; + tx_data[0] |= parity(tx_data); + + int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2); + if (err) { + return err; + } + err = evaluate_status(tx_data[0], tuss4470); + if (err) { + return err; + } + *data = tx_data[1]; + return 0; +} + +int tuss4470_write_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t data) { + if (tuss4470->spiTransfer_fptr == NULL) { + return 1; + } + static uint8_t spi_mode = 0x00; + uint8_t tx_data[2] = {0}; + tx_data[0] = (reg & 0x3F) << 1; + tx_data[1] = data; + tx_data[0] |= parity(tx_data); + int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2); + if (err) { + return err; + } + err = evaluate_status(tx_data[0], tuss4470); + if (err) { + return err; + } + err = tuss4470_read_register(tuss4470, reg, &tx_data[1]); + if (err) { + return err; + } + if (tx_data[1] != data) { + return 1; + } + return 0; +} + +uint8_t tuss4470_get_config(uint8_t configData, uint8_t mask) { + uint8_t dataOut = configData & mask; + // to have a nicer representation of the data, we need to shift it to the right + uint8_t copy_mask = mask; + // count number of bits to shift left until bit 0 is set + uint8_t shift = 0; + while ((copy_mask & 1) == 0) { + copy_mask >>= 1; + shift++; + } + dataOut >>= shift; + return dataOut; +} + +uint8_t tuss4470_set_config(uint8_t configData, uint8_t mask, uint8_t value) { + // first we need to clear the bits that are set in the mask + uint8_t dataOut = configData & ~mask; + // now we need to shift the value to the left until the bits are set in the mask + uint8_t copy_mask = mask; + // count number of bits to shift left until bit 0 is set + uint8_t shift = 0; + while ((copy_mask & 1) == 0) { + copy_mask >>= 1; + shift++; + } + value <<= shift; + dataOut |= value; + return dataOut; +} + + diff --git a/software/development/libTUSS4470/tuss4470.h b/software/development/libTUSS4470/tuss4470.h new file mode 100644 index 0000000..e2df9f6 --- /dev/null +++ b/software/development/libTUSS4470/tuss4470.h @@ -0,0 +1,51 @@ +// +// Created by javerik on 20.01.25. +// + +#ifndef LIBTUSS4470_TUSS4470_H +#define LIBTUSS4470_TUSS4470_H + +#include "tuss4470_types.h" +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + // region function definitions for hw communication + typedef int (*tuss4470_spi_transfer_fptr)(uint8_t mode, uint8_t *data, uint8_t size); + // endregion + + typedef struct { + uint8_t raw_device_state; + tuss4470_device_state_t device_state; + int flag_vdrv_ready; + int flag_pulse_num_flt; + int flag_drv_pulse_flt; + int flag_ee_crc_flt; + tuss4470_config_t *config; + tuss4470_spi_transfer_fptr spiTransfer_fptr; + } tuss4470_t; + + int tuss4470_t_init(tuss4470_spi_transfer_fptr spiTransfer_fptr, tuss4470_t *tuss4470); + int tuu4470_t_free(tuss4470_t *tuss4470); + + // region generic Register access + int tuss4470_read_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t *data); + int tuss4470_write_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t data); + int tuss4470_read_config(tuss4470_t *tuss4470, tuss4470_config_t *config); + + // endregion + + // region specific register access + uint8_t tuss4470_get_config(uint8_t configData, uint8_t mask); + uint8_t tuss4470_set_config(uint8_t configData, uint8_t mask, uint8_t value); + // endregion + + +#ifdef __cplusplus +} +#endif + +#endif //LIBTUSS4470_TUSS4470_H diff --git a/software/development/libTUSS4470/tuss4470_types.h b/software/development/libTUSS4470/tuss4470_types.h new file mode 100644 index 0000000..7dd98e1 --- /dev/null +++ b/software/development/libTUSS4470/tuss4470_types.h @@ -0,0 +1,171 @@ +// +// Created by javerik on 20.01.25. +// + +#ifndef TESTLIBTUSS_TUSS4470_TYPES_H +#define TESTLIBTUSS_TUSS4470_TYPES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + + +// region Register map + +// Define masks for each status bit +#define TUSS4470_STAT_VDRV_READY (1 << 5) // Bit 5 +#define TUSS4470_STAT_PULSE_NUM_FLT (1 << 4) // Bit 4 +#define TUSS4470_STAT_DRV_PULSE_FLT (1 << 3) // Bit 3 +#define TUSS4470_STAT_EE_CRC_FLT (1 << 2) // Bit 2 +#define TUSS4470_STAT_DEV_STATE 0x03 // Bits 1:0 + +typedef enum { + TUSS4470_DEV_STATE_LISTEN = 0x00, // 00 + TUSS4470_DEV_STATE_BURST = 0x01, // 01 + TUSS4470_DEV_STATE_STANDBY = 0x02, // 10 + TUSS4470_DEV_STATE_SLEEP = 0x03 // 11 +} tuss4470_device_state_t; + +#define TUSS4470_REG_OFFSET 0x10 +#define TUSS4470_REG_END 0x1E + +typedef enum { + BPF_CONFIG_1 = 0x10, // Bandpass filter settings + BPF_CONFIG_2 = 0x11, // Bandpass filter settings + DEV_CTRL_1 = 0x12, // Log-amp configuration + DEV_CTRL_2 = 0x13, // Log-amp configuration + DEV_CTRL_3 = 0x14, // Device Configuration + VDRV_CTRL = 0x16, // VDRV Regulator Control + ECHO_INT_CONFIG = 0x17, // Echo Interrupt Control + ZC_CONFIG = 0x18, // Zero Crossing configuration + BURST_PULSE = 0x1A, // Burst pulse configuration + TOF_CONFIG = 0x1B, // Time of Flight Config + DEV_STAT = 0x1C, // Fault status bits + DEVICE_ID = 0x1D, // Device ID + REV_ID = 0x1E // Revision ID +} tuss4470_register_map_t; + +const uint8_t tuss4470_register_map[] = { + BPF_CONFIG_1, + BPF_CONFIG_2, + DEV_CTRL_1, + DEV_CTRL_2, + DEV_CTRL_3, + VDRV_CTRL, + ECHO_INT_CONFIG, + ZC_CONFIG, + BURST_PULSE, + TOF_CONFIG, + DEV_STAT, + DEVICE_ID, + REV_ID +}; + +// endregion + +//region Register bits + +typedef enum { + BPF_FC_TRIM_FRC = (1 << 7), // Bit 7: Override factor settings for Bandpass filter trim + BPF_BYPASS = (1 << 6), // Bit 6: Select between Bandpass filter or High pass filter + BPF_HPF_FREQ = 0x3F // Bits 5:0: Bandpass or High pass filter center frequency +} tuss4470_BPF_CONFIG_1_Bits; + +typedef enum { + tuss4470_BPF_Q_SEL = (0x3 << 4), // Bits 5:4: Bandpass filter Q factor + tuss4470_BPF_FC_TRIM = 0x0F // Bits 3:0: Offset for BPF_HPF_FREQ +} tuss4470_BPF_CONFIG_2_Bits; + +typedef enum { + tuss4470_LOGAMP_FRC = (1 << 7), // Bit 7: Override for factory settings + tuss4470_LOGAMP_SLOPE_ADJ = (0x7 << 4), // Bits 6:4: Slope or gain adjustment + tuss4470_LOGAMP_INT_ADJ = 0x0F // Bits 3:0: Logamp intercept adjustment +} tuss4470_DEV_CTRL_1_Bits; + + +typedef enum { + tuss4470_LOGAMP_DIS_FIRST = (1 << 7), // Bit 7: Disable first logamp stage + tuss4470_LOGAMP_DIS_LAST = (1 << 6), // Bit 6: Disable last logamp stage + tuss4470_VOUT_SCALE_SEL = (1 << 2), // Bit 2: Select VOUT scaling + tuss4470_LNA_GAIN = 0x03 // Bits 1:0: Adjust LNA gain +} tuss4470_DEV_CTRL_2_Bits; + +typedef enum { + tuss4470_DRV_PLS_FLT_DT = (0x7 << 2), // Bits 4:2: Driver Pulse Fault Deglitch Time + tuss4470_IO_MODE = 0x03 // Bits 1:0: Configuration for low voltage IO pins +} tuss4470_DEV_CTRL_3_Bits; + +typedef enum { + tuss4470_DIS_VDRV_REG_LSTN = (1 << 6), // Bit 6: Automatically disable VDRV charging in listen mode + tuss4470_VDRV_HI_Z = (1 << 5), // Bit 5: Turn off current source and disable VDRV regulation + tuss4470_VDRV_CURRENT_LEVEL = (1 << 4), // Bit 4: Pull-up current at VDRV pin + tuss4470_VDRV_VOLTAGE_LEVEL = 0x0F // Bits 3:0: Regulated voltage level at VDRV pin +} tuss4470_VDRV_CTRL_Bits; + + +typedef enum { + tuss4470_ECHO_INT_CMP_EN = (1 << 4), // Bit 4: Enable echo interrupt comparator output + tuss4470_ECHO_INT_THR_SEL = 0x0F // Bits 3:0: Threshold level to issue interrupt +} tuss4470_ECHO_INT_CONFIG_Bits; + + +typedef enum { + tuss4470_ZC_CMP_EN = (1 << 7), // Bit 7: Enable Zero Cross Comparator for frequency detection + tuss4470_ZC_EN_ECHO_INT = (1 << 6), // Bit 6: Provide ZC information only when object is detected + tuss4470_ZC_CMP_IN_SEL = (1 << 5), // Bit 5: Zero Comparator Input Select + tuss4470_ZC_CMP_STG_SEL = (0x3 << 3), // Bits 4:3: Zero Cross Comparator Stage Select + tuss4470_ZC_CMP_HYST = 0x07 // Bits 2:0: Zero Cross Comparator Hysteresis Selection +} tuss4470_ZC_CONFIG_Bits; + + +typedef enum { + tuss4470_HALF_BRG_MODE = (1 << 7), // Bit 7: Enable/disable half-bridge mode + tuss4470_PRE_DRIVER_MODE = (1 << 6), // Bit 6: Enable/disable pre-driver mode + tuss4470_BURST_PULSE = 0x3F // Bits 5:0: Number of burst pulses +} tuss4470_BURST_PULSE_Bits; + + +typedef enum { + tuss4470_SLEEP_MODE_EN = (1 << 7), // Bit 7: Enable/disable sleep mode + tuss4470_STDBY_MODE_EN = (1 << 6), // Bit 6: Enable/disable standby mode + tuss4470_VDRV_TRIGGER = (1 << 1), // Bit 1: Control charging of VDRV pin + tuss4470_CMD_TRIGGER = (1 << 0) // Bit 0: Control enabling of burst mode +} tuss4470_TOF_CONFIG_Bits; + +typedef enum { + tuss4470_VDRV_READY = (1 << 3), // Bit 3: VDRV pin voltage status + tuss4470_PULSE_NUM_FLT = (1 << 2), // Bit 2: Driver has not received the number of pulses defined by BURST_PULSE + tuss4470_DRV_PULSE_FLT = (1 << 1), // Bit 1: Driver stuck in a single state in burst mode + tuss4470_EE_CRC_FLT = (1 << 0) // Bit 0: CRC error for internal memory +} tuss4470_DEV_STAT_Bits; + +// endregion + +// region Configuration structure + +typedef struct __attribute__((packed)) { + uint8_t BPF_CONFIG_1; + uint8_t BPF_CONFIG_2; + uint8_t DEV_CTRL_1; + uint8_t DEV_CTRL_2; + uint8_t DEV_CTRL_3; + uint8_t VDRV_CTRL; + uint8_t ECHO_INT_CONFIG; + uint8_t ZC_CONFIG; + uint8_t BURST_PULSE; + uint8_t TOF_CONFIG; + uint8_t DEV_STAT; + uint8_t DEVICE_ID; + uint8_t REV_ID; +} tuss4470_config_t; + +// endregion + +#ifdef __cplusplus +} +#endif + +#endif //TESTLIBTUSS_TUSS4470_TYPES_H From 0c56e2db84e048253d2e06dd012ab1a4ce225c64 Mon Sep 17 00:00:00 2001 From: javerik Date: Wed, 22 Jan 2025 23:14:06 +0100 Subject: [PATCH 3/5] First very basic cpp wrapper for libTuss4470 C lib --- software/development/libTUSS4470/.gitignore | 1 + .../development/libTUSS4470/libTUSS4470.ino | 35 ++----- software/development/libTUSS4470/tuss4470.c | 18 +++- software/development/libTUSS4470/tuss4470.h | 7 +- .../libTUSS4470/tuss4470_arduino.cpp | 94 +++++++++++++++++++ .../libTUSS4470/tuss4470_arduino.h | 38 ++++++++ 6 files changed, 160 insertions(+), 33 deletions(-) create mode 100644 software/development/libTUSS4470/.gitignore create mode 100644 software/development/libTUSS4470/tuss4470_arduino.cpp create mode 100644 software/development/libTUSS4470/tuss4470_arduino.h diff --git a/software/development/libTUSS4470/.gitignore b/software/development/libTUSS4470/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/software/development/libTUSS4470/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/software/development/libTUSS4470/libTUSS4470.ino b/software/development/libTUSS4470/libTUSS4470.ino index b5726a1..9027e11 100644 --- a/software/development/libTUSS4470/libTUSS4470.ino +++ b/software/development/libTUSS4470/libTUSS4470.ino @@ -1,5 +1,6 @@ #define PLATFORM_ARDUINO #include "tuss4470.h" +#include "tuss4470_arduino.h" #include @@ -23,39 +24,19 @@ int spiTransfer(uint8_t mode, uint8_t *data, uint8_t size) { void setup() { Serial.begin(115200); - SPI.begin(); - SPI.setBitOrder(MSBFIRST); - SPI.setClockDivider(SPI_CLOCK_DIV16); - SPI.setDataMode(SPI_MODE1); // CPOL=0, CPHA=1 - - - pinMode(SPI_CS, OUTPUT); - digitalWrite(SPI_CS, HIGH); - - // Configure GPIOs - pinMode(IO1, OUTPUT); - digitalWrite(IO1, HIGH); - pinMode(IO2, OUTPUT); - pinMode(O3, INPUT); - pinMode(O4, INPUT); - - Serial.println("Initialize TUSS lib"); - tuss4470_t tuss4470; - int err = tuss4470_t_init(&spiTransfer, &tuss4470); + TUSS4470 tuss; + int err = tuss.begin(); if (err) { - Serial.println("Failed to init TUSS lib"); + Serial.println("Error init tuss"); + return; } - - tuss4470_config_t config; - err = tuss4470_read_config(&tuss4470, tuss4470.config); + err = tuss.readConfig(); if (err) { - Serial.println("Failed to read config"); - return; + Serial.println("Error reading config"); } - Serial.println("TUSS4470 Configuration:"); - uint8_t *cfg_data = (uint8_t *)tuss4470.config; + uint8_t *cfg_data = (uint8_t *)tuss.getConfig(); for (int i = 0; i < sizeof(tuss4470_config_t); ++i) { Serial.print("Register 0x"); Serial.print(tuss4470_register_map[i], HEX); diff --git a/software/development/libTUSS4470/tuss4470.c b/software/development/libTUSS4470/tuss4470.c index 9139db9..a776f01 100644 --- a/software/development/libTUSS4470/tuss4470.c +++ b/software/development/libTUSS4470/tuss4470.c @@ -47,10 +47,11 @@ int tuss4470_t_init(tuss4470_spi_transfer_fptr spiTransfer_fptr, tuss4470_t *tus if (tuss4470->config == NULL) { return 1; } + tuss4470->ctx = NULL; return 0; } -int tuu4470_t_free(tuss4470_t *tuss4470) { +int tuss4470_t_free(tuss4470_t *tuss4470) { free(tuss4470->config); return 0; } @@ -68,6 +69,17 @@ int tuss4470_read_config(tuss4470_t *tuss4470, tuss4470_config_t *config) { return 0; } +int tuss4470_write_config(tuss4470_t *tuss4470, tuss4470_config_t *config) { + uint8_t *cfg_data = (uint8_t *)config; + int err = 0; + for (size_t i = 0; i < sizeof(tuss4470_config_t); i++) + { + err = tuss4470_write_register(tuss4470, (tuss4470_register_map_t) (tuss4470_register_map[i]), cfg_data[i]); + if (err) return err; + } + return 0; +} + int tuss4470_read_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t *data) { if (tuss4470->spiTransfer_fptr == NULL) { return 1; @@ -79,7 +91,7 @@ int tuss4470_read_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, ui tx_data[1] = 0; tx_data[0] |= parity(tx_data); - int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2); + int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2, tuss4470->ctx); if (err) { return err; } @@ -100,7 +112,7 @@ int tuss4470_write_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, u tx_data[0] = (reg & 0x3F) << 1; tx_data[1] = data; tx_data[0] |= parity(tx_data); - int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2); + int err = tuss4470->spiTransfer_fptr(spi_mode, tx_data, 2, tuss4470->ctx); if (err) { return err; } diff --git a/software/development/libTUSS4470/tuss4470.h b/software/development/libTUSS4470/tuss4470.h index e2df9f6..d28b0cf 100644 --- a/software/development/libTUSS4470/tuss4470.h +++ b/software/development/libTUSS4470/tuss4470.h @@ -14,7 +14,7 @@ extern "C" { #endif // region function definitions for hw communication - typedef int (*tuss4470_spi_transfer_fptr)(uint8_t mode, uint8_t *data, uint8_t size); + typedef int (*tuss4470_spi_transfer_fptr)(uint8_t mode, uint8_t *data, uint8_t size, void *ctx); // endregion typedef struct { @@ -26,16 +26,17 @@ extern "C" { int flag_ee_crc_flt; tuss4470_config_t *config; tuss4470_spi_transfer_fptr spiTransfer_fptr; + void *ctx; } tuss4470_t; int tuss4470_t_init(tuss4470_spi_transfer_fptr spiTransfer_fptr, tuss4470_t *tuss4470); - int tuu4470_t_free(tuss4470_t *tuss4470); + int tuss4470_t_free(tuss4470_t *tuss4470); // region generic Register access int tuss4470_read_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t *data); int tuss4470_write_register(tuss4470_t *tuss4470, tuss4470_register_map_t reg, uint8_t data); int tuss4470_read_config(tuss4470_t *tuss4470, tuss4470_config_t *config); - + int tuss4470_write_config(tuss4470_t *tuss4470, tuss4470_config_t *config); // endregion // region specific register access diff --git a/software/development/libTUSS4470/tuss4470_arduino.cpp b/software/development/libTUSS4470/tuss4470_arduino.cpp new file mode 100644 index 0000000..0e4fcd8 --- /dev/null +++ b/software/development/libTUSS4470/tuss4470_arduino.cpp @@ -0,0 +1,94 @@ +#include "tuss4470_arduino.h" + +const int _default_cs = 10; +const int _default_io1 = 8; +const int _default_io2 = 9; +const int _default_o3 = 6; +const int _default_o4 = 5; +const int _default_analogIn = A0; + + +// region C callbacks for SPI communication + +int _spiTransfer(uint8_t mode, uint8_t *data, uint8_t size, void *ctx) { + TUSS4470 *tuss = (TUSS4470 *)ctx; + return tuss->spiTransfer(mode, data, size); +} + + +// endregion + + + +TUSS4470::TUSS4470() { + cs = _default_cs; + io1 = _default_io1; + io2 = _default_io2; + o3 = _default_o3; + o4 = _default_o4; + analogIn = _default_analogIn; +} + +TUSS4470::~TUSS4470() { + tuss4470_t_free(&tuss4470); +} + +int TUSS4470::begin() { + // Initialize SPI + SPI.begin(); + SPI.setBitOrder(MSBFIRST); + SPI.setClockDivider(SPI_CLOCK_DIV16); + SPI.setDataMode(SPI_MODE1); // CPOL=0, CPHA=1 + // Configure GPIOs + pinMode(_default_cs, OUTPUT); + + digitalWrite(_default_cs, HIGH); + pinMode(_default_io1, OUTPUT); + digitalWrite(_default_io1, HIGH); + pinMode(_default_io2, OUTPUT); + pinMode(_default_o3, INPUT); + pinMode(_default_o4, INPUT); + + int err = tuss4470_t_init(&_spiTransfer, &tuss4470); + if (err) + { + return err; + } + tuss4470.ctx = (void*)this; + return 0; +} + +int TUSS4470::readConfig() { + return tuss4470_read_config(&tuss4470, tuss4470.config); +} + +int TUSS4470::writeConfig() { + return tuss4470_write_config(&tuss4470, tuss4470.config); +} + +int TUSS4470::readRawRegister(tuss4470_register_map_t reg, uint8_t *data) { + return tuss4470_read_register(&tuss4470, reg, data); +} + +int TUSS4470::writeRawRegister(tuss4470_register_map_t reg, uint8_t data) { + return tuss4470_write_register(&tuss4470, reg, data); +} + +uint8_t TUSS4470::getConfig(uint8_t configData, uint8_t mask) { + return tuss4470_get_config(configData, mask); +} + +uint8_t TUSS4470::setConfig(uint8_t configData, uint8_t mask, uint8_t value) { + return tuss4470_set_config(configData, mask, value); +} + +// region private functions +int TUSS4470::spiTransfer(uint8_t mode, uint8_t *data, uint8_t size) { + digitalWrite(cs, LOW); + data[0] = SPI.transfer(data[0]); + data[1] = SPI.transfer(data[1]); + digitalWrite(cs, HIGH); + return 0; +} + +// endregion diff --git a/software/development/libTUSS4470/tuss4470_arduino.h b/software/development/libTUSS4470/tuss4470_arduino.h new file mode 100644 index 0000000..eae4bb7 --- /dev/null +++ b/software/development/libTUSS4470/tuss4470_arduino.h @@ -0,0 +1,38 @@ +#ifndef LIBTUSS_TUSS4470_ARDUINO_H +#define LIBTUSS_TUSS4470_ARDUINO_H + +#include +#include +#include +#include "tuss4470.h" + +class TUSS4470 { + public: + TUSS4470(); + TUSS4470(int cs, int io1, int io2, int o3, int o4, int analogIn); + ~TUSS4470(); + + int begin(); + int readConfig(); + int writeConfig(); + + int readRawRegister(tuss4470_register_map_t reg, uint8_t *data); + int writeRawRegister(tuss4470_register_map_t reg, uint8_t data); + + uint8_t getConfig(uint8_t configData, uint8_t mask); + uint8_t setConfig(uint8_t configData, uint8_t mask, uint8_t value); + + int spiTransfer(uint8_t mode, uint8_t *data, uint8_t size); + + tuss4470_t *getTuss4470() { return &tuss4470; } + tuss4470_config_t *getConfig() { return tuss4470.config; } + + private: + int cs, io1, io2, o3, o4, analogIn; + tuss4470_t tuss4470; + + + +}; + +#endif //LIBTUSS_TUSS4470_ARDUINO_H \ No newline at end of file From 17719364ef638f924eba4b7b483be7b8016b8875 Mon Sep 17 00:00:00 2001 From: javerik Date: Sun, 26 Jan 2025 22:02:41 +0100 Subject: [PATCH 4/5] Added all register settings as own functions to the arduino cpp wrapper. Added a beginCustomSPI to set a custom spi callback when other SPI settings are used or different platform --- .../development/libTUSS4470/libTUSS4470.ino | 22 +- software/development/libTUSS4470/tuss4470.c | 15 +- software/development/libTUSS4470/tuss4470.h | 4 + .../libTUSS4470/tuss4470_arduino.cpp | 214 ++++++++++++++++++ .../libTUSS4470/tuss4470_arduino.h | 51 +++++ 5 files changed, 296 insertions(+), 10 deletions(-) diff --git a/software/development/libTUSS4470/libTUSS4470.ino b/software/development/libTUSS4470/libTUSS4470.ino index 9027e11..f39118f 100644 --- a/software/development/libTUSS4470/libTUSS4470.ino +++ b/software/development/libTUSS4470/libTUSS4470.ino @@ -12,18 +12,11 @@ const int O4 = 5; const int analogIn = A0; -int spiTransfer(uint8_t mode, uint8_t *data, uint8_t size) { - static uint8_t buffer[2]; - digitalWrite(SPI_CS, LOW); - data[0] = SPI.transfer(data[0]); - data[1] = SPI.transfer(data[1]); - digitalWrite(SPI_CS, HIGH); - return 0; -} - void setup() { Serial.begin(115200); + // Using default contructor which uses the internal Arduino SPI + // Use .beginCustomSPI() for different platforms TUSS4470 tuss; int err = tuss.begin(); if (err) @@ -31,6 +24,7 @@ void setup() { Serial.println("Error init tuss"); return; } + // Read the complete configuration from TUSS device err = tuss.readConfig(); if (err) { Serial.println("Error reading config"); @@ -43,6 +37,16 @@ void setup() { Serial.print(": 0x"); Serial.println(cfg_data[i], HEX); } + + // Set a specific register, the register is written to the device and automatically read again + // written value must match read value otherwise error will be returned + // Also the value is checked if it is in the valid range + err = tuss.setBPF_HPFFreq(0x1D); + if (err) { + Serial.println(F("Failed to set parameter HPFFreq to 0x1D")); + return; + } + Serial.println(F("Successfully set parameter HPFFreq to 0x1D")); } void loop() { diff --git a/software/development/libTUSS4470/tuss4470.c b/software/development/libTUSS4470/tuss4470.c index a776f01..9aeae93 100644 --- a/software/development/libTUSS4470/tuss4470.c +++ b/software/development/libTUSS4470/tuss4470.c @@ -160,4 +160,17 @@ uint8_t tuss4470_set_config(uint8_t configData, uint8_t mask, uint8_t value) { return dataOut; } - +int tuss4470_value_is_in_range(uint8_t mask, uint8_t value) { + uint8_t copy_mask = mask; + // count number of bits to shift left until bit 0 is set + uint8_t shift = 0; + while ((copy_mask & 1) == 0) { + copy_mask >>= 1; + shift++; + } + uint8_t mask_value = mask >> shift; + if (value > mask_value) { + return 1; + } + return 0; +} diff --git a/software/development/libTUSS4470/tuss4470.h b/software/development/libTUSS4470/tuss4470.h index d28b0cf..2e0e559 100644 --- a/software/development/libTUSS4470/tuss4470.h +++ b/software/development/libTUSS4470/tuss4470.h @@ -44,6 +44,10 @@ extern "C" { uint8_t tuss4470_set_config(uint8_t configData, uint8_t mask, uint8_t value); // endregion + // region utility functions + int tuss4470_value_is_in_range(uint8_t mask, uint8_t value); + // endregion + #ifdef __cplusplus } diff --git a/software/development/libTUSS4470/tuss4470_arduino.cpp b/software/development/libTUSS4470/tuss4470_arduino.cpp index 0e4fcd8..ec6dc22 100644 --- a/software/development/libTUSS4470/tuss4470_arduino.cpp +++ b/software/development/libTUSS4470/tuss4470_arduino.cpp @@ -58,6 +58,220 @@ int TUSS4470::begin() { return 0; } +int TUSS4470::beginCustomSPI(tuss4470_spi_transfer_fptr spiTransfer_fptr, void *ctx) { + int err = tuss4470_t_init(spiTransfer_fptr, &tuss4470); + if (err) + { + return err; + } + tuss4470.ctx = ctx; + return 0; +} + +// region register access functions + +int TUSS4470::setBPF_HPFFreq(uint8_t freq) { + if (tuss4470_value_is_in_range(BPF_HPF_FREQ, freq)) { + return 1; + } + tuss4470.config->BPF_CONFIG_1 = setConfig(tuss4470.config->BPF_CONFIG_1, BPF_HPF_FREQ, freq); + return tuss4470_write_register(&tuss4470, BPF_CONFIG_1, tuss4470.config->BPF_CONFIG_1); +} + +int TUSS4470::setBPF_Bypass(bool value) { + tuss4470.config->BPF_CONFIG_1 = setConfig(tuss4470.config->BPF_CONFIG_1, BPF_BYPASS, value); + return tuss4470_write_register(&tuss4470, BPF_CONFIG_1, tuss4470.config->BPF_CONFIG_1); +} + +int TUSS4470::setBPF_FCTrimFrc(bool value) { + tuss4470.config->BPF_CONFIG_1 = setConfig(tuss4470.config->BPF_CONFIG_1, BPF_FC_TRIM_FRC, value); + return tuss4470_write_register(&tuss4470, BPF_CONFIG_1, tuss4470.config->BPF_CONFIG_1); +} + +int TUSS4470::setBPF_QSel(uint8_t qSel) { + if (tuss4470_value_is_in_range(tuss4470_BPF_Q_SEL, qSel)) { + return 1; + } + tuss4470.config->BPF_CONFIG_2 = setConfig(tuss4470.config->BPF_CONFIG_2, tuss4470_BPF_Q_SEL, qSel); + return tuss4470_write_register(&tuss4470, BPF_CONFIG_2, tuss4470.config->BPF_CONFIG_2); +} + +int TUSS4470::setBPF_FCTrim(uint8_t fcTrim) { + if (tuss4470_value_is_in_range(tuss4470_BPF_FC_TRIM, fcTrim)) { + return 1; + } + tuss4470.config->BPF_CONFIG_2 = setConfig(tuss4470.config->BPF_CONFIG_2, tuss4470_BPF_FC_TRIM, fcTrim); + return tuss4470_write_register(&tuss4470, BPF_CONFIG_2, tuss4470.config->BPF_CONFIG_2); +} + +int TUSS4470::setDEV_LogAmpFrc(bool value) { + tuss4470.config->DEV_CTRL_1 = setConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_FRC, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_1, tuss4470.config->DEV_CTRL_1); +} + +int TUSS4470::setDEV_LogAmpSlopeAdj(uint8_t value) { + if (tuss4470_value_is_in_range(tuss4470_LOGAMP_SLOPE_ADJ, value)) { + return 1; + } + tuss4470.config->DEV_CTRL_1 = setConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_SLOPE_ADJ, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_1, tuss4470.config->DEV_CTRL_1); +} + +int TUSS4470::setDEV_LogAmpIntAdj(uint8_t value) { + if (tuss4470_value_is_in_range(tuss4470_LOGAMP_INT_ADJ, value)) { + return 1; + } + tuss4470.config->DEV_CTRL_1 = setConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_INT_ADJ, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_1, tuss4470.config->DEV_CTRL_1); +} + +int TUSS4470::setLogAmpDisableFirstStage(bool value) { + tuss4470.config->DEV_CTRL_2 = setConfig(tuss4470.config->DEV_CTRL_2, tuss4470_LOGAMP_DIS_FIRST, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_2, tuss4470.config->DEV_CTRL_2); +} + +int TUSS4470::setLogAmpDisableLastStage(bool value) { + tuss4470.config->DEV_CTRL_2 = setConfig(tuss4470.config->DEV_CTRL_2, tuss4470_LOGAMP_DIS_LAST, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_2, tuss4470.config->DEV_CTRL_2); +} + +int TUSS4470::setVOUTScaling(bool value) { + tuss4470.config->DEV_CTRL_2 = setConfig(tuss4470.config->DEV_CTRL_2, tuss4470_VOUT_SCALE_SEL, value); + return tuss4470_write_register(&tuss4470, DEV_CTRL_2, tuss4470.config->DEV_CTRL_2); +} + +int TUSS4470::setLNAGain(uint8_t gain) { + if (tuss4470_value_is_in_range(tuss4470_LNA_GAIN, gain)) { + return 1; + } + tuss4470.config->DEV_CTRL_3 = setConfig(tuss4470.config->DEV_CTRL_3, tuss4470_LNA_GAIN, gain); + return tuss4470_write_register(&tuss4470, DEV_CTRL_3, tuss4470.config->DEV_CTRL_3); +} + +int TUSS4470::setDriverPulseFaultDeglitchTime(uint8_t time) { + if (tuss4470_value_is_in_range(tuss4470_DRV_PLS_FLT_DT, time)) { + return 1; + } + tuss4470.config->DEV_CTRL_3 = setConfig(tuss4470.config->DEV_CTRL_3, tuss4470_DRV_PLS_FLT_DT, time); + return tuss4470_write_register(&tuss4470, DEV_CTRL_3, tuss4470.config->DEV_CTRL_3); +} + +int TUSS4470::setLowVoltageIOConfig(uint8_t config) { + if (tuss4470_value_is_in_range(tuss4470_IO_MODE, config)) { + return 1; + } + tuss4470.config->DEV_CTRL_3 = setConfig(tuss4470.config->DEV_CTRL_3, tuss4470_IO_MODE, config); + return tuss4470_write_register(&tuss4470, DEV_CTRL_3, tuss4470.config->DEV_CTRL_3); +} + +int TUSS4470::setDisableVDRVRegulationInListenMode(bool value) { + tuss4470.config->VDRV_CTRL = setConfig(tuss4470.config->VDRV_CTRL, tuss4470_DIS_VDRV_REG_LSTN, value); + return tuss4470_write_register(&tuss4470, VDRV_CTRL, tuss4470.config->VDRV_CTRL); +} + +int TUSS4470::setVDRVHighImpedance(bool value) { + tuss4470.config->VDRV_CTRL = setConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_HI_Z, value); + return tuss4470_write_register(&tuss4470, VDRV_CTRL, tuss4470.config->VDRV_CTRL); +} + +int TUSS4470::setVDRVCurrentLevel(bool value) { + tuss4470.config->VDRV_CTRL = setConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_CURRENT_LEVEL, value); + return tuss4470_write_register(&tuss4470, VDRV_CTRL, tuss4470.config->VDRV_CTRL); +} + +int TUSS4470::setVDRVVoltageLevel(uint8_t level) { + if (tuss4470_value_is_in_range(tuss4470_VDRV_VOLTAGE_LEVEL, level)) { + return 1; + } + tuss4470.config->VDRV_CTRL = setConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_VOLTAGE_LEVEL, level); + return tuss4470_write_register(&tuss4470, VDRV_CTRL, tuss4470.config->VDRV_CTRL); +} + +int TUSS4470::setEchoInterruptComparatorEnable(bool value) { + tuss4470.config->ECHO_INT_CONFIG = setConfig(tuss4470.config->ECHO_INT_CONFIG, tuss4470_ECHO_INT_CMP_EN, value); + return tuss4470_write_register(&tuss4470, ECHO_INT_CONFIG, tuss4470.config->ECHO_INT_CONFIG); +} + +int TUSS4470::setEchoInterruptThreshold(uint8_t threshold) { + if (tuss4470_value_is_in_range(tuss4470_ECHO_INT_THR_SEL, threshold)) { + return 1; + } + tuss4470.config->ECHO_INT_CONFIG = setConfig(tuss4470.config->ECHO_INT_CONFIG, tuss4470_ECHO_INT_THR_SEL, threshold); + return tuss4470_write_register(&tuss4470, ECHO_INT_CONFIG, tuss4470.config->ECHO_INT_CONFIG); +} + +int TUSS4470::setZeroCrossComparatorEnable(bool value) { + tuss4470.config->ZC_CONFIG = setConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_EN, value); + return tuss4470_write_register(&tuss4470, ZC_CONFIG, tuss4470.config->ZC_CONFIG); +} + +int TUSS4470::setZeroCrossEnableEchoInterrupt(bool value) { + tuss4470.config->ZC_CONFIG = setConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_EN_ECHO_INT, value); + return tuss4470_write_register(&tuss4470, ZC_CONFIG, tuss4470.config->ZC_CONFIG); +} + +int TUSS4470::setZeroComparatorInputSelect(bool value) { + tuss4470.config->ZC_CONFIG = setConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_IN_SEL, value); + return tuss4470_write_register(&tuss4470, ZC_CONFIG, tuss4470.config->ZC_CONFIG); +} + +int TUSS4470::setZeroCrossComparatorStageSelect(uint8_t stage) { + if (tuss4470_value_is_in_range(tuss4470_ZC_CMP_STG_SEL, stage)) { + return 1; + } + tuss4470.config->ZC_CONFIG = setConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_STG_SEL, stage); + return tuss4470_write_register(&tuss4470, ZC_CONFIG, tuss4470.config->ZC_CONFIG); +} + +int TUSS4470::setZeroCrossComparatorHysteresis(uint8_t hysteresis) { + if (tuss4470_value_is_in_range(tuss4470_ZC_CMP_HYST, hysteresis)) { + return 1; + } + tuss4470.config->ZC_CONFIG = setConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_HYST, hysteresis); + return tuss4470_write_register(&tuss4470, ZC_CONFIG, tuss4470.config->ZC_CONFIG); +} + +int TUSS4470::setHalfBridgeMode(bool value) { + tuss4470.config->BURST_PULSE = setConfig(tuss4470.config->BURST_PULSE, tuss4470_HALF_BRG_MODE, value); + return tuss4470_write_register(&tuss4470, BURST_PULSE, tuss4470.config->BURST_PULSE); +} + +int TUSS4470::setPreDriverMode(bool value) { + tuss4470.config->BURST_PULSE = setConfig(tuss4470.config->BURST_PULSE, tuss4470_PRE_DRIVER_MODE, value); + return tuss4470_write_register(&tuss4470, BURST_PULSE, tuss4470.config->BURST_PULSE); +} + +int TUSS4470::setBurstPulseNumber(uint8_t pulseCount) { + if (tuss4470_value_is_in_range(tuss4470_BURST_PULSE, pulseCount)) { + return 1; + } + tuss4470.config->BURST_PULSE = setConfig(tuss4470.config->BURST_PULSE, tuss4470_BURST_PULSE, pulseCount); + return tuss4470_write_register(&tuss4470, BURST_PULSE, tuss4470.config->BURST_PULSE); +} + +int TUSS4470::setSleepModeEnable(bool value) { + tuss4470.config->TOF_CONFIG = setConfig(tuss4470.config->TOF_CONFIG, tuss4470_SLEEP_MODE_EN, value); + return tuss4470_write_register(&tuss4470, TOF_CONFIG, tuss4470.config->TOF_CONFIG); +} + +int TUSS4470::setStandbyModeEnable(bool value) { + tuss4470.config->TOF_CONFIG = setConfig(tuss4470.config->TOF_CONFIG, tuss4470_STDBY_MODE_EN, value); + return tuss4470_write_register(&tuss4470, TOF_CONFIG, tuss4470.config->TOF_CONFIG); +} + +int TUSS4470::setVDRVTriggerControl(bool value) { + tuss4470.config->TOF_CONFIG = setConfig(tuss4470.config->TOF_CONFIG, tuss4470_VDRV_TRIGGER, value); + return tuss4470_write_register(&tuss4470, TOF_CONFIG, tuss4470.config->TOF_CONFIG); +} + +int TUSS4470::setCommandTriggerControl(bool value) { + tuss4470.config->TOF_CONFIG = setConfig(tuss4470.config->TOF_CONFIG, tuss4470_CMD_TRIGGER, value); + return tuss4470_write_register(&tuss4470, TOF_CONFIG, tuss4470.config->TOF_CONFIG); +} + +// endregion + + int TUSS4470::readConfig() { return tuss4470_read_config(&tuss4470, tuss4470.config); } diff --git a/software/development/libTUSS4470/tuss4470_arduino.h b/software/development/libTUSS4470/tuss4470_arduino.h index eae4bb7..e7631d3 100644 --- a/software/development/libTUSS4470/tuss4470_arduino.h +++ b/software/development/libTUSS4470/tuss4470_arduino.h @@ -13,9 +13,60 @@ class TUSS4470 { ~TUSS4470(); int begin(); + int beginCustomSPI(tuss4470_spi_transfer_fptr spiTransfer_fptr, void *ctx); int readConfig(); int writeConfig(); + // region specific register access + int setBPF_HPFFreq(uint8_t freq); + int setBPF_Bypass(bool value); + int setBPF_FCTrimFrc(bool value); + + int setBPF_QSel(uint8_t qSel); + int setBPF_FCTrim(uint8_t fcTrim); + + int setDEV_LogAmpFrc(bool value); + int setDEV_LogAmpSlopeAdj(uint8_t value); + int setDEV_LogAmpIntAdj(uint8_t value); + + int setLogAmpDisableFirstStage(bool value); + int setLogAmpDisableLastStage(bool value); + int setVOUTScaling(bool value); + int setLNAGain(uint8_t gain); + + + int setDriverPulseFaultDeglitchTime(uint8_t time); + int setLowVoltageIOConfig(uint8_t config); + + + int setDisableVDRVRegulationInListenMode(bool value); + int setVDRVHighImpedance(bool value); + int setVDRVCurrentLevel(bool value); + int setVDRVVoltageLevel(uint8_t level); + + + int setEchoInterruptComparatorEnable(bool value); + int setEchoInterruptThreshold(uint8_t threshold); + + int setZeroCrossComparatorEnable(bool value); + int setZeroCrossEnableEchoInterrupt(bool value); + int setZeroComparatorInputSelect(bool value); + int setZeroCrossComparatorStageSelect(uint8_t stage); + int setZeroCrossComparatorHysteresis(uint8_t hysteresis); + + int setHalfBridgeMode(bool value); + int setPreDriverMode(bool value); + int setBurstPulseNumber(uint8_t pulseCount); + + int setSleepModeEnable(bool value); + int setStandbyModeEnable(bool value); + int setVDRVTriggerControl(bool value); + int setCommandTriggerControl(bool value); + + // endregion + + + int readRawRegister(tuss4470_register_map_t reg, uint8_t *data); int writeRawRegister(tuss4470_register_map_t reg, uint8_t data); From 64b95276035c40c22533b5ec1a055638ac180b34 Mon Sep 17 00:00:00 2001 From: javerik Date: Mon, 27 Jan 2025 18:38:46 +0100 Subject: [PATCH 5/5] Added getter functions for all register in cpp wrapper --- .../development/libTUSS4470/libTUSS4470.ino | 8 + .../libTUSS4470/tuss4470_arduino.cpp | 256 ++++++++++++++++++ .../libTUSS4470/tuss4470_arduino.h | 44 +++ 3 files changed, 308 insertions(+) diff --git a/software/development/libTUSS4470/libTUSS4470.ino b/software/development/libTUSS4470/libTUSS4470.ino index f39118f..2e1d58c 100644 --- a/software/development/libTUSS4470/libTUSS4470.ino +++ b/software/development/libTUSS4470/libTUSS4470.ino @@ -47,6 +47,14 @@ void setup() { return; } Serial.println(F("Successfully set parameter HPFFreq to 0x1D")); + + // Read the register again to verify the value + uint8_t data = tuss.getBPF_HPFFreq(); + Serial.print(F("Read back HPFFreq: 0x")); + Serial.println(data, HEX); + if (data != 0x1D) { + Serial.println(F("Error: Read back value does not match written value")); + } } void loop() { diff --git a/software/development/libTUSS4470/tuss4470_arduino.cpp b/software/development/libTUSS4470/tuss4470_arduino.cpp index ec6dc22..b73d774 100644 --- a/software/development/libTUSS4470/tuss4470_arduino.cpp +++ b/software/development/libTUSS4470/tuss4470_arduino.cpp @@ -269,6 +269,262 @@ int TUSS4470::setCommandTriggerControl(bool value) { return tuss4470_write_register(&tuss4470, TOF_CONFIG, tuss4470.config->TOF_CONFIG); } +uint8_t TUSS4470::getBPF_HPFFreq() { + int err = tuss4470_read_register(&tuss4470, BPF_CONFIG_1, &tuss4470.config->BPF_CONFIG_1); + if (err) { + return 0; + } + return getConfig(tuss4470.config->BPF_CONFIG_1, BPF_HPF_FREQ); +} + +bool TUSS4470::getBPF_Bypass() { + int err = tuss4470_read_register(&tuss4470, BPF_CONFIG_1, &tuss4470.config->BPF_CONFIG_1); + if (err) { + return false; + } + return getConfig(tuss4470.config->BPF_CONFIG_1, BPF_BYPASS); +} + +bool TUSS4470::getBPF_FCTrimFrc() { + int err = tuss4470_read_register(&tuss4470, BPF_CONFIG_1, &tuss4470.config->BPF_CONFIG_1); + if (err) { + return false; + } + return getConfig(tuss4470.config->BPF_CONFIG_1, BPF_FC_TRIM_FRC); +} + +uint8_t TUSS4470::getBPF_QSel() { + int err = tuss4470_read_register(&tuss4470, BPF_CONFIG_2, &tuss4470.config->BPF_CONFIG_2); + if (err) { + return 0; + } + return getConfig(tuss4470.config->BPF_CONFIG_2, tuss4470_BPF_Q_SEL); +} + +uint8_t TUSS4470::getBPF_FCTrim() { + int err = tuss4470_read_register(&tuss4470, BPF_CONFIG_2, &tuss4470.config->BPF_CONFIG_2); + if (err) { + return 0; + } + return getConfig(tuss4470.config->BPF_CONFIG_2, tuss4470_BPF_FC_TRIM); +} + +bool TUSS4470::getDEV_LogAmpFrc() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_1, &tuss4470.config->DEV_CTRL_1); + if (err) { + return false; + } + return getConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_FRC); +} + +uint8_t TUSS4470::getDEV_LogAmpSlopeAdj() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_1, &tuss4470.config->DEV_CTRL_1); + if (err) { + return 0; + } + return getConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_SLOPE_ADJ); +} + +uint8_t TUSS4470::getDEV_LogAmpIntAdj() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_1, &tuss4470.config->DEV_CTRL_1); + if (err) { + return 0; + } + return getConfig(tuss4470.config->DEV_CTRL_1, tuss4470_LOGAMP_INT_ADJ); +} + +bool TUSS4470::getLogAmpDisableFirstStage() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_2, &tuss4470.config->DEV_CTRL_2); + if (err) { + return false; + } + return getConfig(tuss4470.config->DEV_CTRL_2, tuss4470_LOGAMP_DIS_FIRST); +} + +bool TUSS4470::getLogAmpDisableLastStage() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_2, &tuss4470.config->DEV_CTRL_2); + if (err) { + return false; + } + return getConfig(tuss4470.config->DEV_CTRL_2, tuss4470_LOGAMP_DIS_LAST); +} + +bool TUSS4470::getVOUTScaling() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_2, &tuss4470.config->DEV_CTRL_2); + if (err) { + return false; + } + return getConfig(tuss4470.config->DEV_CTRL_2, tuss4470_VOUT_SCALE_SEL); +} + +uint8_t TUSS4470::getLNAGain() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_3, &tuss4470.config->DEV_CTRL_3); + if (err) { + return 0; + } + return getConfig(tuss4470.config->DEV_CTRL_3, tuss4470_LNA_GAIN); +} + +uint8_t TUSS4470::getDriverPulseFaultDeglitchTime() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_3, &tuss4470.config->DEV_CTRL_3); + if (err) { + return 0; + } + return getConfig(tuss4470.config->DEV_CTRL_3, tuss4470_DRV_PLS_FLT_DT); +} + +uint8_t TUSS4470::getLowVoltageIOConfig() { + int err = tuss4470_read_register(&tuss4470, DEV_CTRL_3, &tuss4470.config->DEV_CTRL_3); + if (err) { + return 0; + } + return getConfig(tuss4470.config->DEV_CTRL_3, tuss4470_IO_MODE); +} + +bool TUSS4470::getDisableVDRVRegulationInListenMode() { + int err = tuss4470_read_register(&tuss4470, VDRV_CTRL, &tuss4470.config->VDRV_CTRL); + if (err) { + return false; + } + return getConfig(tuss4470.config->VDRV_CTRL, tuss4470_DIS_VDRV_REG_LSTN); +} + +bool TUSS4470::getVDRVHighImpedance() { + int err = tuss4470_read_register(&tuss4470, VDRV_CTRL, &tuss4470.config->VDRV_CTRL); + if (err) { + return false; + } + return getConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_HI_Z); +} + +bool TUSS4470::getVDRVCurrentLevel() { + int err = tuss4470_read_register(&tuss4470, VDRV_CTRL, &tuss4470.config->VDRV_CTRL); + if (err) { + return false; + } + return getConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_CURRENT_LEVEL); +} + +uint8_t TUSS4470::getVDRVVoltageLevel() { + int err = tuss4470_read_register(&tuss4470, VDRV_CTRL, &tuss4470.config->VDRV_CTRL); + if (err) { + return 0; + } + return getConfig(tuss4470.config->VDRV_CTRL, tuss4470_VDRV_VOLTAGE_LEVEL); +} + +bool TUSS4470::getEchoInterruptComparatorEnable() { + int err = tuss4470_read_register(&tuss4470, ECHO_INT_CONFIG, &tuss4470.config->ECHO_INT_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->ECHO_INT_CONFIG, tuss4470_ECHO_INT_CMP_EN); +} + +uint8_t TUSS4470::getEchoInterruptThreshold() { + int err = tuss4470_read_register(&tuss4470, ECHO_INT_CONFIG, &tuss4470.config->ECHO_INT_CONFIG); + if (err) { + return 0; + } + return getConfig(tuss4470.config->ECHO_INT_CONFIG, tuss4470_ECHO_INT_THR_SEL); +} + +bool TUSS4470::getZeroCrossComparatorEnable() { + int err = tuss4470_read_register(&tuss4470, ZC_CONFIG, &tuss4470.config->ZC_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_EN); +} + +bool TUSS4470::getZeroCrossEnableEchoInterrupt() { + int err = tuss4470_read_register(&tuss4470, ZC_CONFIG, &tuss4470.config->ZC_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_EN_ECHO_INT); +} + +bool TUSS4470::getZeroComparatorInputSelect() { + int err = tuss4470_read_register(&tuss4470, ZC_CONFIG, &tuss4470.config->ZC_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_IN_SEL); +} + +uint8_t TUSS4470::getZeroCrossComparatorStageSelect() { + int err = tuss4470_read_register(&tuss4470, ZC_CONFIG, &tuss4470.config->ZC_CONFIG); + if (err) { + return 0; + } + return getConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_STG_SEL); +} + +uint8_t TUSS4470::getZeroCrossComparatorHysteresis() { + int err = tuss4470_read_register(&tuss4470, ZC_CONFIG, &tuss4470.config->ZC_CONFIG); + if (err) { + return 0; + } + return getConfig(tuss4470.config->ZC_CONFIG, tuss4470_ZC_CMP_HYST); +} + +bool TUSS4470::getHalfBridgeMode() { + int err = tuss4470_read_register(&tuss4470, BURST_PULSE, &tuss4470.config->BURST_PULSE); + if (err) { + return false; + } + return getConfig(tuss4470.config->BURST_PULSE, tuss4470_HALF_BRG_MODE); +} + +bool TUSS4470::getPreDriverMode() { + int err = tuss4470_read_register(&tuss4470, BURST_PULSE, &tuss4470.config->BURST_PULSE); + if (err) { + return false; + } + return getConfig(tuss4470.config->BURST_PULSE, tuss4470_PRE_DRIVER_MODE); +} + +uint8_t TUSS4470::getBurstPulseNumber() { + int err = tuss4470_read_register(&tuss4470, BURST_PULSE, &tuss4470.config->BURST_PULSE); + if (err) { + return 0; + } + return getConfig(tuss4470.config->BURST_PULSE, tuss4470_BURST_PULSE); +} + +bool TUSS4470::getSleepModeEnable() { + int err = tuss4470_read_register(&tuss4470, TOF_CONFIG, &tuss4470.config->TOF_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->TOF_CONFIG, tuss4470_SLEEP_MODE_EN); +} + +bool TUSS4470::getStandbyModeEnable() { + int err = tuss4470_read_register(&tuss4470, TOF_CONFIG, &tuss4470.config->TOF_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->TOF_CONFIG, tuss4470_STDBY_MODE_EN); +} + +bool TUSS4470::getVDRVTriggerControl() { + int err = tuss4470_read_register(&tuss4470, TOF_CONFIG, &tuss4470.config->TOF_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->TOF_CONFIG, tuss4470_VDRV_TRIGGER); +} + +bool TUSS4470::getCommandTriggerControl() { + int err = tuss4470_read_register(&tuss4470, TOF_CONFIG, &tuss4470.config->TOF_CONFIG); + if (err) { + return false; + } + return getConfig(tuss4470.config->TOF_CONFIG, tuss4470_CMD_TRIGGER); +} + // endregion diff --git a/software/development/libTUSS4470/tuss4470_arduino.h b/software/development/libTUSS4470/tuss4470_arduino.h index e7631d3..b78484a 100644 --- a/software/development/libTUSS4470/tuss4470_arduino.h +++ b/software/development/libTUSS4470/tuss4470_arduino.h @@ -63,6 +63,50 @@ class TUSS4470 { int setVDRVTriggerControl(bool value); int setCommandTriggerControl(bool value); + + uint8_t getBPF_HPFFreq(); + bool getBPF_Bypass(); + bool getBPF_FCTrimFrc(); + + uint8_t getBPF_QSel(); + uint8_t getBPF_FCTrim(); + + bool getDEV_LogAmpFrc(); + uint8_t getDEV_LogAmpSlopeAdj(); + uint8_t getDEV_LogAmpIntAdj(); + + bool getLogAmpDisableFirstStage(); + bool getLogAmpDisableLastStage(); + bool getVOUTScaling(); + uint8_t getLNAGain(); + + uint8_t getDriverPulseFaultDeglitchTime(); + uint8_t getLowVoltageIOConfig(); + + bool getDisableVDRVRegulationInListenMode(); + bool getVDRVHighImpedance(); + bool getVDRVCurrentLevel(); + uint8_t getVDRVVoltageLevel(); + + bool getEchoInterruptComparatorEnable(); + uint8_t getEchoInterruptThreshold(); + + bool getZeroCrossComparatorEnable(); + bool getZeroCrossEnableEchoInterrupt(); + bool getZeroComparatorInputSelect(); + uint8_t getZeroCrossComparatorStageSelect(); + uint8_t getZeroCrossComparatorHysteresis(); + + bool getHalfBridgeMode(); + bool getPreDriverMode(); + uint8_t getBurstPulseNumber(); + + bool getSleepModeEnable(); + bool getStandbyModeEnable(); + bool getVDRVTriggerControl(); + bool getCommandTriggerControl(); + + // endregion