-
Notifications
You must be signed in to change notification settings - Fork 2.1k
drivers: add MCP23x17 I/O expander support #10688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@ZetaR60 Again a GPIO expander driver that is working perfect with your last version of the extension API. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a little bit old but left some comments for a first pass, I hope it gets a rebase in the near future as the driver seems very complete.
drivers/mcp23x17/mcp23x17.c
Outdated
return -MCP23X17_ERROR_NO_DEV; | ||
} | ||
#endif | ||
if (params->reset_pin != GPIO_UNDEF) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (params->reset_pin != GPIO_UNDEF) { | |
if (gpio_is_valid(params->reset_pin)) { |
drivers/mcp23x17/mcp23x17.c
Outdated
* If GPIO pin for interrupt signals INTA and INTB is defined in parameters, | ||
* we use interrupts. | ||
*/ | ||
if (params->int_pin != GPIO_UNDEF) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dito
return MCP23X17_OK; | ||
} | ||
|
||
int mcp23x17_gpio_init(mcp23x17_t *dev, gpio_t pin, gpio_mode_t mode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm bit unsure about using gpio_t
in here as some CPUs could define it as an structure, I think it may be more suitable to add a proper mc23x17_gpio_t
type, same could apply for mode as CPUs could define their own and probably the IO expander has it's modes defined independently of the host.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gpio_t
and gpio_mode_t
are used here intentionally. As a result of the review of the first driver provided in PR #7652, the goal of this PR was to implement an API that is compatible with the GPIO API in order to integrate the GPIO expanders transparently.
At the moment, gpio_t must be a scalar for all GPIO implementations because there are hundreds of comparisons in RIOT that use the == operator.
In PR #12877 and later in PR #14602 I tried to introduce a GPIO API using a structured gpio_t
type more than a year ago, but I didn't succeed until now. So there is no reason not to use gpio_t
at the moment. Once, the structured GPIO is introduced, the driver could easily be changed, as the example of the low-level GPIO API implementation of an expander driver in PR #14602 shows.
b7f0347
to
ddaa3bf
Compare
2afd59e
to
f9a040e
Compare
I think my assessment back then was incorrect, but I can create a separate PR to add the support back in and debug the issues that were present at the time. IMO this is good for merge. I went through it very thoroughly a couple of months ago and the test worked out too. |
drivers/mcp23x17/mcp23x17.c
Outdated
#define MCP23X17_EVENT_PRIO EVENT_PRIO_HIGHEST | ||
#endif | ||
|
||
/* interrutp service routine for IRQs */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* interrutp service routine for IRQs */ | |
/* interrupt service routine for IRQs */ |
This is the typo the static tests were complaining about. Odd that the workflow didn't catch it before?
Edit: a local run of make static-test
doesn't complain about it either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This typo is still here 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎
So basically there are three issues that let the CI fail:
In this case, I'm not sure yet what's the best way to solve this. The Lines 100 to 105 in 04a1698
RIOT/cpu/cc2538/include/cc2538_gpio.h Lines 304 to 313 in 04a1698
BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
#
The easiest solution would probably be to rename the internal I tested the changes locally and they work. |
FYI, you can use |
@crasbe Let's come back to this 6 years old PR which I had to revise completly a couple of times due to bigger changes in the master. First of all, many thanks for testing the PR so comprehensive 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address the one "interrutp" typo. You can squash the changes directly.
If CI is happy, this can be merged :)
e838c06
to
a23a849
Compare
@crasbe Thanks for reviewing and merging. |
Contribution description
This PR adds a driver for Microchip MCP23x17 I/O expanders which can be used to extend the number of GPIOs over I2C or SPI. It is a complete rework of the driver that was provided in PR #7652 and rewritten from scratch.
The following expander devices are supported:
mcp23017
ormcp23x17_i2c
mcp23s17
ormcp23x17_spi
The main features of the driver are:
mcp23x17_
and require an additional parameter, the pointer to the expander device of typemcp23x17_
It supports the GPIO extension API as defined in PR periph_*: Allow extensions for external periph adapters #9582 and provided in PR periph/gpio: support for extension API #9860 and periph/gpio: support for extension API (part 2) #9958.GPIO_IN
,GPIO_IN_PU
andGPIO_OUT
in hardwareGPIO_OD
andGPIO_OD_PU
are emulated.periph_gpio_irq
Benchmarks
MCP23x17 driver run-time performance benchmark over SPI using 10 MHz clock speed:
MCP23x17 driver run-time performance benchmark over SPI using 1 MHz clock speed:
MCP23x17 driver run-time performance benchmark over I2C using 400 kHz clock speed:
Reference board was an
esp8266-esp-12x
.Testing procedure
The test application
tests/driver_mcp23x17
can be used to test each MCP23x17 I/O expander pin with shell commands.Compile the application. Dependent on the MCP23x17 expander module, compile it with at least one of the pseudomodules
mcp23x17_i2c
ormcp23x17_spi
, e.g., for the I2C version MCP23017:Add the interrupt functionality by enabling module
mcp23x17_irq
and override the default GPIO pin for the interrupt signal, e.g. for the I2C version MCP23017:Connect the MCP23x17 I/O expander module using the interface and the GPIO for the interrupt signal as defined during compilation.
Connect a GPIO pin of the MCU, e.g.
GPIO_PIN(0,4)
, and a MCP23x17 I/O expander pin, e.g. Port A (port 16 in test application) and Pin 6.Execute some or all of the following commands (results in comment):
Issues/PRs references
The driver is compatible with the GPIO extension API, PRs #9582, #9860 and #9958, but it does not depend on it.