|
6 | 6 |
|
7 | 7 | #include <zephyr/kernel.h>
|
8 | 8 | #include <zephyr/logging/log.h>
|
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/pm/device.h> |
9 | 12 | #include <zephyr/zbus/zbus.h>
|
10 | 13 | #include <zephyr/drivers/sensor/npm1300_charger.h>
|
11 | 14 | #include <zephyr/drivers/mfd/npm1300.h>
|
@@ -56,6 +59,9 @@ BUILD_ASSERT(CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS >
|
56 | 59 | static const struct device *charger = DEVICE_DT_GET(DT_NODELABEL(npm1300_charger));
|
57 | 60 | static const struct device *pmic = DEVICE_DT_GET(DT_NODELABEL(pmic_main));
|
58 | 61 |
|
| 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 | + |
59 | 65 | /* Forward declarations */
|
60 | 66 | static int subscribe_to_vsbus_events(const struct device *pmic, struct gpio_callback *event_cb);
|
61 | 67 | 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)
|
116 | 122 | return;
|
117 | 123 | }
|
118 | 124 |
|
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 | + } |
124 | 132 | }
|
125 | 133 |
|
126 | 134 | err = charger_read_sensors(¶meters.v0, ¶meters.i0, ¶meters.t0, &chg_status);
|
@@ -163,17 +171,97 @@ static void state_running_run(void *o)
|
163 | 171 |
|
164 | 172 | /* End of state handling */
|
165 | 173 |
|
| 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 | + |
166 | 238 | static void event_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
|
167 | 239 | {
|
168 | 240 | ARG_UNUSED(dev);
|
169 | 241 | ARG_UNUSED(cb);
|
170 | 242 |
|
| 243 | + int err; |
| 244 | + |
171 | 245 | 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 | + } |
173 | 254 | }
|
174 | 255 |
|
175 | 256 | 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 | + } |
177 | 265 | }
|
178 | 266 | }
|
179 | 267 |
|
|
0 commit comments