Skip to content

Commit

Permalink
Fix i2c timeouts (#401)
Browse files Browse the repository at this point in the history
* MCP23018: make timeout configurable

* Fix i2c timeouts for all boards
  • Loading branch information
drashna authored Aug 28, 2024
1 parent 0be58d4 commit 35a93f3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
16 changes: 9 additions & 7 deletions drivers/gpio/mcp23018.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "debug.h"

#define SLAVE_TO_ADDR(n) (n << 1)
#define TIMEOUT 100
#ifndef MCP23018_TIMEOUT
# define MCP23018_TIMEOUT 100
#endif // MCP23018_TIMEOUT

enum {
CMD_IODIRA = 0x00, // i/o direction register
Expand All @@ -33,13 +35,13 @@ bool mcp23018_set_config(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf)
uint8_t cmdDirection = port ? CMD_IODIRB : CMD_IODIRA;
uint8_t cmdPullup = port ? CMD_GPPUB : CMD_GPPUA;

i2c_status_t ret = i2c_write_register(addr, cmdDirection, &conf, sizeof(conf), TIMEOUT);
i2c_status_t ret = i2c_write_register(addr, cmdDirection, &conf, sizeof(conf), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_set_config::directionFAILED::%u\n", ret);
return false;
}

ret = i2c_write_register(addr, cmdPullup, &conf, sizeof(conf), TIMEOUT);
ret = i2c_write_register(addr, cmdPullup, &conf, sizeof(conf), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_set_config::pullupFAILED::%u\n", ret);
return false;
Expand All @@ -52,7 +54,7 @@ bool mcp23018_set_output(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf)
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;

i2c_status_t ret = i2c_write_register(addr, cmd, &conf, sizeof(conf), TIMEOUT);
i2c_status_t ret = i2c_write_register(addr, cmd, &conf, sizeof(conf), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_set_output::FAILED::%u\n", ret);
return false;
Expand All @@ -65,7 +67,7 @@ bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t conf[2] = {confA, confB};

i2c_status_t ret = i2c_write_register(addr, CMD_GPIOA, &conf[0], sizeof(conf), TIMEOUT);
i2c_status_t ret = i2c_write_register(addr, CMD_GPIOA, &conf[0], sizeof(conf), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_set_output::FAILED::%u\n", ret);
return false;
Expand All @@ -78,7 +80,7 @@ bool mcp23018_read_pins(uint8_t slave_addr, mcp23018_port_t port, uint8_t* out)
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;

i2c_status_t ret = i2c_read_register(addr, cmd, out, sizeof(uint8_t), TIMEOUT);
i2c_status_t ret = i2c_read_register(addr, cmd, out, sizeof(uint8_t), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_read_pins::FAILED::%u\n", ret);
return false;
Expand All @@ -97,7 +99,7 @@ bool mcp23018_read_pins_all(uint8_t slave_addr, uint16_t* out) {

data16 data = {.u16 = 0};

i2c_status_t ret = i2c_read_register(addr, CMD_GPIOA, &data.u8[0], sizeof(data), TIMEOUT);
i2c_status_t ret = i2c_read_register(addr, CMD_GPIOA, &data.u8[0], sizeof(data), MCP23018_TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
dprintf("mcp23018_read_pins_all::FAILED::%u\n", ret);
return false;
Expand Down
2 changes: 1 addition & 1 deletion keyboards/zsa/ergodox_ez/ergodox_ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
extern i2c_status_t mcp23018_status;

#ifndef ERGODOX_EZ_I2C_TIMEOUT
# define ERGODOX_EZ_I2C_TIMEOUT 80
# define ERGODOX_EZ_I2C_TIMEOUT 10
#endif
#ifndef MCP23018_EXPANDER_I2C_ADDR
# define MCP23018_EXPANDER_I2C_ADDR (0x20 << 1)
Expand Down
4 changes: 2 additions & 2 deletions keyboards/zsa/ergodox_ez/post_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif
#define WS2812_LED_COUNT RGBLIGHT_LED_COUNT

#ifndef ISSI_TIMEOUT
# define ISSI_TIMEOUT 3
#ifndef IS31FL3731_I2C_TIMEOUT
# define IS31FL3731_I2C_TIMEOUT 3
#endif
3 changes: 3 additions & 0 deletions keyboards/zsa/moonlander/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#define MATRIX_ROWS 12
#define MATRIX_COLS 7

#define MCP23018_TIMEOUT 10


#define EEPROM_I2C_24LC128

#define IS31FL3731_I2C_ADDRESS_1 IS31FL3731_I2C_ADDRESS_GND
Expand Down
1 change: 1 addition & 0 deletions keyboards/zsa/planck_ez/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
#define AUDIO_PIN_ALT_AS_NEGATIVE

#define IS31FL3737_I2C_ADDRESS_1 IS31FL3737_I2C_ADDRESS_GND
#define IS31FL3737_I2C_TIMEOUT 5
2 changes: 2 additions & 0 deletions keyboards/zsa/voyager/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define MATRIX_COLS 7
#define MATRIX_ROWS 12

#define MCP23018_TIMEOUT 10

#define IS31FL3731_I2C_ADDRESS_1 IS31FL3731_I2C_ADDRESS_GND
#define IS31FL3731_I2C_ADDRESS_2 IS31FL3731_I2C_ADDRESS_VCC

Expand Down

0 comments on commit 35a93f3

Please sign in to comment.