-
Notifications
You must be signed in to change notification settings - Fork 1
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
Newnewfeat/pca #198
Newnewfeat/pca #198
Changes from 6 commits
4e9db2f
e5441af
824f3ef
dbf5358
c526b7c
57ef881
d6679ec
72c7b4c
3bb4e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,7 @@ | |
#include <stdint.h> | ||
|
||
/* | ||
PCA 9539 16 bit GPIO expander. Datasheet: | ||
https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 | ||
PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca9539.pdf?ts=1716785085909 | ||
*/ | ||
|
||
/// Possible I2C addresses, see comment below | ||
|
@@ -36,31 +35,46 @@ PCA 9539 16 bit GPIO expander. Datasheet: | |
#define PCA_DIRECTION_0_REG 0x06 | ||
#define PCA_DIRECTION_1_REG 0x07 | ||
|
||
typedef int (*WritePtr)(uint16_t dev_addr, uint16_t mem_address, | ||
uint16_t mem_add_size, uint8_t *data, uint16_t size, | ||
int delay); | ||
typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t mem_address, | ||
uint16_t mem_add_size, uint8_t *data, uint16_t size, | ||
int delay); | ||
|
||
typedef struct { | ||
I2C_HandleTypeDef *i2c_handle; | ||
WritePtr write; | ||
ReadPtr read; | ||
|
||
uint16_t dev_addr; | ||
} pca9539_t; | ||
|
||
/// Init PCA9539, a 16 bit I2C GPIO expander | ||
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, | ||
void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, | ||
uint8_t dev_addr); | ||
/** | ||
* @brief Initialize the PCA9539 Driver | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These also need to include definitions for parameters and return types. If you use VScode, you should get the doxygen extension. It autofills all that stuff for you. |
||
|
||
int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf); | ||
/** | ||
* @brief Read the register of the PCA9539 | ||
*/ | ||
|
||
int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, | ||
uint8_t *buf); | ||
/** | ||
* @brief Read the pin state of the PCA9539 | ||
*/ | ||
|
||
/// @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); | ||
int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf); | ||
/** | ||
* @brief Write the register of the PCA9539 | ||
*/ | ||
|
||
/// @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); | ||
int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin, | ||
uint8_t buf); | ||
/** | ||
* @brief Write the pin of the PCA9539 | ||
*/ | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,43 +4,56 @@ | |
|
||
#define REG_SIZE_BITS 8 | ||
|
||
HAL_StatusTypeDef pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data) | ||
//Write Register returns the Write Function Pointer | ||
int 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); | ||
//Parameters in the HAL Function | ||
uint16_t mem_add_size = 8; | ||
uint16_t data_size = 1; | ||
int delay = 0xFFFFFFFFU; | ||
|
||
return pca->write(pca->dev_addr, address, mem_add_size, data, data_size, | ||
delay); | ||
} | ||
|
||
HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data) | ||
//Read Register returns the Read Function Pointer | ||
int 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); | ||
uint16_t mem_add_size = 8; | ||
uint16_t data_size = 1; | ||
int delay = 0xFFFFFFFFU; | ||
|
||
return pca->read(pca->dev_addr, address, mem_add_size, data, data_size, | ||
delay); | ||
} | ||
|
||
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, | ||
//Intializes the struct | ||
void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc, | ||
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; | ||
|
||
pca->write = writeFunc; | ||
pca->read = readFunc; | ||
} | ||
|
||
HAL_StatusTypeDef pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, | ||
uint8_t *buf) | ||
//Read PCA9539 Register | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments aren't needed since they are already in the @brief section in the header file. |
||
int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf) | ||
{ | ||
HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, buf); | ||
int 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 *buf) | ||
//Read PCA9539 Pin | ||
int 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); | ||
int status = pca_read_reg(pca, reg_type, &data); | ||
if (status) { | ||
return status; | ||
} | ||
|
@@ -50,19 +63,20 @@ HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, | |
return status; | ||
} | ||
|
||
HAL_StatusTypeDef pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, | ||
uint8_t buf) | ||
//Write PCA9539 Register | ||
int 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) | ||
//Write PCA9539 Pin | ||
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); | ||
int status = pca_read_reg(pca, reg_type, &data); | ||
if (status) { | ||
return status; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc comments are missing. Use doxygen, javadoc style comments seen in other repos.