Creataing the Hodor project.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
[codespell]
|
||||
@@ -0,0 +1,141 @@
|
||||
[flake8]
|
||||
|
||||
select =
|
||||
# Full lists are given in order to suppress all errors from other plugins
|
||||
# Full list of pyflakes error codes:
|
||||
F401, # module imported but unused
|
||||
F402, # import module from line N shadowed by loop variable
|
||||
F403, # 'from module import *' used; unable to detect undefined names
|
||||
F404, # future import(s) name after other statements
|
||||
F405, # name may be undefined, or defined from star imports: module
|
||||
F406, # 'from module import *' only allowed at module level
|
||||
F407, # an undefined __future__ feature name was imported
|
||||
F601, # dictionary key name repeated with different values
|
||||
F602, # dictionary key variable name repeated with different values
|
||||
F621, # too many expressions in an assignment with star-unpacking
|
||||
F622, # two or more starred expressions in an assignment (a, *b, *c = d)
|
||||
F631, # assertion test is a tuple, which are always True
|
||||
F701, # a break statement outside of a while or for loop
|
||||
F702, # a continue statement outside of a while or for loop
|
||||
F703, # a continue statement in a finally block in a loop
|
||||
F704, # a yield or yield from statement outside of a function
|
||||
F705, # a return statement with arguments inside a generator
|
||||
F706, # a return statement outside of a function/method
|
||||
F707, # an except: block as not the last exception handler
|
||||
F721, F722, # doctest syntax error syntax error in forward type annotation
|
||||
F811, # redefinition of unused name from line N
|
||||
F812, # list comprehension redefines name from line N
|
||||
F821, # undefined name name
|
||||
F822, # undefined name name in __all__
|
||||
F823, # local variable name referenced before assignment
|
||||
F831, # duplicate argument name in function definition
|
||||
F841, # local variable name is assigned to but never used
|
||||
F901, # raise NotImplemented should be raise NotImplementedError
|
||||
|
||||
# Full list of pycodestyle violations:
|
||||
E101, # indentation contains mixed spaces and tabs
|
||||
E111, # indentation is not a multiple of four
|
||||
E112, # expected an indented block
|
||||
E113, # unexpected indentation
|
||||
E114, # indentation is not a multiple of four (comment)
|
||||
E115, # expected an indented block (comment)
|
||||
E116, # unexpected indentation (comment)
|
||||
E121, # continuation line under-indented for hanging indent
|
||||
E122, # continuation line missing indentation or outdented
|
||||
E123, # closing bracket does not match indentation of opening bracket's line
|
||||
E124, # closing bracket does not match visual indentation
|
||||
E125, # continuation line with same indent as next logical line
|
||||
E126, # continuation line over-indented for hanging indent
|
||||
E127, # continuation line over-indented for visual indent
|
||||
E128, # continuation line under-indented for visual indent
|
||||
E129, # visually indented line with same indent as next logical line
|
||||
E131, # continuation line unaligned for hanging indent
|
||||
E133, # closing bracket is missing indentation
|
||||
E201, # whitespace after '('
|
||||
E202, # whitespace before ')'
|
||||
E203, # whitespace before ':'
|
||||
E211, # whitespace before '('
|
||||
E221, # multiple spaces before operator
|
||||
E222, # multiple spaces after operator
|
||||
E223, # tab before operator
|
||||
E224, # tab after operator
|
||||
E225, # missing whitespace around operator
|
||||
E226, # missing whitespace around arithmetic operator
|
||||
E227, # missing whitespace around bitwise or shift operator
|
||||
E228, # missing whitespace around modulo operator
|
||||
E231, # missing whitespace after ',', ';', or ':'
|
||||
E241, # multiple spaces after ','
|
||||
E242, # tab after ','
|
||||
E251, # unexpected spaces around keyword / parameter equals
|
||||
E261, # at least two spaces before inline comment
|
||||
E262, # inline comment should start with '# '
|
||||
E265, # block comment should start with '# '
|
||||
E266, # too many leading '#' for block comment
|
||||
E271, # multiple spaces after keyword
|
||||
E272, # multiple spaces before keyword
|
||||
E273, # tab after keyword
|
||||
E274, # tab before keyword
|
||||
E275, # missing whitespace after keyword
|
||||
E301, # expected 1 blank line, found 0
|
||||
E302, # expected 2 blank lines, found 0
|
||||
E303, # too many blank lines
|
||||
E304, # blank lines found after function decorator
|
||||
E305, # expected 2 blank lines after end of function or class
|
||||
E306, # expected 1 blank line before a nested definition
|
||||
E401, # multiple imports on one line
|
||||
E402, # module level import not at top of file
|
||||
E501, # line too long (82 > 79 characters)
|
||||
E502, # the backslash is redundant between brackets
|
||||
E701, # multiple statements on one line (colon)
|
||||
E702, # multiple statements on one line (semicolon)
|
||||
E703, # statement ends with a semicolon
|
||||
E704, # multiple statements on one line (def)
|
||||
E711, # comparison to None should be 'if cond is None:'
|
||||
E712, # comparison to True should be 'if cond is True:' or 'if cond:'
|
||||
E713, # test for membership should be 'not in'
|
||||
E714, # test for object identity should be 'is not'
|
||||
E721, # do not compare types, use 'isinstance()'
|
||||
E722, # do not use bare except, specify exception instead
|
||||
E731, # do not assign a lambda expression, use a def
|
||||
E741, # do not use variables named 'l', 'O', or 'I'
|
||||
E742, # do not define classes named 'l', 'O', or 'I'
|
||||
E743, # do not define functions named 'l', 'O', or 'I'
|
||||
E901, # SyntaxError or IndentationError
|
||||
E902, # IOError
|
||||
W191, # indentation contains tabs
|
||||
W291, # trailing whitespace
|
||||
W292, # no newline at end of file
|
||||
W293, # blank line contains whitespace
|
||||
W391, # blank line at end of file
|
||||
W503, # line break before binary operator
|
||||
W504, # line break after binary operator
|
||||
W505, # doc line too long (82 > 79 characters)
|
||||
W601, # .has_key() is deprecated, use 'in'
|
||||
W602, # deprecated form of raising exception
|
||||
W603, # '<>' is deprecated, use '!='
|
||||
W604, # backticks are deprecated, use 'repr()'
|
||||
W605, # invalid escape sequence 'x'
|
||||
W606, # 'async' and 'await' are reserved keywords starting with Python 3.7
|
||||
|
||||
# Full list of flake8 violations
|
||||
E999, # failed to compile a file into an Abstract Syntax Tree for the plugins that require it
|
||||
|
||||
# Full list of mccabe violations
|
||||
C901 # complexity value provided by the user
|
||||
|
||||
ignore =
|
||||
E221, # multiple spaces before operator
|
||||
E231, # missing whitespace after ',', ';', or ':'
|
||||
E241, # multiple spaces after ','
|
||||
W503, # line break before binary operator
|
||||
W504 # line break after binary operator
|
||||
|
||||
max-line-length = 160
|
||||
|
||||
show_source = True
|
||||
|
||||
statistics = True
|
||||
|
||||
exclude =
|
||||
.git,
|
||||
__pycache__,
|
||||
@@ -0,0 +1,71 @@
|
||||
.config
|
||||
*.o
|
||||
*.pyc
|
||||
*.orig
|
||||
|
||||
# gtags
|
||||
GTAGS
|
||||
GRTAGS
|
||||
GPATH
|
||||
|
||||
# emacs
|
||||
.dir-locals.el
|
||||
|
||||
# emacs temp file suffixes
|
||||
*~
|
||||
.#*
|
||||
\#*#
|
||||
|
||||
# eclipse setting
|
||||
.settings
|
||||
|
||||
# MacOS directory files
|
||||
.DS_Store
|
||||
|
||||
# Unit Test CMake compile log folder
|
||||
log_ut_cmake
|
||||
|
||||
TEST_LOGS
|
||||
|
||||
# gcov coverage reports
|
||||
*.gcda
|
||||
*.gcno
|
||||
coverage.info
|
||||
coverage_report/
|
||||
|
||||
test_multi_heap_host
|
||||
|
||||
# VS Code Settings
|
||||
.vscode/
|
||||
|
||||
# VIM files
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Clion IDE CMake build & config
|
||||
.idea/
|
||||
cmake-build-*/
|
||||
|
||||
# Results for the checking of the Python coding style and static analysis
|
||||
.mypy_cache
|
||||
flake8_output.txt
|
||||
|
||||
# esp-idf default build directory name
|
||||
build
|
||||
build_esp*/
|
||||
build_linux*/
|
||||
size_info.txt
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
|
||||
# lock files for examples and components
|
||||
dependencies.lock
|
||||
|
||||
# managed_components for examples
|
||||
managed_components
|
||||
|
||||
# pytest log
|
||||
pytest_embedded_log/
|
||||
pytest_log/
|
||||
.pytest_cache/
|
||||
XUNIT_RESULT.xml
|
||||
@@ -0,0 +1,82 @@
|
||||
# See https://pre-commit.com for more information
|
||||
# See https://pre-commit.com/hooks.html for more hooks
|
||||
|
||||
exclude: 'libraries/ui/'
|
||||
repos:
|
||||
- repo: https://github.com/igrr/astyle_py.git
|
||||
rev: v1.0.5
|
||||
hooks:
|
||||
- id: astyle_py
|
||||
args: ['--style=otbs', '--attach-namespaces', '--attach-classes', '--indent=spaces=4', '--convert-tabs', '--align-pointer=name', '--align-reference=name', '--keep-one-line-statements', '--pad-header', '--pad-oper']
|
||||
|
||||
- repo: https://github.com/espressif/check-copyright/
|
||||
rev: v1.0.3
|
||||
hooks:
|
||||
- id: check-copyright
|
||||
args: ['--config', 'check_copyright_config.yaml']
|
||||
|
||||
- repo: https://github.com/PyCQA/flake8
|
||||
rev: 5.0.4
|
||||
hooks:
|
||||
- id: flake8
|
||||
types: [python]
|
||||
args: ['--config=.flake8', '--tee', '--benchmark']
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.6.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
# note: whitespace exclusions use multiline regex, see https://pre-commit.com/#regular-expressions
|
||||
# items are:
|
||||
# 1 - some file extensions
|
||||
# 2 - any file matching *test*/*expected* (for host tests, if possible use this naming pattern always)
|
||||
# 3 - any file with known-warnings in the name
|
||||
# 4 - any directory named 'testdata'
|
||||
# 5 - protobuf auto-generated files
|
||||
exclude: &whitespace_excludes |
|
||||
(?x)^(
|
||||
.+\.(md|rst|map|bin)|
|
||||
.+test.*\/.*expected.*|
|
||||
.+known-warnings.*|
|
||||
.+\/testdata\/.+|
|
||||
.*_pb2.py|
|
||||
.*.pb-c.h|
|
||||
.*.pb-c.c|
|
||||
.*.yuv
|
||||
)$
|
||||
- id: end-of-file-fixer
|
||||
exclude: *whitespace_excludes
|
||||
- id: check-executables-have-shebangs
|
||||
- id: check-shebang-scripts-are-executable
|
||||
- id: mixed-line-ending
|
||||
args: ['-f=lf']
|
||||
- id: double-quote-string-fixer
|
||||
- id: no-commit-to-branch
|
||||
name: Do not use more than one slash in the branch name
|
||||
args: ['--pattern', '^[^/]*/[^/]*/']
|
||||
- id: no-commit-to-branch
|
||||
name: Do not use uppercase letters in the branch name
|
||||
args: ['--pattern', '^[^A-Z]*[A-Z]']
|
||||
|
||||
- repo: https://github.com/espressif/conventional-precommit-linter
|
||||
rev: v1.8.0
|
||||
hooks:
|
||||
- id: conventional-precommit-linter
|
||||
stages: [commit-msg]
|
||||
args:
|
||||
- --subject-min-length=15
|
||||
- --body-max-line-length=200
|
||||
|
||||
- repo: https://github.com/codespell-project/codespell
|
||||
rev: v2.3.0
|
||||
hooks:
|
||||
- id: codespell
|
||||
args: ['-w' , '--config', '.codespellrc']
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: check-library-versions
|
||||
name: Check library versions
|
||||
entry: ./.github/scripts/check_lib_versions.sh
|
||||
language: system
|
||||
files: '(idf_component.yml|library.properties)'
|
||||
@@ -0,0 +1,42 @@
|
||||
# ChangeLog
|
||||
|
||||
## v1.0.0 - 2024-12-06
|
||||
|
||||
### Enhancements:
|
||||
|
||||
* feat(repo): refactor with esp-lib-utils
|
||||
* feat(repo): support micropython
|
||||
|
||||
## v0.1.0 - 2024-11-05
|
||||
|
||||
### Enhancements:
|
||||
|
||||
* Upload to the ESP Registry and support to build on the esp-idf
|
||||
* Update pre-commit configuration
|
||||
|
||||
## v0.0.4 - 2024-10-18
|
||||
|
||||
### Enhancements:
|
||||
|
||||
* Update CH422G and add a new example to show how to use it (@H-sw123)
|
||||
|
||||
## v0.0.3 - 2024-05-07
|
||||
|
||||
### Enhancements:
|
||||
|
||||
* Add support for CH422G from Waveshare (@lboue)
|
||||
* Update the example to show how to use the CH422G
|
||||
|
||||
## v0.0.2 - 2023-10-07
|
||||
|
||||
### Bug Fixes:
|
||||
|
||||
* Correct library name in `sentence` field of metadata
|
||||
|
||||
## v0.0.1 - 2023-09-20
|
||||
|
||||
### Enhancements:
|
||||
|
||||
* Support for various IO expander chips.
|
||||
* Support to control individual IO in the same way as Arduino
|
||||
* Support to control multiple IOs at the same time.
|
||||
@@ -0,0 +1,27 @@
|
||||
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.3")
|
||||
set(PRIV_REQ "esp_driver_gpio")
|
||||
else()
|
||||
set(PRIV_REQ "driver")
|
||||
endif()
|
||||
|
||||
idf_component_register(
|
||||
SRCS "esp_io_expander.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES ${PRIV_REQ}
|
||||
)
|
||||
|
||||
if(CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER)
|
||||
target_sources(${COMPONENT_LIB} PRIVATE "esp_io_expander_gpio_wrapper.c"
|
||||
"esp_io_expander_gpio_wrapper_weak.c")
|
||||
|
||||
target_include_directories(${COMPONENT_LIB} PRIVATE "priv_include")
|
||||
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=gpio_set_level"
|
||||
"-Wl,--wrap=gpio_get_level"
|
||||
"-Wl,--wrap=gpio_set_direction"
|
||||
"-Wl,--wrap=gpio_set_pull_mode"
|
||||
"-Wl,--wrap=gpio_config"
|
||||
"-Wl,--wrap=gpio_reset_pin"
|
||||
"-u __wrap_gpio_set_level")
|
||||
|
||||
endif()
|
||||
@@ -0,0 +1,9 @@
|
||||
menu "ESP IO Expander"
|
||||
|
||||
config IO_EXPANDER_ENABLE_GPIO_API_WRAPPER
|
||||
bool "Enable GPIO API wrapper"
|
||||
default y
|
||||
help
|
||||
If enabled, ESP-IDF GPIO APIs can be used to control the IOs of IO expander. Full GPIO expander support links when you call esp_io_expander_gpio_wrapper_append_handler()
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,65 @@
|
||||
# ESP IO Expander Component
|
||||
|
||||
[](https://components.espressif.com/components/espressif/esp_io_expander)
|
||||
|
||||
This component is main esp_io_expander component which defines main functions and types for easy adding specific IO expander chip component.
|
||||
|
||||
## Supported features
|
||||
|
||||
### From v1.0
|
||||
|
||||
- Set an IO's direction
|
||||
- Get an IO's direction
|
||||
- Set an IO's output level
|
||||
- Get an IO's input level
|
||||
- Show all IOs' status
|
||||
|
||||
### From v1.1
|
||||
|
||||
- Set an IO's output mode (Push Pull / Open Drain)
|
||||
- Set an IO's Pull-up / Pull-down state
|
||||
|
||||
### From v1.2
|
||||
|
||||
- GPIO API wrapper to control IO expander pins via ESP-IDF `gpio_*` APIs
|
||||
|
||||
## GPIO API wrapper
|
||||
|
||||
When enabled, IO expander pins can be used like regular GPIOs through ESP-IDF's `gpio_*` APIs. This allows existing GPIO-based code to work with IO expanders with minimal changes.
|
||||
|
||||
- Enable in `menuconfig`: `IO_EXPANDER_ENABLE_GPIO_API_WRAPPER` (under `ESP IO Expander`).
|
||||
- Append a handler with a virtual GPIO start index using `esp_io_expander_gpio_wrapper_append_handler(handler, start_io_num)`. The `start_io_num` must be greater than or equal to `GPIO_NUM_MAX`. The handler's `io_count` pins will be mapped to the range `[start_io_num, start_io_num + io_count)`.
|
||||
- Remove a previously appended mapping with `esp_io_expander_gpio_wrapper_remove_handler(handler)`.
|
||||
|
||||
Supported wrapped APIs:
|
||||
- `gpio_set_level`
|
||||
- `gpio_get_level`
|
||||
- `gpio_set_direction` (supports `GPIO_MODE_INPUT`, `GPIO_MODE_OUTPUT`, `GPIO_MODE_OUTPUT_OD` if the chip supports high-Z write)
|
||||
- `gpio_set_pull_mode` (supports `GPIO_PULLUP_ONLY`, `GPIO_PULLDOWN_ONLY` if the chip supports pulldown select, and `GPIO_FLOATING`)
|
||||
- `gpio_config`
|
||||
- `gpio_reset_pin` (supported only as wrapper for compatibility - no function)
|
||||
|
||||
Example:
|
||||
|
||||
```c
|
||||
// 1) Enable IO_EXPANDER_ENABLE_GPIO_API_WRAPPER in menuconfig
|
||||
// 2) Create the IO expander handler via esp_io_expander_new_i2c_xxx(..., &ioexp)
|
||||
esp_io_expander_handle_t ioexp;
|
||||
ESP_ERROR_CHECK(esp_io_expander_new_i2c_xxx(/* i2c_port, i2c_addr, optional cfg */, &ioexp));
|
||||
// 3) Initialize your IO expander and obtain `esp_io_expander_handle_t ioexp`
|
||||
ESP_ERROR_CHECK(esp_io_expander_gpio_wrapper_append_handler(ioexp, GPIO_NUM_MAX));
|
||||
|
||||
gpio_num_t vgpio0 = (gpio_num_t)(GPIO_NUM_MAX + 0);
|
||||
gpio_set_direction(vgpio0, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(vgpio0, 1);
|
||||
|
||||
// When no longer needed:
|
||||
ESP_ERROR_CHECK(esp_io_expander_gpio_wrapper_remove_handler(ioexp));
|
||||
```
|
||||
|
||||
### Future
|
||||
|
||||
- Interrupt mode
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
DEFAULT:
|
||||
perform_check: yes # should the check be performed?
|
||||
# Sections setting this to 'no' don't need to include any other options as they are ignored
|
||||
# When a file is using a section with the option set to 'no', no checks are performed.
|
||||
|
||||
# what licenses (or license expressions) are allowed for files in this section
|
||||
# when setting this option in a section, you need to list all the allowed licenses
|
||||
allowed_licenses:
|
||||
- Apache-2.0
|
||||
- MIT
|
||||
license_for_new_files: Apache-2.0 # license to be used when inserting a new copyright notice
|
||||
new_notice_c: | # notice for new C, CPP, H, HPP and LD files
|
||||
/*
|
||||
* SPDX-FileCopyrightText: {years} Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: {license}
|
||||
*/
|
||||
new_notice_python: | # notice for new python files
|
||||
# SPDX-FileCopyrightText: {years} Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: {license}
|
||||
|
||||
# comment lines matching:
|
||||
# SPDX-FileCopyrightText: year[-year] Espressif Systems
|
||||
# or
|
||||
# SPDX-FileContributor: year[-year] Espressif Systems
|
||||
# are replaced with this template prefixed with the correct comment notation (# or // or *) and SPDX- notation
|
||||
espressif_copyright: '{years} Espressif Systems (Shanghai) CO LTD'
|
||||
|
||||
# You can create your own rules for files or group of files
|
||||
examples_and_unit_tests:
|
||||
include:
|
||||
- 'test_apps/'
|
||||
- 'examples/'
|
||||
allowed_licenses:
|
||||
- Apache-2.0
|
||||
- Unlicense
|
||||
- CC0-1.0
|
||||
license_for_new_files: CC0-1.0
|
||||
|
||||
# ignore: # You can also select ignoring files here
|
||||
# perform_check: no # Don't check files from that block
|
||||
# include:
|
||||
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2026 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"
|
||||
|
||||
#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_HIGHZ,
|
||||
REG_PULLUP_EN,
|
||||
REG_PULLUP_SEL,
|
||||
} 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_set_pullupdown(esp_io_expander_handle_t handle, uint32_t pin_num_mask,
|
||||
esp_io_expander_pullupdown_t state)
|
||||
{
|
||||
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 pull_en, pull_set, pull_en_tmp, pull_set_tmp;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_PULLUP_EN, &pull_en), TAG, "Read Pull-up enable reg failed");
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_PULLUP_SEL, &pull_set), TAG, "Read Pull-up select reg failed");
|
||||
pull_en_tmp = pull_en;
|
||||
pull_set_tmp = pull_set;
|
||||
|
||||
if ((state == IO_EXPANDER_PULL_UP && !handle->config.flags.pullup_high_bit_zero) || (state == IO_EXPANDER_PULL_DOWN
|
||||
&& handle->config.flags.pullup_high_bit_zero)) {
|
||||
/* 1. High level && Set 1 to output high */
|
||||
/* 2. Low level && Set 1 to output low */
|
||||
pull_set |= pin_num_mask;
|
||||
} else {
|
||||
/* 3. High level && Set 0 to output high */
|
||||
/* 4. Low level && Set 0 to output low */
|
||||
pull_set &= ~pin_num_mask;
|
||||
}
|
||||
|
||||
/* Write to reg only when different */
|
||||
if (pull_set != pull_set_tmp) {
|
||||
ESP_RETURN_ON_ERROR(write_reg(handle, REG_PULLUP_SEL, pull_set), TAG, "Write Pull-up select reg failed");
|
||||
}
|
||||
|
||||
if (state == IO_EXPANDER_PULL_UP || state == IO_EXPANDER_PULL_DOWN) {
|
||||
/* Enable pull-up/pull-down */
|
||||
pull_en |= pin_num_mask;
|
||||
} else {
|
||||
/* Disable pull-up/pull-down */
|
||||
pull_en &= ~pin_num_mask;
|
||||
}
|
||||
|
||||
/* Write to reg only when different */
|
||||
if (pull_en != pull_en_tmp) {
|
||||
ESP_RETURN_ON_ERROR(write_reg(handle, REG_PULLUP_EN, pull_en), TAG, "Write Pull-up enable reg failed");
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_set_output_mode(esp_io_expander_handle_t handle, uint32_t pin_num_mask,
|
||||
esp_io_expander_output_mode_t mode)
|
||||
{
|
||||
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_highz = (mode == IO_EXPANDER_OUTPUT_MODE_OPEN_DRAIN) ? true : false;
|
||||
uint32_t highz_reg, temp;
|
||||
ESP_RETURN_ON_ERROR(read_reg(handle, REG_HIGHZ, &highz_reg), TAG, "Read High-Z reg failed");
|
||||
temp = highz_reg;
|
||||
if (is_highz) {
|
||||
/* Open drain && Set 1 */
|
||||
highz_reg |= pin_num_mask;
|
||||
} else {
|
||||
/* Push pull && Set 0 */
|
||||
highz_reg &= ~pin_num_mask;
|
||||
}
|
||||
/* Write to reg only when different */
|
||||
if (highz_reg != temp) {
|
||||
ESP_RETURN_ON_ERROR(write_reg(handle, REG_HIGHZ, highz_reg), TAG, "Write High-Z reg failed");
|
||||
}
|
||||
|
||||
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);
|
||||
case REG_HIGHZ:
|
||||
ESP_RETURN_ON_FALSE(handle->write_highz_reg, ESP_ERR_NOT_SUPPORTED, TAG, "write_highz_reg isn't implemented");
|
||||
return handle->write_highz_reg(handle, value);
|
||||
case REG_PULLUP_EN:
|
||||
ESP_RETURN_ON_FALSE(handle->write_pullup_en_reg, ESP_ERR_NOT_SUPPORTED, TAG, "write_pullup_en_reg isn't implemented");
|
||||
return handle->write_pullup_en_reg(handle, value);
|
||||
case REG_PULLUP_SEL:
|
||||
ESP_RETURN_ON_FALSE(handle->write_pullup_sel_reg, ESP_ERR_NOT_SUPPORTED, TAG, "write_pullup_sel_reg isn't implemented");
|
||||
return handle->write_pullup_sel_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);
|
||||
case REG_HIGHZ:
|
||||
ESP_RETURN_ON_FALSE(handle->read_highz_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_highz_reg isn't implemented");
|
||||
return handle->read_highz_reg(handle, value);
|
||||
case REG_PULLUP_EN:
|
||||
ESP_RETURN_ON_FALSE(handle->read_pullup_en_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_pullup_en_reg isn't implemented");
|
||||
return handle->read_pullup_en_reg(handle, value);
|
||||
case REG_PULLUP_SEL:
|
||||
ESP_RETURN_ON_FALSE(handle->read_pullup_sel_reg, ESP_ERR_NOT_SUPPORTED, TAG, "read_pullup_sel_reg isn't implemented");
|
||||
return handle->read_pullup_sel_reg(handle, value);
|
||||
default:
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Linked only when the app references esp_io_expander_gpio_wrapper_append_handler
|
||||
* (or remove_handler). Provides strong esp_io_expander_gpio_wrapper_*.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_io_expander_gpio_wrapper.h"
|
||||
#include "esp_io_expander_gpio_wrapper_priv.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "soc/gpio_num.h"
|
||||
|
||||
typedef struct ioexp_range_node {
|
||||
uint32_t start_num;
|
||||
uint32_t count;
|
||||
esp_io_expander_handle_t handler;
|
||||
struct ioexp_range_node *next;
|
||||
} ioexp_range_node_t;
|
||||
|
||||
static ioexp_range_node_t s_embedded_range_head = {
|
||||
.start_num = 0,
|
||||
.count = GPIO_NUM_MAX,
|
||||
.handler = NULL,
|
||||
.next = NULL,
|
||||
};
|
||||
static ioexp_range_node_t *s_ioexp_ranges = &s_embedded_range_head;
|
||||
static portMUX_TYPE s_ioexp_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
static char *TAG = "io_expander_wrapper";
|
||||
|
||||
static bool find_ioexp_for_num(uint32_t gpio_num, esp_io_expander_handle_t *out_handler, uint32_t *out_pin_mask)
|
||||
{
|
||||
bool found = false;
|
||||
portENTER_CRITICAL(&s_ioexp_lock);
|
||||
ioexp_range_node_t *node = s_ioexp_ranges;
|
||||
while (node) {
|
||||
uint32_t end = node->start_num + node->count;
|
||||
if (gpio_num >= node->start_num && gpio_num < end) {
|
||||
uint32_t bit_index = gpio_num - node->start_num;
|
||||
if (out_handler) {
|
||||
*out_handler = node->handler;
|
||||
}
|
||||
if (out_pin_mask) {
|
||||
*out_pin_mask = (1U << bit_index);
|
||||
}
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
portEXIT_CRITICAL(&s_ioexp_lock);
|
||||
return found;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_append_handler(esp_io_expander_handle_t handler, uint32_t start_io_num)
|
||||
{
|
||||
if (handler == NULL || start_io_num < GPIO_NUM_MAX) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
uint32_t cnt = handler->config.io_count;
|
||||
if (cnt == 0 || cnt > IO_COUNT_MAX) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
// [start, end)
|
||||
uint32_t start = start_io_num;
|
||||
uint32_t end = start_io_num + cnt;
|
||||
// Pre-allocate node before taking the lock to minimize time spent in critical section
|
||||
ioexp_range_node_t *node = (ioexp_range_node_t *)heap_caps_malloc(sizeof(ioexp_range_node_t),
|
||||
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
if (node == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
esp_err_t err = ESP_OK;
|
||||
// Ensure no overlap and no duplicate handler with existing non-embedded nodes
|
||||
portENTER_CRITICAL(&s_ioexp_lock);
|
||||
for (ioexp_range_node_t *n = s_ioexp_ranges; n != NULL; n = n->next) {
|
||||
// Disallow appending the same handler more than once
|
||||
if (n->handler == handler) {
|
||||
err = ESP_ERR_INVALID_ARG;
|
||||
goto err;
|
||||
}
|
||||
if (n == &s_embedded_range_head) {
|
||||
continue; // embedded range is [0, GPIO_NUM_MAX), guaranteed disjoint by start_io_num >= GPIO_NUM_MAX
|
||||
}
|
||||
uint32_t n_start = n->start_num;
|
||||
uint32_t n_end = n->start_num + n->count;
|
||||
// Overlap if the intervals [start, end) and [n_start, n_end) intersect
|
||||
if ((start < n_end) && (n_start < end)) {
|
||||
err = ESP_ERR_INVALID_ARG;
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
node->start_num = start_io_num;
|
||||
node->count = cnt;
|
||||
node->handler = handler;
|
||||
node->next = s_ioexp_ranges;
|
||||
s_ioexp_ranges = node;
|
||||
err:
|
||||
portEXIT_CRITICAL(&s_ioexp_lock);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG,
|
||||
"Either requested IO expander range overlaps with existing range or the same handler is already appended");
|
||||
free(node);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_remove_handler(esp_io_expander_handle_t handler)
|
||||
{
|
||||
if (handler == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
ioexp_range_node_t *to_free = NULL;
|
||||
ioexp_range_node_t *prev = NULL;
|
||||
portENTER_CRITICAL(&s_ioexp_lock);
|
||||
ioexp_range_node_t *curr = s_ioexp_ranges;
|
||||
while (curr) {
|
||||
if (curr->handler == handler) {
|
||||
to_free = curr;
|
||||
if (prev) {
|
||||
prev->next = curr->next;
|
||||
} else {
|
||||
s_ioexp_ranges = curr->next;
|
||||
}
|
||||
curr = curr->next;
|
||||
break;
|
||||
}
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
}
|
||||
portEXIT_CRITICAL(&s_ioexp_lock);
|
||||
if (to_free) {
|
||||
free(to_free);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_level(gpio_num_t gpio_num, uint32_t level)
|
||||
{
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return esp_io_expander_set_level(handle, pin_mask, (uint8_t)(level ? 1 : 0));
|
||||
}
|
||||
|
||||
int esp_io_expander_gpio_wrapper_get_level(gpio_num_t gpio_num)
|
||||
{
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return -1;
|
||||
}
|
||||
uint32_t level_mask = 0;
|
||||
esp_err_t err = esp_io_expander_get_level(handle, pin_mask, &level_mask);
|
||||
if (err != ESP_OK) {
|
||||
return -1;
|
||||
}
|
||||
return (level_mask & pin_mask) ? 1 : 0;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
|
||||
{
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_io_expander_dir_t dir;
|
||||
esp_io_expander_output_mode_t out_mode = IO_EXPANDER_OUTPUT_MODE_PUSH_PULL;
|
||||
bool mode_valid = true;
|
||||
switch (mode) {
|
||||
case GPIO_MODE_INPUT:
|
||||
dir = IO_EXPANDER_INPUT;
|
||||
break;
|
||||
case GPIO_MODE_OUTPUT:
|
||||
dir = IO_EXPANDER_OUTPUT;
|
||||
out_mode = IO_EXPANDER_OUTPUT_MODE_PUSH_PULL;
|
||||
break;
|
||||
case GPIO_MODE_OUTPUT_OD:
|
||||
dir = IO_EXPANDER_OUTPUT;
|
||||
out_mode = IO_EXPANDER_OUTPUT_MODE_OPEN_DRAIN;
|
||||
if (!handle->write_highz_reg) {
|
||||
mode_valid = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mode_valid = false;
|
||||
}
|
||||
if (!mode_valid) {
|
||||
ESP_LOGE(TAG, "Unsupported GPIO mode %d for IO Expander GPIO %d", mode, gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_err_t err = esp_io_expander_set_dir(handle, pin_mask, dir);
|
||||
if (err == ESP_OK && dir == IO_EXPANDER_OUTPUT && handle->write_highz_reg) {
|
||||
err = esp_io_expander_set_output_mode(handle, pin_mask, out_mode);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
|
||||
{
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_io_expander_pullupdown_t pud = IO_EXPANDER_PULL_NONE;
|
||||
bool pud_valid = true;
|
||||
switch (pull) {
|
||||
case GPIO_PULLUP_ONLY:
|
||||
if (!handle->write_pullup_en_reg) {
|
||||
pud_valid = false;
|
||||
} else {
|
||||
pud = IO_EXPANDER_PULL_UP;
|
||||
}
|
||||
break;
|
||||
case GPIO_PULLDOWN_ONLY:
|
||||
if (!handle->write_pullup_en_reg || !handle->write_pullup_sel_reg) {
|
||||
pud_valid = false;
|
||||
} else {
|
||||
pud = IO_EXPANDER_PULL_DOWN;
|
||||
}
|
||||
break;
|
||||
case GPIO_FLOATING:
|
||||
pud = IO_EXPANDER_PULL_NONE;
|
||||
break;
|
||||
default:
|
||||
pud_valid = false;
|
||||
break;
|
||||
}
|
||||
if (!pud_valid) {
|
||||
ESP_LOGE(TAG, "Unsupported GPIO pull mode %d for IO Expander GPIO %d", pull, gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return esp_io_expander_set_pullupdown(handle, pin_mask, pud);
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_configure_pin(gpio_num_t gpio_num, const gpio_config_t *config)
|
||||
{
|
||||
if (config == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_err_t err = ESP_OK;
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if ((config->mode) & GPIO_MODE_DEF_INPUT) {
|
||||
err = esp_io_expander_set_dir(handle, pin_mask, IO_EXPANDER_INPUT);
|
||||
} else if ((config->mode) & GPIO_MODE_DEF_OUTPUT) {
|
||||
err = esp_io_expander_set_dir(handle, pin_mask, IO_EXPANDER_OUTPUT);
|
||||
if (handle->write_highz_reg) {
|
||||
if ((config->mode) & GPIO_MODE_DEF_OD) {
|
||||
err = esp_io_expander_set_output_mode(handle, pin_mask, IO_EXPANDER_OUTPUT_MODE_OPEN_DRAIN);
|
||||
} else {
|
||||
err = esp_io_expander_set_output_mode(handle, pin_mask, IO_EXPANDER_OUTPUT_MODE_PUSH_PULL);
|
||||
}
|
||||
} else if ((config->mode) & GPIO_MODE_DEF_OD) {
|
||||
ESP_LOGW(TAG, "IO Expander GPIO does not support open drain mode.");
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unsupported GPIO mode %d for IO Expander GPIO %d", config->mode, gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
esp_io_expander_pullupdown_t pull_mode = IO_EXPANDER_PULL_NONE;
|
||||
bool pull_mode_valid = true;
|
||||
if (config->pull_up_en && config->pull_down_en) {
|
||||
pull_mode_valid = false;
|
||||
} else if (config->pull_up_en) {
|
||||
if (!handle->write_pullup_en_reg) {
|
||||
pull_mode_valid = false;
|
||||
} else {
|
||||
pull_mode = IO_EXPANDER_PULL_UP;
|
||||
}
|
||||
} else if (config->pull_down_en) {
|
||||
if (!handle->write_pullup_en_reg || !handle->write_pullup_sel_reg) {
|
||||
pull_mode_valid = false;
|
||||
} else {
|
||||
pull_mode = IO_EXPANDER_PULL_DOWN;
|
||||
}
|
||||
} else {
|
||||
pull_mode = IO_EXPANDER_PULL_NONE;
|
||||
}
|
||||
|
||||
if (pull_mode_valid) {
|
||||
if (handle->write_pullup_en_reg) {
|
||||
err = esp_io_expander_set_pullupdown(handle, pin_mask, pull_mode);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unsupported GPIO pull mode for IO Expander GPIO %d", gpio_num);
|
||||
}
|
||||
|
||||
if (config->intr_type) {
|
||||
ESP_LOGW(TAG, "IO Expander does not support interrupts.");
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t esp_io_expander_gpio_wrapper_reset_pin(gpio_num_t gpio_num)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
esp_io_expander_handle_t handle = NULL;
|
||||
uint32_t pin_mask = 0;
|
||||
if (!find_ioexp_for_num((uint32_t)gpio_num, &handle, &pin_mask)) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not assigned to any IO Expander", gpio_num);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
// Set level to LOW
|
||||
ESP_RETURN_ON_ERROR(esp_io_expander_set_level(handle, pin_mask, (uint8_t)(0)), TAG,
|
||||
"Set level of IO expander pin %d failed.", gpio_num);
|
||||
|
||||
// Set as input
|
||||
ESP_RETURN_ON_ERROR(esp_io_expander_set_dir(handle, pin_mask, IO_EXPANDER_INPUT), TAG,
|
||||
"Set direction of IO expander pin %d failed.", gpio_num);
|
||||
|
||||
// Remove pull resistors
|
||||
if (handle->write_pullup_en_reg) {
|
||||
ESP_RETURN_ON_ERROR(esp_io_expander_set_pullupdown(handle, pin_mask, IO_EXPANDER_PULL_NONE), TAG,
|
||||
"Set pull mode of IO expander pin %d failed.", gpio_num);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_io_expander_gpio_wrapper.h"
|
||||
#include "esp_io_expander_gpio_wrapper_priv.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "soc/gpio_num.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
static const char *TAG = "io_exp_gpio_wrap";
|
||||
|
||||
esp_err_t __real_gpio_set_level(gpio_num_t gpio_num, uint32_t level);
|
||||
int __real_gpio_get_level(gpio_num_t gpio_num);
|
||||
esp_err_t __real_gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode);
|
||||
esp_err_t __real_gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull);
|
||||
esp_err_t __real_gpio_config(const gpio_config_t *pGPIOConfig);
|
||||
esp_err_t __real_gpio_reset_pin(gpio_num_t gpio_num);
|
||||
|
||||
__attribute__((weak)) esp_err_t esp_io_expander_gpio_wrapper_set_level(gpio_num_t gpio_num, uint32_t level)
|
||||
{
|
||||
(void)level;
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
__attribute__((weak)) int esp_io_expander_gpio_wrapper_get_level(gpio_num_t gpio_num)
|
||||
{
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return -1;
|
||||
}
|
||||
|
||||
__attribute__((weak)) esp_err_t esp_io_expander_gpio_wrapper_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
|
||||
{
|
||||
(void)mode;
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
__attribute__((weak)) esp_err_t esp_io_expander_gpio_wrapper_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
|
||||
{
|
||||
(void)pull;
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
__attribute__((weak)) esp_err_t esp_io_expander_gpio_wrapper_configure_pin(gpio_num_t gpio_num,
|
||||
const gpio_config_t *config)
|
||||
{
|
||||
(void)config;
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
__attribute__((weak)) esp_err_t esp_io_expander_gpio_wrapper_reset_pin(gpio_num_t gpio_num)
|
||||
{
|
||||
ESP_LOGW(TAG,
|
||||
"GPIO %d is not a native pin; link expander wrapper by calling esp_io_expander_gpio_wrapper_append_handler()",
|
||||
(int)gpio_num);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_err_t __wrap_gpio_set_level(gpio_num_t gpio_num, uint32_t level)
|
||||
{
|
||||
if (gpio_num < GPIO_NUM_MAX) {
|
||||
return __real_gpio_set_level(gpio_num, level);
|
||||
}
|
||||
return esp_io_expander_gpio_wrapper_set_level(gpio_num, level);
|
||||
}
|
||||
|
||||
int __wrap_gpio_get_level(gpio_num_t gpio_num)
|
||||
{
|
||||
if (gpio_num < GPIO_NUM_MAX) {
|
||||
return __real_gpio_get_level(gpio_num);
|
||||
}
|
||||
return esp_io_expander_gpio_wrapper_get_level(gpio_num);
|
||||
}
|
||||
|
||||
esp_err_t __wrap_gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
|
||||
{
|
||||
if (gpio_num < GPIO_NUM_MAX) {
|
||||
return __real_gpio_set_direction(gpio_num, mode);
|
||||
}
|
||||
return esp_io_expander_gpio_wrapper_set_direction(gpio_num, mode);
|
||||
}
|
||||
|
||||
esp_err_t __wrap_gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
|
||||
{
|
||||
if (gpio_num < GPIO_NUM_MAX) {
|
||||
return __real_gpio_set_pull_mode(gpio_num, pull);
|
||||
}
|
||||
return esp_io_expander_gpio_wrapper_set_pull_mode(gpio_num, pull);
|
||||
}
|
||||
|
||||
esp_err_t __wrap_gpio_config(const gpio_config_t *pGPIOConfig)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
gpio_config_t cfg = *pGPIOConfig;
|
||||
uint64_t invalid_mask = cfg.pin_bit_mask & ~SOC_GPIO_VALID_GPIO_MASK;
|
||||
|
||||
while (invalid_mask) {
|
||||
int io_num = __builtin_ctzll(invalid_mask);
|
||||
invalid_mask &= invalid_mask - 1;
|
||||
ret = esp_io_expander_gpio_wrapper_configure_pin((gpio_num_t)io_num, &cfg);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
cfg.pin_bit_mask &= ~(1ULL << io_num);
|
||||
}
|
||||
|
||||
if (cfg.pin_bit_mask) {
|
||||
return __real_gpio_config(&cfg);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t __wrap_gpio_reset_pin(gpio_num_t gpio_num)
|
||||
{
|
||||
if (gpio_num < GPIO_NUM_MAX) {
|
||||
return __real_gpio_reset_pin(gpio_num);
|
||||
}
|
||||
return esp_io_expander_gpio_wrapper_reset_pin(gpio_num);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* | Supported IO Expanders | CH422G |
|
||||
* | ------------------------- | ------ |
|
||||
*
|
||||
* # CH422G Test Example
|
||||
*
|
||||
* The hardware device used in this example is waveshare ESP32-S3-Touch-LCD-4.3B-BOX. To test the simultaneous use of I/O input and OC output, connect DO0 to DI0, and connect DO1 to DI1.
|
||||
*
|
||||
* ## How to use
|
||||
*
|
||||
* 1. Enable USB CDC.
|
||||
* 2. Verify and upload the example to your board.
|
||||
*
|
||||
* ## Serial Output
|
||||
*
|
||||
* ```
|
||||
* ...
|
||||
* Test begin
|
||||
* Set the OC pin to push-pull output mode.
|
||||
* Set the IO0-7 pin to input mode.
|
||||
* Set pint 8 and 9 to:0, 1
|
||||
*
|
||||
* Read pin 0 and 5 level: 0, 1
|
||||
*
|
||||
* Set pint 8 and 9 to:1, 0
|
||||
*
|
||||
* Read pin 0 and 5 level: 1, 0
|
||||
* ...
|
||||
* ```
|
||||
*
|
||||
* ## Troubleshooting
|
||||
*
|
||||
* The driver initialization by default sets CH422G's IO0-7 to output high-level mode.
|
||||
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.
|
||||
Using pinMode and multiPinMode will be invalid. You can only set the pin working mode through enableAllIO_Input, enableAllIO_Output, enableOC_PushPull and enableOC_OpenDrain
|
||||
*
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <esp_io_expander.hpp>
|
||||
|
||||
#define EXAMPLE_I2C_SDA_PIN (8)
|
||||
#define EXAMPLE_I2C_SCL_PIN (9)
|
||||
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS)
|
||||
|
||||
esp_expander::CH422G *expander = NULL;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("Test begin");
|
||||
|
||||
expander = new esp_expander::CH422G(EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN, EXAMPLE_I2C_ADDR);
|
||||
expander->init();
|
||||
expander->begin();
|
||||
|
||||
Serial.println("Set the OC pin to push-pull output mode.");
|
||||
expander->enableOC_PushPull();
|
||||
|
||||
// Serial.println("Set the OC pin to open_drain output mode.");
|
||||
// expander->enableOC_OpenDrain();
|
||||
|
||||
Serial.println("Set the IO0-7 pin to input mode.");
|
||||
expander->enableAllIO_Input();
|
||||
|
||||
// Serial.println("Set the IO0-7 pin to output mode.");
|
||||
// expander->enableAllIO_Output();
|
||||
}
|
||||
|
||||
int level[2] = { 0, 0 };
|
||||
|
||||
void loop() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
bool toggle = i % 2;
|
||||
|
||||
Serial.print("Set pint 8 and 9 to:");
|
||||
Serial.print(toggle);
|
||||
Serial.print(", ");
|
||||
Serial.println(!toggle);
|
||||
Serial.println();
|
||||
|
||||
// Set pin 8 and 9 level
|
||||
expander->digitalWrite(8, toggle);
|
||||
expander->digitalWrite(9, !toggle);
|
||||
delay(1);
|
||||
|
||||
// Read pin 0 and 5 level
|
||||
level[0] = expander->digitalRead(0);
|
||||
level[1] = expander->digitalRead(5);
|
||||
|
||||
Serial.print("Read pin 0 and 5 level: ");
|
||||
Serial.print(level[0]);
|
||||
Serial.print(", ");
|
||||
Serial.println(level[1]);
|
||||
Serial.println();
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <Arduino.h>
|
||||
#include <esp_io_expander.hpp>
|
||||
|
||||
/* The following default configurations are for the board 'Espressif: ESP32_S3_LCD_EV_BOARD_V1_5, TCA9554' */
|
||||
/**
|
||||
* Choose one of the following chip names:
|
||||
* - TCA95XX_8BIT
|
||||
* - TCA95XX_16BIT
|
||||
* - HT8574
|
||||
* - CH422G
|
||||
*/
|
||||
#define EXAMPLE_CHIP_NAME TCA95XX_8BIT
|
||||
#define EXAMPLE_I2C_SDA_PIN (47)
|
||||
#define EXAMPLE_I2C_SCL_PIN (48)
|
||||
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000) // Change this value according to the
|
||||
// hardware address
|
||||
|
||||
#define _EXAMPLE_CHIP_CLASS(name, ...) esp_expander::name(__VA_ARGS__)
|
||||
#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__)
|
||||
|
||||
esp_expander::Base *expander = nullptr;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Test begin");
|
||||
|
||||
/**
|
||||
* Taking `TCA95XX_8BIT` as an example, the following is the code after macro expansion:
|
||||
* expander = new esp_expander::TCA95XX_8BIT((48), (47), (0x20))
|
||||
*/
|
||||
expander = new EXAMPLE_CHIP_CLASS(
|
||||
EXAMPLE_CHIP_NAME, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000
|
||||
);
|
||||
expander->init();
|
||||
expander->begin();
|
||||
|
||||
/* For CH422G */
|
||||
// static_cast<esp_expander::CH422G *>(expander)->enableOC_OpenDrain();
|
||||
// static_cast<esp_expander::CH422G *>(expander)->enableOC_PushPull();
|
||||
// static_cast<esp_expander::CH422G *>(expander)->enableAllIO_Input();
|
||||
// static_cast<esp_expander::CH422G *>(expander)->enableAllIO_Output();
|
||||
|
||||
Serial.println("Original status:");
|
||||
expander->printStatus();
|
||||
|
||||
expander->pinMode(0, OUTPUT);
|
||||
expander->pinMode(1, OUTPUT);
|
||||
expander->multiPinMode(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, OUTPUT);
|
||||
|
||||
Serial.println("Set pint 0-3 to output mode:");
|
||||
expander->printStatus();
|
||||
|
||||
expander->digitalWrite(0, LOW);
|
||||
expander->digitalWrite(1, LOW);
|
||||
expander->multiDigitalWrite(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, LOW);
|
||||
|
||||
Serial.println("Set pint 0-3 to low level:");
|
||||
expander->printStatus();
|
||||
|
||||
expander->pinMode(0, INPUT);
|
||||
expander->pinMode(1, INPUT);
|
||||
expander->multiPinMode(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, INPUT);
|
||||
|
||||
Serial.println("Set pint 0-3 to input mode:");
|
||||
expander->printStatus();
|
||||
}
|
||||
|
||||
int level[4] = {0, 0, 0, 0};
|
||||
uint32_t level_temp;
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Read pin 0-3 level
|
||||
level[0] = expander->digitalRead(0);
|
||||
level[1] = expander->digitalRead(1);
|
||||
level_temp = expander->multiDigitalRead(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3);
|
||||
level[2] = level_temp & IO_EXPANDER_PIN_NUM_2 ? HIGH : LOW;
|
||||
level[3] = level_temp & IO_EXPANDER_PIN_NUM_3 ? HIGH : LOW;
|
||||
Serial.printf("Pin level: %d, %d, %d, %d\n", level[0], level[1], level[2], level[3]);
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
dependencies:
|
||||
idf: '>=4.4.2'
|
||||
description: ESP IO Expander - main component for using io expander chip
|
||||
repository: git://github.com/espressif/esp-bsp.git
|
||||
repository_info:
|
||||
commit_sha: eb76dc6ecf21ccc4ee7ee58bfea3d3d31fa090cf
|
||||
path: components/io_expander/esp_io_expander
|
||||
url: https://github.com/espressif/esp-bsp/tree/master/components/io_expander/esp_io_expander
|
||||
version: 1.2.1
|
||||
@@ -0,0 +1,408 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2026 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 Pin output mode
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
IO_EXPANDER_OUTPUT_MODE_PUSH_PULL, /*!< Output High-Z = 0 */
|
||||
IO_EXPANDER_OUTPUT_MODE_OPEN_DRAIN, /*!< Output High-Z = 1 */
|
||||
} esp_io_expander_output_mode_t;
|
||||
|
||||
/**
|
||||
* @brief IO Expander Pin pull-up/pull-down
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
IO_EXPANDER_PULL_NONE, /*!< Not used pull-up/pull-down */
|
||||
IO_EXPANDER_PULL_UP, /*!< Set pull-up */
|
||||
IO_EXPANDER_PULL_DOWN, /*!< Set pull-down */
|
||||
} esp_io_expander_pullupdown_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 */
|
||||
uint8_t pullup_high_bit_zero :
|
||||
1; /*!< If the pullup/down 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 diection 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 Write value to high impedance register (optional)
|
||||
*
|
||||
* @note If there are multiple high impedance 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_highz_reg)(esp_io_expander_handle_t handle, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief Read value from high impedance register (optional)
|
||||
*
|
||||
* @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 high impedance 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_highz_reg)(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
|
||||
/**
|
||||
* @brief Write value to enable pullup register (optional)
|
||||
*
|
||||
* @note If there are multiple enable pullup 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_pullup_en_reg)(esp_io_expander_handle_t handle, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief Read value from enable pullup register (optional)
|
||||
*
|
||||
* @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 enable pullup 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_pullup_en_reg)(esp_io_expander_handle_t handle, uint32_t *value);
|
||||
|
||||
/**
|
||||
* @brief Write value to select pullup register (optional)
|
||||
*
|
||||
* @note If there are multiple set pullup 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_pullup_sel_reg)(esp_io_expander_handle_t handle, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief Read value from select pullup register (optional)
|
||||
*
|
||||
* @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 set pullup 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_pullup_sel_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 intput 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 Set the pull-up/pull-down of a set of target IOs
|
||||
*
|
||||
* @note
|
||||
*
|
||||
* @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 state: State of pull-up/pull-down
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_set_pullupdown(esp_io_expander_handle_t handle, uint32_t pin_num_mask,
|
||||
esp_io_expander_pullupdown_t state);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the output mode (High-Z) of a set of target IOs
|
||||
*
|
||||
* @note
|
||||
*
|
||||
* @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 mode: Output mode
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_set_output_mode(esp_io_expander_handle_t handle, uint32_t pin_num_mask,
|
||||
esp_io_expander_output_mode_t mode);
|
||||
|
||||
|
||||
/**
|
||||
* @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,43 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_io_expander.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER
|
||||
/**
|
||||
* @brief Append a range of GPIOs additional to the existing embedded GPIO range, given IO expander handler
|
||||
*
|
||||
* @param handler: IO Expander handle
|
||||
* @param start_io_num: Start GPIO number that maps to the IO expander range
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_gpio_wrapper_append_handler(esp_io_expander_handle_t handler, uint32_t start_io_num);
|
||||
|
||||
/**
|
||||
* @brief Remove a range of already appended GPIOs, given IO expander handler
|
||||
*
|
||||
* @param handler: IO Expander handle
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Success, otherwise returns ESP_ERR_xxx
|
||||
*/
|
||||
esp_err_t esp_io_expander_gpio_wrapper_remove_handler(esp_io_expander_handle_t handler);
|
||||
#endif // CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
name=ESP32_IO_Expander
|
||||
version=1.0.0
|
||||
author=espressif
|
||||
maintainer=espressif
|
||||
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP SoCs
|
||||
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G
|
||||
category=Other
|
||||
architectures=esp32
|
||||
url=https://github.com/esp-arduino-libs/ESP32_IO_Expander
|
||||
includes=esp_io_expander.hpp
|
||||
depends=esp-lib-utils (>=0.1.0 && <0.2.0)
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,16 @@
|
||||
# This file is to be given as "make USER_C_MODULES=..." when building Micropython port
|
||||
|
||||
add_library(usermod_esp_io_expander INTERFACE)
|
||||
|
||||
# Set the source directorya and find all source files.
|
||||
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
|
||||
file(GLOB_RECURSE SRCS ${SRC_DIR}/*.c)
|
||||
|
||||
# Add our source files to the library.
|
||||
target_sources(usermod_esp_io_expander INTERFACE ${SRCS})
|
||||
|
||||
# Add the current directory as an include directory.
|
||||
target_include_directories(usermod_esp_io_expander INTERFACE ${SRC_DIR})
|
||||
|
||||
# Link our INTERFACE library to the usermod target.
|
||||
target_link_libraries(usermod INTERFACE usermod_esp_io_expander)
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
/* Called only for gpio_num >= GPIO_NUM_MAX (virtual expander pins). */
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_level(gpio_num_t gpio_num, uint32_t level);
|
||||
int esp_io_expander_gpio_wrapper_get_level(gpio_num_t gpio_num);
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_direction(gpio_num_t gpio_num, gpio_mode_t mode);
|
||||
esp_err_t esp_io_expander_gpio_wrapper_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull);
|
||||
esp_err_t esp_io_expander_gpio_wrapper_configure_pin(gpio_num_t gpio_num, const gpio_config_t *config);
|
||||
esp_err_t esp_io_expander_gpio_wrapper_reset_pin(gpio_num_t gpio_num);
|
||||
@@ -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
|
||||
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
set(component main)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(io_expander_test)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "test_app_main.cpp" "test_chip_general.cpp"
|
||||
WHOLE_ARCHIVE
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
## IDF Component Manager Manifest File
|
||||
dependencies:
|
||||
test_utils:
|
||||
path: ${IDF_PATH}/tools/unit-test-app/components/test_utils
|
||||
ESP32_IO_Expander:
|
||||
version: "*"
|
||||
override_path: "../../../ESP32_IO_Expander"
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "driver/i2c.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "unity.h"
|
||||
#include "unity_test_utils.h"
|
||||
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (400)
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
|
||||
void setUp(void)
|
||||
{
|
||||
unity_utils_record_free_mem();
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
esp_reent_cleanup(); //clean up some of the newlib's lazy allocations
|
||||
unity_utils_evaluate_leaks_direct(TEST_MEMORY_LEAK_THRESHOLD);
|
||||
}
|
||||
#else
|
||||
static size_t before_free_8bit;
|
||||
static size_t before_free_32bit;
|
||||
|
||||
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||
{
|
||||
ssize_t delta = before_free - after_free;
|
||||
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
|
||||
TEST_ASSERT_MESSAGE(delta < TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
||||
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
||||
}
|
||||
#endif
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
// _____ ___ __ _
|
||||
// \_ \/___\ /__\_ ___ __ __ _ _ __ __| | ___ _ __
|
||||
// / /\// // /_\ \ \/ / '_ \ / _` | '_ \ / _` |/ _ \ '__|
|
||||
// /\/ /_/ \_// //__ > <| |_) | (_| | | | | (_| | __/ |
|
||||
// \____/\___/ \__/ /_/\_\ .__/ \__,_|_| |_|\__,_|\___|_|
|
||||
// |_|
|
||||
printf(" _____ ___ __ _\r\n");
|
||||
printf(" \\_ \\/___\\ /__\\_ ___ __ __ _ _ __ __| | ___ _ __\r\n");
|
||||
printf(" / /\\// // /_\\ \\ \\/ / '_ \\ / _` | '_ \\ / _` |/ _ \\ '__|\r\n");
|
||||
printf("/\\/ /_/ \\_// //__ > <| |_) | (_| | | | | (_| | __/ |\r\n");
|
||||
printf("\\____/\\___/ \\__/ /_/\\_\\ .__/ \\__,_|_| |_|\\__,_|\\___|_|\r\n");
|
||||
printf(" |_|\r\n");
|
||||
unity_run_menu();
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <inttypes.h>
|
||||
#include "driver/i2c.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "unity.h"
|
||||
#include "unity_test_runner.h"
|
||||
#include "esp_io_expander.hpp"
|
||||
|
||||
using namespace esp_expander;
|
||||
|
||||
static const char *TAG = "general_test";
|
||||
|
||||
/* The following default configurations are for the board 'Espressif: ESP32_S3_LCD_EV_BOARD_V1_5, TCA9554' */
|
||||
#define TEST_HOST_ID (I2C_NUM_0)
|
||||
#define TEST_HOST_I2C_SCL_PIN (48)
|
||||
#define TEST_HOST_I2C_SDA_PIN (47)
|
||||
#define TEST_DEVICE_ADDRESS (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000)
|
||||
|
||||
#define HOST_CONFIG_DEFAULT(scl, sda) \
|
||||
{ \
|
||||
.mode = I2C_MODE_MASTER, \
|
||||
.sda_io_num = (sda), \
|
||||
.scl_io_num = (scl), \
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE, \
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE, \
|
||||
.master = { \
|
||||
.clk_speed = 400000, \
|
||||
}, \
|
||||
.clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL, \
|
||||
}
|
||||
|
||||
#define _CREATE_DEVICE(name, ...) \
|
||||
({ \
|
||||
ESP_LOGI(TAG, "Create device: " #name); \
|
||||
std::shared_ptr<Base> device = std::make_shared<name>(__VA_ARGS__); \
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(device, "Create device failed"); \
|
||||
device; \
|
||||
})
|
||||
#define CREATE_DEVICE(name, ...) _CREATE_DEVICE(name, ##__VA_ARGS__)
|
||||
|
||||
static void init_host(void)
|
||||
{
|
||||
const i2c_config_t i2c_config = HOST_CONFIG_DEFAULT(TEST_HOST_I2C_SCL_PIN, TEST_HOST_I2C_SDA_PIN);
|
||||
TEST_ASSERT_EQUAL(i2c_param_config(TEST_HOST_ID, &i2c_config), ESP_OK);
|
||||
TEST_ASSERT_EQUAL(i2c_driver_install(TEST_HOST_ID, i2c_config.mode, 0, 0, 0), ESP_OK);
|
||||
}
|
||||
|
||||
static void deinit_host(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL(i2c_driver_delete(TEST_HOST_ID), ESP_OK);
|
||||
}
|
||||
|
||||
static void test_device(std::shared_ptr<Base> device)
|
||||
{
|
||||
TEST_ASSERT_MESSAGE(device->init(), "Device initialization failed");
|
||||
TEST_ASSERT_MESSAGE(device->begin(), "Device begin failed");
|
||||
TEST_ASSERT_MESSAGE(device->reset(), "Device reset failed");
|
||||
TEST_ASSERT_MESSAGE(device->del(), "Device del failed");
|
||||
}
|
||||
|
||||
#define CREATE_TEST_CASE(device_name) \
|
||||
TEST_CASE("test " #device_name " general functions", "[io_expander][general][" #device_name "]") \
|
||||
{ \
|
||||
std::shared_ptr<Base> expander = nullptr; \
|
||||
\
|
||||
ESP_LOGI(TAG, "Initialize I2C host"); \
|
||||
init_host(); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Test constructor with (int host_id, uint8_t address) (external I2C)"); \
|
||||
expander = CREATE_DEVICE(device_name, TEST_HOST_ID, TEST_DEVICE_ADDRESS); \
|
||||
test_device(expander); \
|
||||
expander = nullptr; \
|
||||
\
|
||||
ESP_LOGI(TAG, "Test constructor with (const Config &config) (external I2C)"); \
|
||||
Base::Config external_i2c_config = { \
|
||||
.host_id = TEST_HOST_ID, \
|
||||
.device_address = TEST_DEVICE_ADDRESS, \
|
||||
.skip_init_host = true, \
|
||||
}; \
|
||||
expander = CREATE_DEVICE(device_name, external_i2c_config); \
|
||||
test_device(expander); \
|
||||
expander = nullptr; \
|
||||
\
|
||||
ESP_LOGI(TAG, "Deinitialize I2C host"); \
|
||||
deinit_host(); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Test constructor with (int scl_io, int sda_io, uint8_t address) (internal I2C)"); \
|
||||
expander = CREATE_DEVICE(device_name, TEST_HOST_I2C_SCL_PIN, TEST_HOST_I2C_SDA_PIN, TEST_DEVICE_ADDRESS); \
|
||||
test_device(expander); \
|
||||
expander = nullptr; \
|
||||
\
|
||||
ESP_LOGI(TAG, "Test constructor with (const Config &config) (internal I2C)"); \
|
||||
Base::Config internal_i2c_config = { \
|
||||
.host_id = TEST_HOST_ID, \
|
||||
.host_sda_io_num = TEST_HOST_I2C_SDA_PIN, \
|
||||
.host_scl_io_num = TEST_HOST_I2C_SCL_PIN, \
|
||||
.device_address = TEST_DEVICE_ADDRESS, \
|
||||
.skip_init_host = false, \
|
||||
}; \
|
||||
expander = CREATE_DEVICE(device_name, internal_i2c_config); \
|
||||
test_device(expander); \
|
||||
expander = nullptr; \
|
||||
\
|
||||
expander = CREATE_DEVICE(device_name, TEST_HOST_I2C_SCL_PIN, TEST_HOST_I2C_SDA_PIN, TEST_DEVICE_ADDRESS); \
|
||||
TEST_ASSERT_MESSAGE(expander->init(), "Device initialization failed"); \
|
||||
TEST_ASSERT_MESSAGE(expander->begin(), "Device begin failed"); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Test input/output functions"); \
|
||||
ESP_LOGI(TAG, "Original status:"); \
|
||||
TEST_ASSERT_MESSAGE(expander->printStatus(), "Print status failed"); \
|
||||
\
|
||||
TEST_ASSERT_MESSAGE(expander->pinMode(0, OUTPUT), "Set pin 0 to output mode failed"); \
|
||||
TEST_ASSERT_MESSAGE(expander->pinMode(1, OUTPUT), "Set pin 1 to output mode failed"); \
|
||||
TEST_ASSERT_MESSAGE( \
|
||||
expander->multiPinMode(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, OUTPUT), \
|
||||
"Set pin 2-3 to output mode failed" \
|
||||
); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Set pint 0-3 to output mode:"); \
|
||||
TEST_ASSERT_MESSAGE(expander->printStatus(), "Print status failed"); \
|
||||
\
|
||||
TEST_ASSERT_MESSAGE(expander->digitalWrite(0, LOW), "Set pin 0 to low level failed"); \
|
||||
TEST_ASSERT_MESSAGE(expander->digitalWrite(1, LOW), "Set pin 1 to low level failed"); \
|
||||
TEST_ASSERT_MESSAGE( \
|
||||
expander->multiDigitalWrite(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, LOW), \
|
||||
"Set pin 2-3 to low level failed" \
|
||||
); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Set pint 0-3 to low level:"); \
|
||||
TEST_ASSERT_MESSAGE(expander->printStatus(), "Print status failed"); \
|
||||
\
|
||||
TEST_ASSERT_MESSAGE(expander->pinMode(0, INPUT), "Set pin 0 to input mode failed"); \
|
||||
TEST_ASSERT_MESSAGE(expander->pinMode(1, INPUT), "Set pin 1 to input mode failed"); \
|
||||
TEST_ASSERT_MESSAGE( \
|
||||
expander->multiPinMode(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, INPUT), \
|
||||
"Set pin 2-3 to input mode failed" \
|
||||
); \
|
||||
\
|
||||
ESP_LOGI(TAG, "Set pint 0-3 to input mode:"); \
|
||||
TEST_ASSERT_MESSAGE(expander->printStatus(), "Print status failed"); \
|
||||
\
|
||||
int level[4] = {0, 0, 0, 0}; \
|
||||
int64_t level_temp; \
|
||||
\
|
||||
level[0] = expander->digitalRead(0); \
|
||||
TEST_ASSERT_MESSAGE(level[0] >= 0, "Read pin 0 level failed"); \
|
||||
level[1] = expander->digitalRead(1); \
|
||||
TEST_ASSERT_MESSAGE(level[1] >= 0, "Read pin 0 level failed"); \
|
||||
level_temp = expander->multiDigitalRead(IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3); \
|
||||
TEST_ASSERT_MESSAGE(level_temp >= 0, "Read pin 2-3 level failed"); \
|
||||
level[2] = level_temp & IO_EXPANDER_PIN_NUM_2 ? HIGH : LOW; \
|
||||
level[3] = level_temp & IO_EXPANDER_PIN_NUM_3 ? HIGH : LOW; \
|
||||
ESP_LOGI(TAG, "Pin 0-3 level: %d %d %d %d", level[0], level[1], level[2], level[3]); \
|
||||
}
|
||||
|
||||
/**
|
||||
* Here to create test cases for different devices
|
||||
*
|
||||
*/
|
||||
CREATE_TEST_CASE(TCA95XX_8BIT)
|
||||
CREATE_TEST_CASE(TCA95XX_16BIT)
|
||||
CREATE_TEST_CASE(CH422G)
|
||||
CREATE_TEST_CASE(HT8574)
|
||||
@@ -0,0 +1,3 @@
|
||||
CONFIG_ESP_TASK_WDT_EN=n
|
||||
CONFIG_FREERTOS_HZ=1000
|
||||
CONFIG_COMPILER_CXX_EXCEPTIONS=y
|
||||
Reference in New Issue
Block a user