Skip to content

feat(ch422g): support enter/exit sleep #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog

## v1.1.1 - 2025-02-19

### Enhancements:

* feat(ch422g): support enter/exit sleep

## v1.1.0 - 2025-02-07

### Enhancements:
Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "1.1.0"
version: "1.1.1"
description: ESP32_IO_Expander is a library designed for driving IO expander chips using ESP SoCs
url: https://github.com/esp-arduino-libs/ESP32_IO_Expander
repository: https://github.com/esp-arduino-libs/ESP32_IO_Expander.git
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32_IO_Expander
version=1.1.0
version=1.1.1
author=espressif
maintainer=espressif
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP SoCs
Expand Down
30 changes: 30 additions & 0 deletions src/chip/esp_expander_ch422g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,34 @@ bool CH422G::enableAllIO_Output(void)
return true;
}

bool CH422G::enterSleep(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_CHECK_ERROR_RETURN(
esp_io_expander_ch422g_enter_sleep(device_handle), false, "Enter sleep failed"
);

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();

return true;
}

bool CH422G::exitSleep(void)
{
ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS();

ESP_UTILS_CHECK_FALSE_RETURN(isOverState(State::BEGIN), false, "Not begun");

ESP_UTILS_CHECK_ERROR_RETURN(
esp_io_expander_ch422g_exit_sleep(device_handle), false, "Exit sleep failed"
);

ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS();

return true;
}

} // namespace esp_expander
14 changes: 14 additions & 0 deletions src/chip/esp_expander_ch422g.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ class CH422G: public Base {
* @return true if success, otherwise false
*/
bool enableAllIO_Output(void);

/**
* @brief Enter sleep mode
*
* @return true if success, otherwise false
*/
bool enterSleep(void);

/**
* @brief Exit sleep mode
*
* @return true if success, otherwise false
*/
bool exitSleep(void);
};

} // namespace esp_expander
Expand Down
43 changes: 38 additions & 5 deletions src/port/esp_io_expander_ch422g.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
// 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_WR_OC_DEFAULT_VAL (0x0FU)
#define REG_WR_IO_DEFAULT_VAL (0xFFU)
#define REG_OUT_DEFAULT_VAL ((REG_WR_OC_DEFAULT_VAL << 8) | REG_WR_IO_DEFAULT_VAL)
#define REG_DIR_DEFAULT_VAL (0xFFFUL)
#define REG_DIR_DEFAULT_VAL (0xFFFU)

#define REG_WR_SET_BIT_IO_OE (1 << 0)
#define REG_WR_SET_BIT_OD_EN (1 << 2)
#define REG_WR_SET_BIT_IO_OE (1U << 0)
#define REG_WR_SET_BIT_OD_EN (1U << 2)
#define REG_WR_SET_BIT_SLEEP (1U << 3)

/**
* @brief Device Structure Type
Expand Down Expand Up @@ -171,6 +172,38 @@ esp_err_t esp_io_expander_ch422g_set_all_output(esp_io_expander_handle_t handle)
return ESP_OK;
}

esp_err_t esp_io_expander_ch422g_enter_sleep(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_SLEEP);

// 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_exit_sleep(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_SLEEP);

// 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);
Expand Down
4 changes: 4 additions & 0 deletions src/port/esp_io_expander_ch422g.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ 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);

esp_err_t esp_io_expander_ch422g_enter_sleep(esp_io_expander_handle_t handle);

esp_err_t esp_io_expander_ch422g_exit_sleep(esp_io_expander_handle_t handle);

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion src/port/esp_io_expander_ht8574.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct {
} regs;
} esp_io_expander_ht8574_t;

static char *TAG = "ht8574";
static const 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);
Expand Down
2 changes: 1 addition & 1 deletion src/port/esp_io_expander_tca9554.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct {
} regs;
} esp_io_expander_tca9554_t;

static char *TAG = "tca9554";
static const 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);
Expand Down
2 changes: 1 addition & 1 deletion src/port/esp_io_expander_tca95xx_16bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct {
} regs;
} esp_io_expander_tca95xx_16bit_t;

static char *TAG = "tca95xx_16";
static const 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);
Expand Down
Loading