1:以下是适用于 nRF Connect SDK (NCS) 的基于 Zephyr 的示例应用程序,展示了:
- 读取电池电压和状态
- 处理来自 nPM1300 的中断(例如,电池或电源轨事件)
- 控制电源轨(通过 GPIO 启用/禁用)
main.c
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/pmic.h>
#include <zephyr/drivers/pmic/npm1300.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/printk.h>
LOG_MODULE_REGISTER(npm1300_sample, LOG_LEVEL_INF);
#define NPM1300_NODE DT_NODELABEL(npm1300)
#define NPM1300_IRQ_NODE DT_NODELABEL(npm1300_irq)
#define NPM1300_PWR_GPIO_NODE DT_NODELABEL(npm1300_pwrgpio)
static const struct device *npm1300_dev = DEVICE_DT_GET(NPM1300_NODE);
static struct gpio_callback irq_cb_data;
void npm1300_irq_handler(const struct device *gpiodev, struct gpio_callback *cb, uint32_t pins)
{
printk("nPM1300 IRQ triggered!\n");
struct pmic_npm1300_irq_status irq_status;
int ret = pmic_npm1300_irq_status_get(npm1300_dev, &irq_status);
if (ret == 0) {
if (irq_status.batt_chg) {
LOG_INF("Battery charging event detected");
}
if (irq_status.vbus_det) {
LOG_INF("VBUS detected event");
}
if (irq_status.temp) {
LOG_INF("Temperature warning/fault event");
}
// Add more handlers as needed.
} else {
LOG_ERR("Failed to read nPM1300 IRQ status: %d", ret);
}
}
void main(void)
{
if (!device_is_ready(npm1300_dev)) {
LOG_ERR("nPM1300 device not ready");
return;
}
LOG_INF("nPM1300 demo starting...");
// Set up IRQ handling
const struct device *irq_gpio_dev = DEVICE_DT_GET(NPM1300_IRQ_NODE);
gpio_pin_configure(irq_gpio_dev, DT_GPIO_PIN(NPM1300_IRQ_NODE, gpios), GPIO_INPUT | DT_GPIO_FLAGS(NPM1300_IRQ_NODE, gpios));
gpio_init_callback(&irq_cb_data, npm1300_irq_handler, BIT(DT_GPIO_PIN(NPM1300_IRQ_NODE, gpios)));
gpio_add_callback(irq_gpio_dev, &irq_cb_data);
gpio_pin_interrupt_configure(irq_gpio_dev, DT_GPIO_PIN(NPM1300_IRQ_NODE, gpios), GPIO_INT_EDGE_TO_ACTIVE);
LOG_INF("nPM1300 IRQ configured");
// Battery monitoring loop
while (1) {
int batt_mv;
int status = pmic_npm1300_battery_voltage_get(npm1300_dev, &batt_mv);
if (status == 0) {
LOG_INF("Battery voltage: %d mV", batt_mv);
} else {
LOG_ERR("Failed to read battery voltage: %d", status);
}
// Power rail control (example: enable/disable)
int pwr_status;
status = pmic_npm1300_power_rail_enable(npm1300_dev, 0, true); // enable rail 0
if (status == 0) {
LOG_INF("Power rail 0 enabled");
}
k_sleep(K_MSEC(1000));
status = pmic_npm1300_power_rail_enable(npm1300_dev, 0, false); // disable rail 0
if (status == 0) {
LOG_INF("Power rail 0 disabled");
}
k_sleep(K_SECONDS(10));
}
}
prj.conf
CONFIG_PMIC_NPM1300=y
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_GPIO=y
nrf54l15dk_nrf54l15.overlay
&i2c1 {
npm1300: npm1300@6b {
compatible = "nordic,npm1300";
reg = <0x6b>;
// IRQ and other properties as needed
};
};
/ {
npm1300_irq: gpio0_pin12 {
gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
};
};
使用方法:
- 将文件放置在您的 NCS 项目中(例如,在
src/
、boards/
和根目录下)。 - Build for your board: 为您的板卡构建:
west build -b nrf54l15dk_nrf54l15
west flash