mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-09 22:08:56 +08:00
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:
committed by
GitHub
parent
38f89a8b69
commit
3438d593a1
@@ -31,5 +31,4 @@
|
||||
#
|
||||
############################################################################
|
||||
|
||||
|
||||
add_subdirectory(mcp23009)
|
||||
add_subdirectory(mcp)
|
||||
|
||||
+3
-3
@@ -30,7 +30,7 @@
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
############################################################################
|
||||
px4_add_library(platform_gpio_mcp23009
|
||||
mcp23009.cpp
|
||||
px4_add_library(platform_gpio_mcp
|
||||
mcp.cpp
|
||||
)
|
||||
target_link_libraries(platform_gpio_mcp23009 PRIVATE drivers__device) # device::I2C
|
||||
target_link_libraries(platform_gpio_mcp PRIVATE drivers__device) # device::I2C
|
||||
@@ -0,0 +1,169 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* 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 <nuttx/ioexpander/gpio.h>
|
||||
#include <lib/drivers/device/Device.hpp>
|
||||
#include <px4_platform/gpio/mcp.hpp>
|
||||
#include <uORB/topics/gpio_config.h>
|
||||
#include <uORB/topics/gpio_out.h>
|
||||
#include <uORB/PublicationMulti.hpp>
|
||||
#include <drivers/drv_hrt.h>
|
||||
|
||||
static int mcp230XX_read(struct gpio_dev_s *dev, bool *value)
|
||||
{
|
||||
mcp_gpio_dev_s *gpio = (struct mcp_gpio_dev_s *)dev;
|
||||
*value = gpio->callback_handler->input & gpio->mask;
|
||||
return OK;
|
||||
}
|
||||
|
||||
static uORB::PublicationMulti<gpio_out_s> toGpioOut{ORB_ID(gpio_out)};
|
||||
static int mcp230XX_write(struct gpio_dev_s *dev, bool value)
|
||||
{
|
||||
mcp_gpio_dev_s *gpio = (struct mcp_gpio_dev_s *)dev;
|
||||
gpio_out_s msg{
|
||||
hrt_absolute_time(),
|
||||
gpio->callback_handler->dev_id,
|
||||
gpio->mask, // clear mask
|
||||
value ? gpio->mask : 0u, // set mask
|
||||
};
|
||||
return toGpioOut.publish(msg) ? OK : -ETIMEDOUT;
|
||||
}
|
||||
|
||||
static uORB::PublicationMulti<gpio_config_s> toGpioConfig{ORB_ID(gpio_config)};
|
||||
static int mcp230XX_setpintype(struct gpio_dev_s *dev, enum gpio_pintype_e pintype)
|
||||
{
|
||||
mcp_gpio_dev_s *gpio = (struct mcp_gpio_dev_s *)dev;
|
||||
gpio_config_s msg{
|
||||
hrt_absolute_time(),
|
||||
gpio->callback_handler->dev_id,
|
||||
gpio->mask,
|
||||
};
|
||||
|
||||
switch (pintype) {
|
||||
case GPIO_INPUT_PIN:
|
||||
msg.config = gpio_config_s::INPUT;
|
||||
break;
|
||||
|
||||
case GPIO_INPUT_PIN_PULLUP:
|
||||
msg.config = gpio_config_s::INPUT_PULLUP;
|
||||
break;
|
||||
|
||||
case GPIO_OUTPUT_PIN:
|
||||
msg.config = gpio_config_s::OUTPUT;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return toGpioConfig.publish(msg) ? OK : -ETIMEDOUT;
|
||||
}
|
||||
|
||||
static const struct gpio_operations_s mcp_gpio_ops {
|
||||
mcp230XX_read,
|
||||
mcp230XX_write,
|
||||
nullptr,
|
||||
nullptr,
|
||||
mcp230XX_setpintype,
|
||||
};
|
||||
|
||||
int mcp230XX_register_gpios(uint8_t i2c_bus, uint8_t i2c_addr, int first_minor, uint16_t dir_mask, int num_pins, uint8_t device_type,
|
||||
mcp_gpio_dev_s *_gpio)
|
||||
{
|
||||
bool all_registered = true;
|
||||
|
||||
for (int i = 0; i < num_pins; i++) {
|
||||
uint16_t mask = 1u << i;
|
||||
|
||||
if (!_gpio[i].registered) {
|
||||
if (dir_mask & mask) {
|
||||
_gpio[i] = { {GPIO_INPUT_PIN, {}, &mcp_gpio_ops}, mask, false, nullptr};
|
||||
|
||||
} else {
|
||||
_gpio[i] = { {GPIO_OUTPUT_PIN, {}, &mcp_gpio_ops}, mask, false, nullptr};
|
||||
}
|
||||
|
||||
int ret = gpio_pin_register(&_gpio[i].gpio, first_minor + i);
|
||||
|
||||
if (ret != OK) {
|
||||
all_registered = false;
|
||||
|
||||
} else {
|
||||
_gpio[i].registered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto device_id = device::Device::DeviceId{device::Device::DeviceBusType_I2C, i2c_bus, i2c_addr, device_type};
|
||||
CallbackHandler *callback_handler = new CallbackHandler(ORB_ID(gpio_in));
|
||||
callback_handler->dev_id = device_id.devid;
|
||||
bool callback_registered = callback_handler->registerCallback();
|
||||
|
||||
if (!all_registered || !callback_registered) {
|
||||
for (int i = 0; i < num_pins; i++) {
|
||||
if (_gpio[i].registered) {
|
||||
gpio_pin_unregister(&_gpio[i].gpio, first_minor + i);
|
||||
_gpio[i].registered = false;
|
||||
}
|
||||
}
|
||||
|
||||
callback_handler->unregisterCallback();
|
||||
delete callback_handler;
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_pins; i++) {
|
||||
_gpio[i].callback_handler = callback_handler;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int mcp230XX_unregister_gpios(int first_minor, int num_pins, mcp_gpio_dev_s *_gpio)
|
||||
{
|
||||
if (_gpio[0].callback_handler) {
|
||||
CallbackHandler *callback_handler = _gpio[0].callback_handler;
|
||||
callback_handler->unregisterCallback();
|
||||
delete callback_handler;
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_pins; ++i) {
|
||||
if (_gpio[i].registered) {
|
||||
mcp230XX_setpintype(&_gpio[i].gpio, GPIO_INPUT_PIN);
|
||||
gpio_pin_unregister(&_gpio[i].gpio, first_minor + i);
|
||||
_gpio[i].registered = false;
|
||||
_gpio[i].callback_handler = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
@@ -1,172 +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 <nuttx/ioexpander/gpio.h>
|
||||
#include <drivers/drv_sensor.h>
|
||||
#include <lib/drivers/device/Device.hpp>
|
||||
#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>
|
||||
|
||||
static uint32_t DEVID{0};
|
||||
struct mcp23009_gpio_dev_s {
|
||||
struct gpio_dev_s gpio;
|
||||
uint8_t mask;
|
||||
};
|
||||
|
||||
/* Copy the read input data */
|
||||
class ReadCallback : public uORB::SubscriptionCallback
|
||||
{
|
||||
public:
|
||||
using SubscriptionCallback::SubscriptionCallback;
|
||||
|
||||
void call() override
|
||||
{
|
||||
px4::msg::GpioIn new_input;
|
||||
|
||||
if (update(&new_input) && new_input.device_id == DEVID) {
|
||||
input = new_input.state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint8_t input;
|
||||
};
|
||||
|
||||
static uORB::Publication<px4::msg::GpioRequest> toGpioRequest{ORB_ID(gpio_request)};
|
||||
static ReadCallback fromGpioIn{ORB_ID(gpio_in)};
|
||||
static int mcp23009_read(struct gpio_dev_s *dev, bool *value)
|
||||
{
|
||||
mcp23009_gpio_dev_s *gpio = (struct mcp23009_gpio_dev_s *)dev;
|
||||
*value = fromGpioIn.input & gpio->mask;
|
||||
return OK;
|
||||
}
|
||||
|
||||
static uORB::Publication<gpio_out_s> toGpioOut{ORB_ID(gpio_out)};
|
||||
static int mcp23009_write(struct gpio_dev_s *dev, bool value)
|
||||
{
|
||||
mcp23009_gpio_dev_s *gpio = (struct mcp23009_gpio_dev_s *)dev;
|
||||
gpio_out_s msg{
|
||||
hrt_absolute_time(),
|
||||
DEVID,
|
||||
gpio->mask, // clear mask
|
||||
value ? gpio->mask : 0u, // set mask
|
||||
};
|
||||
return toGpioOut.publish(msg) ? OK : -ETIMEDOUT;
|
||||
}
|
||||
|
||||
static uORB::Publication<gpio_config_s> toGpioConfig{ORB_ID(gpio_config)};
|
||||
static int mcp23009_setpintype(struct gpio_dev_s *dev, enum gpio_pintype_e pintype)
|
||||
{
|
||||
mcp23009_gpio_dev_s *gpio = (struct mcp23009_gpio_dev_s *)dev;
|
||||
gpio_config_s msg{
|
||||
hrt_absolute_time(),
|
||||
DEVID,
|
||||
gpio->mask,
|
||||
};
|
||||
|
||||
switch (pintype) {
|
||||
case GPIO_INPUT_PIN:
|
||||
msg.config = gpio_config_s::INPUT;
|
||||
break;
|
||||
|
||||
case GPIO_INPUT_PIN_PULLUP:
|
||||
msg.config = gpio_config_s::INPUT_PULLUP;
|
||||
break;
|
||||
|
||||
case GPIO_OUTPUT_PIN:
|
||||
msg.config = gpio_config_s::OUTPUT;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return toGpioConfig.publish(msg) ? OK : -ETIMEDOUT;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
static const struct gpio_operations_s mcp23009_gpio_ops {
|
||||
mcp23009_read,
|
||||
mcp23009_write,
|
||||
nullptr,
|
||||
nullptr,
|
||||
mcp23009_setpintype,
|
||||
};
|
||||
|
||||
static constexpr uint8_t NUM_GPIOS = 8;
|
||||
static mcp23009_gpio_dev_s _gpio[NUM_GPIOS] {
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 0) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 1) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 2) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 3) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 4) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 5) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 6) },
|
||||
{ {GPIO_INPUT_PIN, {}, &mcp23009_gpio_ops}, (1u << 7) }
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int mcp23009_register_gpios(uint8_t i2c_bus, uint8_t i2c_addr, int first_minor)
|
||||
{
|
||||
const auto device_id = device::Device::DeviceId{
|
||||
device::Device::DeviceBusType_I2C, i2c_bus, i2c_addr, DRV_GPIO_DEVTYPE_MCP23009};
|
||||
DEVID = device_id.devid;
|
||||
|
||||
for (int i = 0; i < NUM_GPIOS; ++i) {
|
||||
int ret = gpio_pin_register(&_gpio[i].gpio, first_minor + i);
|
||||
|
||||
if (ret != OK) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
fromGpioIn.registerCallback();
|
||||
return OK;
|
||||
}
|
||||
|
||||
int mcp23009_unregister_gpios(int first_minor)
|
||||
{
|
||||
for (int i = 0; i < NUM_GPIOS; ++i) {
|
||||
mcp23009_setpintype(&_gpio[i].gpio, GPIO_INPUT_PIN);
|
||||
gpio_pin_unregister(&_gpio[i].gpio, first_minor + i);
|
||||
}
|
||||
|
||||
fromGpioIn.unregisterCallback();
|
||||
return OK;
|
||||
}
|
||||
+18
-4
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2023 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,10 +30,24 @@
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/ioexpander/gpio.h>
|
||||
#include <lib/drivers/mcp_common/CallbackHandler.hpp>
|
||||
|
||||
int mcp23009_register_gpios(uint8_t i2c_bus, uint8_t i2c_addr, int first_minor = 0);
|
||||
int mcp23009_unregister_gpios(int first_minor = 0);
|
||||
#ifndef CONFIG_DEV_GPIO
|
||||
# error "CONFIG_DEV_GPIO is required to use MCP GPIO expander, enable it in your NuttX config"
|
||||
#endif
|
||||
|
||||
struct mcp_gpio_dev_s {
|
||||
struct gpio_dev_s gpio;
|
||||
uint16_t mask;
|
||||
bool registered = false;
|
||||
CallbackHandler *callback_handler;
|
||||
};
|
||||
|
||||
int mcp230XX_register_gpios(uint8_t i2c_bus, uint8_t i2c_addr, int first_minor = 0, uint16_t dir_mask = 0x0000, int num_pins = 8,
|
||||
uint8_t device_type = 0, mcp_gpio_dev_s *_gpio = nullptr);
|
||||
int mcp230XX_unregister_gpios(int first_minor = 0, int num_pins = 0, mcp_gpio_dev_s *_gpio = nullptr);
|
||||
Reference in New Issue
Block a user