Driver: Refactor MCP23009 GPIO expander into uORB driver

This commit is contained in:
Niklas Hauser
2023-06-19 07:58:21 +02:00
committed by Beat Küng
parent 3303323971
commit 8fe65c6722
19 changed files with 697 additions and 300 deletions
+9
View File
@@ -0,0 +1,9 @@
menu "GPIO"
menuconfig COMMON_GPIO
bool "Common GPIOs"
default n
select DRIVERS_GPIO_MCP23009
---help---
Enable default set of GPIO drivers
rsource "*/Kconfig"
endmenu
+41
View File
@@ -0,0 +1,41 @@
############################################################################
#
# 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__mcp23009
MAIN mcp23009
COMPILE_FLAGS
SRCS
mcp23009_main.cpp
mcp23009.cpp
DEPENDS
)
+5
View File
@@ -0,0 +1,5 @@
menuconfig DRIVERS_GPIO_MCP23009
bool "mcp23009"
default n
---help---
Enable support for mcp23009
+123
View File
@@ -0,0 +1,123 @@
/****************************************************************************
*
* 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);
}
+111
View File
@@ -0,0 +1,111 @@
/****************************************************************************
*
* Copyright (c) 2020 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 <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>
using namespace time_literals;
class MCP23009 : public device::I2C, public I2CSPIDriver<MCP23009>
{
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
Register : uint8_t {
IODIR = 0x00,
IPOL = 0x01,
GPINTEN = 0x02,
DEFVAL = 0x03,
INTCON = 0x04,
IOCON = 0x05,
GPPU = 0x06,
INTF = 0x07,
INTCAP = 0x08,
GPIO = 0x09,
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);
};
+215
View File
@@ -0,0 +1,215 @@
/****************************************************************************
*
* 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;
}
+2 -2
View File
@@ -314,8 +314,8 @@ $ gpio read H4 PULLUP
Set the output value on Port E pin 7 to high
$ gpio write E7 1 --force
Set the output value on device /dev/gpin1 to high
$ gpio write /dev/gpin1 1
Set the output value on device /dev/gpio1 to high
$ gpio write /dev/gpio1 1
)DESCR_STR");