From 4f055626a969810cba542b7a992b9ba9d58dc299 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Thu, 3 Oct 2024 19:24:20 -0400 Subject: [PATCH 1/9] func pointer update --- general/include/pca9539.h | 59 +++++++++++++++++++++++-------- general/src/pca9539.c | 74 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 18 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 89b2c4f..8b8914a 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -26,36 +26,67 @@ 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_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 -typedef struct -{ +//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 }; +//typedef void(*I2C_ReadPinFuncPtr){uint16_t }; +//typedef void(*I2C_WritePinFuncPtr){}; + +typedef struct { 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 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); +*/ + +//PIN WRITE/READ + /// @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); +//HAL_StatusTypeDef +int pca9539_write_pin(pca9539_t *pca, uint8_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, uint8_t reg_type, uint8_t pin, + uint8_t *buf); #endif \ No newline at end of file diff --git a/general/src/pca9539.c b/general/src/pca9539.c index f1414c0..d97bb4b 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -4,26 +4,91 @@ #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_affr << 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) { 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, uint8_t reg_type, uint8_t *buf) +{ + return pca->read(pca, reg_type, buf); } +int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) +{ + return pca->write(pca, reg_type, &buf); +} + +int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t buf) +{ + uint8_t data; + uint8_t data_new; + + //HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); + //if (status) { + // return status; + //} + int status = pca->write(pca, reg_type, &data); + if (status) { + return status; + } + + data_new = (data & ~(1u << pin)) | (buf << pin); + + return pca->write(pca, reg_type, &data); +} +int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, + uint8_t *buf) +{ + uint8_t data; + + int status = pca->read(pca, reg_type, &data); + if (status) { + return status; + } + //*buf = (data & (1 << pin)) > 0; What to do with this? + + return pca->read(pca, reg_type, &data); +} + +//END OF NEW CODE +/* HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* buf) { @@ -33,6 +98,7 @@ HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* bu } return status; + //AKA return pca_read_reg(pca, reg_type, buf); } HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pin, uint8_t* buf) @@ -68,4 +134,4 @@ HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, uint8_t reg_type, uint8_t pi data_new = (data & ~(1u << pin)) | (buf << pin); return pca_write_reg(pca, reg_type, &data_new); -} +}*/ From a99ed854a130d1bbda86e06596a800a98cda0f71 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Sun, 6 Oct 2024 21:36:31 -0400 Subject: [PATCH 2/9] Your commit message describing the changes --- general/include/pca9539.h | 12 ++++----- general/src/pca9539.c | 55 ++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 8b8914a..47d577d 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -36,10 +36,10 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca #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 }; +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 ); //typedef void(*I2C_ReadPinFuncPtr){uint16_t }; //typedef void(*I2C_WritePinFuncPtr){}; @@ -82,11 +82,11 @@ HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t bu /// @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, uint8_t reg_type, uint8_t pin, +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, uint8_t reg_type, uint8_t pin, +int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, uint8_t *buf); #endif \ No newline at end of file diff --git a/general/src/pca9539.c b/general/src/pca9539.c index d97bb4b..853ff34 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -9,82 +9,83 @@ void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, uint8_t dev_addr) { pca->i2c_handle = i2c_handle; - pca->dev_addr = dev_affr << 1u; + 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) { return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); -}*/ +} +*/ -/*Original PCA Constructor +/* +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 */ + 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, uint8_t reg_type, uint8_t *buf) +int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf) { - return pca->read(pca, reg_type, buf); + return pca->local_I2C_Read(pca->dev_addr, reg_type, *buf); } -int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf) +int pca9539_write_reg(pca9539_t *pca, uint16_t reg_type, uint8_t buf) { - return pca->write(pca, reg_type, &buf); + return pca->local_I2C_Write(pca->dev_addr, reg_type, buf); } -int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, +int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, uint8_t buf) { - uint8_t data; - uint8_t data_new; + //uint8_t data; -> where is this used? + - //HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); - //if (status) { - // return status; - //} - int status = pca->write(pca, reg_type, &data); + 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); - data_new = (data & ~(1u << pin)) | (buf << pin); - - return pca->write(pca, reg_type, &data); + return pca->local_I2C_Write(pca->dev_addr, reg_type, buf); } -int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, +int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, uint8_t *buf) { - uint8_t data; + //uint8_t data; - int status = pca->read(pca, reg_type, &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? - return pca->read(pca, reg_type, &data); + return pca->local_I2C_Read(pca->dev_addr, reg_type, *buf); } //END OF NEW CODE From 2cd92d43474eabb7a76b168dc7e87f4887949bf9 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Wed, 9 Oct 2024 23:57:00 -0400 Subject: [PATCH 3/9] Resolved merge conflicts --- general/src/pca9539.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 708a306..ceea8a3 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -4,7 +4,7 @@ #define REG_SIZE_BITS 8 -<<<<<<< HEAD + void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, I2C_ReadFuncPtr readFunc, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr) @@ -109,12 +109,10 @@ HAL_StatusTypeDef pca9539_read_reg(pca9539_t* pca, uint8_t reg_type, uint8_t* bu 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 */ - - -HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t *buf) { + 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 status = pca_read_reg(pca, reg_type, buf); if (status) { @@ -160,4 +158,4 @@ HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, return pca_write_reg(pca, reg_type, &data_new); -} \ No newline at end of file +}*/ \ No newline at end of file From 60186eb360b992e42b8f787460888fc58f20f21e Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Tue, 15 Oct 2024 22:47:18 -0400 Subject: [PATCH 4/9] changed func pointer names and removed HAL references --- general/include/pca9539.h | 8 +-- general/src/pca9539.c | 106 -------------------------------------- 2 files changed, 4 insertions(+), 110 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 4f9e56c..e03ceca 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -37,9 +37,9 @@ PCA 9539 16 bit GPIO expander. Datasheet: #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, +typedef int(*I2C_WritePtr)( uint16_t address, uint8_t reg_type, uint8_t data ); -typedef int(*I2C_ReadFuncPtr)( uint16_t address, uint8_t reg_type, +typedef int(*I2C_ReadPtr)( uint16_t address, uint8_t reg_type, uint8_t data ); //typedef void(*I2C_ReadPinFuncPtr){uint16_t }; //typedef void(*I2C_WritePinFuncPtr){}; @@ -49,8 +49,8 @@ typedef struct { uint16_t dev_addr; // Two Function Pointers - I2C_WriteFuncPtr local_I2C_Write; - I2C_ReadFuncPtr local_I2C_Read; + I2C_WritePtr local_I2C_Write; + I2C_ReadPtr local_I2C_Read; //I2C_ReadPinFuncPtr local_I2C_Read_Pin; //I2C_WritePinFuncPtr local_I2C_Write_Pin; diff --git a/general/src/pca9539.c b/general/src/pca9539.c index ceea8a3..bb98934 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -12,55 +12,8 @@ void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, 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 - 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); -} -*/ -/* -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); @@ -100,62 +53,3 @@ int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, 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 */ - -/* -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; - //AKA return pca_read_reg(pca, 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; -} - -HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, - uint8_t buf) { - - 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 buf) { - - uint8_t data; - uint8_t data_new; - - HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, &data); - if (status) { - return status; - } - - data_new = (data & ~(1u << pin)) | (buf << pin); - - - return pca_write_reg(pca, reg_type, &data_new); -}*/ \ No newline at end of file From c3d041e853adb1ab0b14213218a28dc7db82e096 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Tue, 15 Oct 2024 22:49:42 -0400 Subject: [PATCH 5/9] changed names from local --- general/include/pca9539.h | 4 ++-- general/src/pca9539.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index e03ceca..6d73a95 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -49,8 +49,8 @@ typedef struct { uint16_t dev_addr; // Two Function Pointers - I2C_WritePtr local_I2C_Write; - I2C_ReadPtr local_I2C_Read; + I2C_WritePtr I2C_Write; + I2C_ReadPtr I2C_Read; //I2C_ReadPinFuncPtr local_I2C_Read_Pin; //I2C_WritePinFuncPtr local_I2C_Write_Pin; diff --git a/general/src/pca9539.c b/general/src/pca9539.c index bb98934..28f3b01 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -16,12 +16,12 @@ void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, 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); + return pca->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); + return pca->I2C_Write(pca->dev_addr, reg_type, buf); } int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, @@ -30,7 +30,7 @@ int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, //uint8_t data; -> where is this used? - int status = pca->local_I2C_Write(pca->dev_addr, reg_type, buf); + int status = pca->I2C_Write(pca->dev_addr, reg_type, buf); if (status) { return status; } @@ -38,18 +38,18 @@ int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, //uint8_t data; //data_new = (data & ~(1u << pin)) | (buf << pin); - return pca->local_I2C_Write(pca->dev_addr, reg_type, buf); + return pca->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); + int status = pca->I2C_Read(pca->dev_addr, reg_type, *buf); if (status) { return status; } //*buf = (data & (1 << pin)) > 0; What to do with this? - return pca->local_I2C_Read(pca->dev_addr, reg_type, *buf); + return pca->I2C_Read(pca->dev_addr, reg_type, *buf); } From aada8120577bbceec593737b35b602a62100c8a8 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Tue, 15 Oct 2024 22:57:26 -0400 Subject: [PATCH 6/9] changed names of func ptrs --- general/include/pca9539.h | 8 ++++---- general/src/pca9539.c | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 6d73a95..36962a1 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -49,8 +49,8 @@ typedef struct { uint16_t dev_addr; // Two Function Pointers - I2C_WritePtr I2C_Write; - I2C_ReadPtr I2C_Read; + WritePtr Write; + ReadPtr Read; //I2C_ReadPinFuncPtr local_I2C_Read_Pin; //I2C_WritePinFuncPtr local_I2C_Write_Pin; @@ -58,8 +58,8 @@ typedef struct { /// Init PCA9539, a 16 bit I2C GPIO expander //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, +void pca9539_init(pca9539_t *pca, WritePtr writeFunc, + ReadPtr readFunc, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr); //READ/WRITE diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 28f3b01..f8d1cf1 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -5,8 +5,8 @@ #define REG_SIZE_BITS 8 -void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, - I2C_ReadFuncPtr readFunc, I2C_HandleTypeDef *i2c_handle, +void pca9539_init(pca9539_t *pca, WritePtr writeFunc, + ReadPtr readFunc, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr) { pca->i2c_handle = i2c_handle; @@ -16,12 +16,12 @@ void pca9539_init(pca9539_t *pca, I2C_WriteFuncPtr writeFunc, int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf) { - return pca->I2C_Read(pca->dev_addr, reg_type, *buf); + return pca->Read(pca->dev_addr, reg_type, *buf); } int pca9539_write_reg(pca9539_t *pca, uint16_t reg_type, uint8_t buf) { - return pca->I2C_Write(pca->dev_addr, reg_type, buf); + return pca->Write(pca->dev_addr, reg_type, buf); } int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, @@ -30,7 +30,7 @@ int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, //uint8_t data; -> where is this used? - int status = pca->I2C_Write(pca->dev_addr, reg_type, buf); + int status = pca->Write(pca->dev_addr, reg_type, buf); if (status) { return status; } @@ -38,18 +38,18 @@ int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, //uint8_t data; //data_new = (data & ~(1u << pin)) | (buf << pin); - return pca->I2C_Write(pca->dev_addr, reg_type, buf); + return pca->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->I2C_Read(pca->dev_addr, reg_type, *buf); + int status = pca->Read(pca->dev_addr, reg_type, *buf); if (status) { return status; } //*buf = (data & (1 << pin)) > 0; What to do with this? - return pca->I2C_Read(pca->dev_addr, reg_type, *buf); + return pca->Read(pca->dev_addr, reg_type, *buf); } From f81659a0fc1c1232ad0f7f1d2e5c0dc270c163d7 Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Tue, 15 Oct 2024 22:59:23 -0400 Subject: [PATCH 7/9] changed names of ptrs --- general/include/pca9539.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 36962a1..6a422d8 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -37,9 +37,9 @@ PCA 9539 16 bit GPIO expander. Datasheet: #define PCA_DIRECTION_1_REG 0x07 //Function Pointer Initializiation, read/write functions will get assigned to these -typedef int(*I2C_WritePtr)( uint16_t address, uint8_t reg_type, +typedef int(*WritePtr)( uint16_t address, uint8_t reg_type, uint8_t data ); -typedef int(*I2C_ReadPtr)( uint16_t address, uint8_t reg_type, +typedef int(*ReadPtr)( uint16_t address, uint8_t reg_type, uint8_t data ); //typedef void(*I2C_ReadPinFuncPtr){uint16_t }; //typedef void(*I2C_WritePinFuncPtr){}; From f86e6b599c3f960420aad7c78179520950b6582a Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Thu, 17 Oct 2024 14:19:04 -0400 Subject: [PATCH 8/9] request changes made, comments removed, but in .c file the commented out code is unused and if included presents an error --- general/include/pca9539.h | 39 ++++++++++----------------------------- general/src/pca9539.c | 29 ++++++++++++++++------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/general/include/pca9539.h b/general/include/pca9539.h index 6a422d8..e532e5a 100644 --- a/general/include/pca9539.h +++ b/general/include/pca9539.h @@ -41,52 +41,33 @@ 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 ); -//typedef void(*I2C_ReadPinFuncPtr){uint16_t }; -//typedef void(*I2C_WritePinFuncPtr){}; + typedef struct { - I2C_HandleTypeDef *i2c_handle; - uint16_t dev_addr; + + uint8_t dev_addr; // Two Function Pointers - WritePtr Write; - ReadPtr Read; - //I2C_ReadPinFuncPtr local_I2C_Read_Pin; - //I2C_WritePinFuncPtr local_I2C_Write_Pin; + WritePtr write; + ReadPtr read; } pca9539_t; -/// Init PCA9539, a 16 bit I2C GPIO expander + //Includes use of Function Pointers with instance writeFunc and readFunc + void pca9539_init(pca9539_t *pca, WritePtr writeFunc, - ReadPtr readFunc, I2C_HandleTypeDef *i2c_handle, - uint8_t dev_addr); + ReadPtr readFunc, 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 -HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, - 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); diff --git a/general/src/pca9539.c b/general/src/pca9539.c index f8d1cf1..3bce193 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -6,50 +6,53 @@ void pca9539_init(pca9539_t *pca, WritePtr writeFunc, - ReadPtr readFunc, I2C_HandleTypeDef *i2c_handle, - uint8_t dev_addr) + ReadPtr readFunc, uint8_t dev_addr) { - pca->i2c_handle = i2c_handle; pca->dev_addr = dev_addr << 1u; } int pca9539_read_reg(pca9539_t *pca, uint16_t reg_type, uint8_t *buf) { - return pca->Read(pca->dev_addr, reg_type, *buf); + return pca->read(pca->dev_addr, reg_type, *buf); } + int pca9539_write_reg(pca9539_t *pca, uint16_t reg_type, uint8_t buf) { - return pca->Write(pca->dev_addr, reg_type, buf); + return pca->write(pca->dev_addr, reg_type, buf); } +//Errors in write_pin and read_pin functions if the commented code left in +//Variables within function are unused int pca9539_write_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, uint8_t buf) { - //uint8_t data; -> where is this used? + //int8_t data; - int status = pca->Write(pca->dev_addr, reg_type, buf); + int status = pca->write(pca->dev_addr, reg_type, buf); if (status) { return status; } - //uint8_t data_new; - //uint8_t data; + //uint8_t data_new; + //data_new = (data & ~(1u << pin)) | (buf << pin); - return pca->Write(pca->dev_addr, reg_type, buf); + return pca->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->Read(pca->dev_addr, reg_type, *buf); + int status = pca->read(pca->dev_addr, reg_type, *buf); if (status) { return status; } - //*buf = (data & (1 << pin)) > 0; What to do with this? + //*buf = (data & (1 << pin)) > 0; - return pca->Read(pca->dev_addr, reg_type, *buf); + return pca->read(pca->dev_addr, reg_type, *buf); } From 1459dd2df971227b594fbf6b0b01a6fb97cf348a Mon Sep 17 00:00:00 2001 From: Ruben Noroian Date: Thu, 17 Oct 2024 14:54:10 -0400 Subject: [PATCH 9/9] final edits made to get build working --- general/src/pca9539.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/general/src/pca9539.c b/general/src/pca9539.c index 3bce193..29d7ce5 100644 --- a/general/src/pca9539.c +++ b/general/src/pca9539.c @@ -28,15 +28,15 @@ 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) { - //int8_t data; + //uint8_t data; + //uint8_t data_new; int status = pca->write(pca->dev_addr, reg_type, buf); if (status) { return status; } - //uint8_t data_new; - + //data_new = (data & ~(1u << pin)) | (buf << pin); return pca->write(pca->dev_addr, reg_type, buf); @@ -53,6 +53,6 @@ int pca9539_read_pin(pca9539_t *pca, uint16_t reg_type, uint8_t pin, return status; } //*buf = (data & (1 << pin)) > 0; - + return pca->read(pca->dev_addr, reg_type, *buf); }