From 817c0e3019623574759e4e2d1d91f15f97a05b6d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 30 Mar 2020 16:06:27 +0200 Subject: [PATCH] esp_modem: UART runtime configuration of esp-modem Basic set of configuration related to UART moved from KConfig to runtime configuration structure to simplify reusing the component for testing and examples. --- .../pppos_client/components/modem/Kconfig | 75 +------------------ .../components/modem/include/esp_modem.h | 24 +++++- .../components/modem/src/esp_modem.c | 39 +++++----- .../pppos_client/main/Kconfig.projbuild | 71 ++++++++++++++++++ .../pppos_client/main/pppos_client_main.c | 12 +++ 5 files changed, 128 insertions(+), 93 deletions(-) diff --git a/examples/protocols/pppos_client/components/modem/Kconfig b/examples/protocols/pppos_client/components/modem/Kconfig index d3a193b25b..932c1c091e 100644 --- a/examples/protocols/pppos_client/components/modem/Kconfig +++ b/examples/protocols/pppos_client/components/modem/Kconfig @@ -1,82 +1,9 @@ menu "ESP-MODEM" - config MODEM_APN + config EXAMPLE_COMPONENT_MODEM_APN string "Set Access Point Name (APN)" default "CMNET" help Logical name which is used to select the GGSN or the external packet data network. - menu "UART Configuration" - config MODEM_UART_MODEM_TX_PIN - int "TXD Pin Number" - default 25 - range 0 31 - help - Pin number of UART TX. - - config MODEM_UART_MODEM_RX_PIN - int "RXD Pin Number" - default 26 - range 0 31 - help - Pin number of UART RX. - - config MODEM_UART_MODEM_RTS_PIN - int "RTS Pin Number" - default 27 - range 0 31 - help - Pin number of UART RTS. - - config MODEM_UART_MODEM_CTS_PIN - int "CTS Pin Number" - default 23 - range 0 31 - help - Pin number of UART CTS. - - config MODEM_UART_EVENT_TASK_STACK_SIZE - int "UART Event Task Stack Size" - range 2000 6000 - default 2048 - help - Stack size of UART event task. - - config MODEM_UART_EVENT_TASK_PRIORITY - int "UART Event Task Priority" - range 3 22 - default 5 - help - Priority of UART event task. - - config MODEM_UART_EVENT_QUEUE_SIZE - int "UART Event Queue Size" - range 10 40 - default 30 - help - Length of UART event queue. - - config MODEM_UART_PATTERN_QUEUE_SIZE - int "UART Pattern Queue Size" - range 10 40 - default 20 - help - Length of UART pattern queue. - - config MODEM_UART_TX_BUFFER_SIZE - int "UART TX Buffer Size" - range 256 2048 - default 512 - help - Buffer size of UART TX buffer. - - config MODEM_UART_RX_BUFFER_SIZE - int "UART RX Buffer Size" - range 256 2048 - default 1024 - help - Buffer size of UART RX buffer. - endmenu - - endmenu diff --git a/examples/protocols/pppos_client/components/modem/include/esp_modem.h b/examples/protocols/pppos_client/components/modem/include/esp_modem.h index 689fe32b52..6b6185fc66 100644 --- a/examples/protocols/pppos_client/components/modem/include/esp_modem.h +++ b/examples/protocols/pppos_client/components/modem/include/esp_modem.h @@ -50,6 +50,17 @@ typedef struct { uart_parity_t parity; /*!< Parity type */ modem_flow_ctrl_t flow_control; /*!< Flow control type */ uint32_t baud_rate; /*!< Communication baud rate */ + int tx_io_num; /*!< TXD Pin Number */ + int rx_io_num; /*!< RXD Pin Number */ + int rts_io_num; /*!< RTS Pin Number */ + int cts_io_num; /*!< CTS Pin Number */ + int rx_buffer_size; /*!< UART RX Buffer Size */ + int tx_buffer_size; /*!< UART TX Buffer Size */ + int pattern_queue_size; /*!< UART Pattern Queue Size */ + int event_queue_size; /*!< UART Event Queue Size */ + uint32_t event_task_stack_size; /*!< UART Event Task Stack size */ + int event_task_priority; /*!< UART Event Task Priority */ + int line_buffer_size; /*!< Line buffer size for command mode */ } esp_modem_dte_config_t; /** @@ -69,7 +80,18 @@ typedef esp_err_t (*esp_modem_on_receive)(void *buffer, size_t len, void *contex .stop_bits = UART_STOP_BITS_1, \ .parity = UART_PARITY_DISABLE, \ .baud_rate = 115200, \ - .flow_control = MODEM_FLOW_CONTROL_NONE \ + .flow_control = MODEM_FLOW_CONTROL_NONE,\ + .tx_io_num = 25, \ + .rx_io_num = 26, \ + .rts_io_num = 27, \ + .cts_io_num = 23, \ + .rx_buffer_size = 1024, \ + .tx_buffer_size = 512, \ + .pattern_queue_size = 20, \ + .event_queue_size = 30, \ + .event_task_stack_size = 2048, \ + .event_task_priority = 5, \ + .line_buffer_size = 512 \ } /** diff --git a/examples/protocols/pppos_client/components/modem/src/esp_modem.c b/examples/protocols/pppos_client/components/modem/src/esp_modem.c index 86b0a3f918..1af3eed2ec 100644 --- a/examples/protocols/pppos_client/components/modem/src/esp_modem.c +++ b/examples/protocols/pppos_client/components/modem/src/esp_modem.c @@ -21,7 +21,6 @@ #include "esp_log.h" #include "sdkconfig.h" -#define ESP_MODEM_LINE_BUFFER_SIZE (CONFIG_MODEM_UART_RX_BUFFER_SIZE / 2) #define ESP_MODEM_EVENT_QUEUE_SIZE (16) #define MIN_PATTERN_INTERVAL (9) @@ -57,8 +56,10 @@ typedef struct { TaskHandle_t uart_event_task_hdl; /*!< UART event task handle */ SemaphoreHandle_t process_sem; /*!< Semaphore used for indicating processing status */ modem_dte_t parent; /*!< DTE interface that should extend */ - esp_modem_on_receive receive_cb; /*!< ptr to data reception */ - void *receive_cb_ctx; /*!< ptr to rx fn context data */ + esp_modem_on_receive receive_cb; /*!< ptr to data reception */ + void *receive_cb_ctx; /*!< ptr to rx fn context data */ + int line_buffer_size; /*!< line buffer size in commnad mode */ + int event_queue_size; /*!< UART event queue size */ } esp_modem_dte_t; @@ -108,12 +109,12 @@ static void esp_handle_uart_pattern(esp_modem_dte_t *esp_dte) int pos = uart_pattern_pop_pos(esp_dte->uart_port); int read_len = 0; if (pos != -1) { - if (pos < ESP_MODEM_LINE_BUFFER_SIZE - 1) { + if (pos < esp_dte->line_buffer_size - 1) { /* read one line(include '\n') */ read_len = pos + 1; } else { ESP_LOGW(MODEM_TAG, "ESP Modem Line buffer too small"); - read_len = ESP_MODEM_LINE_BUFFER_SIZE - 1; + read_len = esp_dte->line_buffer_size - 1; } read_len = uart_read_bytes(esp_dte->uart_port, esp_dte->buffer, read_len, pdMS_TO_TICKS(100)); if (read_len) { @@ -146,7 +147,7 @@ static void esp_handle_uart_data(esp_modem_dte_t *esp_dte) } size_t length = 0; uart_get_buffered_data_len(esp_dte->uart_port, &length); - length = MIN(ESP_MODEM_LINE_BUFFER_SIZE, length); + length = MIN(esp_dte->line_buffer_size, length); length = uart_read_bytes(esp_dte->uart_port, esp_dte->buffer, length, portMAX_DELAY); /* pass the input data to configured callback */ if (length) { @@ -314,7 +315,7 @@ static esp_err_t esp_modem_dte_change_mode(modem_dte_t *dte, modem_mode_t new_mo uart_disable_rx_intr(esp_dte->uart_port); uart_flush(esp_dte->uart_port); uart_enable_pattern_det_baud_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); - uart_pattern_queue_reset(esp_dte->uart_port, CONFIG_MODEM_UART_PATTERN_QUEUE_SIZE); + uart_pattern_queue_reset(esp_dte->uart_port, esp_dte->event_queue_size); MODEM_CHECK(dce->set_working_mode(dce, new_mode) == ESP_OK, "set new working mode:%d failed", err, new_mode); break; default: @@ -368,7 +369,8 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) esp_modem_dte_t *esp_dte = calloc(1, sizeof(esp_modem_dte_t)); MODEM_CHECK(esp_dte, "calloc esp_dte failed", err_dte_mem); /* malloc memory to storing lines from modem dce */ - esp_dte->buffer = calloc(1, ESP_MODEM_LINE_BUFFER_SIZE); + esp_dte->line_buffer_size = config->line_buffer_size; + esp_dte->buffer = calloc(1, config->line_buffer_size); MODEM_CHECK(esp_dte->buffer, "calloc line memory failed", err_line_mem); /* Set attributes */ esp_dte->uart_port = config->port_num; @@ -392,10 +394,10 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) }; MODEM_CHECK(uart_param_config(esp_dte->uart_port, &uart_config) == ESP_OK, "config uart parameter failed", err_uart_config); if (config->flow_control == MODEM_FLOW_CONTROL_HW) { - res = uart_set_pin(esp_dte->uart_port, CONFIG_MODEM_UART_MODEM_TX_PIN, CONFIG_MODEM_UART_MODEM_RX_PIN, - CONFIG_MODEM_UART_MODEM_RTS_PIN, CONFIG_MODEM_UART_MODEM_CTS_PIN); + res = uart_set_pin(esp_dte->uart_port, config->tx_io_num, config->rx_io_num, + config->rts_io_num, config->cts_io_num); } else { - res = uart_set_pin(esp_dte->uart_port, CONFIG_MODEM_UART_MODEM_TX_PIN, CONFIG_MODEM_UART_MODEM_RX_PIN, + res = uart_set_pin(esp_dte->uart_port, config->tx_io_num, config->rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } MODEM_CHECK(res == ESP_OK, "config uart gpio failed", err_uart_config); @@ -407,14 +409,15 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) } MODEM_CHECK(res == ESP_OK, "config uart flow control failed", err_uart_config); /* Install UART driver and get event queue used inside driver */ - res = uart_driver_install(esp_dte->uart_port, CONFIG_MODEM_UART_RX_BUFFER_SIZE, CONFIG_MODEM_UART_TX_BUFFER_SIZE, - CONFIG_MODEM_UART_EVENT_QUEUE_SIZE, &(esp_dte->event_queue), 0); + esp_dte->event_queue_size = config->event_queue_size; + res = uart_driver_install(esp_dte->uart_port, config->rx_buffer_size, config->tx_buffer_size, + config->event_queue_size, &(esp_dte->event_queue), 0); MODEM_CHECK(res == ESP_OK, "install uart driver failed", err_uart_config); /* Set pattern interrupt, used to detect the end of a line. */ res = uart_enable_pattern_det_baud_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); /* Set pattern queue size */ - res |= uart_pattern_queue_reset(esp_dte->uart_port, CONFIG_MODEM_UART_PATTERN_QUEUE_SIZE); + res |= uart_pattern_queue_reset(esp_dte->uart_port, config->pattern_queue_size); /* Starting in command mode -> explicitly disable RX interrupt */ uart_disable_rx_intr(esp_dte->uart_port); @@ -430,10 +433,10 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) MODEM_CHECK(esp_dte->process_sem, "create process semaphore failed", err_sem); /* Create UART Event task */ BaseType_t ret = xTaskCreate(uart_event_task_entry, //Task Entry - "uart_event", //Task Name - CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE, //Task Stack Size(Bytes) + "uart_event", //Task Name + config->event_task_stack_size, //Task Stack Size(Bytes) esp_dte, //Task Parameter - CONFIG_MODEM_UART_EVENT_TASK_PRIORITY, //Task Priority + config->event_task_priority, //Task Priority & (esp_dte->uart_event_task_hdl) //Task Handler ); MODEM_CHECK(ret == pdTRUE, "create uart event task failed", err_tsk_create); @@ -473,7 +476,7 @@ esp_err_t esp_modem_start_ppp(modem_dte_t *dte) MODEM_CHECK(dce, "DTE has not yet bind with DCE", err); esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); /* Set PDP Context */ - MODEM_CHECK(dce->define_pdp_context(dce, 1, "IP", CONFIG_MODEM_APN) == ESP_OK, "set MODEM APN failed", err); + MODEM_CHECK(dce->define_pdp_context(dce, 1, "IP", CONFIG_EXAMPLE_COMPONENT_MODEM_APN) == ESP_OK, "set MODEM APN failed", err); /* Enter PPP mode */ MODEM_CHECK(dte->change_mode(dte, MODEM_PPP_MODE) == ESP_OK, "enter ppp mode failed", err); diff --git a/examples/protocols/pppos_client/main/Kconfig.projbuild b/examples/protocols/pppos_client/main/Kconfig.projbuild index c2c3dec4cf..c85e73b45e 100644 --- a/examples/protocols/pppos_client/main/Kconfig.projbuild +++ b/examples/protocols/pppos_client/main/Kconfig.projbuild @@ -42,5 +42,76 @@ menu "Example Configuration" Enter the peer phone number that you want to send message to. endif + menu "UART Configuration" + config EXAMPLE_MODEM_UART_TX_PIN + int "TXD Pin Number" + default 25 + range 0 31 + help + Pin number of UART TX. + + config EXAMPLE_MODEM_UART_RX_PIN + int "RXD Pin Number" + default 26 + range 0 31 + help + Pin number of UART RX. + + config EXAMPLE_MODEM_UART_RTS_PIN + int "RTS Pin Number" + default 27 + range 0 31 + help + Pin number of UART RTS. + + config EXAMPLE_MODEM_UART_CTS_PIN + int "CTS Pin Number" + default 23 + range 0 31 + help + Pin number of UART CTS. + + config EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE + int "UART Event Task Stack Size" + range 2000 6000 + default 2048 + help + Stack size of UART event task. + + config EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY + int "UART Event Task Priority" + range 3 22 + default 5 + help + Priority of UART event task. + + config EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE + int "UART Event Queue Size" + range 10 40 + default 30 + help + Length of UART event queue. + + config EXAMPLE_MODEM_UART_PATTERN_QUEUE_SIZE + int "UART Pattern Queue Size" + range 10 40 + default 20 + help + Length of UART pattern queue. + + config EXAMPLE_MODEM_UART_TX_BUFFER_SIZE + int "UART TX Buffer Size" + range 256 2048 + default 512 + help + Buffer size of UART TX buffer. + + config EXAMPLE_MODEM_UART_RX_BUFFER_SIZE + int "UART RX Buffer Size" + range 256 2048 + default 1024 + help + Buffer size of UART RX buffer. + endmenu endmenu diff --git a/examples/protocols/pppos_client/main/pppos_client_main.c b/examples/protocols/pppos_client/main/pppos_client_main.c index c4b5fd8ab0..2a03c4a461 100644 --- a/examples/protocols/pppos_client/main/pppos_client_main.c +++ b/examples/protocols/pppos_client/main/pppos_client_main.c @@ -234,6 +234,18 @@ void app_main(void) /* create dte object */ esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG(); + /* setup UART specific configuration based on kconfig options */ + config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN; + config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN; + config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN; + config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN; + config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE; + config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE; + config.pattern_queue_size = CONFIG_EXAMPLE_MODEM_UART_PATTERN_QUEUE_SIZE; + config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE; + config.event_task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE; + config.event_task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY; + config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE/2; modem_dte_t *dte = esp_modem_dte_init(&config); /* Register event handler */ ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));