Skip to content

Commit da81128

Browse files
committed
rg_i2c: Add support for PCF8574
1 parent 7933db3 commit da81128

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

components/retro-go/rg_i2c.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,16 @@ static const _gpio_port gpio_ports[] = {
170170
};
171171
static const _gpio_sequence gpio_init_seq[] = {};
172172
static const _gpio_sequence gpio_deinit_seq[] = {};
173-
static rg_gpio_mode_t PCF8575_mode = -1;
173+
static rg_gpio_mode_t PCF857x_mode = -1;
174+
175+
#elif RG_I2C_GPIO_DRIVER == 5 // PCF8574
176+
177+
static const _gpio_port gpio_ports[] = {
178+
{-1, -1, -1, -1}, // PORT 0
179+
};
180+
static const _gpio_sequence gpio_init_seq[] = {};
181+
static const _gpio_sequence gpio_deinit_seq[] = {};
182+
static rg_gpio_mode_t PCF857x_mode = -1;
174183

175184
#else
176185

@@ -240,14 +249,15 @@ bool rg_i2c_gpio_configure_port(int port, uint8_t mask, rg_gpio_mode_t mode)
240249
{
241250
if (port < 0 || port >= gpio_ports_count)
242251
return false;
243-
#if RG_I2C_GPIO_DRIVER == 4 // PCF8575
244-
uint16_t temp = 0xFFFF;
245-
if (mask != 0xFF && mode != PCF8575_mode && (mode == RG_GPIO_OUTPUT || PCF8575_mode == RG_GPIO_OUTPUT))
246-
RG_LOGW("PCF8575 mode cannot be set by pin. (mask is 0x%02X, expected 0xFF)", mask);
247-
PCF8575_mode = mode;
252+
#if RG_I2C_GPIO_DRIVER == 4 || RG_I2C_GPIO_DRIVER == 5 // PCF8575/PCF8574
253+
uint32_t temp = 0xFFFFFFFF;
254+
if (mask != 0xFF && mode != PCF857x_mode && (mode == RG_GPIO_OUTPUT || PCF857x_mode == RG_GPIO_OUTPUT))
255+
RG_LOGW("PCF857x mode cannot be set by pin. (mask is 0x%02X, expected 0xFF)", mask);
256+
PCF857x_mode = mode;
248257
if (mode != RG_GPIO_OUTPUT)
249-
return rg_i2c_write(gpio_address, -1, &temp, 2) && rg_i2c_read(gpio_address, -1, &temp, 2);
250-
return rg_i2c_write(gpio_address, -1, &gpio_output_values, 2);
258+
return rg_i2c_write(gpio_address, -1, &temp, gpio_ports_count)
259+
&& rg_i2c_read(gpio_address, -1, &temp, gpio_ports_count);
260+
return rg_i2c_write(gpio_address, -1, &gpio_output_values, gpio_ports_count);
251261
#else
252262
int direction_reg = gpio_ports[port].direction_reg;
253263
int pullup_reg = gpio_ports[port].pullup_reg;
@@ -261,9 +271,9 @@ int rg_i2c_gpio_read_port(int port)
261271
{
262272
if (port < 0 || port >= gpio_ports_count)
263273
return -1;
264-
#if RG_I2C_GPIO_DRIVER == 4 // PCF8575
265-
uint8_t values[2];
266-
return rg_i2c_read(gpio_address, -1, &values, 2) ? values[port & 1] : -1;
274+
#if RG_I2C_GPIO_DRIVER == 4 || RG_I2C_GPIO_DRIVER == 5 // PCF8575/PCF8574
275+
uint8_t values[gpio_ports_count];
276+
return rg_i2c_read(gpio_address, -1, &values, gpio_ports_count) ? values[port] : -1;
267277
#else
268278
return rg_i2c_read_byte(gpio_address, gpio_ports[port].input_reg);
269279
#endif
@@ -274,10 +284,10 @@ bool rg_i2c_gpio_write_port(int port, uint8_t value)
274284
if (port < 0 || port >= gpio_ports_count)
275285
return false;
276286
gpio_output_values[port] = value;
277-
#if RG_I2C_GPIO_DRIVER == 4 // PCF8575
278-
if (PCF8575_mode != RG_GPIO_OUTPUT)
287+
#if RG_I2C_GPIO_DRIVER == 4 || RG_I2C_GPIO_DRIVER == 5 // PCF8575/PCF8574
288+
if (PCF857x_mode != RG_GPIO_OUTPUT)
279289
return true; // This is consistent with other extenders, where the output latch is updated even in input mode
280-
return rg_i2c_write(gpio_address, -1, &gpio_output_values, 2);
290+
return rg_i2c_write(gpio_address, -1, &gpio_output_values, gpio_ports_count);
281291
#else
282292
return rg_i2c_write_byte(gpio_address, gpio_ports[port].output_reg, value);
283293
#endif

components/retro-go/rg_input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ bool rg_input_read_gamepad_raw(uint32_t *out)
137137
uint32_t buttons = 0;
138138
#if defined(RG_I2C_GPIO_DRIVER)
139139
int data0 = rg_i2c_gpio_read_port(0), data1 = rg_i2c_gpio_read_port(1);
140-
if (data0 > -1 && data1 > -1)
140+
if (data0 > -1) // && data1 > -1)
141141
{
142142
buttons = (data1 << 8) | (data0);
143143
#elif defined(RG_TARGET_T_DECK_PLUS)

components/retro-go/targets/esplay-micro/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// #define RG_STORAGE_FLASH_PARTITION "vfs"
1111

1212
// GPIO Extender
13-
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017
13+
// #define RG_I2C_GPIO_DRIVER 5 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575, 5 = PCF8574
1414
#define RG_I2C_GPIO_ADDR 0x20
1515

1616
// Audio

components/retro-go/targets/esplay-s3/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// #define RG_STORAGE_FLASH_PARTITION "vfs"
1111

1212
// GPIO Extender
13-
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017
13+
#define RG_I2C_GPIO_DRIVER 5 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575, 5 = PCF8574
1414
#define RG_I2C_GPIO_ADDR 0x20
1515

1616
// Audio

components/retro-go/targets/mrgc-g32/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/****************************************************************************
1616
* I2C / GPIO Extender *
1717
****************************************************************************/
18-
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575
18+
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575, 5 = PCF8574
1919
#define RG_I2C_GPIO_ADDR 0x20
2020
#define RG_GPIO_I2C_SDA GPIO_NUM_21
2121
#define RG_GPIO_I2C_SCL GPIO_NUM_22

components/retro-go/targets/odroid-go/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/****************************************************************************
1616
* I2C / GPIO Extender *
1717
****************************************************************************/
18-
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575
18+
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017, 4 = PCF8575, 5 = PCF8574
1919
// #define RG_I2C_GPIO_ADDR 0x00
2020
// #define RG_GPIO_I2C_SDA GPIO_NUM_15
2121
// #define RG_GPIO_I2C_SCL GPIO_NUM_4

0 commit comments

Comments
 (0)