Skip to content

Commit

Permalink
Update esp-idf example
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Jan 9, 2024
1 parent 5253b15 commit af62c5b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
12 changes: 12 additions & 0 deletions examples/ESP_IDF_Example/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ menu "XPowersLib Configuration"
bool "Xpowers AXP192"
endchoice

choice I2C_COMMUNICATION_METHOD
prompt "XPowersLIb read and write methods"
default I2C_COMMUNICATION_METHOD_BUILTIN_RW
help
Define XPowersLIb read and write methods

config I2C_COMMUNICATION_METHOD_BUILTIN_RW
bool "Implemented using built-in read and write methods"
config I2C_COMMUNICATION_METHOD_CALLBACK_RW
bool "Implemented using read and write callback methods"
endchoice

config I2C_MASTER_PORT_NUM
int "PMU I2C Port Number"
default 1
Expand Down
5 changes: 4 additions & 1 deletion examples/ESP_IDF_Example/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ extern "C" void app_main(void)
// Register PMU interrupt pins
irq_init();

#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
ESP_ERROR_CHECK(i2c_init());

ESP_LOGI(TAG, "I2C initialized successfully");
#endif

ESP_ERROR_CHECK(pmu_init());

xTaskCreate(pmu_hander_task, "App/pwr", 4 * 1024, NULL, 10, NULL);

ESP_LOGI(TAG, "Run...");

}


Expand Down
15 changes: 15 additions & 0 deletions examples/ESP_IDF_Example/main/port_axp192.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ extern int pmu_register_write_byte(uint8_t devAddr, uint8_t regAddr, uint8_t *da

esp_err_t pmu_init()
{
//* Implemented using read and write callback methods, applicable to other platforms
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
ESP_LOGI(TAG, "Implemented using read and write callback methods");
if (PMU.begin(AXP192_SLAVE_ADDRESS, pmu_register_read, pmu_register_write_byte)) {
ESP_LOGI(TAG, "Init PMU SUCCESS!");
} else {
ESP_LOGE(TAG, "Init PMU FAILED!");
return ESP_FAIL;
}
#endif

//* Use the built-in esp-idf communication method
#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
ESP_LOGI(TAG, "Implemented using built-in read and write methods");
if (PMU.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, AXP192_SLAVE_ADDRESS, CONFIG_PMU_I2C_SDA, CONFIG_PMU_I2C_SCL)) {
ESP_LOGI(TAG, "Init PMU SUCCESS!");
} else {
ESP_LOGE(TAG, "Init PMU FAILED!");
return false;
}
#endif

ESP_LOGI(TAG, "getID:0x%x", PMU.getChipID());

Expand Down
19 changes: 17 additions & 2 deletions examples/ESP_IDF_Example/main/port_axp2101.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_err.h"
#include "driver/i2c.h"

#ifdef CONFIG_XPOWERS_CHIP_AXP2101

Expand All @@ -18,13 +19,27 @@ extern int pmu_register_write_byte(uint8_t devAddr, uint8_t regAddr, uint8_t *da

esp_err_t pmu_init()
{
//* Implemented using read and write callback methods, applicable to other platforms
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
ESP_LOGI(TAG, "Implemented using read and write callback methods");
if (PMU.begin(AXP2101_SLAVE_ADDRESS, pmu_register_read, pmu_register_write_byte)) {
ESP_LOGI(TAG, "Init PMU SUCCESS!");
} else {
ESP_LOGE(TAG, "Init PMU FAILED!");
return ESP_FAIL;
}
#endif

//* Use the built-in esp-idf communication method
#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
ESP_LOGI(TAG, "Implemented using built-in read and write methods");
if (PMU.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, AXP2101_SLAVE_ADDRESS, CONFIG_PMU_I2C_SDA, CONFIG_PMU_I2C_SCL)) {
ESP_LOGI(TAG, "Init PMU SUCCESS!");
} else {
ESP_LOGE(TAG, "Init PMU FAILED!");
return false;
}
#endif
//Turn off not use power channel
PMU.disableDC2();
PMU.disableDC3();
Expand Down Expand Up @@ -134,9 +149,9 @@ esp_err_t pmu_init()
// Set the watchdog trigger event type
// PMU.setWatchdogConfig(XPOWERS_AXP2101_WDT_IRQ_TO_PIN);
// Set watchdog timeout
// PMU.setWatchdogTimeout(XPOWERS_AXP2101_WDT_TIMEOUT_4S);
PMU.setWatchdogTimeout(XPOWERS_AXP2101_WDT_TIMEOUT_4S);
// Enable watchdog to trigger interrupt event
// PMU.enableWatchdog();
PMU.enableWatchdog();
return ESP_OK;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/ESP_IDF_Example/main/port_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

static const char *TAG = "XPowersLib";


#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
#define I2C_MASTER_NUM (i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM
#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY /*!< I2C master clock frequency */
#define I2C_MASTER_SDA_IO (gpio_num_t)CONFIG_PMU_I2C_SDA
Expand Down Expand Up @@ -106,4 +106,5 @@ esp_err_t i2c_init(void)
i2c_conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(I2C_MASTER_NUM, &i2c_conf);
return i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
}
#endif

0 comments on commit af62c5b

Please sign in to comment.