Skip to content

Commit

Permalink
WIP: npm1300
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Nov 12, 2023
1 parent 8794193 commit ee5fccb
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"${workspaceFolder}\\app"
],
"nrf-connect.debugging.bindings": {
"${workspaceFolder}/app/build": "Launch build"
"${workspaceFolder}/app/build": "Launch build",
"${workspaceFolder}/app/build_dk": "Launch build_dk",
"${workspaceFolder}/app/build_debug": "Launch build_debug"
},
}
84 changes: 84 additions & 0 deletions app/boards/npm1300_ek.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2023 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/

#include <dt-bindings/regulator/npm1300.h>
#include <zephyr/dt-bindings/input/input-event-codes.h>

&i2c3 {
status = "okay";
npm1300_ek_pmic: pmic@6b {
compatible = "nordic,npm1300";
reg = <0x6b>;

npm1300_ek_gpio: gpio-controller {
compatible = "nordic,npm1300-gpio";
gpio-controller;
#gpio-cells = <2>;
ngpios = <5>;
};

npm1300_ek_regulators: regulators {
compatible = "nordic,npm1300-regulator";

/* limits are set to min/max allowed values */
npm1300_ek_buck1: BUCK1 {
regulator-min-microvolt = <1800000>;
regulator-max-microvolt = <3300000>;
};

npm1300_ek_buck2: BUCK2 {
regulator-min-microvolt = <1000000>;
regulator-max-microvolt = <3300000>;
regulator-init-microvolt = <3300000>;
retention-microvolt = <2500000>;
enable-gpios = <&npm1300_ek_gpio 1 GPIO_ACTIVE_LOW>;
retention-gpios = <&npm1300_ek_gpio 2 GPIO_ACTIVE_HIGH>;
pwm-gpios = <&npm1300_ek_gpio 2 GPIO_ACTIVE_LOW>;
};

npm1300_ek_ldo1: LDO1 {
regulator-min-microvolt = <1000000>;
regulator-max-microvolt = <3300000>;
regulator-initial-mode = <NPM1300_LDSW_MODE_LDO>;
enable-gpios = <&npm1300_ek_gpio 2 GPIO_ACTIVE_LOW>;
};

npm1300_ek_ldo2: LDO2 {
regulator-min-microvolt = <1000000>;
regulator-max-microvolt = <3300000>;
regulator-initial-mode = <NPM1300_LDSW_MODE_LDSW>;
enable-gpios = <&npm1300_ek_gpio 2 GPIO_ACTIVE_LOW>;
};
};

npm1300_ek_charger: charger {
compatible = "nordic,npm1300-charger";
term-microvolt = <4150000>;
term-warm-microvolt = <4000000>;
current-microamp = <150000>;
dischg-limit-microamp = <1000000>;
vbus-limit-microamp = <500000>;
thermistor-ohms = <10000>;
thermistor-beta = <3380>;
charging-enable;
};

npm1300_ek_buttons: buttons {
compatible = "gpio-keys";
pmic_button0: pmic_button_0 {
gpios = < &npm1300_ek_gpio 0 GPIO_ACTIVE_HIGH>;
label = "Pmic button switch 0";
zephyr,code = <INPUT_KEY_0>;
};
};

npm1300_ek_leds: leds {
compatible = "nordic,npm1300-led";
nordic,led0-mode = "error";
nordic,led1-mode = "charging";
nordic,led2-mode = "host";
};
};
};
32 changes: 26 additions & 6 deletions app/boards/nrf5340dk_nrf5340_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

&pinctrl {
// Set to I2C pins that the sensors are connected to
i2c1_default: i2c1_default {
i2c3_default: i2c3_default {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
<NRF_PSEL(TWIM_SCL, 1, 3)>;
psels = <NRF_PSEL(TWIM_SDA, 1, 5)>,
<NRF_PSEL(TWIM_SCL, 1, 4)>;
};
};

i2c1_sleep: i2c1_sleep {
i2c3_sleep: i2c3_sleep {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
<NRF_PSEL(TWIM_SCL, 1, 3)>;
psels = <NRF_PSEL(TWIM_SDA, 1, 5)>,
<NRF_PSEL(TWIM_SCL, 1, 4)>;
low-power-enable;
};
};
Expand Down Expand Up @@ -122,6 +122,26 @@
status = "okay";
};

&i2c3_default {
status = "okay";
};

&i2c3_default {
group1 {
bias-pull-up;
};
};

&i2c3 {
compatible = "nordic,nrf-twim";
status = "okay";
clock-frequency = <I2C_BITRATE_FAST>;
zephyr,concat-buf-size = <512>;
pinctrl-0 = <&i2c3_default>;
pinctrl-1 = <&i2c3_sleep>;
pinctrl-names = "default", "sleep";
};

&i2c1 {
compatible = "nordic,nrf-twim";
status = "okay";
Expand Down
2 changes: 2 additions & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ CONFIG_GC9A01=y
CONFIG_INPUT=y
CONFIG_REGULATOR=y
CONFIG_REGULATOR_FIXED=y
CONFIG_SHELL=y
CONFIG_REGULATOR_SHELL=y

# LVGL configuration
CONFIG_LVGL=y
Expand Down
87 changes: 86 additions & 1 deletion app/src/drivers/zsw_display_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,72 @@
#include <zephyr/logging/log.h>
#include "lvgl.h"

#include <zephyr/device.h>
#include <zephyr/drivers/sensor/npm1300_charger.h>
#include <zephyr/drivers/led.h>
#include <zephyr/dt-bindings/regulator/npm1300.h>
#include <zephyr/drivers/mfd/npm1300.h>
#include <getopt.h>

LOG_MODULE_REGISTER(display_control, LOG_LEVEL_WRN);

static void lvgl_render(struct k_work *item);

#define SLEEP_TIME_MS 100
#define UPDATE_TIME_MS 2000

static const struct device *regulators = DEVICE_DT_GET(DT_NODELABEL(npm1300_ek_buck1));

static const struct device *charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_ek_charger));

static const struct device *pmic = DEVICE_DT_GET(DT_NODELABEL(npm1300_ek_pmic));

static void event_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
printk("Event detected\n");
}

void configure_events(void)
{
if (!device_is_ready(pmic)) {
printk("Pmic device not ready.\n");
return;
}

/* Setup callback for shiphold button press */
static struct gpio_callback event_cb;

gpio_init_callback(&event_cb, event_callback, BIT(NPM1300_EVENT_SHIPHOLD_PRESS));

mfd_npm1300_add_callback(pmic, &event_cb);
}


void read_sensors(void)
{
struct sensor_value volt;
struct sensor_value current;
struct sensor_value temp;
struct sensor_value error;
struct sensor_value status;

sensor_sample_fetch(charger);
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_VOLTAGE, &volt);
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_AVG_CURRENT, &current);
sensor_channel_get(charger, SENSOR_CHAN_GAUGE_TEMP, &temp);
sensor_channel_get(charger, SENSOR_CHAN_NPM1300_CHARGER_STATUS, &status);
sensor_channel_get(charger, SENSOR_CHAN_NPM1300_CHARGER_ERROR, &error);

printk("V: %d.%03d ", volt.val1, volt.val2 / 1000);

printk("I: %s%d.%04d ", ((current.val1 < 0) || (current.val2 < 0)) ? "-" : "",
abs(current.val1), abs(current.val2) / 100);

printk("T: %d.%02d\n", temp.val1, temp.val2 / 10000);

printk("Charger Status: %d, Error: %d\n", status.val1, error.val1);
}

typedef enum display_state {
DISPLAY_STATE_AWAKE,
DISPLAY_STATE_SLEEPING,
Expand Down Expand Up @@ -66,6 +128,28 @@ void zsw_display_control_init(void)
}

display_state = DISPLAY_STATE_SLEEPING;

configure_events();

if (!device_is_ready(regulators)) {
LOG_ERR("Error: Regulator device is not ready\n");
return;
}

if(regulator_is_enabled(regulators) == false)
{
LOG_ERR("Regulator not enabled!");
//return -ENODEV;
}
int32_t Voltage;
regulator_get_voltage(regulators, &Voltage);
LOG_WRN("Voltage: %i", Voltage);

if (!device_is_ready(charger)) {
LOG_ERR("Charger device not ready.\n");
//return;
}
read_sensors();
}

int zsw_display_control_sleep_ctrl(bool on)
Expand Down Expand Up @@ -229,5 +313,6 @@ static void lvgl_render(struct k_work *item)
zsw_display_control_set_brightness(last_brightness);
first_render_since_poweron = false;
}
k_work_schedule(&lvgl_work, K_MSEC(next_update_in_ms));
k_work_schedule(&lvgl_work, K_MSEC(500));
read_sensors();
}
8 changes: 4 additions & 4 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ static void run_init_work(struct k_work *item)

load_retention_ram();
zsw_notification_manager_init();
enable_bluetoth();
zsw_imu_init();
zsw_magnetometer_init();
zsw_pressure_sensor_init();
//enable_bluetoth();
//zsw_imu_init();
//zsw_magnetometer_init();
//zsw_pressure_sensor_init();
zsw_clock_init(retained.current_time_seconds);
// Not to self, PWM consumes like 250uA...
// Need to disable also when screen is off.
Expand Down

0 comments on commit ee5fccb

Please sign in to comment.