-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
33 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |