From 28d61cd0275438663d3bbd36e5e374a4751f3939 Mon Sep 17 00:00:00 2001 From: tdb3 <106488469+tdb3@users.noreply.github.com> Date: Mon, 27 May 2024 11:43:29 -0400 Subject: [PATCH] refactor: deduplicate i2c parameters Pulls common i2c macros (e.g. GPIO pins, speed, etc.) into a common header, to deduplicate and increase maintainability. Also adds missing include for DS4432U.h in DS4432U.c. --- main/DS4432U.c | 10 ++-------- main/DS4432U.h | 4 +++- main/EMC2101.c | 9 --------- main/EMC2101.h | 4 +++- main/INA260.c | 10 +--------- main/INA260.h | 4 +++- main/i2c_params.h | 12 ++++++++++++ 7 files changed, 24 insertions(+), 29 deletions(-) create mode 100644 main/i2c_params.h diff --git a/main/DS4432U.c b/main/DS4432U.c index 23232f04..8432e57c 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -3,13 +3,7 @@ #include "esp_log.h" #include "driver/i2c.h" -#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ -#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ -#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ -#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ -#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_TIMEOUT_MS 1000 +#include "DS4432U.h" // DS4432U+ -- Adjustable current DAC for use with the TPS40305 voltage regulator // address: 0x90 @@ -142,4 +136,4 @@ bool DS4432U_set_vcore(float core_voltage) DS4432U_set(reg_setting); /// eek! return true; -} \ No newline at end of file +} diff --git a/main/DS4432U.h b/main/DS4432U.h index 668ed3dd..4f682b45 100644 --- a/main/DS4432U.h +++ b/main/DS4432U.h @@ -3,10 +3,12 @@ #include "driver/i2c.h" +#include "i2c_params.h" + esp_err_t i2c_master_init(void); esp_err_t i2c_master_delete(void); void DS4432U_read(void); bool DS4432U_test(void); bool DS4432U_set_vcore(float); -#endif /* DS4432U_H_ */ \ No newline at end of file +#endif /* DS4432U_H_ */ diff --git a/main/EMC2101.c b/main/EMC2101.c index 3aabf99c..620d0529 100644 --- a/main/EMC2101.c +++ b/main/EMC2101.c @@ -4,15 +4,6 @@ #include "EMC2101.h" -#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ -#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ -#define I2C_MASTER_NUM \ - 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ -#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ -#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_TIMEOUT_MS 1000 - // static const char *TAG = "EMC2101.c"; /** diff --git a/main/EMC2101.h b/main/EMC2101.h index bc962351..c15b855e 100644 --- a/main/EMC2101.h +++ b/main/EMC2101.h @@ -1,6 +1,8 @@ #ifndef EMC2101_H_ #define EMC2101_H_ +#include "i2c_params.h" + #define EMC2101_I2CADDR_DEFAULT 0x4C ///< EMC2101 default i2c address #define EMC2101_CHIP_ID 0x16 ///< EMC2101 default device id from part id #define EMC2101_ALT_CHIP_ID 0x28 ///< EMC2101 alternate device id from part id @@ -87,4 +89,4 @@ uint16_t EMC2101_get_fan_speed(void); void EMC2101_init(bool); float EMC2101_get_external_temp(void); uint8_t EMC2101_get_internal_temp(void); -#endif /* EMC2101_H_ */ \ No newline at end of file +#endif /* EMC2101_H_ */ diff --git a/main/INA260.c b/main/INA260.c index 68d22326..22bd4ddf 100644 --- a/main/INA260.c +++ b/main/INA260.c @@ -4,14 +4,6 @@ #include "INA260.h" -#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ -#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ -#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ -#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ -#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_TIMEOUT_MS 1000 - // static const char *TAG = "INA260.c"; /** @@ -67,4 +59,4 @@ float INA260_read_power(void) // ESP_LOGI(TAG, "Raw Power = %02X %02X", data[1], data[0]); return (data[1] | (data[0] << 8)) * 10; -} \ No newline at end of file +} diff --git a/main/INA260.h b/main/INA260.h index 574164d3..45cba1d9 100644 --- a/main/INA260.h +++ b/main/INA260.h @@ -1,6 +1,8 @@ #ifndef INA260_H_ #define INA260_H_ +#include "i2c_params.h" + #define INA260_I2CADDR_DEFAULT 0x40 ///< INA260 default i2c address #define INA260_REG_CONFIG 0x00 ///< Configuration register #define INA260_REG_CURRENT 0x01 ///< Current measurement register (signed) in mA @@ -107,4 +109,4 @@ float INA260_read_current(void); float INA260_read_voltage(void); float INA260_read_power(void); -#endif /* INA260_H_ */ \ No newline at end of file +#endif /* INA260_H_ */ diff --git a/main/i2c_params.h b/main/i2c_params.h new file mode 100644 index 00000000..3237213c --- /dev/null +++ b/main/i2c_params.h @@ -0,0 +1,12 @@ +#ifndef I2C_PARAMS_H_ +#define I2C_PARAMS_H_ + +#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ +#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ +#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ +#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ +#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ +#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ +#define I2C_MASTER_TIMEOUT_MS 1000 + +#endif /* I2C_PARAMS_H_ */