Skip to content

Commit e8242fe

Browse files
committed
app: Add support for AT+IPR to change baud rate
Add V.250 command AT+IPR=<baud> for changing the fixed baud rate. Update the linux dial-up script to utilize the AT+IPR. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 7592e44 commit e8242fe

4 files changed

Lines changed: 181 additions & 6 deletions

File tree

app/scripts/sm_start_ppp.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#
2626
MODEM=/dev/ttyACM0
2727
BAUD=115200
28+
IPR_BAUD=0
2829
TIMEOUT=60
2930
APN="internet"
3031
TYPE="IPV4V6"
@@ -39,11 +40,15 @@ TRACE_PID_FILE="/var/run/nrf91-modem-trace.pid"
3940
TRACE=0
4041

4142
usage() {
42-
echo "Usage: $0 [-s serial_port] [-b baud_rate] [-t timeout] [-a APN] [-f IP|IPV6|IPV4V6] \
43-
[-p PDN] [-T] [-v] [-h]"
43+
echo "Usage: $0 [-s serial_port] [-b baud_rate] [-B new_speed] [-t timeout] [-a APN]"
44+
echo " [-f IP|IPV6|IPV4V6] [-p PDN] [-T] [-v] [-h]"
4445
echo ""
4546
echo " -s serial_port : Serial port where the modem is connected (default: $MODEM)"
46-
echo " -b baud_rate : Baud rate for serial communication (default: $BAUD)"
47+
echo " -b baud_rate : Current baud rate of Serial Modem (default: $BAUD)"
48+
echo " -B new_speed : Use AT+IPR to change baud rate to <new_speed>"
49+
echo " Start with current baud rate and switch to new_speed after modem is"
50+
echo " responsive. If not set, baud rate will not be changed."
51+
echo " When terminated, baud rate will be switched back to original."
4752
echo " -t timeout : Timeout for dialup commands in seconds (default: $TIMEOUT)"
4853
echo " -a APN : Access Point Name for cellular connection (default: $APN)"
4954
echo " -f FAMILY : PDP_type, one of IP, IPV6, IPV4V6 (default: $TYPE)"
@@ -56,11 +61,12 @@ usage() {
5661
}
5762

5863
# Parse command line parameters
59-
while getopts s:b:t:a::f:p:Thv flag
64+
while getopts s:b:B:t:a::f:p:Thv flag
6065
do
6166
case "${flag}" in
6267
s) MODEM=${OPTARG};;
6368
b) BAUD=${OPTARG};;
69+
B) IPR_BAUD=${OPTARG};;
6470
t) TIMEOUT=${OPTARG};;
6571
a) APN=${OPTARG};;
6672
p) PDN=${OPTARG};;
@@ -199,6 +205,13 @@ else
199205
cmux_close
200206
fi
201207

208+
if [ $IPR_BAUD -ne 0 ]; then
209+
log_dbg "Set baud rate on modem to $IPR_BAUD"
210+
chat $CHATOPT -t1 '' "AT+IPR=$IPR_BAUD" "OK" >$MODEM <$MODEM
211+
# Reconfigure serial port
212+
stty -F $MODEM $IPR_BAUD pass8 raw crtscts clocal
213+
fi
214+
202215
log_dbg "Attach CMUX channel to modem..."
203216
ldattach -c $'\rAT#XCMUX=1\r' GSM0710 $MODEM
204217

@@ -235,6 +248,11 @@ shutdown_modem() {
235248
cmux_close
236249
chat $CHATOPT -t5 '' $SHUTDOWN_SCRIPT >$MODEM <$MODEM
237250
fi
251+
if [ $IPR_BAUD -ne 0 ]; then
252+
# Restore baud rate on modem
253+
chat $CHATOPT -t1 '' "AT+IPR=$BAUD" "OK" >$MODEM <$MODEM
254+
stty -F $MODEM $BAUD
255+
fi
238256
}
239257

240258
ppp_start() {
@@ -259,6 +277,8 @@ export SHUTDOWN_SCRIPT
259277
export PPP_OPTIONS
260278
export PIDFILE
261279
export CHATOPT
280+
export BAUD
281+
export IPR_BAUD
262282
export TRACE_PID_FILE
263283
export -f ppp_start
264284
export -f shutdown_modem

app/src/sm_at_commands.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ STATIC int handle_at_clac(enum at_parser_cmd_type cmd_type, struct at_parser *,
303303
rsp_send("\r\n");
304304
for (size_t i = 0; i < cmd_custom_count; i++) {
305305
const char *cmd = _nrf_modem_at_cmd_custom_list_start[i].cmd;
306-
/* Modem AT comamands start with 'AT+' or AT%. Other commands are
306+
/* Modem AT commands start with 'AT+' or AT%. Other commands are
307307
* Serial Modem specific'. Skip modem AT commands.
308+
* Exception: AT+IPR is implemented in Serial Modem.
308309
*/
309-
if (strncasecmp(cmd, "AT+", strlen("AT+")) == 0 ||
310+
if ((strncasecmp(cmd, "AT+", strlen("AT+")) == 0 &&
311+
strncasecmp(cmd, "AT+IPR", strlen("AT+IPR")) != 0) ||
310312
strncasecmp(cmd, "AT%%", strlen("AT%%")) == 0) {
311313
continue;
312314
}

app/src/sm_uart_handler.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "sm_uart_handler.h"
1616
#include "sm_at_host.h"
1717
#include "sm_util.h"
18+
#include "sm_cmux.h"
1819

1920
#include <zephyr/logging/log.h>
2021
LOG_MODULE_REGISTER(sm_uart_handler, CONFIG_SM_LOG_LEVEL);
@@ -554,6 +555,66 @@ int sm_tx_write(const uint8_t *data, size_t len, bool flush, bool urc)
554555
return sm_uart_tx_write(data, len, flush, urc);
555556
}
556557

558+
SM_AT_CMD_CUSTOM(xppp, "AT+IPR", handle_at_ipr);
559+
static int handle_at_ipr(enum at_parser_cmd_type cmd_type, struct at_parser *parser,
560+
uint32_t param_count)
561+
{
562+
int err;
563+
struct uart_config cfg;
564+
565+
err = uart_config_get(sm_uart_dev, &cfg);
566+
if (err) {
567+
LOG_ERR("uart_config_get: %d", err);
568+
return err;
569+
}
570+
571+
if (cmd_type == AT_PARSER_CMD_TYPE_READ) {
572+
rsp_send("\r\n+IPR: %u\r\n", cfg.baudrate);
573+
return 0;
574+
}
575+
576+
if (cmd_type == AT_PARSER_CMD_TYPE_TEST) {
577+
rsp_send("\r\n+IPR: (),(%u,%u,%u,%u,%u)\r\n",
578+
115200, 230400, 460800, 921600, 1000000);
579+
return 0;
580+
}
581+
582+
if (cmd_type != AT_PARSER_CMD_TYPE_SET || param_count != 2) {
583+
return -EINVAL;
584+
}
585+
586+
if (sm_cmux_is_started()) {
587+
LOG_ERR("Cannot change baudrate while CMUX is active.");
588+
return -EBUSY;
589+
}
590+
591+
err = at_parser_num_get(parser, 1, &cfg.baudrate);
592+
if (err) {
593+
return err;
594+
}
595+
596+
if ((cfg.baudrate != 115200) &&
597+
(cfg.baudrate != 230400) &&
598+
(cfg.baudrate != 460800) &&
599+
(cfg.baudrate != 921600) &&
600+
(cfg.baudrate != 1000000)) {
601+
LOG_ERR("Unsupported baudrate: %u", cfg.baudrate);
602+
return -EINVAL;
603+
}
604+
605+
rsp_send_ok();
606+
607+
sm_uart_handler_disable();
608+
err = uart_configure(sm_uart_dev, &cfg);
609+
if (err) {
610+
LOG_ERR("uart_configure: %d", err);
611+
return err;
612+
}
613+
sm_uart_handler_enable();
614+
615+
return -SILENT_AT_COMMAND_RET;
616+
}
617+
557618
int sm_uart_handler_enable(void)
558619
{
559620
int err;

doc/app/at_generic.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,98 @@ Generic AT commands
99

1010
This page describes generic AT commands.
1111

12+
UART baud rate AT+IPR
13+
=====================
14+
15+
The ``AT+IPR`` command sets the UART baud rate.
16+
17+
Set command
18+
-----------
19+
20+
The set command sets the UART baud rate.
21+
|SM| does not support automatic baud rate detection.
22+
When the device resets, the baud rate is set to the default value set in the devicetree.
23+
24+
Syntax
25+
~~~~~~
26+
27+
::
28+
29+
AT+IPR=<baud_rate>
30+
31+
The ``<baud_rate>`` parameter is an integer value specifying the desired baud rate.
32+
33+
.. note::
34+
35+
The baud rate change takes effect after the modem responds with ``OK``.
36+
The host must switch to the new baud rate to continue communication.
37+
38+
Example
39+
~~~~~~~
40+
41+
Set the baud rate to 115200.
42+
43+
::
44+
45+
AT+IPR=115200
46+
OK
47+
48+
Read command
49+
------------
50+
51+
The read command reads the current UART baud rate.
52+
53+
Syntax
54+
~~~~~~
55+
56+
::
57+
58+
AT+IPR?
59+
60+
Response syntax
61+
~~~~~~~~~~~~~~~
62+
63+
::
64+
65+
+IPR: <baud_rate>
66+
67+
Example
68+
~~~~~~~
69+
70+
::
71+
72+
AT+IPR?
73+
+IPR: 115200
74+
OK
75+
76+
Test command
77+
------------
78+
79+
The test command lists the supported baud rates.
80+
81+
Syntax
82+
~~~~~~
83+
84+
::
85+
86+
AT+IPR=?
87+
88+
Response syntax
89+
~~~~~~~~~~~~~~~
90+
91+
::
92+
93+
+IPR: (list of supported autodetectable baud rates)[,(list of fixed-only baud rates)]
94+
95+
Example
96+
~~~~~~~
97+
98+
::
99+
100+
AT+IPR=?
101+
+IPR: (),(115200,230400,460800,921600,1000000)
102+
OK
103+
12104
|SM| echo E0/E1
13105
===============
14106

0 commit comments

Comments
 (0)