From d896b3864cc2ef2a1231ae925f1567eb8e3ade10 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Wed, 29 May 2024 20:35:25 -0400 Subject: [PATCH] misc fixes --- general/include/pca9539.h | 24 +++++++++++------------ general/src/pca9539.c | 40 ++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index af237f9..28fceea 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -26,14 +26,14 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca /// POLARITY: Inversion state, 1=Inverted 0=Uninverted /// DIRECTION: Input/Output selection 1=Input 0=Output -#define PCA_INPUT_0 0x00 -#define PCA_INPUT_1 0x01 -#define PCA_OUTPUT_0 0x02 -#define PCA_OUTPUT_1 0x03 -#define PCA_POLARITY_0 0x04 -#define PCA_POLARITY_1 0x05 -#define PCA_DIRECTION_0 0x06 -#define PCA_DIRECTION_1 0x07 +#define PCA_INPUT_0_REG 0x00 +#define PCA_INPUT_1_REG 0x01 +#define PCA_OUTPUT_0_REG 0x02 +#define PCA_OUTPUT_1_REG 0x03 +#define PCA_POLARITY_0_REG 0x04 +#define PCA_POLARITY_1_REG 0x05 +#define PCA_DIRECTION_0_REG 0x06 +#define PCA_DIRECTION_1_REG 0x07 typedef struct { @@ -46,16 +46,16 @@ void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, uint8_t dev_add /// @brief Read all pins on a bus, for example using reg_type input to get incoming logic level HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t config); + uint8_t buf); /// @brief Read a specific pin on a bus, do not iterate over this, use read_pins instead HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, - uint8_t pin, uint8_t *config); + uint8_t pin, uint8_t *buf); /// @brief Write all pins on a bus, for example using reg_type OUTPUT to set logic level or DIRECTION to set as /// output -HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t config); +HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); /// @brief Write a specific pin on a bus, do not iterate over this, use write_pins instead HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, - uint8_t config); + uint8_t buf); #endif \ No newline at end of file diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 3a21578..e85ab88 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -1,69 +1,71 @@ #include "pca9539.h" #define REG_SIZE_BITS 8 -#define TIMEOUT HAL_MAX_DELAY -HAL_StatusTypeDef pca_write_reg(pca9539_t* pca, uint16_t address, uint8_t* data) +HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) { // ensure shifting left one, HAL adds the write bit return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, - data, sizeof(data), TIMEOUT); + data, sizeof(data), HAL_MAX_DELAY); } -HAL_StatusTypeDef pca_read_reg(pca9539_t* pca, uint16_t address, uint8_t* data) +HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) { return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, - data, sizeof(data), TIMEOUT); + data, sizeof(data), HAL_MAX_DELAY); } -void pca9539_init(pca9539_t* pca, I2C_HandleTypeDef* i2c_handle, uint8_t dev_addr) +void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr) { pca->i2c_handle = i2c_handle; - pca->dev_addr = dev_addr << 1u; /* shifted one to the left cuz STM says so */ + pca->dev_addr = dev_addr << 1u; /* shifted one to the left cuz STM says so */ } -HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t config) +HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) { - HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &config); - if (status) { + HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &buf); + if (status) + { return status; } return status; } -HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t* config) +HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, uint8_t *buf) { uint8_t data; HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); - if (status) { + if (status) + { return status; } - *config = (data & (1 << pin)) > 0; + *buf = (data & (1 << pin)) > 0; return status; } -HAL_StatusTypeDef pca9539_write_reg(pca9539_t* pca, uint8_t reg_type, uint8_t config) +HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) { - return pca_write_reg(pca, reg_type, &config); + return pca_write_reg(pca, reg_type, &buf); } -HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t config) +HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, uint8_t buf) { uint8_t data; - HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); - if (status) { + HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &buf); + if (status) + { return status; } - uint8_t data_new = (data & ~(1u << pin)) | (config << pin); + uint8_t data_new = (data & ~(1 << pin)) | (buf << pin); return pca_write_reg(pca, reg_type, &data_new); }