Skip to content

Commit 5a9d8b4

Browse files
authored
Merge branch 'esp-arduino-libs:master' into master
2 parents 5531264 + e5378da commit 5a9d8b4

File tree

7 files changed

+289
-14
lines changed

7 files changed

+289
-14
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# ChangeLog
22

3+
## v0.0.3 - 2024-05-07
4+
5+
### Enhancements:
6+
7+
* Add support for CH422G from Waveshare (@lboue)
8+
* Update the example to show how to use the CH422G
9+
10+
## v0.0.2 - 2023-10-07
11+
12+
### Bug Fixes:
13+
14+
* Correct library name in `sentence` field of metadata
15+
316
## v0.0.1 - 2023-09-20
417

518
### Enhancements:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ ESP32_IO_Expander encapsulates various components from the [Espressif Components
2020
| [TCA95xx (8bit)](https://components.espressif.com/components/espressif/esp_io_expander_tca9554) | 1.0.1 |
2121
| [TCA95xx (16bit)](https://components.espressif.com/components/espressif/esp_io_expander_tca95xx_16bit) | 1.0.0 |
2222
| [HT8574](https://components.espressif.com/components/espressif/esp_io_expander_ht8574) | 1.0.0 |
23+
| CH422G | x |
2324

2425
## Dependencies Version
2526

2627
| **Name** | **Version** |
2728
| ----------------------------------------------------------- | ----------- |
28-
| ESP32_IO_Expander | v0.x.x |
2929
| [arduino-esp32](https://github.com/espressif/arduino-esp32) | >= v2.0.9 |
3030

3131
## How to Use

examples/TestFunctions/TestFunctions.ino

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
#include <Arduino.h>
22
#include <ESP_IOExpander_Library.h>
33

4-
#define EXAMPLE_I2C_NUM (0)
5-
#define EXAMPLE_I2C_SDA_PIN (8)
6-
#define EXAMPLE_I2C_SCL_PIN (18)
7-
84
/**
95
* Create an ESP_IOExpander object, Currently supports:
10-
* - TCA95xx (8bit)
11-
* - TCA95xx (16bit)
6+
* - TCA95xx_8bit
7+
* - TCA95xx_16bit
128
* - HT8574
9+
* - CH422G
1310
*/
14-
ESP_IOExpander *expander = new ESP_IOExpander_TCA95xx_8bit(EXAMPLE_I2C_NUM, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
11+
#define EXAMPLE_CHIP_NAME TCA95xx_8bit
12+
#define EXAMPLE_I2C_NUM (0)
13+
#define EXAMPLE_I2C_SDA_PIN (8)
14+
#define EXAMPLE_I2C_SCL_PIN (18)
15+
16+
#define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__)
17+
#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__)
18+
19+
ESP_IOExpander *expander = NULL;
1520

1621
void setup()
1722
{
1823
Serial.begin(115200);
1924
Serial.println("Test begin");
2025

26+
expander = new EXAMPLE_CHIP_CLASS(EXAMPLE_CHIP_NAME,
27+
(i2c_port_t)EXAMPLE_I2C_NUM, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000,
28+
EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
2129
expander->init();
2230
expander->begin();
2331

@@ -68,5 +76,5 @@ void loop()
6876
Serial.print(", ");
6977
Serial.println(level[3]);
7078

71-
sleep(1);
79+
delay(1000);
7280
}

library.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=ESP32_IO_Expander
2-
version=0.0.1
3-
author=Lzw655 <[email protected]>
4-
maintainer=lzw655 <liuzhongwei@espressif.com>
5-
sentence=ESP32_Display_Panel is a library designed for driving IO expander chips using ESP32 SoCs
6-
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574
2+
version=0.0.3
3+
author=lzw655
4+
maintainer=espressif
5+
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP32 SoCs
6+
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G
77
category=Other
88
architectures=esp32
99
url=https://github.com/esp-arduino-libs/ESP32_IO_Expander

src/ESP_IOExpander_Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
#include "chip/TCA95xx_8bit.h"
1313
#include "chip/TCA95xx_16bit.h"
1414
#include "chip/HT8574.h"
15+
#include "chip/CH422G.h"
1516

1617
#endif

src/chip/CH422G.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <inttypes.h>
8+
#include <string.h>
9+
#include <stdlib.h>
10+
11+
#include "driver/i2c.h"
12+
#include "esp_bit_defs.h"
13+
#include "esp_check.h"
14+
#include "esp_log.h"
15+
16+
#include "../private/CheckResult.h"
17+
#include "CH422G.h"
18+
19+
/* Timeout of each I2C communication */
20+
#define I2C_TIMEOUT_MS (10)
21+
22+
#define IO_COUNT (8)
23+
24+
/* Register address */
25+
#define CH422G_REG_IN (0x26)
26+
#define CH422G_REG_OUT (0x38)
27+
28+
/* Default register value on power-up */
29+
#define DIR_REG_DEFAULT_VAL (0xff)
30+
#define OUT_REG_DEFAULT_VAL (0xdf)
31+
32+
/**
33+
* @brief Device Structure Type
34+
*
35+
*/
36+
typedef struct {
37+
esp_io_expander_t base;
38+
i2c_port_t i2c_num;
39+
uint32_t i2c_address;
40+
struct {
41+
uint8_t direction;
42+
uint8_t output;
43+
} regs;
44+
} esp_io_expander_ch422g_t;
45+
46+
static const char *TAG = "ch422g";
47+
48+
static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle);
49+
50+
ESP_IOExpander_CH422G::~ESP_IOExpander_CH422G()
51+
{
52+
if (i2c_need_init) {
53+
i2c_driver_delete(i2c_id);
54+
}
55+
if (handle) {
56+
del();
57+
}
58+
}
59+
60+
void ESP_IOExpander_CH422G::begin(void)
61+
{
62+
CHECK_ERROR_RETURN(esp_io_expander_new_i2c_ch422g(i2c_id, i2c_address, &handle));
63+
}
64+
65+
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
66+
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);
67+
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value);
68+
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value);
69+
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value);
70+
static esp_err_t reset(esp_io_expander_t *handle);
71+
static esp_err_t del(esp_io_expander_t *handle);
72+
73+
static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c_address, esp_io_expander_handle_t *handle)
74+
{
75+
ESP_RETURN_ON_FALSE(i2c_num < I2C_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "Invalid i2c num");
76+
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "Invalid handle");
77+
78+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)calloc(1, sizeof(esp_io_expander_ch422g_t));
79+
ESP_RETURN_ON_FALSE(ch422g, ESP_ERR_NO_MEM, TAG, "Malloc failed");
80+
81+
ch422g->base.config.io_count = IO_COUNT;
82+
ch422g->base.config.flags.dir_out_bit_zero = 1;
83+
ch422g->i2c_num = i2c_num;
84+
ch422g->i2c_address = i2c_address;
85+
ch422g->regs.output = OUT_REG_DEFAULT_VAL;
86+
ch422g->base.read_input_reg = read_input_reg;
87+
ch422g->base.write_output_reg = write_output_reg;
88+
ch422g->base.read_output_reg = read_output_reg;
89+
ch422g->base.write_direction_reg = write_direction_reg;
90+
ch422g->base.read_direction_reg = read_direction_reg;
91+
ch422g->base.del = del;
92+
ch422g->base.reset = reset;
93+
94+
esp_err_t ret = ESP_OK;
95+
/* Reset configuration and register status */
96+
ESP_GOTO_ON_ERROR(reset(&ch422g->base), err, TAG, "Reset failed");
97+
98+
*handle = &ch422g->base;
99+
return ESP_OK;
100+
err:
101+
free(ch422g);
102+
return ret;
103+
}
104+
105+
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
106+
{
107+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
108+
109+
uint8_t temp = 0;
110+
111+
ESP_RETURN_ON_ERROR(
112+
i2c_master_read_from_device(ch422g->i2c_num, ch422g->i2c_address, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
113+
TAG, "Read input reg failed");
114+
115+
// *INDENT-OFF*
116+
ESP_RETURN_ON_ERROR(
117+
i2c_master_read_from_device(ch422g->i2c_num, CH422G_REG_IN, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
118+
TAG, "Read input reg failed");
119+
// *INDENT-ON*
120+
*value = temp;
121+
return ESP_OK;
122+
}
123+
124+
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
125+
{
126+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
127+
value &= 0xff;
128+
129+
uint8_t out_temp = 0x01;
130+
ESP_RETURN_ON_ERROR(
131+
i2c_master_write_to_device(ch422g->i2c_num, ch422g->i2c_address, &out_temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
132+
TAG, "Write output reg failed");
133+
134+
uint8_t data = (uint8_t)value;
135+
ESP_RETURN_ON_ERROR(
136+
i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_OUT, &data, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
137+
TAG, "Write output reg failed");
138+
ch422g->regs.output = value;
139+
return ESP_OK;
140+
}
141+
142+
static esp_err_t read_output_reg(esp_io_expander_handle_t handle, uint32_t *value)
143+
{
144+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
145+
146+
*value = ch422g->regs.output;
147+
return ESP_OK;
148+
}
149+
150+
static esp_err_t write_direction_reg(esp_io_expander_handle_t handle, uint32_t value)
151+
{
152+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
153+
value &= 0xff;
154+
ch422g->regs.direction = value;
155+
return ESP_OK;
156+
}
157+
158+
static esp_err_t read_direction_reg(esp_io_expander_handle_t handle, uint32_t *value)
159+
{
160+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
161+
162+
*value = ch422g->regs.direction;
163+
return ESP_OK;
164+
}
165+
166+
static esp_err_t reset(esp_io_expander_t *handle)
167+
{
168+
ESP_RETURN_ON_ERROR(write_output_reg(handle, OUT_REG_DEFAULT_VAL), TAG, "Write output reg failed");
169+
return ESP_OK;
170+
}
171+
172+
static esp_err_t del(esp_io_expander_t *handle)
173+
{
174+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
175+
176+
free(ch422g);
177+
return ESP_OK;
178+
}

src/chip/CH422G.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
11+
#include "driver/i2c.h"
12+
#include "esp_err.h"
13+
14+
#include "../ESP_IOExpander.h"
15+
16+
class ESP_IOExpander_CH422G: public ESP_IOExpander {
17+
public:
18+
/**
19+
* @brief Constructor to create ESP_IOExpander object
20+
*
21+
* @note After using this function, call `init()` will initialize I2C bus.
22+
*
23+
* @param id I2C port number
24+
* @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`.
25+
* Can be found in the header file of each IO expander.h.
26+
* @param config Pointer to I2C bus configuration
27+
*/
28+
ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, const i2c_config_t *config): ESP_IOExpander(id, address, config) { };
29+
30+
/**
31+
* @brief Constructor to create ESP_IOExpander object
32+
*
33+
* @note After using this function, call `init()` will initialize I2C bus.
34+
*
35+
* @param id I2C port number
36+
* @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`.
37+
* Can be found in the header file of each IO expander.h.
38+
* @param scl SCL pin number
39+
* @param sda SDA pin number
40+
*/
41+
ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address, int scl, int sda): ESP_IOExpander(id, address, scl, sda) { };
42+
43+
/**
44+
* @brief Constructor to create ESP_IOExpander object
45+
*
46+
* @note If use this function, should initialize I2C bus before call `init()`.
47+
*
48+
* @param id I2C port number
49+
* @param address I2C device address. Should be like `ESP_IO_EXPANDER_I2C_*`.
50+
* Can be found in the header file of each IO expander.h.
51+
*/
52+
ESP_IOExpander_CH422G(i2c_port_t id, uint8_t address): ESP_IOExpander(id, address) { };
53+
54+
/**
55+
* @brief Destructor
56+
*
57+
* @note This function will delete I2C driver if it is initialized by ESP_IOExpander and delete ESP_IOExpander object.
58+
*/
59+
~ESP_IOExpander_CH422G() override;
60+
61+
/**
62+
* @brief Begin IO expander
63+
*
64+
*/
65+
void begin(void) override;
66+
};
67+
68+
/**
69+
* @brief I2C address of the ch422g
70+
*
71+
* And the 7-bit slave address is the most important data for users.
72+
* For example, if a chip's A0,A1,A2 are connected to GND, it's 7-bit slave address is 1001000b(0x48).
73+
* Then users can use `ESP_IO_EXPANDER_I2C_CH422G_ADDRESS_000` to init it.
74+
*/
75+
#define ESP_IO_EXPANDER_I2C_CH422G_ADDRESS_000 (0x24)

0 commit comments

Comments
 (0)