Skip to content

Commit

Permalink
Better Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Noroian committed Nov 24, 2024
1 parent c526b7c commit 57ef881
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 100 deletions.
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,
uint8_t dev_addr);
/**
* @brief Initialize the PCA9539 Driver
*/

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

0 comments on commit 57ef881

Please sign in to comment.