Skip to content

Commit

Permalink
Merge pull request zephyrproject-rtos#10 from converge-io/lucas/varia…
Browse files Browse the repository at this point in the history
…ble-baudrate

Lucas/variable baudrate
  • Loading branch information
davidbascelli authored Aug 1, 2024
2 parents 8fb601a + e873942 commit 00ee185
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
39 changes: 39 additions & 0 deletions drivers/modem/Kconfig.cellular
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,43 @@ config MODEM_CELLULAR_USER_PIPE_BUFFER_SIZES
int "The size of the buffers used for each user pipe in bytes."
default 128

choice
prompt "Choose a uart baudrate"

config MODEM_CELLULAR_UART_BAUDRATE_9600
bool "9600"
config MODEM_CELLULAR_UART_BAUDRATE_19200
bool "19200"
config MODEM_CELLULAR_UART_BAUDRATE_38400
bool "38400"
config MODEM_CELLULAR_UART_BAUDRATE_57600
bool "57600"
config MODEM_CELLULAR_UART_BAUDRATE_115200
bool "115200"
config MODEM_CELLULAR_UART_BAUDRATE_230400
bool "230400"
config MODEM_CELLULAR_UART_BAUDRATE_460800
bool "460800"
endchoice

config MODEM_CELLULAR_UART_BAUDRATE
int
default 9600 if MODEM_CELLULAR_UART_BAUDRATE_9600
default 19200 if MODEM_CELLULAR_UART_BAUDRATE_19200
default 38400 if MODEM_CELLULAR_UART_BAUDRATE_38400
default 57600 if MODEM_CELLULAR_UART_BAUDRATE_57600
default 115200 if MODEM_CELLULAR_UART_BAUDRATE_115200
default 230400 if MODEM_CELLULAR_UART_BAUDRATE_230400
default 460800 if MODEM_CELLULAR_UART_BAUDRATE_460800

config MODEM_CELLULAR_CMUX_BAUDRATE
int
default 1 if MODEM_CELLULAR_UART_BAUDRATE_9600
default 2 if MODEM_CELLULAR_UART_BAUDRATE_19200
default 3 if MODEM_CELLULAR_UART_BAUDRATE_38400
default 4 if MODEM_CELLULAR_UART_BAUDRATE_57600
default 5 if MODEM_CELLULAR_UART_BAUDRATE_115200
default 6 if MODEM_CELLULAR_UART_BAUDRATE_230400
default 7 if MODEM_CELLULAR_UART_BAUDRATE_460800

endif
48 changes: 46 additions & 2 deletions drivers/modem/modem_cellular.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <zephyr/net/ppp.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/drivers/uart.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(modem_cellular, CONFIG_MODEM_LOG_LEVEL);
Expand All @@ -34,6 +35,7 @@ LOG_MODULE_REGISTER(modem_cellular, CONFIG_MODEM_LOG_LEVEL);
#define MODEM_CELLULAR_DATA_MANUFACTURER_LEN (65)
#define MODEM_CELLULAR_DATA_FW_VERSION_LEN (65)

#define MODEM_CELLULAR_DEFAULT_BAUDRATE (115200)
#define MODEM_CELLULAR_RESERVED_DLCIS (2)

/* Magic constants */
Expand Down Expand Up @@ -239,6 +241,18 @@ static bool modem_cellular_gpio_is_enabled(const struct gpio_dt_spec *gpio)
return gpio->port != NULL;
}

static int modem_cellular_set_baudrate(const struct device * uart, uint32_t baudrate) {
struct uart_config config;
int rc = uart_config_get(uart, &config);
if (rc != 0) {
return rc;
}

config.baudrate = baudrate;

return uart_configure(uart, &config);
}

static void modem_cellular_notify_user_pipes_connected(struct modem_cellular_data *data)
{
const struct modem_cellular_config *config =
Expand Down Expand Up @@ -477,7 +491,22 @@ static void modem_cellular_chat_on_cxreg(struct modem_chat *chat, char **argv, u
}
}


static void modem_cellular_chat_on_ipr(struct modem_chat *chat, char **argv, uint16_t argc, void *user_data){
int rc;
struct modem_cellular_data *data = (struct modem_cellular_data *)user_data;

const struct device * uart = data->uart_backend.uart;

rc = modem_cellular_set_baudrate(uart, CONFIG_MODEM_CELLULAR_UART_BAUDRATE);

if (rc != 0) {
LOG_DBG("Configure UART to %d", CONFIG_MODEM_CELLULAR_UART_BAUDRATE);
}
}

MODEM_CHAT_MATCH_DEFINE(ok_match, "OK", "", NULL);
MODEM_CHAT_MATCH_DEFINE(ipr_match, "OK", "", modem_cellular_chat_on_ipr);
MODEM_CHAT_MATCHES_DEFINE(allow_match,
MODEM_CHAT_MATCH("OK", "", NULL),
MODEM_CHAT_MATCH("ERROR", "", NULL));
Expand Down Expand Up @@ -651,6 +680,10 @@ static int modem_cellular_on_reset_pulse_state_enter(struct modem_cellular_data
(const struct modem_cellular_config *)data->dev->config;

gpio_pin_set_dt(&config->reset_gpio, 1);

const struct device * uart = data->uart_backend.uart;
modem_cellular_set_baudrate(uart, MODEM_CELLULAR_DEFAULT_BAUDRATE);

modem_cellular_start_timer(data, K_MSEC(config->reset_pulse_duration_ms));
return 0;
}
Expand Down Expand Up @@ -1772,7 +1805,16 @@ MODEM_CHAT_SCRIPT_DEFINE(quectel_bg95_get_signal_chat_script,

#if DT_HAS_COMPAT_STATUS_OKAY(quectel_eg25_g)
MODEM_CHAT_SCRIPT_CMDS_DEFINE(
quectel_eg25_g_init_chat_script_cmds, MODEM_CHAT_SCRIPT_CMD_RESP("ATE0", ok_match),
quectel_eg25_g_init_chat_script_cmds,
MODEM_CHAT_SCRIPT_CMD_RESP("AT+IPR=" STRINGIFY(CONFIG_MODEM_CELLULAR_UART_BAUDRATE), ipr_match),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("ATE0", 100),
MODEM_CHAT_SCRIPT_CMD_RESP("ATE0", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP("AT+CFUN=4", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP("AT+CMEE=1", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP("AT+CREG=1", ok_match),
Expand All @@ -1791,7 +1833,9 @@ MODEM_CHAT_SCRIPT_CMDS_DEFINE(
MODEM_CHAT_SCRIPT_CMD_RESP("", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP("AT+CIMI", cimi_match),
MODEM_CHAT_SCRIPT_CMD_RESP("", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("AT+CMUX=0,0,5,127,10,3,30,10,2", 100));
MODEM_CHAT_SCRIPT_CMD_RESP("AT+QCCID", qccid_match),
MODEM_CHAT_SCRIPT_CMD_RESP("", ok_match),
MODEM_CHAT_SCRIPT_CMD_RESP_NONE("AT+CMUX=0,0," STRINGIFY(CONFIG_MODEM_CMUX_BAUDRATE) ",127,10,3,30,10,2", 100));

MODEM_CHAT_SCRIPT_DEFINE(quectel_eg25_g_init_chat_script, quectel_eg25_g_init_chat_script_cmds,
abort_matches, modem_cellular_chat_callback_handler, 10);
Expand Down

0 comments on commit 00ee185

Please sign in to comment.