Skip to content

Commit 096cf83

Browse files
committed
feat(ch422g): support enter/exit sleep
1 parent e79a638 commit 096cf83

10 files changed

+96
-10
lines changed

CHANGELOG.md

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

3+
## v1.1.1 - 2025-02-19
4+
5+
### Enhancements:
6+
7+
* feat(ch422g): support enter/exit sleep
8+
39
## v1.1.0 - 2025-02-07
410

511
### Enhancements:

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.1.0"
1+
version: "1.1.1"
22
description: ESP32_IO_Expander is a library designed for driving IO expander chips using ESP SoCs
33
url: https://github.com/esp-arduino-libs/ESP32_IO_Expander
44
repository: https://github.com/esp-arduino-libs/ESP32_IO_Expander.git

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32_IO_Expander
2-
version=1.1.0
2+
version=1.1.1
33
author=espressif
44
maintainer=espressif
55
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP SoCs

src/chip/esp_expander_ch422g.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,33 @@ bool CH422G::enableAllIO_Output(void)
104104
return true;
105105
}
106106

107+
bool CH422G::enterSleep(void)
108+
{
109+
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
110+
111+
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");
112+
113+
ESP_UTILS_CHECK_ERROR_RETURN(
114+
esp_io_expander_ch422g_enter_sleep(device_handle), false, "Enter sleep failed"
115+
);
116+
117+
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
118+
119+
return true;
120+
}
121+
122+
bool CH422G::exitSleep(void)
123+
{
124+
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();
125+
126+
ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");
127+
128+
ESP_UTILS_CHECK_ERROR_RETURN(
129+
esp_io_expander_ch422g_exit_sleep(device_handle), false, "Exit sleep failed"
130+
);
131+
132+
ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();
133+
134+
return true;
135+
107136
} // namespace esp_expander

src/chip/esp_expander_ch422g.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ class CH422G: public Base {
9999
* @return true if success, otherwise false
100100
*/
101101
bool enableAllIO_Output(void);
102+
103+
/**
104+
* @brief Enter sleep mode
105+
*
106+
* @return true if success, otherwise false
107+
*/
108+
bool enterSleep(void);
109+
110+
/**
111+
* @brief Exit sleep mode
112+
*
113+
* @return true if success, otherwise false
114+
*/
115+
bool exitSleep(void);
102116
};
103117

104118
} // namespace esp_expander

src/port/esp_io_expander_ch422g.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
// Default: | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
3939

4040
// *INDENT-OFF*
41-
#define REG_WR_OC_DEFAULT_VAL (0x0FUL)
42-
#define REG_WR_IO_DEFAULT_VAL (0xFFUL)
41+
#define REG_WR_OC_DEFAULT_VAL (0x0FU)
42+
#define REG_WR_IO_DEFAULT_VAL (0xFFU)
4343
#define REG_OUT_DEFAULT_VAL ((REG_WR_OC_DEFAULT_VAL << 8) | REG_WR_IO_DEFAULT_VAL)
44-
#define REG_DIR_DEFAULT_VAL (0xFFFUL)
44+
#define REG_DIR_DEFAULT_VAL (0xFFFU)
4545

46-
#define REG_WR_SET_BIT_IO_OE (1 << 0)
47-
#define REG_WR_SET_BIT_OD_EN (1 << 2)
46+
#define REG_WR_SET_BIT_IO_OE (1U << 0)
47+
#define REG_WR_SET_BIT_OD_EN (1U << 2)
48+
#define REG_WR_SET_BIT_SLEEP (1U << 3)
4849

4950
/**
5051
* @brief Device Structure Type
@@ -171,6 +172,38 @@ esp_err_t esp_io_expander_ch422g_set_all_output(esp_io_expander_handle_t handle)
171172
return ESP_OK;
172173
}
173174

175+
esp_err_t esp_io_expander_ch422g_enter_sleep(esp_io_expander_handle_t handle)
176+
{
177+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
178+
uint8_t data = (uint8_t)(ch422g->regs.wr_set | REG_WR_SET_BIT_SLEEP);
179+
180+
// WR-SET
181+
ESP_RETURN_ON_ERROR(
182+
i2c_master_write_to_device(
183+
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
184+
), TAG, "Write WR_SET reg failed"
185+
);
186+
ch422g->regs.wr_set = data;
187+
188+
return ESP_OK;
189+
}
190+
191+
esp_err_t esp_io_expander_ch422g_exit_sleep(esp_io_expander_handle_t handle)
192+
{
193+
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
194+
uint8_t data = (uint8_t)(ch422g->regs.wr_set & ~REG_WR_SET_BIT_SLEEP);
195+
196+
// WR-SET
197+
ESP_RETURN_ON_ERROR(
198+
i2c_master_write_to_device(
199+
ch422g->i2c_num, CH422G_REG_WR_SET, &data, sizeof(data), pdMS_TO_TICKS(I2C_TIMEOUT_MS)
200+
), TAG, "Write WR_SET reg failed"
201+
);
202+
ch422g->regs.wr_set = data;
203+
204+
return ESP_OK;
205+
}
206+
174207
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
175208
{
176209
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);

src/port/esp_io_expander_ch422g.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ esp_err_t esp_io_expander_ch422g_set_all_input(esp_io_expander_handle_t handle);
4848

4949
esp_err_t esp_io_expander_ch422g_set_all_output(esp_io_expander_handle_t handle);
5050

51+
esp_err_t esp_io_expander_ch422g_enter_sleep(esp_io_expander_handle_t handle);
52+
53+
esp_err_t esp_io_expander_ch422g_exit_sleep(esp_io_expander_handle_t handle);
54+
5155
#ifdef __cplusplus
5256
}
5357
#endif

src/port/esp_io_expander_ht8574.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef struct {
4040
} regs;
4141
} esp_io_expander_ht8574_t;
4242

43-
static char *TAG = "ht8574";
43+
static const char *TAG = "ht8574";
4444

4545
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
4646
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);

src/port/esp_io_expander_tca9554.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct {
4545
} regs;
4646
} esp_io_expander_tca9554_t;
4747

48-
static char *TAG = "tca9554";
48+
static const char *TAG = "tca9554";
4949

5050
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
5151
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);

src/port/esp_io_expander_tca95xx_16bit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct {
4545
} regs;
4646
} esp_io_expander_tca95xx_16bit_t;
4747

48-
static char *TAG = "tca95xx_16";
48+
static const char *TAG = "tca95xx_16";
4949

5050
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value);
5151
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value);

0 commit comments

Comments
 (0)