-
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
pi4io3 Driver Agnostic #187
Changes from all commits
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 |
---|---|---|
|
@@ -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, | ||
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. not sure that reg size is needed here. Both read na dwrite cna just take "data, reg, length) |
||
uint16_t data_size); | ||
|
||
typedef struct { | ||
uint16_t i2c_addr; // I2C address of the GPIO expander | ||
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. this can be handled at app layer, we dont need tos tore the i2c address here |
||
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, ®, 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, ®, 1, buf, 2); | ||
} |
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.
nitpick but can the names be changed :
I2C_WriteFuncPtr --> WritePtr
... --> ReadPtr
i2c_write --> write
i2c_read --> read