Skip to content

Commit 53f6127

Browse files
authored
Merge pull request #709 from espressif/feat/io_expander_gpio_example
feat(io_expander): Update versions, documentations and example for using IO Expanders with GPIO API (BSP-773)
2 parents c610947 + 1b00669 commit 53f6127

File tree

14 files changed

+361
-12
lines changed

14 files changed

+361
-12
lines changed

components/io_expander/esp_io_expander_ht8574/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Implementation of the HT8574 io expander chip with esp_io_expander component.
1111
Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/).
1212
You can add them to your project via `idf.py add-dependency`, e.g.
1313
```
14-
idf.py add-dependency esp_io_expander_ht8574==1.0.0
14+
idf.py add-dependency esp_io_expander_ht8574==2.0.2
1515
```
1616

1717
Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html).
@@ -58,3 +58,47 @@ Set pin 2 and pin 3 with input direction:
5858
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_INPUT);
5959
esp_io_expander_get_level(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, &pin_levels);
6060
```
61+
62+
## Example use with GPIO API
63+
64+
1. Enable `CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER=y` in `menuconfig`
65+
2. Include `#include "esp_io_expander_gpio_wrapper.h"` to your project.
66+
67+
3. Creation of the i2c bus.
68+
69+
```c
70+
i2c_master_bus_handle_t i2c_handle = NULL;
71+
const i2c_master_bus_config_t bus_config = {
72+
.i2c_port = I2C_NUM_0,
73+
.sda_io_num = 47,
74+
.scl_io_num = 48,
75+
.clk_source = I2C_CLK_SRC_DEFAULT,
76+
};
77+
i2c_new_master_bus(&bus_config, &i2c_handle);
78+
```
79+
80+
4. Creation of the component.
81+
82+
```c
83+
esp_io_expander_handle_t io_expander = NULL;
84+
esp_io_expander_new_i2c_ht8574(i2c_handle, ESP_IO_EXPANDER_I2C_HT8574_ADDRESS_000, &io_expander);
85+
```
86+
87+
5. Append IO Expander to GPIO wrapper.
88+
89+
```c
90+
esp_io_expander_gpio_wrapper_append_handler(io_expander, GPIO_NUM_MAX);
91+
```
92+
93+
6. Use it as standard GPIO.
94+
95+
```c
96+
const uint8_t io_led1 = GPIO_NUM_MAX + 6; // IO Expander pin 6
97+
gpio_set_direction(io_led1, GPIO_MODE_OUTPUT);
98+
99+
ESP_LOGI(TAG, "LED1 set ON");
100+
gpio_set_level(io_led1, 1);
101+
102+
ESP_LOGI(TAG, "LED1 set OFF");
103+
gpio_set_level(io_led1, 0);
104+
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
version: 2.0.1
1+
version: 2.0.2
22
description: ESP IO Expander - HT8574
33
url: https://github.com/espressif/esp-bsp/tree/master/components/io_expander/esp_io_expander_ht8574
44
dependencies:
55
idf: ">=5.2"
66
esp_io_expander:
7-
version: "^1.0.1"
7+
version: "^1.2.0"
88
public: true
99
override_path: ../esp_io_expander
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# ESP IO Expander Chip PI4IOE5V6408
2+
3+
[![Component Registry](https://components.espressif.com/components/espressif/esp_io_expander_pi4ioe5v6408/badge.svg)](https://components.espressif.com/components/espressif/esp_io_expander_pi4ioe5v6408)
4+
5+
Implementation of the PI4IOE5V6408 io expander chip with esp_io_expander component.
6+
7+
| Chip | Communication interface | Component name | Link to datasheet |
8+
| :--------------: | :---------------------: | :------------: | :---------------: |
9+
| PI4IOE5V6408 | I2C | esp_io_expander_pi4ioe5v6408 | --- |
10+
11+
## Add to project
12+
13+
Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/).
14+
You can add them to your project via `idf.py add-dependency`, e.g.
15+
```
16+
idf.py add-dependency esp_io_expander_pi4ioe5v6408==1.0.1
17+
```
18+
19+
Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html).
20+
21+
## Example use
22+
23+
Creation of the i2c bus.
24+
25+
```c
26+
i2c_master_bus_handle_t i2c_handle = NULL;
27+
const i2c_master_bus_config_t bus_config = {
28+
.i2c_port = I2C_NUM_0,
29+
.sda_io_num = 47,
30+
.scl_io_num = 48,
31+
.clk_source = I2C_CLK_SRC_DEFAULT,
32+
};
33+
i2c_new_master_bus(&bus_config, &i2c_handle);
34+
```
35+
36+
Creation of the component.
37+
38+
```c
39+
esp_io_expander_handle_t io_expander = NULL;
40+
esp_io_expander_new_i2c_pi4ioe5v6408(i2c_handle, ESP_IO_EXPANDER_I2C_PI4IOE5V6408_ADDRESS_LOW, &io_expander);
41+
```
42+
43+
Print all pins's status to the log:
44+
45+
```c
46+
esp_io_expander_print_state(io_expander);
47+
```
48+
49+
Set pin 0 and pin 1 with output direction and low level:
50+
51+
```c
52+
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, IO_EXPANDER_OUTPUT);
53+
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, 0);
54+
```
55+
56+
Set pin 2 and pin 3 with input direction:
57+
58+
```c
59+
uint32_t pin_levels = 0;
60+
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_INPUT);
61+
esp_io_expander_get_level(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, &pin_levels);
62+
```
63+
64+
## Example use with GPIO API
65+
66+
1. Enable `CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER=y` in `menuconfig`
67+
2. Include `#include "esp_io_expander_gpio_wrapper.h"` to your project.
68+
69+
3. Creation of the i2c bus.
70+
71+
```c
72+
i2c_master_bus_handle_t i2c_handle = NULL;
73+
const i2c_master_bus_config_t bus_config = {
74+
.i2c_port = I2C_NUM_0,
75+
.sda_io_num = 47,
76+
.scl_io_num = 48,
77+
.clk_source = I2C_CLK_SRC_DEFAULT,
78+
};
79+
i2c_new_master_bus(&bus_config, &i2c_handle);
80+
```
81+
82+
4. Creation of the component.
83+
84+
```c
85+
esp_io_expander_handle_t io_expander = NULL;
86+
esp_io_expander_new_i2c_pi4ioe5v6408(i2c_handle, ESP_IO_EXPANDER_I2C_PI4IOE5V6408_ADDRESS_LOW, &io_expander);
87+
```
88+
89+
5. Append IO Expander to GPIO wrapper.
90+
91+
```c
92+
esp_io_expander_gpio_wrapper_append_handler(io_expander, GPIO_NUM_MAX);
93+
```
94+
95+
6. Use it as standard GPIO.
96+
97+
```c
98+
const uint8_t io_led1 = GPIO_NUM_MAX + 6; // IO Expander pin 6
99+
gpio_set_direction(io_led1, GPIO_MODE_OUTPUT);
100+
101+
ESP_LOGI(TAG, "LED1 set ON");
102+
gpio_set_level(io_led1, 1);
103+
104+
ESP_LOGI(TAG, "LED1 set OFF");
105+
gpio_set_level(io_led1, 0);
106+
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
version: "1.0.0"
1+
version: "1.0.1"
22
description: ESP IO Expander - PI4IOE5V6408
33
url: https://github.com/espressif/esp-bsp/tree/master/components/io_expander/esp_io_expander_pi4ioe5v6408
44
dependencies:
55
idf: ">=5.2"
66
esp_io_expander:
7-
version: "^1.1.0"
7+
version: "^1.2.0"
88
public: true
99
override_path: "../esp_io_expander"

components/io_expander/esp_io_expander_tca9554/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Implementation of the TCA9554 io expander chip with esp_io_expander component.
1313
Packages from this repository are uploaded to [Espressif's component service](https://components.espressif.com/).
1414
You can add them to your project via `idf.py add-dependency`, e.g.
1515
```
16-
idf.py add-dependency esp_io_expander_tca9554==1.0.0
16+
idf.py add-dependency esp_io_expander_tca9554==2.0.3
1717
```
1818

1919
Alternatively, you can create `idf_component.yml`. More is in [Espressif's documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html).
@@ -60,3 +60,47 @@ Set pin 2 and pin 3 with input direction:
6060
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_INPUT);
6161
esp_io_expander_get_level(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, &pin_levels);
6262
```
63+
64+
## Example use with GPIO API
65+
66+
1. Enable `CONFIG_IO_EXPANDER_ENABLE_GPIO_API_WRAPPER=y` in `menuconfig`
67+
2. Include `#include "esp_io_expander_gpio_wrapper.h"` to your project.
68+
69+
3. Creation of the i2c bus.
70+
71+
```c
72+
i2c_master_bus_handle_t i2c_handle = NULL;
73+
const i2c_master_bus_config_t bus_config = {
74+
.i2c_port = I2C_NUM_0,
75+
.sda_io_num = 47,
76+
.scl_io_num = 48,
77+
.clk_source = I2C_CLK_SRC_DEFAULT,
78+
};
79+
i2c_new_master_bus(&bus_config, &i2c_handle);
80+
```
81+
82+
4. Creation of the component.
83+
84+
```c
85+
esp_io_expander_handle_t io_expander = NULL;
86+
esp_io_expander_new_i2c_tca9554(i2c_handle, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &io_expander);
87+
```
88+
89+
5. Append IO Expander to GPIO wrapper.
90+
91+
```c
92+
esp_io_expander_gpio_wrapper_append_handler(io_expander, GPIO_NUM_MAX);
93+
```
94+
95+
6. Use it as standard GPIO.
96+
97+
```c
98+
const uint8_t io_led1 = GPIO_NUM_MAX + 6; // IO Expander pin 6
99+
gpio_set_direction(io_led1, GPIO_MODE_OUTPUT);
100+
101+
ESP_LOGI(TAG, "LED1 set ON");
102+
gpio_set_level(io_led1, 1);
103+
104+
ESP_LOGI(TAG, "LED1 set OFF");
105+
gpio_set_level(io_led1, 0);
106+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4+
project(gpio)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# IO Expander GPIO Example
2+
3+
This example using GPIO wrapper in IO Expander component for control IOs by standard GPIO functions from ESP-IDF.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.3")
2+
set(REQ esp_driver_gpio esp_driver_i2c)
3+
else()
4+
set(REQ driver)
5+
endif()
6+
7+
idf_component_register(
8+
SRCS "gpio_example_main.c"
9+
INCLUDE_DIRS "."
10+
REQUIRES ${REQ}
11+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: CC0-1.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include "driver/gpio.h"
9+
#include "freertos/FreeRTOS.h"
10+
#include "freertos/task.h"
11+
#include "driver/i2c_master.h"
12+
#include "esp_err.h"
13+
#include "esp_log.h"
14+
#include "esp_io_expander_tca9554.h"
15+
#include "esp_io_expander_gpio_wrapper.h"
16+
17+
static const char *TAG = "example";
18+
19+
#define I2C_HOST 0
20+
21+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
22+
//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
23+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24+
/* Pins are compatible with board esp32_s3_korvo_2 */
25+
#define EXAMPLE_PIN_NUM_SDA 17
26+
#define EXAMPLE_PIN_NUM_SCL 18
27+
28+
void app_main(void)
29+
{
30+
ESP_LOGI(TAG, "Initialize I2C bus");
31+
i2c_master_bus_handle_t i2c_bus = NULL;
32+
i2c_master_bus_config_t bus_config = {
33+
.clk_source = I2C_CLK_SRC_DEFAULT,
34+
.glitch_ignore_cnt = 7,
35+
.i2c_port = I2C_HOST,
36+
.sda_io_num = EXAMPLE_PIN_NUM_SDA,
37+
.scl_io_num = EXAMPLE_PIN_NUM_SCL,
38+
.flags.enable_internal_pullup = true,
39+
};
40+
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &i2c_bus));
41+
42+
ESP_LOGI(TAG, "Initialize IO Expander TCA9554");
43+
esp_io_expander_handle_t io_expander_handle = NULL; // IO expander tca9554 handle
44+
if (esp_io_expander_new_i2c_tca9554(i2c_bus, ESP_IO_EXPANDER_I2C_TCA9554A_ADDRESS_000, &io_expander_handle) == ESP_OK
45+
&& io_expander_handle) {
46+
if (esp_io_expander_gpio_wrapper_append_handler(io_expander_handle, GPIO_NUM_MAX) == ESP_OK) {
47+
ESP_LOGI(TAG, "Registered IO Expander to GPIOs. IOs from %d", GPIO_NUM_MAX);
48+
} else {
49+
ESP_LOGE(TAG, "Failed to register IO Expander to GPIOs.");
50+
}
51+
} else {
52+
ESP_LOGE(TAG, "Failed to initialize TCA9554 IO Expander");
53+
return;
54+
}
55+
56+
const uint8_t io_led1 = GPIO_NUM_MAX + 6; // IO Expander pin 6 (RED)
57+
const uint8_t io_led2 = GPIO_NUM_MAX + 7; // IO Expander pin 7 (BLUE)
58+
59+
ESP_LOGI(TAG, "Set IOs output");
60+
gpio_set_direction(io_led1, GPIO_MODE_OUTPUT);
61+
gpio_set_direction(io_led2, GPIO_MODE_OUTPUT);
62+
63+
while (1) {
64+
ESP_LOGI(TAG, "LED1 set ON");
65+
gpio_set_level(io_led1, 1);
66+
67+
vTaskDelay(pdMS_TO_TICKS(500));
68+
69+
ESP_LOGI(TAG, "LED2 set ON");
70+
gpio_set_level(io_led2, 1);
71+
72+
vTaskDelay(pdMS_TO_TICKS(500));
73+
74+
ESP_LOGI(TAG, "LED1 set OFF");
75+
gpio_set_level(io_led1, 0);
76+
77+
vTaskDelay(pdMS_TO_TICKS(500));
78+
79+
ESP_LOGI(TAG, "LED2 set OFF");
80+
gpio_set_level(io_led2, 0);
81+
82+
vTaskDelay(pdMS_TO_TICKS(500));
83+
}
84+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies:
2+
esp_io_expander_tca9554:
3+
version: "^2"
4+
public: true
5+
override_path: "../../../"

0 commit comments

Comments
 (0)