Creataing the Hodor project.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* This file is just to keep the compatibility with the old version of the library. Please use the file `esp_io_expander.hpp` instead.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#warning "This file is deprecated. Please use the file `esp_io_expander.hpp` instead."
|
||||
|
||||
#include "esp_io_expander.hpp"
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_expander_utils.h"
|
||||
#include "esp_expander_base.hpp"
|
||||
|
||||
// Check whether it is a valid pin number
|
||||
#define IS_VALID_PIN(pin_num) (pin_num < IO_COUNT_MAX)
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
bool Base::init(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(!checkIsInit(), false, "Already initialized");
|
||||
|
||||
if (!checkIsSkipInitHost()) {
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(i2c_param_config(getHostID(), &_host_config), false, "I2C param config failed");
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
i2c_driver_install(getHostID(), _host_config.mode, 0, 0, 0), false, "I2C driver install failed"
|
||||
);
|
||||
ESP_UTILS_LOGI("Init I2C host(%d)", _host_id);
|
||||
}
|
||||
_flags.is_init = true;
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Base::reset(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_reset(device_handle), false, "Reset failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Base::del(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
if (checkIsBegun()) {
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_del(device_handle), false, "Delete failed");
|
||||
device_handle = nullptr;
|
||||
ESP_UTILS_LOGD("Delete IO expander(@%p)", device_handle);
|
||||
}
|
||||
|
||||
if (checkIsInit()) {
|
||||
if (!checkIsSkipInitHost()) {
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(i2c_driver_delete(getHostID()), false, "I2C driver delete failed");
|
||||
ESP_UTILS_LOGI("Delete I2C host(%d)", _host_id);
|
||||
}
|
||||
_flags.is_init = false;
|
||||
}
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Base::pinMode(uint8_t pin, uint8_t mode)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin(%d), mode(%d)", pin, mode);
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), false, "Invalid pin");
|
||||
ESP_UTILS_CHECK_FALSE_RETURN((mode == INPUT) || (mode == OUTPUT), false, "Invalid mode");
|
||||
|
||||
esp_io_expander_dir_t dir = (mode == INPUT) ? IO_EXPANDER_INPUT : IO_EXPANDER_OUTPUT;
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_set_dir(device_handle, BIT64(pin), dir), false, "Set dir failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Base::digitalWrite(uint8_t pin, uint8_t value)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin(%d), value(%d)", pin, value);
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), false, "Invalid pin");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_set_level(device_handle, BIT64(pin), value), false, "Set level failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int Base::digitalRead(uint8_t pin)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin(%d)", pin);
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(IS_VALID_PIN(pin), -1, "Invalid pin");
|
||||
|
||||
uint32_t level = 0;
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_get_level(device_handle, BIT64(pin), &level), -1, "Get level failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return (level & BIT64(pin)) ? HIGH : LOW;
|
||||
}
|
||||
|
||||
bool Base::multiPinMode(uint32_t pin_mask, uint8_t mode)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin_mask(%0x), mode(%d)", pin_mask, mode);
|
||||
ESP_UTILS_CHECK_FALSE_RETURN((mode == INPUT) || (mode == OUTPUT), false, "Invalid mode");
|
||||
|
||||
esp_io_expander_dir_t dir = (mode == INPUT) ? IO_EXPANDER_INPUT : IO_EXPANDER_OUTPUT;
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_set_dir(device_handle, pin_mask, dir), false, "Set dir failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Base::multiDigitalWrite(uint32_t pin_mask, uint8_t value)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin_mask(%0x), value(%d)", pin_mask, value);
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_set_level(device_handle, pin_mask, value), false, "Set level failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t Base::multiDigitalRead(uint32_t pin_mask)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_LOGD("Param: pin_mask(%0x)", pin_mask);
|
||||
|
||||
uint32_t level = 0;
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_get_level(device_handle, pin_mask, &level), false, "Get level failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return level;
|
||||
}
|
||||
|
||||
bool Base::printStatus(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(esp_io_expander_print_state(device_handle), false, "Print state failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace esp_expander
|
||||
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "driver/i2c.h"
|
||||
#include "port/esp_io_expander.h"
|
||||
|
||||
// Refer to `esp32-hal-gpio.h` in Arduino
|
||||
#ifndef INPUT
|
||||
#define INPUT 0x01
|
||||
#endif
|
||||
#ifndef OUTPUT
|
||||
#define OUTPUT 0x03
|
||||
#endif
|
||||
#ifndef LOW
|
||||
#define LOW 0x0
|
||||
#endif
|
||||
#ifndef HIGH
|
||||
#define HIGH 0x1
|
||||
#endif
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
/**
|
||||
* @brief The IO expander device class
|
||||
*
|
||||
* @note This is a base class for all chips. Due to it is a virtual class, users cannot use it directly
|
||||
*
|
||||
*/
|
||||
class Base {
|
||||
public:
|
||||
/* Default I2C host ID */
|
||||
constexpr static int HOST_ID_DEFAULT = static_cast<int>(I2C_NUM_0);
|
||||
|
||||
/**
|
||||
* @brief Configuration for Base object
|
||||
*
|
||||
*/
|
||||
struct Config {
|
||||
i2c_port_t getHostID(void) const
|
||||
{
|
||||
return static_cast<i2c_port_t>(host_id);
|
||||
}
|
||||
|
||||
i2c_config_t getHostConfig(void) const
|
||||
{
|
||||
return {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = host_sda_io_num,
|
||||
.scl_io_num = host_scl_io_num,
|
||||
.sda_pullup_en = host_sda_pullup_en,
|
||||
.scl_pullup_en = host_scl_pullup_en,
|
||||
.master = {
|
||||
.clk_speed = host_clk_speed,
|
||||
},
|
||||
.clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL,
|
||||
};
|
||||
}
|
||||
|
||||
uint8_t getDeviceAddress(void) const
|
||||
{
|
||||
return device_address;
|
||||
}
|
||||
|
||||
static Config create(int scl_io, int sda_io, uint8_t address)
|
||||
{
|
||||
return Config{
|
||||
.host_sda_io_num = sda_io,
|
||||
.host_scl_io_num = scl_io,
|
||||
.device_address = address,
|
||||
.skip_init_host = false,
|
||||
};
|
||||
}
|
||||
|
||||
static Config create(int host_id, uint8_t address)
|
||||
{
|
||||
return Config{
|
||||
.host_id = host_id,
|
||||
.device_address = address,
|
||||
.skip_init_host = true,
|
||||
};
|
||||
}
|
||||
|
||||
// Host
|
||||
int host_id = HOST_ID_DEFAULT; /*!< I2C host ID */
|
||||
int host_sda_io_num = -1; /*!< I2C SDA pin number */
|
||||
int host_scl_io_num = -1; /*!< I2C SCL pin number */
|
||||
bool host_sda_pullup_en = GPIO_PULLUP_ENABLE; /*!< I2C SDA pullup enable */
|
||||
bool host_scl_pullup_en = GPIO_PULLUP_ENABLE; /*!< I2C SCL pullup enable */
|
||||
uint32_t host_clk_speed = 400000; /*!< I2C clock speed */
|
||||
// Device
|
||||
uint8_t device_address = 0; /*!< I2C device 7-bit address */
|
||||
// Extra
|
||||
bool skip_init_host = false; /*!< Skip I2C initialization when call `init()` */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Construct a base device. With this function, call `init()` will initialize I2C by using the host
|
||||
* configuration.
|
||||
*
|
||||
* @param[in] scl_io I2C SCL pin number
|
||||
* @param[in] sda_io I2C SDA pin number
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
Base(int scl_io, int sda_io, uint8_t address)
|
||||
{
|
||||
auto config = Config::create(scl_io, sda_io, address);
|
||||
_host_id = config.getHostID();
|
||||
_host_config = config.getHostConfig();
|
||||
_device_address = config.getDeviceAddress();
|
||||
_flags.skip_init_host = config.skip_init_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a base device. With this function, call `init()` will not initialize I2C, and users should
|
||||
* initialize it manually.
|
||||
*
|
||||
* @param[in] host_id I2C host ID.
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
Base(int host_id, uint8_t address)
|
||||
{
|
||||
auto config = Config::create(host_id, address);
|
||||
_host_id = config.getHostID();
|
||||
_host_config = config.getHostConfig();
|
||||
_device_address = config.getDeviceAddress();
|
||||
_flags.skip_init_host = config.skip_init_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Construct a base device.
|
||||
*
|
||||
* @param[in] config Configuration for the object
|
||||
*
|
||||
*/
|
||||
Base(const Config &config)
|
||||
{
|
||||
_host_id = config.getHostID();
|
||||
_host_config = config.getHostConfig();
|
||||
_device_address = config.getDeviceAddress();
|
||||
_flags.skip_init_host = config.skip_init_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use other constructors instead.
|
||||
*
|
||||
*/
|
||||
Base(i2c_port_t id, uint8_t address, int scl_io, int sda_io):
|
||||
Base(scl_io, sda_io, address)
|
||||
{
|
||||
_host_id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Virtual desutruct object.
|
||||
*
|
||||
* @note Here make it virtual so that we can delete the derived object by using the base pointer.
|
||||
*
|
||||
*/
|
||||
virtual ~Base() = default;
|
||||
|
||||
/**
|
||||
* @brief Initialize object
|
||||
*
|
||||
* @note This function will initialize I2C if needed.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool init(void);
|
||||
|
||||
/**
|
||||
* @brief Begin object
|
||||
*
|
||||
* @note This function typically calls `esp_io_expander_new_i2c_*()` to create the IO expander handle.
|
||||
*
|
||||
*/
|
||||
virtual bool begin(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief Reset object
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool reset(void);
|
||||
|
||||
/**
|
||||
* @brief Delete object
|
||||
*
|
||||
*/
|
||||
bool del(void);
|
||||
|
||||
/**
|
||||
* @brief Set pin mode
|
||||
*
|
||||
* @note This function is same as Arduino's `pinMode()`.
|
||||
*
|
||||
* @param[in] pin Pin number (0-31)
|
||||
* @param[in] mode Pin mode (INPUT / OUTPUT)
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool pinMode(uint8_t pin, uint8_t mode);
|
||||
|
||||
/**
|
||||
* @brief Set pin level
|
||||
*
|
||||
* @note This function is same as Arduino's `digitalWrite()`.
|
||||
*
|
||||
* @param[in] pin Pin number (0-31)
|
||||
* @param[in] value Pin level (HIGH / LOW)
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool digitalWrite(uint8_t pin, uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief Read pin level
|
||||
*
|
||||
* @note This function is same as Arduino's `digitalRead()`.
|
||||
*
|
||||
* @param[in] pin Pin number (0-31)
|
||||
*
|
||||
* @return Pin level. HIGH or LOW if success, otherwise -1
|
||||
*
|
||||
*/
|
||||
int digitalRead(uint8_t pin);
|
||||
|
||||
/**
|
||||
* @brief Set multiple pin modes
|
||||
*
|
||||
* @param pin_mask Pin mask (Bitwise OR of `IO_EXPANDER_PIN_NUM_*`)
|
||||
* @param mode Mode to set (INPUT / OUTPUT)
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool multiPinMode(uint32_t pin_mask, uint8_t mode);
|
||||
|
||||
/**
|
||||
* @brief Set multiple pins level
|
||||
*
|
||||
* @param pin_mask Pin mask (Bitwise OR of `IO_EXPANDER_PIN_NUM_*`)
|
||||
* @param value Value to write (HIGH / LOW)
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool multiDigitalWrite(uint32_t pin_mask, uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief Read multiple pin levels
|
||||
*
|
||||
* @param pin_mask Pin mask (Bitwise OR of `IO_EXPANDER_PIN_NUM_*`)
|
||||
*
|
||||
* @return Pin levels, every bit represents a pin (HIGH / LOW)
|
||||
*
|
||||
*/
|
||||
int64_t multiDigitalRead(uint32_t pin_mask);
|
||||
|
||||
/**
|
||||
* @brief Print IO expander status, include pin index, direction, input level and output level
|
||||
*
|
||||
* @return Pin levels, every bit represents a pin (HIGH / LOW)
|
||||
*
|
||||
*/
|
||||
bool printStatus(void);
|
||||
|
||||
/**
|
||||
* @brief Get low-level handle. Users can use this handle to call low-level functions (esp_io_expander_*()).
|
||||
*
|
||||
* @return Handle if success, otherwise nullptr
|
||||
*
|
||||
*/
|
||||
esp_io_expander_handle_t getDeviceHandle(void)
|
||||
{
|
||||
return device_handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `getDeviceHandle()` instead.
|
||||
*
|
||||
*/
|
||||
[[deprecated("Deprecated and will be removed in the next major version. Please use `getDeviceHandle()` instead.")]]
|
||||
esp_io_expander_handle_t getHandle(void)
|
||||
{
|
||||
return getDeviceHandle();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool checkIsInit(void)
|
||||
{
|
||||
return _flags.is_init;
|
||||
}
|
||||
|
||||
bool checkIsBegun(void)
|
||||
{
|
||||
return (device_handle != nullptr);
|
||||
}
|
||||
|
||||
bool checkIsSkipInitHost(void)
|
||||
{
|
||||
return _flags.skip_init_host;
|
||||
}
|
||||
|
||||
i2c_port_t getHostID(void)
|
||||
{
|
||||
return static_cast<i2c_port_t>(_host_id);
|
||||
}
|
||||
|
||||
const i2c_config_t &getHostConfig(void)
|
||||
{
|
||||
return _host_config;
|
||||
}
|
||||
|
||||
uint8_t getDeviceAddress(void)
|
||||
{
|
||||
return _device_address;
|
||||
}
|
||||
|
||||
esp_io_expander_handle_t device_handle = nullptr;
|
||||
|
||||
private:
|
||||
struct {
|
||||
uint8_t is_init: 1;
|
||||
uint8_t skip_init_host: 1;
|
||||
} _flags = {};
|
||||
// Host
|
||||
int _host_id = HOST_ID_DEFAULT;
|
||||
i2c_config_t _host_config = {};
|
||||
// Device
|
||||
uint8_t _device_address = 0;
|
||||
};
|
||||
|
||||
} // namespace esp_expander
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `esp_expander::Base` instead.
|
||||
*
|
||||
*/
|
||||
typedef esp_expander::Base ESP_IOExpander __attribute__((deprecated("Deprecated and will be removed in the next major version. Please use `esp_expander::Base` instead.")));
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
#include "port/esp_io_expander_ch422g.h"
|
||||
#include "esp_expander_ch422g.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
CH422G::~CH422G()
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_EXIT(del(), "Delete failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
}
|
||||
|
||||
bool CH422G::begin(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsInit(), false, "Not initialized");
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(!checkIsBegun(), false, "Already begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_new_i2c_ch422g(getHostID(), getDeviceAddress(), &device_handle), false,
|
||||
"Create CH422G IO expander failed"
|
||||
);
|
||||
ESP_UTILS_LOGD("Create CH422G IO expander(@%p)", device_handle);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CH422G::enableOC_OpenDrain(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_ch422g_set_oc_open_drain(device_handle), false, "Set OC open-drain failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CH422G::enableOC_PushPull(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_ch422g_set_oc_push_pull(device_handle), false, "Set OC push-pull failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CH422G::enableAllIO_Input(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_ch422g_set_all_input(device_handle), false, "Set all input failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CH422G::enableAllIO_Output(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsBegun(), false, "Not begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_ch422g_set_all_output(device_handle), false, "Set all output failed"
|
||||
);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace esp_expander
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_expander_base.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
/**
|
||||
* @brief The CH422G IO expander device class
|
||||
*
|
||||
* @note This class is a derived class of `esp_expander::Base`, user can use it directly
|
||||
* @note Pin map:
|
||||
* | Pin Number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|
||||
* | ------------ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
* | Function | IO0 | IO1 | IO2 | IO3 | IO4 | IO5 | IO6 | IO7 | OC0 | OC1 | OC2 | OC3 |
|
||||
*
|
||||
*/
|
||||
class CH422G: public Base {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a CH422G device. With this function, call `init()` will initialize I2C by using the host
|
||||
* configuration.
|
||||
*
|
||||
* @param[in] scl_io I2C SCL pin number
|
||||
* @param[in] sda_io I2C SDA pin number
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
CH422G(int scl_io, int sda_io, uint8_t address): Base(scl_io, sda_io, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a CH422G device. With this function, call `init()` will not initialize I2C, and users should
|
||||
* initialize it manually.
|
||||
*
|
||||
* @param[in] host_id I2C host ID.
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
CH422G(int host_id, uint8_t address): Base(host_id, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a CH422G device.
|
||||
*
|
||||
* @param[in] config Configuration for the object
|
||||
*
|
||||
*/
|
||||
CH422G(const Config &config): Base(config) {}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use other constructors instead.
|
||||
*
|
||||
*/
|
||||
[[deprecated("Deprecated and will be removed in the next major version. Please use other constructors instead.")]]
|
||||
CH422G(i2c_port_t id, uint8_t address, int scl_io, int sda_io): Base(id, address, scl_io, sda_io) {}
|
||||
|
||||
/**
|
||||
* @brief Desutruct object. This function will call `del()` to delete the object.
|
||||
*
|
||||
*/
|
||||
~CH422G() override;
|
||||
|
||||
/**
|
||||
* @brief Begin object
|
||||
*
|
||||
* @note This function typically calls `esp_io_expander_new_i2c_*()` to create the IO expander handle.
|
||||
* @note This function sets all IO0-7 pins to output high-level mode by default.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool begin(void) override;
|
||||
|
||||
/**
|
||||
* @brief Enable OC0-OC3 output open-drain
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool enableOC_OpenDrain(void);
|
||||
|
||||
/**
|
||||
* @brief Enable OC0-OC3 output push-pull (default mode when power-on)
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool enableOC_PushPull(void);
|
||||
|
||||
/**
|
||||
* @brief Enable IO0-7 input mode
|
||||
*
|
||||
* @note The driver initialization by default sets CH422G's IO0-7 to output high-level mode.
|
||||
* @note Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to
|
||||
* input mode when it determines that all pins are configured as input.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool enableAllIO_Input(void);
|
||||
|
||||
/**
|
||||
* @brief Enable IO0-7 output mode
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool enableAllIO_Output(void);
|
||||
};
|
||||
|
||||
} // namespace esp_expander
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `esp_expander::CH422G` instead.
|
||||
*
|
||||
*/
|
||||
typedef esp_expander::CH422G ESP_IOExpander_CH422G __attribute__((deprecated("Deprecated and will be removed in the next major version. Please use `esp_expander::CH422G` instead.")));
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
#include "port/esp_io_expander_ht8574.h"
|
||||
#include "esp_expander_ht8574.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
HT8574::~HT8574()
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_EXIT(del(), "Delete failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
}
|
||||
|
||||
bool HT8574::begin(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsInit(), false, "Not initialized");
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(!checkIsBegun(), false, "Already begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_new_i2c_ht8574(getHostID(), getDeviceAddress(), &device_handle), false,
|
||||
"Create HT8574 IO expander failed"
|
||||
);
|
||||
ESP_UTILS_LOGD("Create HT8574 IO expander(@%p)", device_handle);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace esp_expander
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_expander_base.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
/**
|
||||
* @brief The HT8574 IO expander device class
|
||||
*
|
||||
* @note This class is a derived class of `esp_expander::Base`, user can use it directly
|
||||
*
|
||||
*/
|
||||
class HT8574: public Base {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a HT8574 device. With this function, call `init()` will initialize I2C by using the host
|
||||
* configuration.
|
||||
*
|
||||
* @param[in] scl_io I2C SCL pin number
|
||||
* @param[in] sda_io I2C SDA pin number
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
HT8574(int scl_io, int sda_io, uint8_t address): Base(scl_io, sda_io, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a HT8574 device. With this function, call `init()` will not initialize I2C, and users should
|
||||
* initialize it manually.
|
||||
*
|
||||
* @param[in] host_id I2C host ID.
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
HT8574(int host_id, uint8_t address): Base(host_id, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a HT8574 device.
|
||||
*
|
||||
* @param[in] config Configuration for the object
|
||||
*
|
||||
*/
|
||||
HT8574(const Config &config): Base(config) {}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use other constructors instead.
|
||||
*
|
||||
*/
|
||||
[[deprecated("Deprecated and will be removed in the next major version. Please use other constructors instead.")]]
|
||||
HT8574(i2c_port_t id, uint8_t address, int scl_io, int sda_io): Base(id, address, scl_io, sda_io) {}
|
||||
|
||||
/**
|
||||
* @brief Desutruct object. This function will call `del()` to delete the object.
|
||||
*
|
||||
*/
|
||||
~HT8574() override;
|
||||
|
||||
/**
|
||||
* @brief Begin object
|
||||
*
|
||||
* @note This function typically calls `esp_io_expander_new_i2c_*()` to create the IO expander handle.
|
||||
* @note The driver initialization by default sets CH422G's IO0-7 to output high-level mode.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool begin(void) override;
|
||||
};
|
||||
|
||||
} // namespace esp_expander
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `esp_expander::HT8574` instead.
|
||||
*
|
||||
*/
|
||||
typedef esp_expander::HT8574 ESP_IOExpander_HT8574 __attribute__((deprecated("Deprecated and will be removed in the next major version. Please use `esp_expander::HT8574` instead.")));
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
#include "port/esp_io_expander_tca95xx_16bit.h"
|
||||
#include "esp_expander_tca95xx_16bit.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
TCA95XX_16BIT::~TCA95XX_16BIT()
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_EXIT(del(), "Delete failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
}
|
||||
|
||||
bool TCA95XX_16BIT::begin(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsInit(), false, "Not initialized");
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(!checkIsBegun(), false, "Already begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_new_i2c_tca95xx_16bit(getHostID(), getDeviceAddress(), &device_handle), false,
|
||||
"Create TCA95XX_16BIT IO expander failed"
|
||||
);
|
||||
ESP_UTILS_LOGD("Create TCA95XX_16BIT IO expander(@%p)", device_handle);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace esp_expander
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_expander_base.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
/**
|
||||
* @brief The TCA95XX_16BIT IO expander device class
|
||||
*
|
||||
* @note This class is a derived class of `esp_expander::Base`, user can use it directly
|
||||
*
|
||||
*/
|
||||
class TCA95XX_16BIT: public Base {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a TCA95XX_16BIT device. With this function, call `init()` will initialize I2C by using the host
|
||||
* configuration.
|
||||
*
|
||||
* @param[in] scl_io I2C SCL pin number
|
||||
* @param[in] sda_io I2C SDA pin number
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
TCA95XX_16BIT(int scl_io, int sda_io, uint8_t address): Base(scl_io, sda_io, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a TCA95XX_16BIT device. With this function, call `init()` will not initialize I2C, and users
|
||||
* should initialize it manually.
|
||||
*
|
||||
* @param[in] host_id I2C host ID.
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
TCA95XX_16BIT(int host_id, uint8_t address): Base(host_id, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a TCA95XX_16BIT device.
|
||||
*
|
||||
* @param[in] config Configuration for the object
|
||||
*
|
||||
*/
|
||||
TCA95XX_16BIT(const Config &config): Base(config) {}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use other constructors instead.
|
||||
*
|
||||
*/
|
||||
[[deprecated("Deprecated and will be removed in the next major version. Please use other constructors instead.")]]
|
||||
TCA95XX_16BIT(i2c_port_t id, uint8_t address, int scl_io, int sda_io): Base(id, address, scl_io, sda_io) {}
|
||||
|
||||
/**
|
||||
* @brief Desutruct object. This function will call `del()` to delete the object.
|
||||
*
|
||||
*/
|
||||
~TCA95XX_16BIT() override;
|
||||
|
||||
/**
|
||||
* @brief Begin object
|
||||
*
|
||||
* @note This function typically calls `esp_io_expander_new_i2c_*()` to create the IO expander handle.
|
||||
* @note This function sets all pins to inpurt mode by default.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool begin(void) override;
|
||||
};
|
||||
|
||||
} // namespace esp_expander
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `esp_expander::TCA95XX_16BIT` instead.
|
||||
*
|
||||
*/
|
||||
typedef esp_expander::TCA95XX_16BIT ESP_IOExpander_TCA95xx_16bit __attribute__((deprecated("Deprecated and will be removed in the next major version. Please use `esp_expander::TCA95XX_16BIT` instead.")));
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
#include "port/esp_io_expander_tca9554.h"
|
||||
#include "esp_expander_tca95xx_8bit.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
TCA95XX_8BIT::~TCA95XX_8BIT()
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_EXIT(del(), "Delete failed");
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
}
|
||||
|
||||
bool TCA95XX_8BIT::begin(void)
|
||||
{
|
||||
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
|
||||
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(checkIsInit(), false, "Not initialized");
|
||||
ESP_UTILS_CHECK_FALSE_RETURN(!checkIsBegun(), false, "Already begun");
|
||||
|
||||
ESP_UTILS_CHECK_ERROR_RETURN(
|
||||
esp_io_expander_new_i2c_tca9554(getHostID(), getDeviceAddress(), &device_handle), false,
|
||||
"Create TCA95XX_8BIT IO expander failed"
|
||||
);
|
||||
ESP_UTILS_LOGD("Create TCA95XX_8BIT IO expander(@%p)", device_handle);
|
||||
|
||||
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace esp_expander
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_expander_base.hpp"
|
||||
|
||||
namespace esp_expander {
|
||||
|
||||
/**
|
||||
* @brief The TCA95XX_8BIT IO expander device class
|
||||
*
|
||||
* @note This class is a derived class of `esp_expander::Base`, user can use it directly
|
||||
*
|
||||
*/
|
||||
class TCA95XX_8BIT: public Base {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a TCA95XX_8BIT device. With this function, call `init()` will initialize I2C by using the host
|
||||
* configuration.
|
||||
*
|
||||
* @param[in] scl_io I2C SCL pin number
|
||||
* @param[in] sda_io I2C SDA pin number
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
TCA95XX_8BIT(int scl_io, int sda_io, uint8_t address): Base(scl_io, sda_io, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a TCA95XX_8BIT device. With this function, call `init()` will not initialize I2C, and users should
|
||||
* initialize it manually.
|
||||
*
|
||||
* @param[in] host_id I2C host ID.
|
||||
* @param[in] address I2C device 7-bit address. Should be like `ESP_IO_EXPANDER_I2C_<chip name>_ADDRESS`.
|
||||
*
|
||||
*/
|
||||
TCA95XX_8BIT(int host_id, uint8_t address): Base(host_id, address) {}
|
||||
|
||||
/**
|
||||
* @brief Construct a TCA95XX_8BIT device.
|
||||
*
|
||||
* @param[in] config Configuration for the object
|
||||
*
|
||||
*/
|
||||
TCA95XX_8BIT(const Config &config): Base(config) {}
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use other constructors instead.
|
||||
*
|
||||
*/
|
||||
[[deprecated("Deprecated and will be removed in the next major version. Please use other constructors instead.")]]
|
||||
TCA95XX_8BIT(i2c_port_t id, uint8_t address, int scl_io, int sda_io): Base(id, address, scl_io, sda_io) {}
|
||||
|
||||
/**
|
||||
* @brief Desutruct object. This function will call `del()` to delete the object.
|
||||
*
|
||||
*/
|
||||
~TCA95XX_8BIT() override;
|
||||
|
||||
/**
|
||||
* @brief Begin object
|
||||
*
|
||||
* @note This function typically calls `esp_io_expander_new_i2c_*()` to create the IO expander handle.
|
||||
* @note This function sets all pins to inpurt mode by default.
|
||||
*
|
||||
* @return true if success, otherwise false
|
||||
*
|
||||
*/
|
||||
bool begin(void) override;
|
||||
};
|
||||
|
||||
} // namespace esp_expander
|
||||
|
||||
/**
|
||||
* @deprecated Deprecated and will be removed in the next major version. Please use `esp_expander::TCA95XX_8BIT` instead.
|
||||
*
|
||||
*/
|
||||
typedef esp_expander::TCA95XX_8BIT ESP_IOExpander_TCA95xx_8bit __attribute__((deprecated("Deprecated and will be removed in the next major version. Please use `esp_expander::TCA95XX_8BIT` instead.")));
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// Define the log tag for the current library, should be declared before `esp_lib_utils.hpp`
|
||||
#define ESP_UTILS_LOG_TAG "Expander"
|
||||
#include "esp_lib_utils.h"
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/* Porting drivers */
|
||||
#include "port/esp_io_expander.h"
|
||||
#include "port/esp_io_expander_ch422g.h"
|
||||
#include "port/esp_io_expander_ht8574.h"
|
||||
#include "port/esp_io_expander_tca9554.h"
|
||||
#include "port/esp_io_expander_tca95xx_16bit.h"
|
||||
|
||||
/* Wrapper classes */
|
||||
#include "chip/esp_expander_base.hpp"
|
||||
#include "chip/esp_expander_ch422g.hpp"
|
||||
#include "chip/esp_expander_ht8574.hpp"
|
||||
#include "chip/esp_expander_tca95xx_8bit.hpp"
|
||||
#include "chip/esp_expander_tca95xx_16bit.hpp"
|
||||
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "esp_bit_defs.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
|
||||
#define VALID_IO_COUNT(handle) ((handle)->config.io_count <= IO_COUNT_MAX ? (handle)->config.io_count : IO_COUNT_MAX)
|
||||
|
||||
/**
|
||||
* @brief Register type
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
REG_INPUT = 0,
|
||||
REG_OUTPUT,
|
||||
REG_DIRECTION,
|
||||
} reg_type_t;
|
||||
|
||||
static char *TAG = "io_expander";
|
||||
|
||||
static esp_err_t write_reg(esp_io_expander_handle_t handle, reg_type_t reg, uint32_t value);
|
||||
static esp_err_t read_reg(esp_io_expander_handle_t handle, reg_type_t reg, uint32_t *value);
|
||||
|
||||
esp_err_t esp_io_expander_set_dir(esp_io_expander_handle_t handle, uint32_t pin_num_mask, esp_io_expander_dir_t direction)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
if (pin_num_mask >= BIT64(VALID_IO_COUNT(handle))) {
|
||||
ESP_LOGW(TAG, "Pin num mask out of range, bit higher than %d won't work", VALID_IO_COUNT(handle) - 1);
|
||||
}
|
||||
|
||||
bool is_output = (direction == IO_EXPANDER_OUTPUT) ? true : false;
|
||||
uint32_t dir_reg, temp;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_DIRECTION, &dir_reg), TAG, "Read direction reg failed");
|
||||
temp = dir_reg;
|
||||
if ((is_output && !handle->config.flags.dir_out_bit_zero) || (!is_output && handle->config.flags.dir_out_bit_zero)) {
|
||||
/* 1. Output && Set 1 to output */
|
||||
/* 2. Input && Set 1 to input */
|
||||
dir_reg |= pin_num_mask;
|
||||
} else {
|
||||
/* 3. Output && Set 0 to output */
|
||||
/* 4. Input && Set 0 to input */
|
||||
dir_reg &= ~pin_num_mask;
|
||||
}
|
||||
/* Write to reg only when different */
|
||||
if (dir_reg != temp) {
|
||||
ESP_RETURN_ON_ERROR(write_reg(handle, REG_DIRECTION, dir_reg), TAG, "Write direction reg failed");
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_set_level(esp_io_expander_handle_t handle, uint32_t pin_num_mask, uint8_t level)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
if (pin_num_mask >= BIT64(VALID_IO_COUNT(handle))) {
|
||||
ESP_LOGW(TAG, "Pin num mask out of range, bit higher than %d won't work", VALID_IO_COUNT(handle) - 1);
|
||||
}
|
||||
|
||||
uint32_t dir_reg, dir_bit;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_DIRECTION, &dir_reg), TAG, "Read direction reg failed");
|
||||
|
||||
uint8_t io_count = VALID_IO_COUNT(handle);
|
||||
/* Check every target pin's direction, must be in output mode */
|
||||
for (int i = 0; i < io_count; i++) {
|
||||
if (pin_num_mask & BIT(i)) {
|
||||
dir_bit = dir_reg & BIT(i);
|
||||
/* Check whether it is in input mode */
|
||||
if ((dir_bit && handle->config.flags.dir_out_bit_zero) || (!dir_bit && !handle->config.flags.dir_out_bit_zero)) {
|
||||
/* 1. 1 && Set 1 to input */
|
||||
/* 2. 0 && Set 0 to input */
|
||||
ESP_LOGE(TAG, "Pin[%d] can't set level in input mode", i);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t output_reg, temp;
|
||||
/* Read the current output level */
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_OUTPUT, &output_reg), TAG, "Read Output reg failed");
|
||||
temp = output_reg;
|
||||
/* Set expected output level */
|
||||
if ((level && !handle->config.flags.output_high_bit_zero) || (!level && handle->config.flags.output_high_bit_zero)) {
|
||||
/* 1. High level && Set 1 to output high */
|
||||
/* 2. Low level && Set 1 to output low */
|
||||
output_reg |= pin_num_mask;
|
||||
} else {
|
||||
/* 3. High level && Set 0 to output high */
|
||||
/* 4. Low level && Set 0 to output low */
|
||||
output_reg &= ~pin_num_mask;
|
||||
}
|
||||
/* Write to reg only when different */
|
||||
if (output_reg != temp) {
|
||||
ESP_RETURN_ON_ERROR(write_reg(handle, REG_OUTPUT, output_reg), TAG, "Write Output reg failed");
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_get_level(esp_io_expander_handle_t handle, uint32_t pin_num_mask, uint32_t *level_mask)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
ESP_RETURN_ON_FALSE(level_mask, ESP_ERR_INVALID_ARG, TAG, "Invalid level");
|
||||
if (pin_num_mask >= BIT64(VALID_IO_COUNT(handle))) {
|
||||
ESP_LOGW(TAG, "Pin num mask out of range, bit higher than %d won't work", VALID_IO_COUNT(handle) - 1);
|
||||
}
|
||||
|
||||
uint32_t input_reg;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_INPUT, &input_reg), TAG, "Read input reg failed");
|
||||
if (!handle->config.flags.input_high_bit_zero) {
|
||||
/* Get 1 when input high level */
|
||||
*level_mask = input_reg & pin_num_mask;
|
||||
} else {
|
||||
/* Get 0 when input high level */
|
||||
*level_mask = ~input_reg & pin_num_mask;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_print_state(esp_io_expander_handle_t handle)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
|
||||
uint8_t io_count = VALID_IO_COUNT(handle);
|
||||
uint32_t input_reg, output_reg, dir_reg;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_INPUT, &input_reg), TAG, "Read input reg failed");
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_OUTPUT, &output_reg), TAG, "Read output reg failed");
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_DIRECTION, &dir_reg), TAG, "Read direction reg failed");
|
||||
/* Get 1 if high level */
|
||||
if (handle->config.flags.input_high_bit_zero) {
|
||||
input_reg ^= 0xffffffff;
|
||||
}
|
||||
/* Get 1 if high level */
|
||||
if (handle->config.flags.output_high_bit_zero) {
|
||||
output_reg ^= 0xffffffff;
|
||||
}
|
||||
/* Get 1 if output */
|
||||
if (handle->config.flags.dir_out_bit_zero) {
|
||||
dir_reg ^= 0xffffffff;
|
||||
}
|
||||
|
||||
for (int i = 0; i < io_count; i++) {
|
||||
ESP_LOGI(TAG, "Index[%d] | Dir[%s] | In[%d] | Out[%d]", i, (dir_reg & BIT(i)) ? "Out" : "In",
|
||||
(input_reg & BIT(i)) ? 1 : 0, (output_reg & BIT(i)) ? 1 : 0);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_reset(esp_io_expander_handle_t handle)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
ESP_RETURN_ON_FALSE(handle->reset, ESP_ERR_NOT_SUPPORTED, TAG, "reset isn't implemented");
|
||||
|
||||
return handle->reset(handle);
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_del(esp_io_expander_handle_t handle)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
ESP_RETURN_ON_FALSE(handle->del, ESP_ERR_NOT_SUPPORTED, TAG, "del isn't implemented");
|
||||
|
||||
return handle->del(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write the value to a specific register
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param reg: Specific type of register
|
||||
* @param value: Expected register's value
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
static esp_err_t write_reg(esp_io_expander_handle_t handle, reg_type_t reg, uint32_t value)
|
||||
{
|
||||
switch (reg) {
|
||||
case REG_OUTPUT:
|
||||
ESP_RETURN_ON_FALSE(handle->write_output_reg, ESP_ERR_NOT_SUPPORTED, TAG, "write_output_reg isn't implemented");
|
||||
return handle->write_output_reg(handle, value);
|
||||
case REG_DIRECTION:
|
||||
ESP_RETURN_ON_FALSE(handle->write_direction_reg, ESP_ERR_NOT_SUPPORTED, TAG, "write_direction_reg isn't implemented");
|
||||
return handle->write_direction_reg(handle, value);
|
||||
default:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the value from a specific register
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param reg: Specific type of register
|
||||
* @param value: Actual register's value
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
static esp_err_t read_reg(esp_io_expander_handle_t handle, reg_type_t reg, uint32_t *value)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(value, ESP_ERR_INVALID_ARG, TAG, "Invalid value");
|
||||
|
||||
switch (reg) {
|
||||
case REG_INPUT:
|
||||
ESP_RETURN_ON_FALSE(handle->read_input_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_input_reg isn't implemented");
|
||||
return handle->read_input_reg(handle, value);
|
||||
case REG_OUTPUT:
|
||||
ESP_RETURN_ON_FALSE(handle->read_output_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_output_reg isn't implemented");
|
||||
return handle->read_output_reg(handle, value);
|
||||
case REG_DIRECTION:
|
||||
ESP_RETURN_ON_FALSE(handle->read_direction_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_direction_reg isn't implemented");
|
||||
return handle->read_direction_reg(handle, value);
|
||||
default:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief ESP IO expander
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define IO_COUNT_MAX (sizeof(uint32_t) * 8)
|
||||
|
||||
/**
|
||||
* @brief IO Expander Device Type
|
||||
*
|
||||
*/
|
||||
typedef struct esp_io_expander_s esp_io_expander_t;
|
||||
typedef esp_io_expander_t *esp_io_expander_handle_t;
|
||||
|
||||
/**
|
||||
* @brief IO Expander Pin Num
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
IO_EXPANDER_PIN_NUM_0 = (1ULL << 0),
|
||||
IO_EXPANDER_PIN_NUM_1 = (1ULL << 1),
|
||||
IO_EXPANDER_PIN_NUM_2 = (1ULL << 2),
|
||||
IO_EXPANDER_PIN_NUM_3 = (1ULL << 3),
|
||||
IO_EXPANDER_PIN_NUM_4 = (1ULL << 4),
|
||||
IO_EXPANDER_PIN_NUM_5 = (1ULL << 5),
|
||||
IO_EXPANDER_PIN_NUM_6 = (1ULL << 6),
|
||||
IO_EXPANDER_PIN_NUM_7 = (1ULL << 7),
|
||||
IO_EXPANDER_PIN_NUM_8 = (1ULL << 8),
|
||||
IO_EXPANDER_PIN_NUM_9 = (1ULL << 9),
|
||||
IO_EXPANDER_PIN_NUM_10 = (1ULL << 10),
|
||||
IO_EXPANDER_PIN_NUM_11 = (1ULL << 11),
|
||||
IO_EXPANDER_PIN_NUM_12 = (1ULL << 12),
|
||||
IO_EXPANDER_PIN_NUM_13 = (1ULL << 13),
|
||||
IO_EXPANDER_PIN_NUM_14 = (1ULL << 14),
|
||||
IO_EXPANDER_PIN_NUM_15 = (1ULL << 15),
|
||||
IO_EXPANDER_PIN_NUM_16 = (1ULL << 16),
|
||||
IO_EXPANDER_PIN_NUM_17 = (1ULL << 17),
|
||||
IO_EXPANDER_PIN_NUM_18 = (1ULL << 18),
|
||||
IO_EXPANDER_PIN_NUM_19 = (1ULL << 19),
|
||||
IO_EXPANDER_PIN_NUM_20 = (1ULL << 20),
|
||||
IO_EXPANDER_PIN_NUM_21 = (1ULL << 21),
|
||||
IO_EXPANDER_PIN_NUM_22 = (1ULL << 22),
|
||||
IO_EXPANDER_PIN_NUM_23 = (1ULL << 23),
|
||||
IO_EXPANDER_PIN_NUM_24 = (1ULL << 24),
|
||||
IO_EXPANDER_PIN_NUM_25 = (1ULL << 25),
|
||||
IO_EXPANDER_PIN_NUM_26 = (1ULL << 26),
|
||||
IO_EXPANDER_PIN_NUM_27 = (1ULL << 27),
|
||||
IO_EXPANDER_PIN_NUM_28 = (1ULL << 28),
|
||||
IO_EXPANDER_PIN_NUM_29 = (1ULL << 29),
|
||||
IO_EXPANDER_PIN_NUM_30 = (1ULL << 30),
|
||||
IO_EXPANDER_PIN_NUM_31 = (1ULL << 31),
|
||||
} esp_io_expander_pin_num_t;
|
||||
|
||||
/**
|
||||
* @brief IO Expander Pin direction
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
IO_EXPANDER_INPUT, /*!< Input direction */
|
||||
IO_EXPANDER_OUTPUT, /*!< Output direction */
|
||||
} esp_io_expander_dir_t;
|
||||
|
||||
/**
|
||||
* @brief IO Expander Configuration Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t io_count; /*!< Count of device's IO, must be less or equal than `IO_COUNT_MAX` */
|
||||
struct {
|
||||
uint8_t dir_out_bit_zero : 1; /*!< If the direction of IO is output, the corresponding bit of the direction register is 0 */
|
||||
uint8_t input_high_bit_zero : 1; /*!< If the input level of IO is high, the corresponding bit of the input register is 0 */
|
||||
uint8_t output_high_bit_zero : 1; /*!< If the output level of IO is high, the corresponding bit of the output register is 0 */
|
||||
} flags;
|
||||
/* Don't support with interrupt mode yet, will be added soon */
|
||||
} esp_io_expander_config_t;
|
||||
|
||||
struct esp_io_expander_s {
|
||||
|
||||
/**
|
||||
* @brief Read value from input register (mandatory)
|
||||
*
|
||||
* @note The value represents the input level from IO
|
||||
* @note If there are multiple input registers in the device, their values should be spliced together in order to form the `value`.
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param value: Register's value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*read_input_reg)(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
|
||||
/**
|
||||
* @brief Write value to output register (mandatory)
|
||||
*
|
||||
* @note The value represents the output level to IO
|
||||
* @note If there are multiple input registers in the device, their values should be spliced together in order to form the `value`.
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param value: Register's value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*write_output_reg)(esp_io_expander_handle_t handle, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief Read value from output register (mandatory)
|
||||
*
|
||||
* @note The value represents the expected output level to IO
|
||||
* @note This function can be implemented by reading the physical output register, or simply by reading a variable that record the output value (more faster)
|
||||
* @note If there are multiple input registers in the device, their values should be spliced together in order to form the `value`.
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param value: Register's value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*read_output_reg)(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
|
||||
/**
|
||||
* @brief Write value to direction register (mandatory)
|
||||
*
|
||||
* @note The value represents the direction of IO
|
||||
* @note If there are multiple input registers in the device, their values should be spliced together in order to form the `value`.
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param value: Register's value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*write_direction_reg)(esp_io_expander_handle_t handle, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief Read value from directioin register (mandatory)
|
||||
*
|
||||
* @note The value represents the expected direction of IO
|
||||
* @note This function can be implemented by reading the physical direction register, or simply by reading a variable that record the direction value (more faster)
|
||||
* @note If there are multiple input registers in the device, their values should be spliced together in order to form the `value`.
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
* @param value: Register's value
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*read_direction_reg)(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
|
||||
/**
|
||||
* @brief Reset the device to its initial state (mandatory)
|
||||
*
|
||||
* @note This function will reset all device's registers
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*reset)(esp_io_expander_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Delete device (mandatory)
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t (*del)(esp_io_expander_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Configuration structure
|
||||
*/
|
||||
esp_io_expander_config_t config;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Set the direction of a set of target IOs
|
||||
*
|
||||
* @param handle: IO Exapnder handle
|
||||
* @param pin_num_mask: Bitwise OR of allowed pin num with type of `esp_io_expander_pin_num_t`
|
||||
* @param direction: IO direction (only support input or output now)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_set_dir(esp_io_expander_handle_t handle, uint32_t pin_num_mask, esp_io_expander_dir_t direction);
|
||||
|
||||
/**
|
||||
* @brief Set the output level of a set of target IOs
|
||||
*
|
||||
* @note All target IOs must be in output mode first, otherwise this function will return the error `ESP_ERR_INVALID_STATE`
|
||||
*
|
||||
* @param handle: IO Exapnder handle
|
||||
* @param pin_num_mask: Bitwise OR of allowed pin num with type of `esp_io_expander_pin_num_t`
|
||||
* @param level: 0 - Low level, 1 - High level
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_set_level(esp_io_expander_handle_t handle, uint32_t pin_num_mask, uint8_t level);
|
||||
|
||||
/**
|
||||
* @brief Get the input level of a set of target IOs
|
||||
*
|
||||
* @note This function can be called whenever target IOs are in input mode or output mode
|
||||
*
|
||||
* @param handle: IO Exapnder handle
|
||||
* @param pin_num_mask: Bitwise OR of allowed pin num with type of `esp_io_expander_pin_num_t`
|
||||
* @param level_mask: Bitwise OR of levels. For each bit, 0 - Low level, 1 - High level
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_get_level(esp_io_expander_handle_t handle, uint32_t pin_num_mask, uint32_t *level_mask);
|
||||
|
||||
/**
|
||||
* @brief Print the current status of each IO of the device, including direction, input level and output level
|
||||
*
|
||||
* @param handle: IO Exapnder handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_print_state(esp_io_expander_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Reset the device to its initial status
|
||||
*
|
||||
* @note This function will reset all device's registers
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_reset(esp_io_expander_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Delete device
|
||||
*
|
||||
* @param handle: IO Expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_del(esp_io_expander_handle_t handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_bit_defs.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
#include "esp_io_expander_ch422g.h"
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
|
||||
/* Timeout of each I2C communication */
|
||||
#define I2C_TIMEOUT_MS (10)
|
||||
|
||||
#define IO_COUNT (12)
|
||||
|
||||
/* Register address */
|
||||
#define CH422G_REG_WR_SET (0x48 >> 1)
|
||||
#define CH422G_REG_WR_OC (0x46 >> 1)
|
||||
#define CH422G_REG_WR_IO (0x70 >> 1)
|
||||
#define CH422G_REG_RD_IO (0x4D >> 1)
|
||||
|
||||
/* Default register value when reset */
|
||||
// *INDENT-OFF*
|
||||
#define REG_WR_SET_DEFAULT_VAL (0x01UL) // Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|
||||
// | --- | --- | --- | --- | ------- | ------- | -------- | ------- |
|
||||
// Value: | / | / | / | / | [SLEEP] | [OD_EN] | [A_SCAN] | [IO_OE] |
|
||||
// | --- | --- | --- | --- | ------- | ------- | -------- | ------- |
|
||||
// Default: | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
|
||||
|
||||
// *INDENT-OFF*
|
||||
#define REG_WR_OC_DEFAULT_VAL (0x0FUL)
|
||||
#define REG_WR_IO_DEFAULT_VAL (0xFFUL)
|
||||
#define REG_OUT_DEFAULT_VAL ((REG_WR_OC_DEFAULT_VAL << 8) | REG_WR_IO_DEFAULT_VAL)
|
||||
#define REG_DIR_DEFAULT_VAL (0xFFFUL)
|
||||
|
||||
#define REG_WR_SET_BIT_IO_OE (1 << 0)
|
||||
#define REG_WR_SET_BIT_OD_EN (1 << 2)
|
||||
|
||||
/**
|
||||
* @brief Device Structure Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
esp_io_expander_t base;
|
||||
i2c_port_t i2c_num;
|
||||
uint32_t i2c_address;
|
||||
struct {
|
||||
uint8_t wr_set;
|
||||
uint8_t wr_oc;
|
||||
uint8_t wr_io;
|
||||
} regs;
|
||||
} esp_io_expander_ch422g_t;
|
||||
|
||||
static const char *TAG = "ch422g";
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t reset(esp_io_expander_t *handle);
|
||||
static esp_err_t del(esp_io_expander_t *handle);
|
||||
|
||||
esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "version: %d.%d.%d", ESP_IO_EXPANDER_CH422G_VER_MAJOR, ESP_IO_EXPANDER_CH422G_VER_MINOR,
|
||||
ESP_IO_EXPANDER_CH422G_VER_PATCH);
|
||||
ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "Invalid i2c num");
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)calloc(1, sizeof(esp_io_expander_ch422g_t));
|
||||
ESP_RETURN_ON_FALSE(ch422g, ESP_ERR_NO_MEM, TAG, "Malloc failed");
|
||||
|
||||
ch422g->base.config.io_count = IO_COUNT;
|
||||
ch422g->i2c_num = i2c_num;
|
||||
ch422g->i2c_address = i2c_address;
|
||||
ch422g->regs.wr_set = REG_WR_SET_DEFAULT_VAL;
|
||||
ch422g->regs.wr_oc = REG_WR_OC_DEFAULT_VAL;
|
||||
ch422g->regs.wr_io = REG_WR_IO_DEFAULT_VAL;
|
||||
ch422g->base.read_input_reg = read_input_reg;
|
||||
ch422g->base.write_output_reg = write_output_reg;
|
||||
ch422g->base.read_output_reg = read_output_reg;
|
||||
ch422g->base.write_direction_reg = write_direction_reg;
|
||||
ch422g->base.read_direction_reg = read_direction_reg;
|
||||
ch422g->base.del = del;
|
||||
ch422g->base.reset = reset;
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
/* Reset configuration and register status */
|
||||
ESP_GOTO_ON_ERROR(reset(&ch422g->base), err, TAG, "Reset failed");
|
||||
|
||||
*handle = &ch422g->base;
|
||||
return ESP_OK;
|
||||
err:
|
||||
free(ch422g);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_oc_open_drain(esp_io_expander_handle_t handle)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t data = (uint8_t)(ch422g->regs.wr_set | REG_WR_SET_BIT_OD_EN);
|
||||
|
||||
// WR-SET
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(
|
||||
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
|
||||
), TAG, "Write WR_SET reg failed"
|
||||
);
|
||||
ch422g->regs.wr_set = data;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_oc_push_pull(esp_io_expander_handle_t handle)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t data = (uint8_t)(ch422g->regs.wr_set & ~REG_WR_SET_BIT_OD_EN);
|
||||
|
||||
// WR-SET
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(
|
||||
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
|
||||
), TAG, "Write WR_SET reg failed"
|
||||
);
|
||||
ch422g->regs.wr_set = data;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_all_input(esp_io_expander_handle_t handle)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t data = (uint8_t)(ch422g->regs.wr_set & ~REG_WR_SET_BIT_IO_OE);
|
||||
|
||||
// WR-SET
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(
|
||||
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
|
||||
), TAG, "Write WR_SET reg failed"
|
||||
);
|
||||
ch422g->regs.wr_set = data;
|
||||
// Delay 1ms to wait for the IO expander to switch to input mode
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_all_output(esp_io_expander_handle_t handle)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t data = (uint8_t)(ch422g->regs.wr_set | REG_WR_SET_BIT_IO_OE);
|
||||
|
||||
// WR-SET
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(
|
||||
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
|
||||
), TAG, "Write WR_SET reg failed"
|
||||
);
|
||||
ch422g->regs.wr_set = data;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t temp = 0;
|
||||
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_read_from_device(ch422g->i2c_num, CH422G_REG_RD_IO, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Read RD-IO reg failed"
|
||||
);
|
||||
*value = temp;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
|
||||
uint8_t wr_oc_data = (value & 0xF00) >> 8;
|
||||
uint8_t wr_io_data = value & 0xFF;
|
||||
|
||||
// WR-OC
|
||||
if (wr_oc_data) {
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_OC, &wr_oc_data, sizeof(wr_oc_data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write WR-OC reg failed"
|
||||
);
|
||||
ch422g->regs.wr_oc = wr_oc_data;
|
||||
}
|
||||
|
||||
// WR-IO
|
||||
if (wr_io_data) {
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_IO, &wr_io_data, sizeof(wr_io_data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write WR-IO reg failed"
|
||||
);
|
||||
ch422g->regs.wr_io = wr_io_data;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
|
||||
*value = ch422g->regs.wr_io | (((uint32_t)ch422g->regs.wr_oc) << 8);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
uint8_t data = ch422g->regs.wr_set;
|
||||
|
||||
value &= 0xFF;
|
||||
if (value != 0) {
|
||||
data |= REG_WR_SET_BIT_IO_OE;
|
||||
} else {
|
||||
data &= ~REG_WR_SET_BIT_IO_OE;
|
||||
}
|
||||
|
||||
// WR-SET
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write WR_SET reg failed"
|
||||
);
|
||||
ch422g->regs.wr_set = data;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#define DIR_OUT_VALUE (0xFFF)
|
||||
#define DIR_IN_VALUE (0xF00)
|
||||
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
|
||||
*value = (ch422g->regs.wr_set & REG_WR_SET_BIT_IO_OE) ? DIR_OUT_VALUE : DIR_IN_VALUE;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t reset(esp_io_expander_t *handle)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(write_direction_reg(handle, REG_DIR_DEFAULT_VAL), TAG, "Write direction reg (WR_SET) failed");
|
||||
ESP_RETURN_ON_ERROR(write_output_reg(handle, REG_OUT_DEFAULT_VAL), TAG, "Write output reg (WR_OC & WR_IO) failed");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t del(esp_io_expander_t *handle)
|
||||
{
|
||||
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
|
||||
|
||||
free(ch422g);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_IO_EXPANDER_CH422G_VER_MAJOR (0)
|
||||
#define ESP_IO_EXPANDER_CH422G_VER_MINOR (1)
|
||||
#define ESP_IO_EXPANDER_CH422G_VER_PATCH (0)
|
||||
|
||||
/**
|
||||
* @brief Create a new ch422g IO expander driver
|
||||
*
|
||||
* @note The I2C communication should be initialized before use this function
|
||||
*
|
||||
* @param i2c_num: I2C port num
|
||||
* @param i2c_address: I2C address of chip
|
||||
* @param handle: IO expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief I2C address of the ch422g. Just to keep the same with other IO expanders, but it is ignored.
|
||||
*
|
||||
*/
|
||||
#define ESP_IO_EXPANDER_I2C_CH422G_ADDRESS (0x24)
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_oc_open_drain(esp_io_expander_handle_t handle);
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_oc_push_pull(esp_io_expander_handle_t handle);
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_all_input(esp_io_expander_handle_t handle);
|
||||
|
||||
esp_err_t esp_io_expander_ch422g_set_all_output(esp_io_expander_handle_t handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_bit_defs.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
#include "esp_io_expander_ht8574.h"
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
|
||||
/* Timeout of each I2C communication */
|
||||
#define I2C_TIMEOUT_MS (10)
|
||||
|
||||
#define IO_COUNT (8)
|
||||
|
||||
/* Default register value on power-up */
|
||||
#define DIR_REG_DEFAULT_VAL (0xff)
|
||||
#define OUT_REG_DEFAULT_VAL (0xff)
|
||||
|
||||
/**
|
||||
* @brief Device Structure Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
esp_io_expander_t base;
|
||||
i2c_port_t i2c_num;
|
||||
uint32_t i2c_address;
|
||||
struct {
|
||||
uint8_t direction;
|
||||
uint8_t output;
|
||||
} regs;
|
||||
} esp_io_expander_ht8574_t;
|
||||
|
||||
static char *TAG = "ht8574";
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t reset(esp_io_expander_t *handle);
|
||||
static esp_err_t del(esp_io_expander_t *handle);
|
||||
|
||||
esp_err_t esp_io_expander_new_i2c_ht8574(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "version: %d.%d.%d", ESP_IO_EXPANDER_HT8574_VER_MAJOR, ESP_IO_EXPANDER_HT8574_VER_MINOR,
|
||||
ESP_IO_EXPANDER_HT8574_VER_PATCH);
|
||||
ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "Invalid i2c num");
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)calloc(1, sizeof(esp_io_expander_ht8574_t));
|
||||
ESP_RETURN_ON_FALSE(ht8574, ESP_ERR_NO_MEM, TAG, "Malloc failed");
|
||||
|
||||
ht8574->base.config.io_count = IO_COUNT;
|
||||
ht8574->base.config.flags.dir_out_bit_zero = 1;
|
||||
ht8574->i2c_num = i2c_num;
|
||||
ht8574->i2c_address = i2c_address;
|
||||
ht8574->base.read_input_reg = read_input_reg;
|
||||
ht8574->base.write_output_reg = write_output_reg;
|
||||
ht8574->base.read_output_reg = read_output_reg;
|
||||
ht8574->base.write_direction_reg = write_direction_reg;
|
||||
ht8574->base.read_direction_reg = read_direction_reg;
|
||||
ht8574->base.del = del;
|
||||
ht8574->base.reset = reset;
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
/* Reset configuration and register status */
|
||||
ESP_GOTO_ON_ERROR(reset(&ht8574->base), err, TAG, "Reset failed");
|
||||
|
||||
*handle = &ht8574->base;
|
||||
return ESP_OK;
|
||||
err:
|
||||
free(ht8574);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
|
||||
uint8_t temp = 0;
|
||||
// *INDENT-OFF*
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_read_from_device(ht8574->i2c_num, ht8574->i2c_address, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Read input reg failed");
|
||||
// *INDENT-ON*
|
||||
*value = temp;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
value &= 0xff;
|
||||
|
||||
uint8_t data = (uint8_t)value;
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(ht8574->i2c_num, ht8574->i2c_address, &data, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write output reg failed");
|
||||
ht8574->regs.output = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
|
||||
*value = ht8574->regs.output;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
value &= 0xff;
|
||||
ht8574->regs.direction = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
|
||||
*value = ht8574->regs.direction;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t reset(esp_io_expander_t *handle)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t del(esp_io_expander_t *handle)
|
||||
{
|
||||
esp_io_expander_ht8574_t *ht8574 = (esp_io_expander_ht8574_t *)__containerof(handle, esp_io_expander_ht8574_t, base);
|
||||
|
||||
free(ht8574);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_IO_EXPANDER_HT8574_VER_MAJOR (0)
|
||||
#define ESP_IO_EXPANDER_HT8574_VER_MINOR (1)
|
||||
#define ESP_IO_EXPANDER_HT8574_VER_PATCH (0)
|
||||
|
||||
/**
|
||||
* @brief Create a new ht8574 IO expander driver
|
||||
*
|
||||
* @note The I2C communication should be initialized before use this function
|
||||
*
|
||||
* @param i2c_num: I2C port num
|
||||
* @param i2c_address: I2C address of chip
|
||||
* @param handle: IO expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_new_i2c_ht8574(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief I2C address of the ht8574
|
||||
*
|
||||
* The 8-bit address format is as follows:
|
||||
*
|
||||
* (Slave Address)
|
||||
* ┌─────────────────┷─────────────────┐
|
||||
* ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐
|
||||
* | 0 | 1 | 1 | 1 | A2 | A1 | A0 | R/W |
|
||||
* └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘
|
||||
* └────────┯────────┘ └─────┯──────┘
|
||||
* (Fixed) (Hardware Selectable)
|
||||
*
|
||||
* And the 7-bit slave address is the most important data for users.
|
||||
* For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0111000b(0x38).
|
||||
* Then users can use `ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_000` to init it.
|
||||
*/
|
||||
#define ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_000 (0x38)
|
||||
#define ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_001 (0x29)
|
||||
#define ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_010 (0x2A)
|
||||
#define ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_011 (0x2B)
|
||||
#define ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_100 (0x2C)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_bit_defs.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
#include "esp_io_expander_tca9554.h"
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
|
||||
/* Timeout of each I2C communication */
|
||||
#define I2C_TIMEOUT_MS (10)
|
||||
|
||||
#define IO_COUNT (8)
|
||||
|
||||
/* Register address */
|
||||
#define INPUT_REG_ADDR (0x00)
|
||||
#define OUTPUT_REG_ADDR (0x01)
|
||||
#define DIRECTION_REG_ADDR (0x03)
|
||||
|
||||
/* Default register value on power-up */
|
||||
#define DIR_REG_DEFAULT_VAL (0xff)
|
||||
#define OUT_REG_DEFAULT_VAL (0xff)
|
||||
|
||||
/**
|
||||
* @brief Device Structure Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
esp_io_expander_t base;
|
||||
i2c_port_t i2c_num;
|
||||
uint32_t i2c_address;
|
||||
struct {
|
||||
uint8_t direction;
|
||||
uint8_t output;
|
||||
} regs;
|
||||
} esp_io_expander_tca9554_t;
|
||||
|
||||
static char *TAG = "tca9554";
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t reset(esp_io_expander_t *handle);
|
||||
static esp_err_t del(esp_io_expander_t *handle);
|
||||
|
||||
esp_err_t esp_io_expander_new_i2c_tca9554(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "version: %d.%d.%d", ESP_IO_EXPANDER_TCA9554_VER_MAJOR, ESP_IO_EXPANDER_TCA9554_VER_MINOR,
|
||||
ESP_IO_EXPANDER_TCA9554_VER_PATCH);
|
||||
ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "Invalid i2c num");
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)calloc(1, sizeof(esp_io_expander_tca9554_t));
|
||||
ESP_RETURN_ON_FALSE(tca9554, ESP_ERR_NO_MEM, TAG, "Malloc failed");
|
||||
|
||||
tca9554->base.config.io_count = IO_COUNT;
|
||||
tca9554->base.config.flags.dir_out_bit_zero = 1;
|
||||
tca9554->i2c_num = i2c_num;
|
||||
tca9554->i2c_address = i2c_address;
|
||||
tca9554->base.read_input_reg = read_input_reg;
|
||||
tca9554->base.write_output_reg = write_output_reg;
|
||||
tca9554->base.read_output_reg = read_output_reg;
|
||||
tca9554->base.write_direction_reg = write_direction_reg;
|
||||
tca9554->base.read_direction_reg = read_direction_reg;
|
||||
tca9554->base.del = del;
|
||||
tca9554->base.reset = reset;
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
/* Reset configuration and register status */
|
||||
ESP_GOTO_ON_ERROR(reset(&tca9554->base), err, TAG, "Reset failed");
|
||||
|
||||
*handle = &tca9554->base;
|
||||
return ESP_OK;
|
||||
err:
|
||||
free(tca9554);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
|
||||
uint8_t temp = 0;
|
||||
// *INDENT-OFF*
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_read_device(tca9554->i2c_num, tca9554->i2c_address, (uint8_t[]){INPUT_REG_ADDR}, 1, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Read input reg failed");
|
||||
// *INDENT-ON*
|
||||
*value = temp;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
value &= 0xff;
|
||||
|
||||
uint8_t data[] = {OUTPUT_REG_ADDR, value};
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(tca9554->i2c_num, tca9554->i2c_address, data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write output reg failed");
|
||||
tca9554->regs.output = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
|
||||
*value = tca9554->regs.output;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
value &= 0xff;
|
||||
|
||||
uint8_t data[] = {DIRECTION_REG_ADDR, value};
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(tca9554->i2c_num, tca9554->i2c_address, data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write direction reg failed");
|
||||
tca9554->regs.direction = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
|
||||
*value = tca9554->regs.direction;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t reset(esp_io_expander_t *handle)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(write_direction_reg(handle, DIR_REG_DEFAULT_VAL), TAG, "Write dir reg failed");
|
||||
ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t del(esp_io_expander_t *handle)
|
||||
{
|
||||
esp_io_expander_tca9554_t *tca9554 = (esp_io_expander_tca9554_t *)__containerof(handle, esp_io_expander_tca9554_t, base);
|
||||
|
||||
free(tca9554);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief ESP IO expander: TCA9554
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_IO_EXPANDER_TCA9554_VER_MAJOR (1)
|
||||
#define ESP_IO_EXPANDER_TCA9554_VER_MINOR (0)
|
||||
#define ESP_IO_EXPANDER_TCA9554_VER_PATCH (1)
|
||||
|
||||
/**
|
||||
* @brief Create a new TCA9554 IO expander driver
|
||||
*
|
||||
* @note The I2C communication should be initialized before use this function
|
||||
*
|
||||
* @param i2c_num: I2C port num
|
||||
* @param i2c_address: I2C address of chip
|
||||
* @param handle: IO expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_new_i2c_tca9554(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief I2C address of the TCA9554
|
||||
*
|
||||
* The 8-bit address format is as follows:
|
||||
*
|
||||
* (Slave Address)
|
||||
* ┌─────────────────┷─────────────────┐
|
||||
* ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐
|
||||
* | 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W |
|
||||
* └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘
|
||||
* └────────┯────────┘ └─────┯──────┘
|
||||
* (Fixed) (Hardware Selectable)
|
||||
*
|
||||
* And the 7-bit slave address is the most important data for users.
|
||||
* For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0100000b(0x20).
|
||||
* Then users can use `ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000` to init it.
|
||||
*/
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000 (0x20)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_001 (0x21)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_010 (0x22)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_011 (0x23)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_100 (0x24)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_101 (0x25)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_110 (0x26)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_111 (0x27)
|
||||
|
||||
|
||||
/**
|
||||
* @brief I2C address of the TCA9554A
|
||||
*
|
||||
* The 8-bit address format is as follows:
|
||||
*
|
||||
* (Slave Address)
|
||||
* ┌─────────────────┷─────────────────┐
|
||||
* ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐
|
||||
* | 0 | 1 | 1 | 1 | A2 | A1 | A0 | R/W |
|
||||
* └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘
|
||||
* └────────┯────────┘ └─────┯──────┘
|
||||
* (Fixed) (Hardware Selectable)
|
||||
*
|
||||
* And the 7-bit slave address is the most important data for users.
|
||||
* For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0111000b(0x38).
|
||||
* Then users can use `ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000` to init it.
|
||||
*/
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000 (0x38)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_001 (0x39)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_010 (0x3A)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_011 (0x3B)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_100 (0x3C)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_101 (0x3D)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_110 (0x3E)
|
||||
#define ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_111 (0x3F)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_bit_defs.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
#include "esp_io_expander_tca95xx_16bit.h"
|
||||
|
||||
#include "esp_expander_utils.h"
|
||||
|
||||
/* Timeout of each I2C communication */
|
||||
#define I2C_TIMEOUT_MS (10)
|
||||
|
||||
#define IO_COUNT (16)
|
||||
|
||||
/* Register address */
|
||||
#define INPUT_REG_ADDR (0x00)
|
||||
#define OUTPUT_REG_ADDR (0x02)
|
||||
#define DIRECTION_REG_ADDR (0x06)
|
||||
|
||||
/* Default register value on power-up */
|
||||
#define DIR_REG_DEFAULT_VAL (0xffff)
|
||||
#define OUT_REG_DEFAULT_VAL (0xffff)
|
||||
|
||||
/**
|
||||
* @brief Device Structure Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
esp_io_expander_t base;
|
||||
i2c_port_t i2c_num;
|
||||
uint32_t i2c_address;
|
||||
struct {
|
||||
uint16_t direction;
|
||||
uint16_t output;
|
||||
} regs;
|
||||
} esp_io_expander_tca95xx_16bit_t;
|
||||
|
||||
static char *TAG = "tca95xx_16";
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value);
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
static esp_err_t reset(esp_io_expander_t *handle);
|
||||
static esp_err_t del(esp_io_expander_t *handle);
|
||||
|
||||
esp_err_t esp_io_expander_new_i2c_tca95xx_16bit(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "version: %d.%d.%d", ESP_IO_EXPANDER_TCA95XX_16BIT_VER_MAJOR, ESP_IO_EXPANDER_TCA95XX_16BIT_VER_MINOR,
|
||||
ESP_IO_EXPANDER_TCA95XX_16BIT_VER_PATCH);
|
||||
ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "Invalid i2c num");
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
|
||||
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)calloc(1, sizeof(esp_io_expander_tca95xx_16bit_t));
|
||||
ESP_RETURN_ON_FALSE(tca, ESP_ERR_NO_MEM, TAG, "Malloc failed");
|
||||
|
||||
tca->base.config.io_count = IO_COUNT;
|
||||
tca->base.config.flags.dir_out_bit_zero = 1;
|
||||
tca->i2c_num = i2c_num;
|
||||
tca->i2c_address = i2c_address;
|
||||
tca->base.read_input_reg = read_input_reg;
|
||||
tca->base.write_output_reg = write_output_reg;
|
||||
tca->base.read_output_reg = read_output_reg;
|
||||
tca->base.write_direction_reg = write_direction_reg;
|
||||
tca->base.read_direction_reg = read_direction_reg;
|
||||
tca->base.del = del;
|
||||
tca->base.reset = reset;
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
/* Reset configuration and register status */
|
||||
ESP_GOTO_ON_ERROR(reset(&tca->base), err, TAG, "Reset failed");
|
||||
|
||||
*handle = &tca->base;
|
||||
return ESP_OK;
|
||||
err:
|
||||
free(tca);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
|
||||
uint8_t temp[2] = {0, 0};
|
||||
// *INDENT-OFF*
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_read_device(tca->i2c_num, tca->i2c_address, (uint8_t[]){INPUT_REG_ADDR}, 1, (uint8_t*)&temp, 2, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Read input reg failed");
|
||||
// *INDENT-ON*
|
||||
*value = (((uint32_t)temp[1]) << 8) | (temp[0]);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
value &= 0xffff;
|
||||
|
||||
uint8_t data[] = {OUTPUT_REG_ADDR, value & 0xff, value >> 8};
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(tca->i2c_num, tca->i2c_address, data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write output reg failed");
|
||||
tca->regs.output = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
|
||||
*value = tca->regs.output;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
value &= 0xffff;
|
||||
|
||||
uint8_t data[] = {DIRECTION_REG_ADDR, value & 0xff, value >> 8};
|
||||
ESP_RETURN_ON_ERROR(
|
||||
i2c_master_write_to_device(tca->i2c_num, tca->i2c_address, data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
|
||||
TAG, "Write direction reg failed");
|
||||
tca->regs.direction = value;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
|
||||
*value = tca->regs.direction;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t reset(esp_io_expander_t *handle)
|
||||
{
|
||||
ESP_RETURN_ON_ERROR(write_direction_reg(handle, DIR_REG_DEFAULT_VAL), TAG, "Write dir reg failed");
|
||||
ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t del(esp_io_expander_t *handle)
|
||||
{
|
||||
esp_io_expander_tca95xx_16bit_t *tca = (esp_io_expander_tca95xx_16bit_t *)__containerof(handle, esp_io_expander_tca95xx_16bit_t, base);
|
||||
|
||||
free(tca);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_IO_EXPANDER_TCA95XX_16BIT_VER_MAJOR (1)
|
||||
#define ESP_IO_EXPANDER_TCA95XX_16BIT_VER_MINOR (0)
|
||||
#define ESP_IO_EXPANDER_TCA95XX_16BIT_VER_PATCH (0)
|
||||
|
||||
/**
|
||||
* @brief Create a new TCA95XX_16BIT IO expander driver
|
||||
*
|
||||
* @note The I2C communication should be initialized before use this function
|
||||
*
|
||||
* @param i2c_num: I2C port num
|
||||
* @param i2c_address: I2C address of chip (\see esp_io_expander_tca_95xx_16bit_address)
|
||||
* @param handle: IO expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_new_i2c_tca95xx_16bit(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle);
|
||||
|
||||
/**
|
||||
* @brief I2C address of the TCA9539 or TCA9555
|
||||
*
|
||||
* The 8-bit address format for the TCA9539 is as follows:
|
||||
*
|
||||
* (Slave Address)
|
||||
* ┌─────────────────┷─────────────────┐
|
||||
* ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐
|
||||
* | 1 | 1 | 1 | 0 | 1 | A1 | A0 | R/W |
|
||||
* └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘
|
||||
* └────────┯──────────────┘ └──┯──┘
|
||||
* (Fixed) (Hardware Selectable)
|
||||
*
|
||||
* The 8-bit address format for the TCA9555 is as follows:
|
||||
*
|
||||
* (Slave Address)
|
||||
* ┌─────────────────┷─────────────────┐
|
||||
* ┌─────┐─────┐─────┐─────┐─────┐─────┐─────┐─────┐
|
||||
* | 0 | 1 | 0 | 0 | A2 | A1 | A0 | R/W |
|
||||
* └─────┘─────┘─────┘─────┘─────┘─────┘─────┘─────┘
|
||||
* └────────┯────────┘ └─────┯──────┘
|
||||
* (Fixed) (Hardware Selectable)
|
||||
*
|
||||
* And the 7-bit slave address is the most important data for users.
|
||||
* For example, if a TCA9555 chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 0b0100000.
|
||||
* Then users can use `ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_000` to init it.
|
||||
*/
|
||||
enum esp_io_expander_tca_95xx_16bit_address {
|
||||
ESP_IO_EXPANDER_I2C_TCA9539_ADDRESS_00 = 0b1110100,
|
||||
ESP_IO_EXPANDER_I2C_TCA9539_ADDRESS_01 = 0b1110101,
|
||||
ESP_IO_EXPANDER_I2C_TCA9539_ADDRESS_10 = 0b1110110,
|
||||
ESP_IO_EXPANDER_I2C_TCA9539_ADDRESS_11 = 0b1110111,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_000 = 0b0100000,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_001 = 0b0100001,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_010 = 0b0100010,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_011 = 0b0100011,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_100 = 0b0100000,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_101 = 0b0100101,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_110 = 0b0100110,
|
||||
ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_111 = 0b0100111,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user