Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed May 30, 2024
1 parent 34e9d7a commit d896b38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
24 changes: 12 additions & 12 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
40 changes: 21 additions & 19 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit d896b38

Please sign in to comment.