Skip to content

Commit 25f5124

Browse files
committed
Merge branch 'chore/optimize_interface_flush_and_ops' into 'master'
chore: Optimized interface flush and operations See merge request application/esp-at!2015
2 parents 5873127 + 9fe0e0c commit 25f5124

5 files changed

Lines changed: 23 additions & 63 deletions

File tree

main/interface/at_interface_api.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -27,7 +27,8 @@ static const char *TAG = "at-intf";
2727

2828
static int32_t at_port_read_data(uint8_t *buffer, int32_t len)
2929
{
30-
if (!s_interface_ops.read_data) {
30+
if (!s_interface_ops.read_data || !buffer || len < 0) {
31+
ESP_LOGE(TAG, "invalid read_fn:%p or buffer:%p or len:%d", s_interface_ops.read_data, buffer, len);
3132
return -1;
3233
}
3334

@@ -60,7 +61,8 @@ static int32_t at_port_read_data(uint8_t *buffer, int32_t len)
6061

6162
static int32_t at_port_write_data(uint8_t *data, int32_t len)
6263
{
63-
if (!s_interface_ops.write_data) {
64+
if (!s_interface_ops.write_data || !data || len < 0) {
65+
ESP_LOGE(TAG, "invalid write_fn:%p or data:%p or len:%d", s_interface_ops.write_data, data, len);
6466
return -1;
6567
}
6668

main/interface/sdio/at_sdio_task.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -46,13 +46,11 @@ static const char *TAG = "at-sdio";
4646

4747
static int32_t at_sdio_write_data(uint8_t *data, int32_t len)
4848
{
49-
if (len < 0 || data == NULL) {
50-
ESP_LOGE(TAG, "invalid data:%p or len:%d", data, len);
51-
return -1;
49+
if (len == 0) {
50+
return 0;
5251
}
5352

5453
xSemaphoreTake(s_sdio_rw_sema, portMAX_DELAY);
55-
5654
uint32_t had_written_len = 0;
5755
do {
5856
int to_send_len = (len - had_written_len) > AT_SDIO_DMA_SIZE ? AT_SDIO_DMA_SIZE : (len - had_written_len);
@@ -82,11 +80,6 @@ static int32_t at_sdio_write_data(uint8_t *data, int32_t len)
8280

8381
static int32_t at_sdio_read_data(uint8_t *data, int32_t len)
8482
{
85-
if (data == NULL || len < 0) {
86-
ESP_LOGE(TAG, "invalid data:%p or len:%d", data, len);
87-
return -1;
88-
}
89-
9083
if (len == 0) {
9184
ESP_LOGI(TAG, "read empty data");
9285
return 0;

main/interface/socket/at_socket_task.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -36,10 +36,6 @@ static const char *TAG = "at-socket";
3636

3737
static int32_t at_socket_read_data(uint8_t *data, int32_t len)
3838
{
39-
if (data == NULL || len < 0) {
40-
return -1;
41-
}
42-
4339
if (len == 0) {
4440
ESP_LOGI(TAG, "read empty data");
4541
return 0;
@@ -60,11 +56,6 @@ static int32_t at_socket_read_data(uint8_t *data, int32_t len)
6056

6157
static int32_t at_socket_write_data(uint8_t *data, int32_t len)
6258
{
63-
if (len < 0 || data == NULL) {
64-
ESP_LOGE(TAG, "invalid data:%p or len:%d", data, len);
65-
return -1;
66-
}
67-
6859
if (len == 0) {
6960
ESP_LOGI(TAG, "write empty data");
7061
return 0;

main/interface/spi/at_spi_task.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -96,11 +96,6 @@ inline static void at_spi_write_transmit_len(spi_mode_t spi_mode, uint16_t trans
9696

9797
static int32_t at_spi_read_data(uint8_t *data, int32_t len)
9898
{
99-
if (data == NULL || len < 0) {
100-
ESP_LOGE(TAG, "invalid data:%p or len:%d", data, len);
101-
return -1;
102-
}
103-
10499
if (len == 0) {
105100
ESP_LOGI(TAG, "read empty data");
106101
return 0;
@@ -118,8 +113,8 @@ static int32_t at_spi_read_data(uint8_t *data, int32_t len)
118113

119114
static int32_t at_spi_write_data(uint8_t *data, int32_t len)
120115
{
121-
if (len < 0 || len > CONFIG_TX_STREAM_BUFFER_SIZE || data == NULL) {
122-
ESP_LOGE(TAG, "invalid data:%p or len:%d", data, len);
116+
if (len > CONFIG_TX_STREAM_BUFFER_SIZE) {
117+
ESP_LOGE(TAG, "invalid len:%d", len);
123118
return -1;
124119
}
125120

main/interface/uart/at_uart_task.c

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,12 @@ at_uart_port_pins_t g_uart_port_pin;
3535

3636
static int32_t at_uart_write_data(uint8_t *data, int32_t len)
3737
{
38-
uint32_t length = 0;
39-
40-
length = uart_write_bytes(g_at_cmd_port, (char *)data, len);
41-
return length;
38+
return uart_write_bytes(g_at_cmd_port, (char *)data, len);
4239
}
4340

4441
static int32_t at_uart_read_data(uint8_t *buffer, int32_t len)
4542
{
46-
if (len == 0) {
47-
return 0;
48-
}
49-
50-
if (buffer == NULL) {
51-
if (len == -1) {
52-
size_t size = 0;
53-
if (uart_get_buffered_data_len(g_at_cmd_port, &size) != ESP_OK) {
54-
return -1;
55-
}
56-
len = size;
57-
}
58-
59-
if (len == 0) {
60-
return 0;
61-
}
62-
63-
uint8_t *data = (uint8_t *)malloc(len);
64-
if (data) {
65-
len = uart_read_bytes(g_at_cmd_port, data, len, portTICK_PERIOD_MS);
66-
free(data);
67-
return len;
68-
} else {
69-
return -1;
70-
}
71-
} else {
72-
return uart_read_bytes(g_at_cmd_port, buffer, len, portTICK_PERIOD_MS);
73-
}
43+
return uart_read_bytes(g_at_cmd_port, buffer, len, portTICK_PERIOD_MS);
7444
}
7545

7646
static int32_t at_uart_get_data_len(void)
@@ -87,6 +57,12 @@ static int32_t at_uart_get_data_len(void)
8757
}
8858
}
8959

60+
static void at_uart_flush(void)
61+
{
62+
uart_flush_input(g_at_cmd_port);
63+
xQueueReset(s_at_uart_queue);
64+
}
65+
9066
static bool at_uart_wait_tx_done(int32_t ms)
9167
{
9268
if (uart_wait_tx_done(g_at_cmd_port, ms / portTICK_PERIOD_MS) == ESP_OK) {
@@ -203,6 +179,9 @@ static void at_uart_init(void)
203179
at_uart_workaround();
204180
#endif
205181

182+
// flush uart buffer to make sure no data left before AT start
183+
at_uart_flush();
184+
206185
ESP_AT_LOGI(TAG, "AT cmd port:uart%d tx:%d rx:%d cts:%d rts:%d baudrate:%d",
207186
g_at_cmd_port, g_uart_port_pin.tx_pin, g_uart_port_pin.rx_pin,
208187
g_uart_port_pin.cts_pin, g_uart_port_pin.rts_pin, config.baud_rate);

0 commit comments

Comments
 (0)