Skip to content

Commit 420c9b6

Browse files
committed
feat(ch422g): support enter/exit sleep
1 parent ecddae2 commit 420c9b6

File tree

5 files changed

+96
-5
lines changed

5 files changed

+96
-5
lines changed

CHANGELOG.md

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

3+
## v1.1.1 - 2025-07-07
4+
5+
### Enhancements:
6+
7+
* feat(ch422g): support enter/exit sleep
8+
9+
### Bug Fixes:
10+
11+
* fix(port): fix discarded qualifiers warning
12+
313
## v1.1.0 - 2025-02-07
414

515
### Enhancements:

src/chip/esp_expander_ch422g.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,34 @@ 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+
}
136+
107137
} // 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

0 commit comments

Comments
 (0)