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

Newnewfeat/pca #198

Merged
merged 9 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 15 additions & 25 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca
#define PCA_DIRECTION_0_REG 0x06
#define PCA_DIRECTION_1_REG 0x07

//return HAL_I2C_Mem_Write(pca->i2c_handle, pca->dev_addr, address,
//I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY);

typedef int (*WritePtr)(uint16_t dev_addr, uint16_t mem_address,
uint16_t mem_add_size, uint8_t *data, uint16_t size,
int delay);
Expand All @@ -46,10 +43,6 @@ typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t mem_address,
int delay);

typedef struct {
//int i2c_handler;
//void *i2c_handler;
//I2C_HandleTypeDef *i2c_handle;

WritePtr write;
ReadPtr read;

Expand All @@ -58,33 +51,30 @@ typedef struct {

void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc,
Copy link
Contributor

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.

uint8_t dev_addr);
/**
* @brief Initialize the PCA9539 Driver
*/
Copy link
Contributor

Choose a reason for hiding this comment

The 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
*/

int pca9539_write_reg(pca9539_t *pca, uint8_t reg_type, uint8_t buf);
/**
* @brief Write the register of the PCA9539
*/

int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin,
uint8_t buf);
/**
* @brief Write the pin of the PCA9539
*/

/*IGNORE THIS CODE - SERVES AS A REFERENCE
/// 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);
*/
#endif
81 changes: 6 additions & 75 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define REG_SIZE_BITS 8

//RETURNS THE WRITE POINTER INSTEAD
//Write Register returns the Write Function Pointer
int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
{
//Parameters in the HAL Function
Expand All @@ -16,14 +16,7 @@ int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
delay);
}

/*IGNORE THIS CODE - LEFT AS A REFERENCE
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_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1,
HAL_MAX_DELAY);
}*/

//Read Register returns the Read Function Pointer
int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
{
uint16_t mem_add_size = 8;
Expand All @@ -34,32 +27,17 @@ int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
delay);
}

/* IGNORE THIS CODE - LEFT AS A REFERENCE
HAL_StatusTypeDef pca_read_reg(pca9539_t* pca, uint16_t address, uint8_t* data)
{

return HAL_I2C_Mem_Read(pca->i2c_handler, pca->dev_addr, address, I2C_MEMADD_SIZE_8BIT, data, 1,
HAL_MAX_DELAY);
}*/

//Intializes the struct
void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc,
uint8_t dev_addr)
{
//pca->i2c_handler = i2c_handler;
pca->dev_addr = dev_addr << 1u;

pca->write = writeFunc;
pca->read = readFunc;
}

/*IGNORE THIS CODE - LEFT AS A REFERENCE
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
}
*/
//Read PCA9539 Register
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
{
int status = pca_read_reg(pca, reg_type, buf);
Expand All @@ -70,6 +48,7 @@ int pca9539_read_reg(pca9539_t *pca, uint8_t reg_type, uint8_t *buf)
return status;
}

//Read PCA9539 Pin
int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin,
uint8_t *buf)
{
Expand All @@ -84,11 +63,13 @@ int pca9539_read_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin,
return status;
}

//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);
}

//Write PCA9539 Pin
int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin,
uint8_t buf)
{
Expand All @@ -104,53 +85,3 @@ int pca9539_write_pin(pca9539_t *pca, uint8_t reg_type, uint8_t pin,

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

//IGNORE THIS CODE - JUST LEFT AS REFERENCE, WILL DELETE ONCE APPROVED
/*
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;
}

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);
}
*/
Loading