Skip to content

Commit 7b5eee4

Browse files
serial_lte_modem: refactor to use pm_device_runtime
Refactor serial_lte_modem to use pm_device_runtime. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent c509075 commit 7b5eee4

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

applications/serial_lte_modem/src/slm_at_host.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
#include <zephyr/drivers/uart.h>
2020
#include <zephyr/kernel.h>
2121
#include <zephyr/logging/log.h>
22-
#include <zephyr/pm/device.h>
22+
#include <zephyr/pm/device_runtime.h>
2323
#include <zephyr/sys/ring_buffer.h>
24+
#include <zephyr/sys/atomic.h>
2425
LOG_MODULE_REGISTER(slm_at_host, CONFIG_SLM_LOG_LEVEL);
2526

2627
#define SLM_SYNC_STR "Ready\r\n"
@@ -53,11 +54,33 @@ static uint8_t quit_str_partial_match;
5354
K_MUTEX_DEFINE(mutex_data); /* Protects the data_rb and quit_str_partial_match. */
5455

5556
static struct k_work raw_send_scheduled_work;
57+
static bool slm_uart_dev_active;
5658

5759
/* global functions defined in different files */
5860
int slm_at_init(void);
5961
void slm_at_uninit(void);
6062

63+
static int set_uart_dev_power_state(bool active)
64+
{
65+
int err;
66+
67+
if ((active && slm_uart_dev_active) || (!active && !slm_uart_dev_active)) {
68+
return 0;
69+
}
70+
71+
if (active) {
72+
err = pm_device_runtime_get(slm_uart_dev);
73+
} else {
74+
err = pm_device_runtime_put(slm_uart_dev);
75+
}
76+
77+
if (err == 0) {
78+
slm_uart_dev_active = active;
79+
}
80+
81+
return err;
82+
}
83+
6184
static enum slm_operation_mode get_slm_mode(void)
6285
{
6386
enum slm_operation_mode mode;
@@ -506,10 +529,7 @@ static int slm_at_send_indicate(const uint8_t *data, size_t len,
506529
}
507530

508531
if (indicate) {
509-
enum pm_device_state state = PM_DEVICE_STATE_OFF;
510-
511-
pm_device_state_get(slm_uart_dev, &state);
512-
if (state != PM_DEVICE_STATE_ACTIVE) {
532+
if (!slm_uart_dev_is_active()) {
513533
slm_ctrl_pin_indicate();
514534
}
515535
}
@@ -1006,10 +1026,7 @@ static int at_host_power_off(bool shutting_down)
10061026
if (!err || shutting_down) {
10071027

10081028
/* Power off UART module */
1009-
err = pm_device_action_run(slm_uart_dev, PM_DEVICE_ACTION_SUSPEND);
1010-
if (err == -EALREADY) {
1011-
err = 0;
1012-
}
1029+
err = set_uart_dev_power_state(false);
10131030
if (err) {
10141031
LOG_WRN("Failed to suspend UART. (%d)", err);
10151032
if (!shutting_down) {
@@ -1033,9 +1050,9 @@ int slm_at_host_power_off(void)
10331050

10341051
int slm_at_host_power_on(void)
10351052
{
1036-
const int err = pm_device_action_run(slm_uart_dev, PM_DEVICE_ACTION_RESUME);
1053+
const int err = set_uart_dev_power_state(true);
10371054

1038-
if (err && err != -EALREADY) {
1055+
if (err) {
10391056
LOG_ERR("Failed to resume UART. (%d)", err);
10401057
return err;
10411058
}
@@ -1047,6 +1064,11 @@ int slm_at_host_power_on(void)
10471064
return 0;
10481065
}
10491066

1067+
bool slm_uart_dev_is_active(void)
1068+
{
1069+
return slm_uart_dev_active;
1070+
}
1071+
10501072
void slm_at_host_uninit(void)
10511073
{
10521074
k_mutex_lock(&mutex_mode, K_FOREVER);

applications/serial_lte_modem/src/slm_at_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ int slm_at_host_power_off(void);
8787
/** @brief Counterpart to @c slm_at_host_power_off(). */
8888
int slm_at_host_power_on(void);
8989

90+
/** Check if UART is active */
91+
bool slm_uart_dev_is_active(void);
92+
9093
/**
9194
* @brief Uninitialize AT host for serial LTE modem
9295
*/

applications/serial_lte_modem/src/slm_ppp.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ static void ppp_data_passing_thread(void*, void*, void*)
703703
{
704704
const size_t mtu = net_if_get_mtu(ppp_iface);
705705
struct zsock_pollfd fds[PPP_FDS_COUNT];
706-
enum pm_device_state state = PM_DEVICE_STATE_OFF;
707706

708707
for (size_t i = 0; i != ARRAY_SIZE(fds); ++i) {
709708
fds[i].fd = ppp_fds[i];
@@ -745,8 +744,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
745744

746745
/* When DL data is received from the network, check if UART is suspended */
747746
if (src == MODEM_FD_IDX) {
748-
pm_device_state_get(ppp_uart_dev, &state);
749-
if (state != PM_DEVICE_STATE_ACTIVE) {
747+
if (!slm_uart_dev_is_active()) {
750748
LOG_DBG("PPP data received but UART not active");
751749
slm_ctrl_pin_indicate();
752750
}

applications/serial_lte_modem/src/slm_uart_handler.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ static int tx_start(void)
180180
uint8_t *buf;
181181
size_t len;
182182
int err;
183-
enum pm_device_state state = PM_DEVICE_STATE_OFF;
184183

185-
pm_device_state_get(slm_uart_dev, &state);
186-
if (state != PM_DEVICE_STATE_ACTIVE) {
184+
if (!slm_uart_dev_is_active()) {
187185
return 1;
188186
}
189187

0 commit comments

Comments
 (0)