Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
8q8b committed Oct 27, 2024
1 parent ea57165 commit 4c933d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added general/.DS_Store
Binary file not shown.
48 changes: 30 additions & 18 deletions general/src/pi4ioe.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,61 @@
uint8_t oreg_1_data;
uint8_t oreg_2_data;

HAL_StatusTypeDef pi4ioe_init(pi4ioe_t *gpio, I2C_HandleTypeDef *i2c_handle)
typedef (*I2C_WriteFuncPtr)(uint16_t device_addr, uint8_t *data, uint16_t size);
typedef (*I2C_ReadFuncPtr)(uint16_t device_addr, uint8_t *reg,
uint16_t reg_size, uint8_t *data,
uint16_t data_size);

typedef struct {
uint16_t i2c_addr; // I2C address of the GPIO expander
uint8_t port_config; // Port configuration buffer
uint8_t oreg_1_data; // Output register 1 data
uint8_t oreg_2_data; // Output register 2 data

I2C_WriteFuncPtr i2c_write; // Function pointer for I2C write operation
I2C_ReadFuncPtr i2c_read; // Function pointer for I2C read operation
} pi4ioe_t;

int pi4ioe_init(pi4ioe_t *gpio, I2C_WriteFuncPtr i2c_write, uint16_t i2c_addr)
{
gpio->i2c_handle = i2c_handle;
gpio->i2c_addr = i2c_addr;
gpio->port_config = IO_CONFIG_BUF;
gpio->oreg_1_data = 0x00;
gpio->oreg_2_data = 0x00;

oreg_1_data = 0x00;
oreg_2_data = 0x00;
gpio->i2c_write = i2c_write;

uint8_t buf[2] = { IO_CONFIG_REG, IO_CONFIG_BUF };

/* write to config reg setting TSMS to input, rest to output */
return HAL_I2C_Master_Transmit(i2c_handle, PI4IOE_I2C_ADDR, buf, 2,
HAL_MAX_DELAY);
/* Write to the configuration register using the provided I2C write function */
return gpio->i2c_write(gpio->i2c_addr, buf, 2);
}

HAL_StatusTypeDef pi4ioe_write(uint8_t device, uint8_t val,
I2C_HandleTypeDef *i2c_handle)
int pi4ioe_write(pi4ioe_t *gpio, uint8_t device, uint8_t val)
{
uint8_t reg;
uint8_t *oreg_data;

if (device > 7) {
reg = OUTPUT1_REG;
oreg_data = &oreg_1_data;
oreg_data = &gpio->oreg_1_data;
} else {
reg = OUTPUT0_REG;
oreg_data = &oreg_2_data;
oreg_data = &gpio->oreg_2_data;
}

*oreg_data |= val << device;

uint8_t buf[2] = { reg, *oreg_data };

return HAL_I2C_Master_Transmit(i2c_handle, PI4IOE_I2C_ADDR, buf, 2,
HAL_MAX_DELAY);
/* Use the generalized I2C write function */
return gpio->i2c_write(gpio->i2c_addr, buf, 2);
}

HAL_StatusTypeDef pi4ioe_read(uint8_t *buf, I2C_HandleTypeDef *i2c_handle)
int pi4ioe_read(pi4ioe_t *gpio, uint8_t *buf)
{
uint8_t reg = INPUT0_REG;

HAL_I2C_Master_Transmit(i2c_handle, PI4IOE_I2C_ADDR, &reg, 1,
HAL_MAX_DELAY);
return HAL_I2C_Master_Receive(i2c_handle, PI4IOE_I2C_ADDR, buf, 2,
HAL_MAX_DELAY);
/* Use the generalized I2C read function */
return gpio->i2c_read(gpio->i2c_addr, &reg, 1, buf, 2);
}

0 comments on commit 4c933d0

Please sign in to comment.