Skip to content

Commit 56f3097

Browse files
committed
rg_i2c+rg_system: Moved I2C GPIO extender init from rg_input to rg_system
If RG_I2C_GPIO_DRIVER is defined, rg_i2c_gpio_init() will be called very early in rg_system_init That is because input, display, audio, everything really, could depend on it.
1 parent e7bcbb4 commit 56f3097

File tree

7 files changed

+57
-31
lines changed

7 files changed

+57
-31
lines changed

components/retro-go/rg_i2c.c

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#endif
1111

1212
static bool i2c_initialized = false;
13-
static bool gpio_extender_initialized = false;
14-
static uint8_t gpio_extender_address = 0x00;
1513

1614
#define TRY(x) \
1715
if ((err = (x)) != ESP_OK) \
@@ -127,14 +125,12 @@ bool rg_i2c_write_byte(uint8_t addr, uint8_t reg, uint8_t value)
127125
return rg_i2c_write(addr, reg, &value, 1);
128126
}
129127

130-
#if RG_I2C_DRIVER == 1
131128

132-
#define AW9523_REG_INPUT0 0x00 ///< Register for reading input values
133-
#define AW9523_REG_OUTPUT0 0x02 ///< Register for writing output values
134-
#define AW9523_REG_POLARITY0 0x04 ///< Register for polarity inversion of inputs
135-
#define AW9523_REG_CONFIG0 0x06 ///< Register for configuring direction
129+
#ifdef RG_I2C_GPIO_DRIVER
130+
static bool gpio_extender_initialized = false;
131+
static uint8_t gpio_extender_address = 0x00;
136132

137-
#else
133+
#if RG_I2C_GPIO_DRIVER == 1 // AW9523
138134

139135
#define AW9523_REG_CHIPID 0x10 ///< Register for hardcode chip ID
140136
#define AW9523_REG_SOFTRESET 0x7F ///< Register for soft resetting
@@ -145,6 +141,13 @@ bool rg_i2c_write_byte(uint8_t addr, uint8_t reg, uint8_t value)
145141
#define AW9523_REG_GCR 0x11 ///< Register for general configuration
146142
#define AW9523_REG_LEDMODE 0x12 ///< Register for configuring const current
147143

144+
#elif RG_I2C_GPIO_DRIVER == 2 // PCF9539
145+
146+
#define AW9523_REG_INPUT0 0x00 ///< Register for reading input values
147+
#define AW9523_REG_OUTPUT0 0x02 ///< Register for writing output values
148+
#define AW9523_REG_POLARITY0 0x04 ///< Register for polarity inversion of inputs
149+
#define AW9523_REG_CONFIG0 0x06 ///< Register for configuring direction
150+
148151
#endif
149152

150153

@@ -156,36 +159,45 @@ bool rg_i2c_gpio_init(void)
156159
if (!i2c_initialized && !rg_i2c_init())
157160
return false;
158161

159-
gpio_extender_initialized = true;
160-
#if RG_I2C_DRIVER == 1
161-
gpio_extender_address = 0x74;
162-
163-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_OUTPUT0, 0xFF);
164-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_OUTPUT0 + 1, 0xFF);
165-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_POLARITY0, 0x00);
166-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_POLARITY0 + 1, 0x00);
167-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0, 0xFF);
168-
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0 + 1, 0xFF);
169-
#else
170-
gpio_extender_address = 0x58;
162+
#if RG_I2C_GPIO_DRIVER == 1 // AW9523
171163

164+
gpio_extender_address = RG_I2C_GPIO_ADDR;
172165
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_SOFTRESET, 0);
173166
rg_usleep(10 * 1000);
174-
175167
uint8_t id = rg_i2c_read_byte(gpio_extender_address, AW9523_REG_CHIPID);
176168
if (id != 0x23)
177169
{
178170
RG_LOGE("AW9523 invalid ID 0x%x found", id);
179171
return false;
180172
}
181-
182173
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0, 0xFF);
183174
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0 + 1, 0xFF);
184175
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_LEDMODE, 0xFF);
185176
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_LEDMODE + 1, 0xFF);
186177
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_GCR, 1 << 4);
187-
#endif
178+
179+
gpio_extender_initialized = true;
188180
return true;
181+
182+
#elif RG_I2C_GPIO_DRIVER == 2 // PCF9539
183+
184+
gpio_extender_address = RG_I2C_GPIO_ADDR;
185+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_OUTPUT0, 0xFF);
186+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_OUTPUT0 + 1, 0xFF);
187+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_POLARITY0, 0x00);
188+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_POLARITY0 + 1, 0x00);
189+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0, 0xFF);
190+
rg_i2c_write_byte(gpio_extender_address, AW9523_REG_CONFIG0 + 1, 0xFF);
191+
192+
gpio_extender_initialized = true;
193+
return true;
194+
195+
#else
196+
197+
RG_LOGE("Unknown driver type");
198+
return false;
199+
200+
#endif
189201
}
190202

191203
bool rg_i2c_gpio_deinit(void)
@@ -223,3 +235,4 @@ bool rg_i2c_gpio_set_level(int pin, int level)
223235
uint8_t val = rg_i2c_read_byte(gpio_extender_address, reg);
224236
return rg_i2c_write_byte(gpio_extender_address, reg, level ? (val | mask) : (val & ~mask));
225237
}
238+
#endif

components/retro-go/rg_input.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bool rg_input_read_gamepad_raw(uint32_t *out)
128128

129129
#if defined(RG_GAMEPAD_I2C_MAP)
130130
uint32_t buttons = 0;
131-
#if defined(RG_TARGET_QTPY_GAMER) || defined(RG_TARGET_BYTEBOI_REV1)
131+
#if defined(RG_I2C_GPIO_DRIVER)
132132
buttons = ~(rg_i2c_gpio_read_port(0) | rg_i2c_gpio_read_port(1) << 8);
133133
#elif defined(RG_TARGET_T_DECK_PLUS)
134134
uint8_t data[5];
@@ -286,9 +286,7 @@ void rg_input_init(void)
286286
#if defined(RG_GAMEPAD_I2C_MAP)
287287
RG_LOGI("Initializing I2C gamepad driver...");
288288
rg_i2c_init();
289-
#if defined(RG_TARGET_QTPY_GAMER) || defined(RG_TARGET_BYTEBOI_REV1)
290-
rg_i2c_gpio_init();
291-
#elif defined(RG_TARGET_T_DECK_PLUS)
289+
#if defined(RG_TARGET_T_DECK_PLUS)
292290
rg_i2c_write_byte(T_DECK_KBD_ADDRESS, -1, T_DECK_KBD_MODE_RAW_CMD);
293291
#endif
294292
UPDATE_GLOBAL_MAP(keymap_i2c);

components/retro-go/rg_system.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
444444
printf(" built for: %s. type: %s\n", RG_TARGET_NAME, app.isRelease ? "release" : "dev");
445445
printf("========================================================\n\n");
446446

447+
#ifdef RG_I2C_GPIO_DRIVER
448+
rg_i2c_init();
449+
rg_i2c_gpio_init();
450+
#endif
451+
447452
rg_storage_init();
448453
rg_input_init();
449454

components/retro-go/targets/byteboi-rev1/config.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
ILI9341_CMD(0xE1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
4646

4747

48-
4948
// Input
5049
// Refer to rg_input.h to see all available RG_KEY_* and RG_GAMEPAD_*_MAP types
5150
#define RG_GAMEPAD_I2C_MAP {\
@@ -70,11 +69,14 @@
7069
#define RG_BATTERY_CALC_PERCENT(raw) (((raw) * 2.f - 3500.f) / (4200.f - 3500.f) * 100.f)
7170
#define RG_BATTERY_CALC_VOLTAGE(raw) ((raw) * 2.f * 0.001f)
7271

72+
// GPIO Extender
73+
#define RG_I2C_GPIO_DRIVER 2 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017
74+
#define RG_I2C_GPIO_ADDR 0x74
75+
7376
// Status LED
7477
//#define RG_GPIO_LED GPIO_NUM_14
7578

7679
// I2C BUS
77-
#define RG_I2C_DRIVER 1 // 0 = AW9523, 1 = PCF9539
7880
#define RG_GPIO_I2C_SDA GPIO_NUM_23
7981
#define RG_GPIO_I2C_SCL GPIO_NUM_22
8082

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
#define RG_BATTERY_CALC_PERCENT(raw) (((raw) * 2.f - 3500.f) / (4200.f - 3500.f) * 100.f)
6868
#define RG_BATTERY_CALC_VOLTAGE(raw) ((raw) * 2.f * 0.001f)
6969

70+
// GPIO Extender
71+
// #define RG_I2C_GPIO_DRIVER 0 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017
72+
// #define RG_I2C_GPIO_ADDR 0x00
73+
7074
// Status LED
7175
#define RG_GPIO_LED GPIO_NUM_2
7276

components/retro-go/targets/qtpy-gamer/config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Target definition
2-
#define RG_TARGET_NAME "QTPY ESP32"
2+
#define RG_TARGET_NAME "QTPY-GAMER"
33

44
// Storage
55
#define RG_STORAGE_ROOT "/sd"
@@ -48,6 +48,10 @@
4848
#define RG_BATTERY_CALC_PERCENT(raw) (99)
4949
#define RG_BATTERY_CALC_VOLTAGE(raw) (0)
5050

51+
// GPIO Extender
52+
#define RG_I2C_GPIO_DRIVER 1 // 1 = AW9523, 2 = PCF9539, 3 = MCP23017
53+
#define RG_I2C_GPIO_ADDR 0x58
54+
5155
// Status LED - not actually used, we've got neopixels instead :/
5256
// #define RG_GPIO_LED GPIO_NUM_NC
5357

components/retro-go/targets/qtpy-gamer/docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# QTPY ESP32
1+
# QTPY-GAMER
22

33
- Status:
44
- Ref:

0 commit comments

Comments
 (0)