Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pca #179

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,48 @@ PCA 9539 16 bit GPIO expander. Datasheet:
/// POLARITY: Inversion state, 1=Inverted 0=Uninverted
/// DIRECTION: Input/Output selection 1=Input 0=Output

#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_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

//Function Pointer Initializiation, read/write functions will get assigned to these
typedef int(*WritePtr)( uint16_t address, uint8_t reg_type,
uint8_t data );
typedef int(*ReadPtr)( uint16_t address, uint8_t reg_type,
uint8_t data );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick but can we change names:

I2c write function ptr --> Write_Ptr
read func ptr ----> Read_Ptr

then the local versions from "local..." to just "read" and "write"



typedef struct {
I2C_HandleTypeDef *i2c_handle;
uint16_t dev_addr;

uint8_t dev_addr;

// Two Function Pointers
WritePtr write;
ReadPtr read;

} pca9539_t;

/// Init PCA9539, a 16 bit I2C GPIO expander
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle,
uint8_t dev_addr);

/// @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 *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 *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 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 buf);

//Includes use of Function Pointers with instance writeFunc and readFunc

void pca9539_init(pca9539_t *pca, WritePtr writeFunc,
ReadPtr readFunc, uint8_t dev_addr);


int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf);

int pca9539_write_reg(pca9539_t *pca, uint16_t reg_type, uint8_t buf);


int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t buf);

int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t *buf);

#endif
85 changes: 36 additions & 49 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,55 @@

#define REG_SIZE_BITS 8

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, 1, HAL_MAX_DELAY);
}

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, 1, HAL_MAX_DELAY);
}

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 */
void pca9539_init(pca9539_t *pca, WritePtr writeFunc,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i2c Handle is STM HAL, so that should be removed here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the i2c_handle member of the pca type can be removed too

ReadPtr readFunc, uint8_t dev_addr)
{
pca->dev_addr = dev_addr << 1u;
}

HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type,
uint8_t *buf) {

HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, buf);
if (status) {
return status;
}

return status;
int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf)
{
return pca->read(pca->dev_addr, reg_type, *buf);
}

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) {
return status;
}

*buf = (data & (1 << pin)) > 0;

return status;
int pca9539_write_reg(pca9539_t *pca, uint16_t reg_type, uint8_t buf)
{
return pca->write(pca->dev_addr, reg_type, buf);
}

HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type,
uint8_t buf) {
//Errors in write_pin and read_pin functions if the commented code left in
//Variables within function are unused
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logic of this function has been changed, hence the errors. Take a look at the diff - the old code first read the state of the pin with a read call, then set data_new to the opposite state, and then wrote that back to the device. If you undo changes to that logic and keep it the same as it was u shoudlnt get any errors

int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t buf)
{
//uint8_t data;
//uint8_t data_new;


return pca_write_reg(pca, reg_type, &buf);
}
int status = pca->write(pca->dev_addr, reg_type, buf);
if (status) {
return status;
}

HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type,
uint8_t pin, uint8_t buf) {
//data_new = (data & ~(1u << pin)) | (buf << pin);

uint8_t data;
uint8_t data_new;
return pca->write(pca->dev_addr, reg_type, buf);
}

HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data);
if (status) {
return status;
}

data_new = (data & ~(1u << pin)) | (buf << pin);
int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t *buf)
{
//uint8_t data;

return pca_write_reg(pca, reg_type, &data_new);
int status = pca->read(pca->dev_addr, reg_type, *buf);
if (status) {
return status;
}
//*buf = (data & (1 << pin)) > 0;

return pca->read(pca->dev_addr, reg_type, *buf);
}
Loading