Skip to content

Commit 4586b12

Browse files
committed
modules: power: disable/enable UART on VBUS events
Disable/enable UART on VBUS events Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 0742f94 commit 4586b12

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

app/prj.conf

+1
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,4 @@ CONFIG_TASK_WDT_MIN_TIMEOUT=10000
210210

211211
# Device power management
212212
CONFIG_PM_DEVICE=y
213+
CONFIG_PM_DEVICE_SHELL=y

app/src/modules/power/Kconfig.power

+6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ menu "Power"
88

99
config APP_POWER
1010
bool "Power module"
11+
depends on NRF_FUEL_GAUGE
12+
depends on NPM1300_CHARGER
1113

1214
if APP_POWER
1315

16+
config APP_POWER_DISABLE_UART_ON_VBUS_REMOVED
17+
bool "Disable UART when VBUS is removed"
18+
default y
19+
1420
config APP_POWER_THREAD_STACK_SIZE
1521
int "Thread stack size"
1622
default 1536

app/src/modules/power/power.c

+95-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include <zephyr/kernel.h>
88
#include <zephyr/logging/log.h>
9+
#include <zephyr/device.h>
10+
#include <zephyr/devicetree.h>
11+
#include <zephyr/pm/device.h>
912
#include <zephyr/zbus/zbus.h>
1013
#include <zephyr/drivers/sensor/npm1300_charger.h>
1114
#include <zephyr/drivers/mfd/npm1300.h>
@@ -56,6 +59,9 @@ BUILD_ASSERT(CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS >
5659
static const struct device *charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_charger));
5760
static const struct device *pmic = DEVICE_DT_GET(DT_NODELABEL(pmic_main));
5861

62+
static const struct device *const uart0_dev = DEVICE_DT_GET(DT_NODELABEL(uart0));
63+
static const struct device *const uart1_dev = DEVICE_DT_GET(DT_NODELABEL(uart1));
64+
5965
/* Forward declarations */
6066
static int subscribe_to_vsbus_events(const struct device *pmic, struct gpio_callback *event_cb);
6167
static int charger_read_sensors(float *voltage, float *current, float *temp, int32_t *chg_status);
@@ -116,11 +122,13 @@ static void state_running_entry(void *o)
116122
return;
117123
}
118124

119-
err = subscribe_to_vsbus_events(pmic, &event_cb);
120-
if (err) {
121-
LOG_ERR("subscribe_to_vsbus_events, error: %d", err);
122-
SEND_FATAL_ERROR();
123-
return;
125+
if (IS_ENABLED(CONFIG_APP_POWER_DISABLE_UART_ON_VBUS_REMOVED)) {
126+
err = subscribe_to_vsbus_events(pmic, &event_cb);
127+
if (err) {
128+
LOG_ERR("subscribe_to_vsbus_events, error: %d", err);
129+
SEND_FATAL_ERROR();
130+
return;
131+
}
124132
}
125133

126134
err = charger_read_sensors(&parameters.v0, &parameters.i0, &parameters.t0, &chg_status);
@@ -163,17 +171,97 @@ static void state_running_run(void *o)
163171

164172
/* End of state handling */
165173

174+
static int uart_disable(void)
175+
{
176+
int err;
177+
178+
if (!device_is_ready(uart0_dev) || !device_is_ready(uart1_dev)) {
179+
LOG_ERR("UART devices are not ready");
180+
return -ENODEV;
181+
}
182+
183+
#ifdef CONFIG_NRF_MODEM_LIB_TRACE_BACKEND_UART
184+
err = nrf_modem_lib_trace_level_set(NRF_MODEM_LIB_TRACE_LEVEL_OFF);
185+
if (err) {
186+
LOG_ERR("nrf_modem_lib_trace_level_set, error: %d", err);
187+
return err;
188+
}
189+
#endif
190+
191+
/* Wait for UART buffers to be emptied before suspending.
192+
* If a transfer is ongoing, the driver will cause an assertion to fail.
193+
* 100 ms is an arbitrary value that should be enough for the buffers to empty.
194+
*/
195+
k_busy_wait(100 * USEC_PER_MSEC);
196+
197+
err = pm_device_action_run(uart1_dev, PM_DEVICE_ACTION_SUSPEND);
198+
if (err) {
199+
LOG_ERR("pm_device_action_run, error: %d", err);
200+
return err;
201+
}
202+
203+
err = pm_device_action_run(uart0_dev, PM_DEVICE_ACTION_SUSPEND);
204+
if (err) {
205+
LOG_ERR("pm_device_actiöon_run, error: %d", err);
206+
return err;
207+
}
208+
209+
LOG_DBG("UART devices disabled");
210+
return 0;
211+
}
212+
213+
static int uart_enable(void)
214+
{
215+
int err;
216+
217+
if (!device_is_ready(uart0_dev) || !device_is_ready(uart1_dev)) {
218+
LOG_ERR("UART devices are not ready");
219+
return -ENODEV;
220+
}
221+
222+
err = pm_device_action_run(uart0_dev, PM_DEVICE_ACTION_RESUME);
223+
if (err) {
224+
LOG_ERR("pm_device_action_run, error: %d", err);
225+
return err;
226+
}
227+
228+
err = pm_device_action_run(uart1_dev, PM_DEVICE_ACTION_RESUME);
229+
if (err) {
230+
LOG_ERR("pm_device_action_run, error: %d", err);
231+
return err;
232+
}
233+
234+
LOG_DBG("UART devices enabled");
235+
return 0;
236+
}
237+
166238
static void event_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
167239
{
168240
ARG_UNUSED(dev);
169241
ARG_UNUSED(cb);
170242

243+
int err;
244+
171245
if (pins & BIT(NPM1300_EVENT_VBUS_DETECTED)) {
172-
LOG_WRN("Vbus detected");
246+
LOG_DBG("VBUS detected");
247+
248+
err = uart_enable();
249+
if (err) {
250+
LOG_ERR("uart_enable, error: %d", err);
251+
SEND_FATAL_ERROR();
252+
return;
253+
}
173254
}
174255

175256
if (pins & BIT(NPM1300_EVENT_VBUS_REMOVED)) {
176-
LOG_WRN("Vbus removed");
257+
LOG_DBG("VBUS removed");
258+
259+
err = uart_disable();
260+
if (err) {
261+
LOG_ERR("uart_disable, error: %d", err);
262+
SEND_FATAL_ERROR();
263+
return;
264+
}
177265
}
178266
}
179267

0 commit comments

Comments
 (0)