diff --git a/drivers/gpio/mcp23018.c b/drivers/gpio/mcp23018.c index e159e5448d79..dc8ab03c50b7 100644 --- a/drivers/gpio/mcp23018.c +++ b/drivers/gpio/mcp23018.c @@ -1,4 +1,4 @@ -// Copyright 2020 zvecr +// Copyright 2022 zvecr // SPDX-License-Identifier: GPL-2.0-or-later #include "mcp23018.h" @@ -28,7 +28,7 @@ void mcp23018_init(uint8_t addr) { } } -bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) { +bool mcp23018_set_config(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf) { uint8_t addr = SLAVE_TO_ADDR(slave_addr); uint8_t cmdDirection = port ? CMD_IODIRB : CMD_IODIRA; uint8_t cmdPullup = port ? CMD_GPPUB : CMD_GPPUA; @@ -48,7 +48,7 @@ bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) { return true; } -bool mcp23018_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) { +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; @@ -74,7 +74,7 @@ bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) { return true; } -bool mcp23018_readPins(uint8_t slave_addr, uint8_t port, uint8_t* out) { +bool mcp23018_readPins(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; diff --git a/drivers/gpio/mcp23018.h b/drivers/gpio/mcp23018.h index e8ed61b121ce..3af3b22f2a4a 100644 --- a/drivers/gpio/mcp23018.h +++ b/drivers/gpio/mcp23018.h @@ -6,17 +6,60 @@ #include #include -#define mcp23018_PORTA 0 -#define mcp23018_PORTB 1 +/** + * Port ID + */ +typedef enum { + mcp23018_PORTA, + mcp23018_PORTB, +} mcp23018_port_t; -#define ALL_OUTPUT 0 -#define ALL_INPUT 0xFF -#define ALL_LOW 0 -#define ALL_HIGH 0xFF +/** + * Helpers for set_config + */ +enum { + ALL_OUTPUT = 0, + ALL_INPUT = 0xFF, +}; +/** + * Helpers for set_output + */ +enum { + ALL_LOW = 0, + ALL_HIGH = 0xFF, +}; + +/** + * Init expander and any other dependent drivers + */ void mcp23018_init(uint8_t slave_addr); -bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf); -bool mcp23018_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf); + +/** + * Configure input/output to a given port + */ +bool mcp23018_set_config(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf); + +/** + * Write high/low to a given port + */ +bool mcp23018_set_output(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf); + +/** + * Write high/low to both ports sequentially + * + * - slightly faster than multiple set_output + */ bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB); -bool mcp23018_readPins(uint8_t slave_addr, uint8_t port, uint8_t* ret); + +/** + * Read state of a given port + */ +bool mcp23018_readPins(uint8_t slave_addr, mcp23018_port_t port, uint8_t* ret); + +/** + * Read state of both ports sequentially + * + * - slightly faster than multiple readPins + */ bool mcp23018_readPins_all(uint8_t slave_addr, uint16_t* ret); diff --git a/drivers/gpio/pca9555.c b/drivers/gpio/pca9555.c index 50ba21b07a5b..adcd040083df 100644 --- a/drivers/gpio/pca9555.c +++ b/drivers/gpio/pca9555.c @@ -33,7 +33,7 @@ void pca9555_init(uint8_t slave_addr) { // i2c_stop(); } -bool pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) { +bool pca9555_set_config(uint8_t slave_addr, pca9555_port_t port, uint8_t conf) { uint8_t addr = SLAVE_TO_ADDR(slave_addr); uint8_t cmd = port ? CMD_CONFIG_1 : CMD_CONFIG_0; @@ -46,7 +46,7 @@ bool pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) { return true; } -bool pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) { +bool pca9555_set_output(uint8_t slave_addr, pca9555_port_t port, uint8_t conf) { uint8_t addr = SLAVE_TO_ADDR(slave_addr); uint8_t cmd = port ? CMD_OUTPUT_1 : CMD_OUTPUT_0; @@ -72,7 +72,7 @@ bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) { return true; } -bool pca9555_readPins(uint8_t slave_addr, uint8_t port, uint8_t* out) { +bool pca9555_readPins(uint8_t slave_addr, pca9555_port_t port, uint8_t* out) { uint8_t addr = SLAVE_TO_ADDR(slave_addr); uint8_t cmd = port ? CMD_INPUT_1 : CMD_INPUT_0; diff --git a/drivers/gpio/pca9555.h b/drivers/gpio/pca9555.h index b853c7e47bd5..21a4bab8be28 100644 --- a/drivers/gpio/pca9555.h +++ b/drivers/gpio/pca9555.h @@ -29,22 +29,60 @@ `----------' */ -#define PCA9555_PORT0 0 -#define PCA9555_PORT1 1 +/** + * Port ID + */ +typedef enum { + PCA9555_PORT0, + PCA9555_PORT1, +} pca9555_port_t; -#define ALL_OUTPUT 0 -#define ALL_INPUT 0xFF -#define ALL_LOW 0 -#define ALL_HIGH 0xFF +/** + * Helpers for set_config + */ +enum { + ALL_OUTPUT = 0, + ALL_INPUT = 0xFF, +}; +/** + * Helpers for set_output + */ +enum { + ALL_LOW = 0, + ALL_HIGH = 0xFF, +}; + +/** + * Init expander and any other dependent drivers + */ void pca9555_init(uint8_t slave_addr); -bool pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf); +/** + * Configure input/output to a given port + */ +bool pca9555_set_config(uint8_t slave_addr, pca9555_port_t port, uint8_t conf); -bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB); +/** + * Write high/low to a given port + */ +bool pca9555_set_output(uint8_t slave_addr, pca9555_port_t port, uint8_t conf); -bool pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf); +/** + * Write high/low to both ports sequentially + * + * - slightly faster than multiple set_output + */ +bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB); -bool pca9555_readPins(uint8_t slave_addr, uint8_t port, uint8_t* ret); +/** + * Read state of a given port + */ +bool pca9555_readPins(uint8_t slave_addr, pca9555_port_t port, uint8_t* ret); +/** + * Read state of both ports sequentially + * + * - slightly faster than multiple readPins + */ bool pca9555_readPins_all(uint8_t slave_addr, uint16_t* ret);