Skip to content

Commit

Permalink
#2: tweaked GPIO expander driver (#8)
Browse files Browse the repository at this point in the history
* #2: removed cerberus macros &  new register variables

* #2: adjusted output reg

* #2: oreg data

* #2: dereferenced the pointer & fixed  formatting
  • Loading branch information
ibarra000 authored Oct 8, 2023
1 parent 54ac5dd commit 33faed3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 0 additions & 7 deletions general/include/pi4ioe.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
#define OUTPUT0_REG 0x02
#define OUTPUT1_REG 0x03

#define PUMP_CTRL 0x00
#define RADFAN_CTRL 0x01
#define BRKLIGHT_CTRL 0x02
#define BATBOXFAN_CTRL 0x03
#define TSMS_CTRL 0x04
#define SMBALERT 0x05


typedef struct
{
Expand Down
25 changes: 22 additions & 3 deletions general/src/pi4ioe.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
*/
#include "pi4ioe.h"

uint8_t oreg_1_data;
uint8_t oreg_2_data;

HAL_StatusTypeDef pi4ioe_init(pi4ioe_t *gpio, I2C_HandleTypeDef *i2c_handle)
{
gpio->i2c_handle = i2c_handle;
gpio->port_config = IO_CONFIG_BUF;

oreg_1_data = 0x00;
oreg_2_data = 0x00;

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

/* write to config reg setting TSMS to input, rest to output */
Expand All @@ -20,12 +26,25 @@ HAL_StatusTypeDef pi4ioe_init(pi4ioe_t *gpio, I2C_HandleTypeDef *i2c_handle)

HAL_StatusTypeDef pi4ioe_write(uint8_t device, uint8_t val, I2C_HandleTypeDef *i2c_handle)
{

uint8_t reg;
uint8_t* oreg_data;

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

*oreg_data |= val << device;

if (device > 7) reg = OUTPUT1_REG;
else reg = OUTPUT0_REG;
uint8_t buf[2] = {reg, *oreg_data};

uint8_t buf[2] = {reg, val << device};
return HAL_I2C_Master_Transmit(i2c_handle, PI4IOE_I2C_ADDR, buf, 2, HAL_MAX_DELAY);

}
Expand Down

0 comments on commit 33faed3

Please sign in to comment.