From 4c933d06b208f1b7f72dcf1c51efdc0ff62548cd Mon Sep 17 00:00:00 2001 From: 8q8b Date: Sun, 27 Oct 2024 16:57:08 -0400 Subject: [PATCH] push --- .DS_Store | Bin 0 -> 6148 bytes general/.DS_Store | Bin 0 -> 6148 bytes general/src/pi4ioe.c | 48 +++++++++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 .DS_Store create mode 100644 general/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..213abefa95037dc161761f127904646df9af575b GIT binary patch literal 6148 zcmeHK%Wl&^6upy##%T+R5J=r1Sz?=pKnhaDCWLgsf=z>90Vu?=Nv*n`$d5)Sij+0{ z1HZtQFX3NU!MQU7vXinbf@*FwbLMf+JdWp)AtKSw{rf~sB61KIYukt}Fs|pYVHHzy z9Vk>A0maWEIq@RtZFt)PtAJJDUsHhh?iNjGOfgNVynp#n_=3>>p-c_E^N_SCvrZ_##x^B$H{BXy^u2Uex=9W%P^Ranzx?HI2(s? zkSIdv2XJ}wDvW(O@5?;)Q^g(X3a8;TM$Ns&qWkcm<#xM=%a*%1e9&pR2i-@@Wy86B z_uk{<(P?xR%a3Z*Byc_{rLM>FR&1?zfUL5&2uMP)@QtaDMfU8Q!8)i|LOnt|a{8~CBj zqbJ~fF09LKe3cNsKKKF6!AmG8bHtrB&Q0{fqZ&~P%qT}cZKS67?W47rzxP@j`~{2s zHo)Qak^Np9X_>(z6*meaZO}0tp>#mcfo+W2Ut?g;l^E#qQq1fLXXjE{w~bl_tOEbD z0(?HW2#h_0D~;;tK%|}kz#58`AZ4NPb%P*a6@VhBw~xoiA-23Hz2 zorHOO2-CALFBGAANBgdFC(+YrW2=BwpsYYuXPbQgAN~3LUnbd{Rlq86r4$gg!En&W zl+4|_F*&|#U4(ZCZ0xtvs3?fcb*vJ674IP^L!HYBV9(%6BT8WQkARZFMpl8ps=!b5 C(*s`s literal 0 HcmV?d00001 diff --git a/general/.DS_Store b/general/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5172429f264de2441865cb4700216d4256da9242 GIT binary patch literal 6148 zcmeH~J!%6%427R!7lt%jx}3%b$PET#pTHLgIFQEJ;E>dF^gR7ES*H$5cmnB-G%I%Z zD|S`@Z2$T80!#olbXV*=%*>dt@PRwdU#I)^a=X5>;#J@&VrHyNnC;iLL0pQvfVyTmjO&;ssLc!1UOG})p;=82 zR;?Ceh}WZ?+UmMqI#RP8R>OzYoz15hnq@nzF`-!xQ4j$Um=RcIKKc27r2jVm&svm< zfC&6E0=7P!4tu^-ovjbA=k?dB`g+i*aXG_}p8zI)6mRKa+;6_1_R^8c3Qa!(fk8n8 H{*=HsM+*^= literal 0 HcmV?d00001 diff --git a/general/src/pi4ioe.c b/general/src/pi4ioe.c index 0199eb2..cf3e5f9 100644 --- a/general/src/pi4ioe.c +++ b/general/src/pi4ioe.c @@ -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, ®, 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); }