Skip to content

Commit 0efca19

Browse files
committed
samples: sm_at_client_shell: Fix UART stuck in re-enable
Add fix for re-enabling the UART which was caused by the disable failing as the worker was cancelled. Signed-off-by: Markus Lassila <markus.lassila@nordicsemi.no>
1 parent be01b94 commit 0efca19

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

include/sm_at_client.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,9 @@ int sm_at_client_register_ri_handler(sm_ri_handler_t handler);
9696
* If automatic DTR UART handling is enabled, the library will enable DTR UART when RI
9797
* signal is detected, and disable it after inactivity timeout.
9898
*
99-
* @param automatic If true, DTR UART is automatically managed by the library.
100-
* @param inactivity Inactivity timeout for DTR UART disablement. Only used if @p
101-
* automatic is true.
99+
* @param inactivity Inactivity timeout for DTR UART disablement.
102100
*/
103-
void sm_at_client_configure_dtr_uart(bool automatic, k_timeout_t inactivity);
101+
void sm_at_client_automatic_dtr_uart(k_timeout_t inactivity);
104102

105103
/**
106104
* @brief Disable DTR UART

lib/sm_at_client/sm_at_client.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ static int rx_enable(void)
168168

169169
buf = buf_alloc();
170170
if (!buf) {
171-
LOG_DBG("Failed to allocate RX buffer");
171+
LOG_ERR("Failed to allocate RX buffer");
172172
return -ENOMEM;
173173
}
174174

175175
ret = uart_rx_enable(uart_dev, buf->buf, sizeof(buf->buf), UART_RX_TIMEOUT_US);
176-
if (ret) {
177-
LOG_WRN("uart_rx_enable failed: %d", ret);
176+
if (ret && ret != -EBUSY) {
177+
LOG_ERR("uart_rx_enable failed: %d", ret);
178178
buf_unref(buf->buf);
179179
return ret;
180180
}
@@ -208,7 +208,10 @@ static int rx_disable(void)
208208
}
209209

210210
/* Wait until RX is actually disabled. */
211-
k_sem_take(&uart_disabled_sem, K_MSEC(100));
211+
err = k_sem_take(&uart_disabled_sem, K_MSEC(100));
212+
if (err) {
213+
LOG_ERR("UART RX disable timeout: %d", err);
214+
}
212215
atomic_clear_bit(&uart_state, SM_AT_CLIENT_RX_ENABLED_BIT);
213216

214217
return 0;
@@ -451,9 +454,6 @@ static void reschedule_disable(void)
451454
if (dtr_config.active && dtr_config.automatic) {
452455
/* Restart the inactivity timer. */
453456
k_work_reschedule(&dtr_uart_disable_work, dtr_config.inactivity);
454-
} else {
455-
/* Stop the inactivity timer. */
456-
k_work_cancel_delayable(&dtr_uart_disable_work);
457457
}
458458
}
459459

@@ -913,21 +913,24 @@ int sm_at_client_send_data(const uint8_t *const data, size_t datalen)
913913
return tx_write(data, datalen, true);
914914
}
915915

916-
void sm_at_client_configure_dtr_uart(bool automatic, k_timeout_t inactivity)
916+
void sm_at_client_automatic_dtr_uart(k_timeout_t inactivity)
917917
{
918918
if (!initialized) {
919919
LOG_ERR("AT client not initialized");
920920
return;
921921
}
922922

923-
dtr_config.automatic = automatic;
923+
dtr_config.automatic = true;
924924
dtr_config.inactivity = inactivity;
925925

926-
if (dtr_config.automatic && !dtr_config.active && !ring_buf_is_empty(&tx_buf)) {
927-
/* If automatic DTR UART is enabled and there is data to send, enable DTR UART. */
926+
if (!dtr_config.active && !ring_buf_is_empty(&tx_buf)) {
927+
/* Automatic mode enabled with pending TX data: trigger enable. */
928928
k_work_submit(&dtr_uart_enable_work);
929-
} else {
930-
reschedule_disable();
929+
}
930+
931+
if (dtr_config.active) {
932+
/* Restart the inactivity timer. */
933+
k_work_reschedule(&dtr_uart_disable_work, dtr_config.inactivity);
931934
}
932935
}
933936

@@ -938,7 +941,8 @@ void sm_at_client_disable_dtr_uart(void)
938941
return;
939942
}
940943

941-
sm_at_client_configure_dtr_uart(false, K_NO_WAIT);
944+
dtr_config.automatic = false;
945+
k_work_cancel(&dtr_uart_enable_work);
942946
k_work_reschedule(&dtr_uart_disable_work, K_NO_WAIT);
943947
}
944948

@@ -949,7 +953,8 @@ void sm_at_client_enable_dtr_uart(void)
949953
return;
950954
}
951955

952-
sm_at_client_configure_dtr_uart(false, K_NO_WAIT);
956+
dtr_config.automatic = false;
957+
k_work_cancel_delayable(&dtr_uart_disable_work);
953958
k_work_submit(&dtr_uart_enable_work);
954959
}
955960

@@ -993,7 +998,7 @@ int sm_at_client_shell_smsh_dtr_uart_auto(const struct shell *shell, size_t argc
993998
return -EINVAL;
994999
}
9951000

996-
sm_at_client_configure_dtr_uart(true, K_MSEC(timeout));
1001+
sm_at_client_automatic_dtr_uart(K_MSEC(timeout));
9971002

9981003
shell_print(shell, "Automatic DTR UART. Inactivity timeout %u ms", timeout);
9991004

samples/sm_at_client_shell/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CONFIG_SHELL_ARGC_MAX=40
1919
CONFIG_SHELL_CMD_BUFF_SIZE=1024
2020
CONFIG_SHELL_STACK_SIZE=2048
2121
CONFIG_SHELL_LOG_BACKEND=n
22+
CONFIG_LOG_RUNTIME_FILTERING=n
2223

2324
CONFIG_PM_DEVICE=y
2425
CONFIG_PM_DEVICE_RUNTIME=y

0 commit comments

Comments
 (0)