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 4 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
79 changes: 53 additions & 26 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,67 @@ 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(*I2C_WriteFuncPtr)( uint16_t address, uint8_t reg_type,
uint8_t data );
typedef int(*I2C_ReadFuncPtr)( 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 void(*I2C_ReadPinFuncPtr){uint16_t };
//typedef void(*I2C_WritePinFuncPtr){};

typedef struct {
I2C_HandleTypeDef *i2c_handle;
uint16_t dev_addr;
I2C_HandleTypeDef *i2c_handle;
uint16_t dev_addr;

// Two Function Pointers
I2C_WriteFuncPtr local_I2C_Write;
I2C_ReadFuncPtr local_I2C_Read;
//I2C_ReadPinFuncPtr local_I2C_Read_Pin;
//I2C_WritePinFuncPtr local_I2C_Write_Pin;

} pca9539_t;

/// Init PCA9539, a 16 bit I2C GPIO expander
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle,
uint8_t dev_addr);
//Includes use of Function Pointers with instance writeFunc and readFunc
void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc,
I2C_ReadFuncPtr readFunc, I2C_HandleTypeDef *i2c_handle,
uint8_t dev_addr);

//READ/WRITE
//Don't need struct pointer from initialization?
//***The pointers for these functions will be general pca_read_reg, general pca_write_reg which are linked to HAL functions
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);

/// @brief Read all pins on a bus, for example using reg_type input to get
/// incoming logic level
/*
/// @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);
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);
*/

//PIN WRITE/READ

/// @brief Write a specific pin on a bus, do not iterate over this, use write_pins instead
//HAL_StatusTypeDef
int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t buf);
/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins instead
//HAL_StatusTypeDef
int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t *buf);

#endif
104 changes: 97 additions & 7 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@

#define REG_SIZE_BITS 8


void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc,
I2C_ReadFuncPtr readFunc, I2C_HandleTypeDef *i2c_handle,
uint8_t dev_addr)
{
pca->i2c_handle = i2c_handle;
pca->dev_addr = dev_addr << 1u;
}
/*General PCA Functions
int pca_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t *buf){
return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1,
HAL_MAX_DELAY);
}*/
/*
int pca_write_reg(pca9539_t pca, uint8_t reg_type, uint8_t buf){
return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1,
HAL_MAX_DELAY);
}*/
//Original PCA Functions
/*
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)
{

HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address,
uint8_t *data) {
// ensure shifting left one, HAL adds the write bit
Expand All @@ -14,25 +45,83 @@ HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address,
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);
}
*/


/*
Original PCA Constructor
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
//}
*/
//FILL IN THESE
int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf)
{
return pca->local_I2C_Read(pca->dev_addr, reg_type, *buf);
}

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

int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t buf)
{
//uint8_t data; -> where is this used?


int status = pca->local_I2C_Write(pca->dev_addr, reg_type, buf);
if (status) {
return status;
}
//uint8_t data_new;
//uint8_t data;
//data_new = (data & ~(1u << pin)) | (buf << pin);

return pca->local_I2C_Write(pca->dev_addr, reg_type, buf);
}
int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin,
uint8_t *buf)
{
//uint8_t data;

int status = pca->local_I2C_Read(pca->dev_addr, reg_type, *buf);
if (status) {
return status;
}
//*buf = (data & (1 << pin)) > 0; What to do with this?
Copy link
Contributor

Choose a reason for hiding this comment

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

i dont know without looking much what this does, but changing exisint stuff outside of HAL related info is not needed, so you can keep this logic the same as it was initially


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

//END OF NEW CODE
/*
HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* buf)
{

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 *buf) {
/*
HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

need to remove HAL_Status_TypeDef to make this agnostic - those are just enums to ints so can replace with int


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

return status;

return status;
//AKA return pca_read_reg(pca, reg_type, buf);
}

HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type,
Expand Down Expand Up @@ -67,5 +156,6 @@ HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type,

data_new = (data & ~(1u << pin)) | (buf << pin);

return pca_write_reg(pca, reg_type, &data_new);
}

return pca_write_reg(pca, reg_type, &data_new);
}*/
Loading