drivers: MCP23009 & MCP23017 shared code base (#25924)

* Implemented driver for MCP23017

* fixed compatability with mcp23009. (naming of instantiated GPIO-Devices)

* removed some comments

* removed even more comments

* commented out instatntiation of driver since it will not be used with v6x

* removed last useless comments

* re-activated gpio_mcp23009 driver, removed useless comments and empty lines

* removed empty lines at the end of mcp23017.cpp

* removed empty line

* Implemented driver for MCP23017

* fixed compatability with mcp23009. (naming of instantiated GPIO-Devices)

* removed some comments

* removed even more comments

* commented out instatntiation of driver since it will not be used with v6x

* removed last useless comments

* re-activated gpio_mcp23009 driver, removed useless comments and empty lines

* removed empty lines at the end of mcp23017.cpp

* removed empty line

* basic working implementation

* first improved driver version with shared code base for MCP23009 & MCP23017 (built as state machine with sanity checks)

* removed unused imports

* changed module name from MCP to MCP230XX

* removed debug print statements

* adjusted auto start command of driver

* removed comments

* -added seperate main functions for both derivative modules (mcp23009 and mcp23017)

* compile common functions as shared library in src/lib/drivers

* fixed cleanup of modules

* ->unclean working version with shared common library

* used make format

* working & cleaned version

* -> Added CallbackHandler to be able to use multiple reading GPIO-expanders simultaneously
-> Removed old mcap23009 calls and pin registrations
-> Adjusted GpioIn.msg to contain MAX_INSTANCES

* ->removed unused imports
->used make format

* Fix: Re-enabled platform_mcp_gpio for accton-godwit, cuav, fmu-v5x

* Fix: enabled platform_mcp_gpio in px4/fmu-v5x

* added depency to fmu-v5x

* Fix: removed new lines

* Fix: fixed linker errors

* removed unused linkage against mcp-library

* Made mcp start calls consistent for fmu-v5x and fmu-v6x

* moved logging of comm errors to read/write function directly

* added perf_count for sanity_check

* removed error message

* ensured member variables follow naming convention

* added retries to probe function

* simplyfied state-logic

* add break to terminate loop early

* ensured clean state when register_gpios() fails

* add registered-flag to pins

* used path from top dir instead of relative path in CMakeLists

* used constexpr to set parameters instead of calculating them at runtime

* style: used make format

* fix: corrected i2c_bus assignment

* style: init input of callbackhandler to 0

* fix: mark pin as registered if successful

* style: made arguments const type

---------

Co-authored-by: Alexander Lerach <alexander@auterion.com>
This commit is contained in:
Phil-Engljaehringer
2025-12-17 17:48:30 +01:00
committed by GitHub
co-authored by Alexander Lerach
parent 38f89a8b69
commit 3438d593a1
36 changed files with 1425 additions and 625 deletions
+1
View File
@@ -201,6 +201,7 @@
#define DRV_DIST_DEVTYPE_GY_US42 0x9C
#define DRV_BAT_DEVTYPE_BATMON_SMBUS 0x9d
#define DRV_GPIO_DEVTYPE_MCP23017 0x9E
#define DRV_GPIO_DEVTYPE_MCP23009 0x9F
#define DRV_GPS_DEVTYPE_ASHTECH 0xA0
-6
View File
@@ -1,9 +1,3 @@
menu "GPIO"
menuconfig COMMON_GPIO
bool "Common GPIOs"
default n
select DRIVERS_GPIO_MCP23009
---help---
Enable default set of GPIO drivers
rsource "*/Kconfig"
endmenu
+5 -4
View File
@@ -30,12 +30,13 @@
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE drivers__mcp23009
MODULE drivers__gpio__mcp23009
MAIN mcp23009
COMPILE_FLAGS
SRCS
mcp23009_main.cpp
mcp23009.cpp
MCP23009_main.cpp
MCP23009.cpp
DEPENDS
mcp_common
)
+2 -2
View File
@@ -1,5 +1,5 @@
menuconfig DRIVERS_GPIO_MCP23009
bool "mcp23009"
bool "MCP23009"
default n
---help---
Enable support for mcp23009
Enable support for MCP23009 8-bit GPIO expander
+92
View File
@@ -0,0 +1,92 @@
/****************************************************************************
*
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "MCP23009.hpp"
MCP23009::MCP23009(const I2CSPIDriverConfig &config) :
MCP230XX(config)
{
}
int MCP23009::get_olat(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::OLAT;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23009::get_gppu(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::GPPU;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23009::get_iodir(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::IODIR;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23009::get_gpio(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::GPIO;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23009::get_probe_reg(uint8_t *addr)
{
*addr = (uint8_t) Register::IOCON;
return PX4_OK;
}
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -30,41 +30,15 @@
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#pragma once
#include <stdint.h>
#include <drivers/device/i2c.h>
#include <px4_platform_common/i2c_spi_buses.h>
#include <uORB/topics/gpio_config.h>
#include <uORB/topics/gpio_in.h>
#include <uORB/topics/gpio_out.h>
#include <uORB/topics/gpio_request.h>
#include <uORB/Publication.hpp>
#include <uORB/SubscriptionCallback.hpp>
#include <lib/perf/perf_counter.h>
#include <drivers/drv_hrt.h>
#include <lib/drivers/mcp_common/MCP.hpp>
using namespace time_literals;
class MCP23009 : public device::I2C, public I2CSPIDriver<MCP23009>
class MCP23009 : public MCP230XX
{
public:
MCP23009(const I2CSPIDriverConfig &config);
~MCP23009() override;
void RunImpl();
static void print_usage();
static I2CSPIDriverBase *instantiate(const I2CSPIDriverConfig &config, int runtime_instance);
protected:
int init_uorb();
int init(uint8_t direction, uint8_t state, uint8_t pull_up);
int probe() override;
void print_status() override;
void exit_and_cleanup() override;
private:
enum class
@@ -82,30 +56,9 @@ private:
OLAT = 0x0a
};
enum class
PinType : uint8_t {
Output,
Input,
InputPullUp,
};
uORB::SubscriptionCallbackWorkItem _gpio_out_sub{this, ORB_ID(gpio_out)};
uORB::SubscriptionCallbackWorkItem _gpio_request_sub{this, ORB_ID(gpio_request)};
uORB::SubscriptionCallbackWorkItem _gpio_config_sub{this, ORB_ID(gpio_config)};
uORB::Publication<gpio_in_s> _to_gpio_in{ORB_ID(gpio_in)};
perf_counter_t _cycle_perf;
// buffer often used (write-only!) registers here
uint8_t _olat;
uint8_t _iodir;
uint8_t _gppu;
int read(uint8_t *mask);
int write(uint8_t mask_set, uint8_t mask_clear);
int configure(uint8_t mask, PinType type);
int read_reg(Register address, uint8_t &data);
int write_reg(Register address, uint8_t data);
int get_olat(int bank, uint8_t *addr) override;
int get_gppu(int bank, uint8_t *addr) override;
int get_iodir(int bank, uint8_t *addr) override;
int get_gpio(int bank, uint8_t *addr) override;
int get_probe_reg(uint8_t *addr) override;
};
+102
View File
@@ -0,0 +1,102 @@
/****************************************************************************
*
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "MCP23009.hpp"
constexpr MCP230XX_config_t def_mcp_config{
.device_type = DRV_GPIO_DEVTYPE_MCP23009,
.i2c_addr = I2C_ADDRESS_MCP23009,
.num_pins = 8,
.num_banks = 1,
};
extern "C" int mcp23009_main(int argc, char *argv[])
{
using ThisDriver = MCP23009;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 400000;
cli.i2c_address = 0x25;
MCP230XX_config_t mcp_config = def_mcp_config;
int ch;
while ((ch = cli.getOpt(argc, argv, "D:O:P:U:R:M:")) != EOF) {
switch (ch) {
case 'D':
mcp_config.direction = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'O':
mcp_config.state = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'P':
mcp_config.pullup = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'U':
mcp_config.interval = (uint16_t)atoi(cli.optArg());
break;
case 'M':
mcp_config.first_minor = (uint8_t)atoi(cli.optArg());
break;
}
}
const char *verb = cli.optArg();
if (!verb) {
MCP230XX::print_usage();
return -1;
}
cli.custom_data = &mcp_config;
BusInstanceIterator iterator("MCP23009", cli, mcp_config.device_type);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
MCP230XX::print_usage();
return -1;
}
-123
View File
@@ -1,123 +0,0 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "mcp23009.h"
int MCP23009::read_reg(Register address, uint8_t &data)
{
return transfer((uint8_t *)&address, 1, &data, 1);
}
int MCP23009::write_reg(Register address, uint8_t value)
{
uint8_t data[2] = {(uint8_t)address, value};
return transfer(data, sizeof(data), nullptr, 0);
}
int MCP23009::init(uint8_t direction, uint8_t state, uint8_t pull_up)
{
// do I2C init (and probe) first
int ret = I2C::init();
if (ret != PX4_OK) {
PX4_ERR("I2C init failed");
return ret;
}
// buffer the new initial states
_iodir = direction;
_olat = state;
_gppu = pull_up;
// Write the initial state to the device
ret = write_reg(Register::OLAT, _olat);
ret |= write_reg(Register::IODIR, _iodir);
ret |= write_reg(Register::GPPU, _gppu);
if (ret != PX4_OK) {
PX4_ERR("Device init failed (%i)", ret);
return ret;
}
return init_uorb();
}
int MCP23009::probe()
{
// no whoami, try to read IOCON
uint8_t data;
return read_reg(Register::IOCON, data);
}
int MCP23009::read(uint8_t *mask)
{
return read_reg(Register::GPIO, *mask);
}
int MCP23009::write(uint8_t mask_set, uint8_t mask_clear)
{
// no need to read, we can use the buffered register value
_olat = (_olat & ~mask_clear) | mask_set;
return write_reg(Register::OLAT, _olat);
}
int MCP23009::configure(uint8_t mask, PinType type)
{
// no need to read, we can use the buffered register values
switch (type) {
case PinType::Input:
_iodir |= mask;
_gppu &= ~mask;
break;
case PinType::InputPullUp:
_iodir |= mask;
_gppu |= mask;
break;
case PinType::Output:
_iodir &= ~mask;
break;
default:
return -EINVAL;
}
int ret = write_reg(Register::GPPU, _gppu);
if (ret != 0) {
return ret;
}
return write_reg(Register::IODIR, _iodir);
}
-215
View File
@@ -1,215 +0,0 @@
/****************************************************************************
*
* Copyright (C) 2023 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* Driver for the MCP23009 connected via I2C.
*/
#include "mcp23009.h"
#include <px4_platform_common/module.h>
MCP23009::MCP23009(const I2CSPIDriverConfig &config) :
I2C(config),
I2CSPIDriver(config),
_cycle_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": single-sample"))
{
}
MCP23009::~MCP23009()
{
ScheduleClear();
perf_free(_cycle_perf);
}
int MCP23009::init_uorb()
{
if (!_gpio_config_sub.registerCallback() ||
!_gpio_request_sub.registerCallback() ||
!_gpio_out_sub.registerCallback()) {
PX4_ERR("callback registration failed");
return -1;
}
return PX4_OK;
}
void MCP23009::exit_and_cleanup()
{
_gpio_config_sub.unregisterCallback();
_gpio_request_sub.unregisterCallback();
_gpio_out_sub.unregisterCallback();
}
void MCP23009::RunImpl()
{
perf_begin(_cycle_perf);
gpio_config_s config;
if (_gpio_config_sub.update(&config) && config.device_id == get_device_id()) {
PinType type = PinType::Input;
switch (config.config) {
case config.INPUT_PULLUP: type = PinType::InputPullUp; break;
case config.OUTPUT: type = PinType::Output; break;
}
write(config.state, config.mask);
configure(config.mask, type);
}
gpio_out_s output;
if (_gpio_out_sub.update(&output) && output.device_id == get_device_id()) {
write(output.state, output.mask);
}
// read every time we run, either when requested or when scheduled on interval
{
gpio_in_s _gpio_in;
_gpio_in.timestamp = hrt_absolute_time();
_gpio_in.device_id = get_device_id();
uint8_t input;
read(&input);
_gpio_in.state = input;
_to_gpio_in.publish(_gpio_in);
}
perf_end(_cycle_perf);
}
void MCP23009::print_usage()
{
PRINT_MODULE_USAGE_NAME("MCP23009", "driver");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_PARAMS_I2C_SPI_DRIVER(true, false);
PRINT_MODULE_USAGE_PARAMS_I2C_ADDRESS(0x25);
PRINT_MODULE_USAGE_PARAM_INT('D', 0, 0, 255, "Direction", true);
PRINT_MODULE_USAGE_PARAM_INT('O', 0, 0, 255, "Output", true);
PRINT_MODULE_USAGE_PARAM_INT('P', 0, 0, 255, "Pullups", true);
PRINT_MODULE_USAGE_PARAM_INT('U', 0, 0, 1000, "Update Interval [ms]", true);
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
}
void MCP23009::print_status()
{
I2CSPIDriverBase::print_status();
perf_print_counter(_cycle_perf);
}
struct init_config_t {
uint16_t interval;
uint8_t direction;
uint8_t state;
uint8_t pullup;
};
I2CSPIDriverBase *MCP23009::instantiate(const I2CSPIDriverConfig &config, int runtime_instance)
{
auto *init = (const init_config_t *)config.custom_data;
auto *instance = new MCP23009(config);
if (!instance) {
PX4_ERR("alloc failed");
return nullptr;
}
if (OK != instance->init(init->direction, init->state, init->pullup)) {
delete instance;
return nullptr;
}
if (init->interval) {
instance->ScheduleOnInterval(init->interval * 1000);
}
return instance;
}
extern "C" int mcp23009_main(int argc, char *argv[])
{
using ThisDriver = MCP23009;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 400000;
cli.i2c_address = 0x25;
init_config_t config_data{};
int ch;
while ((ch = cli.getOpt(argc, argv, "D:O:P:U:")) != EOF) {
switch (ch) {
case 'D':
config_data.direction = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'O':
config_data.state = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'P':
config_data.pullup = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'U':
config_data.interval = atoi(cli.optArg());
break;
}
}
cli.custom_data = &config_data;
const char *verb = cli.optArg();
if (!verb) {
ThisDriver::print_usage();
return -1;
}
BusInstanceIterator iterator(MODULE_NAME, cli, DRV_GPIO_DEVTYPE_MCP23009);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
ThisDriver::print_usage();
return -1;
}
+42
View File
@@ -0,0 +1,42 @@
############################################################################
#
# Copyright (c) 2023 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
px4_add_module(
MODULE drivers__gpio__mcp23017
MAIN mcp23017
SRCS
MCP23017_main.cpp
MCP23017.cpp
DEPENDS
mcp_common
)
+5
View File
@@ -0,0 +1,5 @@
menuconfig DRIVERS_GPIO_MCP23017
bool "MCP23017"
default n
---help---
Enable support for MCP23017 16-bit GPIO expander
+108
View File
@@ -0,0 +1,108 @@
/****************************************************************************
*
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "MCP23017.hpp"
MCP23017::MCP23017(const I2CSPIDriverConfig &config) :
MCP230XX(config)
{
}
int MCP23017::get_olat(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::OLATA;
return PX4_OK;
case 1:
*addr = (uint8_t) Register::OLATB;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23017::get_gppu(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::GPPUA;
return PX4_OK;
case 1:
*addr = (uint8_t) Register::GPPUB;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23017::get_iodir(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::IODIRA;
return PX4_OK;
case 1:
*addr = (uint8_t) Register::IODIRB;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23017::get_gpio(int bank, uint8_t *addr)
{
switch (bank) {
case 0:
*addr = (uint8_t) Register::GPIOA;
return PX4_OK;
case 1:
*addr = (uint8_t) Register::GPIOB;
return PX4_OK;
default:
return PX4_ERROR;
}
}
int MCP23017::get_probe_reg(uint8_t *addr)
{
*addr = (uint8_t) Register::IOCONA;
return PX4_OK;
}
+75
View File
@@ -0,0 +1,75 @@
/****************************************************************************
*
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#pragma once
#include <lib/drivers/mcp_common/MCP.hpp>
using namespace time_literals;
class MCP23017 : public MCP230XX
{
public:
MCP23017(const I2CSPIDriverConfig &config);
private:
enum class
Register : uint8_t {
IODIRA = 0x00,
IODIRB = 0x01,
IPOLA = 0x02,
IPOLB = 0x03,
GPINTENA = 0x04,
GPINTENB = 0x05,
DEFVALA = 0x06,
DEFVALB = 0x07,
INTCONA = 0x08,
INTCONB = 0x09,
IOCONA = 0x0a,
IOCONB = 0x0b,
GPPUA = 0x0c,
GPPUB = 0x0d,
INTFA = 0x0e,
INTFB = 0x0f,
INTCAPA = 0x10,
INTCAPB = 0x11,
GPIOA = 0x12,
GPIOB = 0x13,
OLATA = 0x14,
OLATB = 0x15
};
int get_olat(int bank, uint8_t *addr) override;
int get_gppu(int bank, uint8_t *addr) override;
int get_iodir(int bank, uint8_t *addr) override;
int get_gpio(int bank, uint8_t *addr) override;
int get_probe_reg(uint8_t *addr) override;
};
+102
View File
@@ -0,0 +1,102 @@
/****************************************************************************
*
* Copyright (C) 2025 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "MCP23017.hpp"
constexpr MCP230XX_config_t def_mcp_config{
.device_type = DRV_GPIO_DEVTYPE_MCP23017,
.i2c_addr = I2C_ADDRESS_MCP23017,
.num_pins = 16,
.num_banks = 2,
};
extern "C" int mcp23017_main(int argc, char *argv[])
{
using ThisDriver = MCP23017;
BusCLIArguments cli{true, false};
cli.default_i2c_frequency = 400000;
cli.i2c_address = 0x27;
MCP230XX_config_t mcp_config = def_mcp_config;
int ch;
while ((ch = cli.getOpt(argc, argv, "D:O:P:U:R:M:")) != EOF) {
switch (ch) {
case 'D':
mcp_config.direction = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'O':
mcp_config.state = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'P':
mcp_config.pullup = (int)strtol(cli.optArg(), nullptr, 0);
break;
case 'U':
mcp_config.interval = (uint16_t)atoi(cli.optArg());
break;
case 'M':
mcp_config.first_minor = (uint8_t)atoi(cli.optArg());
break;
}
}
const char *verb = cli.optArg();
if (!verb) {
MCP230XX::print_usage();
return -1;
}
cli.custom_data = &mcp_config;
BusInstanceIterator iterator("MCP23017", cli, mcp_config.device_type);
if (!strcmp(verb, "start")) {
return ThisDriver::module_start(cli, iterator);
}
if (!strcmp(verb, "stop")) {
return ThisDriver::module_stop(iterator);
}
if (!strcmp(verb, "status")) {
return ThisDriver::module_status(iterator);
}
MCP230XX::print_usage();
return -1;
}