@@ -35,42 +35,12 @@ at_uart_port_pins_t g_uart_port_pin;
3535
3636static 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
4441static 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
7646static 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+
9066static 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