@@ -53,6 +53,9 @@ extern TaskHandle_t xLTETaskHndl;
5353 DECLARE PRIVATE DATA
5454 ******************************************************************************/
5555static char lteppp_trx_buffer [LTE_UART_BUFFER_SIZE + 1 ];
56+ #ifdef LTE_DEBUG_BUFF
57+ static lte_log_t lteppp_log ;
58+ #endif
5659static char lteppp_queue_buffer [LTE_UART_BUFFER_SIZE ];
5760static uart_dev_t * lteppp_uart_reg ;
5861static QueueHandle_t xCmdQueue ;
@@ -166,13 +169,34 @@ void lteppp_init(void) {
166169 xTaskCreatePinnedToCore (TASK_LTE , "LTE" , LTE_TASK_STACK_SIZE / sizeof (StackType_t ), NULL , LTE_TASK_PRIORITY , & xLTETaskHndl , 1 );
167170
168171 lteppp_connstatus = LTE_PPP_IDLE ;
172+ #ifdef LTE_DEBUG_BUFF
173+ lteppp_log .log = heap_caps_malloc (LTE_LOG_BUFF_SIZE , MALLOC_CAP_SPIRAM );
174+ #endif
169175}
170176
171177void lteppp_start (void ) {
172178 uart_set_hw_flow_ctrl (LTE_UART_ID , UART_HW_FLOWCTRL_CTS_RTS , 64 );
173179 vTaskDelay (5 );
174180}
175-
181+ #ifdef LTE_DEBUG_BUFF
182+ char * lteppp_get_log_buff (void )
183+ {
184+ if (lteppp_log .truncated )
185+ {
186+ if (lteppp_log .ptr < LTE_LOG_BUFF_SIZE - strlen ("\n********BUFFER WRAPAROUND********\n" ) - 1 )
187+ {
188+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), "\n********BUFFER WRAPAROUND********\n" , strlen ("\n********BUFFER WRAPAROUND********\n" ));
189+ lteppp_log .ptr += strlen ("\n********BUFFER WRAPAROUND********\n" );
190+ }
191+ lteppp_log .log [LTE_LOG_BUFF_SIZE - 1 ] = '\0' ;
192+ }
193+ else
194+ {
195+ lteppp_log .log [lteppp_log .ptr ] = '\0' ;
196+ }
197+ return lteppp_log .log ;
198+ }
199+ #endif
176200void lteppp_connect_modem (void ) {
177201
178202 lteppp_enabled = true;
@@ -242,46 +266,90 @@ bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_m
242266
243267 memset (lteppp_trx_buffer , 0 , LTE_UART_BUFFER_SIZE );
244268 uint16_t len_count = 0 ;
245- /* reset timeout to 1000ms to account for pause in response */
246- timeout_cnt = 1000 ;
247- bool pause = false;
248- while (rx_len > 0 || (pause && timeout_cnt > 0 )) {
249- // try to read up to the size of the buffer minus null terminator (minus 2 because we store the OK status in the last byte)
250- rx_len = uart_read_bytes (LTE_UART_ID , (uint8_t * )lteppp_trx_buffer , LTE_UART_BUFFER_SIZE - 2 , LTE_TRX_WAIT_MS (LTE_UART_BUFFER_SIZE ) / portTICK_RATE_MS );
269+
270+ while (rx_len > 0 ) {
271+ if (len_count == 0 ) {
272+ // try to read up to the size of the buffer minus null terminator (minus 2 because we store the OK status in the last byte)
273+ rx_len = uart_read_bytes (LTE_UART_ID , (uint8_t * )lteppp_trx_buffer , LTE_UART_BUFFER_SIZE - 2 , LTE_TRX_WAIT_MS (LTE_UART_BUFFER_SIZE ) / portTICK_RATE_MS );
274+ }
275+ else
276+ {
277+ // try to read up to the size of the buffer minus null terminator (minus 2 because we store the OK status in the last byte)
278+ rx_len = uart_read_bytes (LTE_UART_ID , (uint8_t * )(& (lteppp_trx_buffer [len_count ])), LTE_UART_BUFFER_SIZE - len_count - 2 , LTE_TRX_WAIT_MS (LTE_UART_BUFFER_SIZE ) / portTICK_RATE_MS );
279+ }
251280 len_count += rx_len ;
252281
253282 if (rx_len > 0 ) {
254283 // NULL terminate the string
255- lteppp_trx_buffer [rx_len ] = '\0' ;
256- /* Check for pause after start of response */
257- if (strcmp (lteppp_trx_buffer , "\r\n" ) == 0 )
258- {
259- pause = true;
284+ lteppp_trx_buffer [len_count ] = '\0' ;
285+ #ifdef LTE_DEBUG_BUFF
286+ if (lteppp_log .ptr < LTE_LOG_BUFF_SIZE - rx_len ) {
287+ if (len_count == rx_len ) {
288+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), "[RSP]: " , strlen ("[RSP]: " ));
289+ lteppp_log .ptr += strlen ("[RSP]: " );
290+ }
291+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), lteppp_trx_buffer , rx_len );
292+ lteppp_log .ptr += rx_len ;
293+ lteppp_log .log [lteppp_log .ptr ] = '\n' ;
294+ lteppp_log .ptr ++ ;
260295 }
261296 else
262297 {
263- pause = false;
298+ lteppp_log .ptr = 0 ;
299+ lteppp_log .truncated = true;
264300 }
301+ #endif
302+
265303 if (expected_rsp != NULL ) {
266304 if (strstr (lteppp_trx_buffer , expected_rsp ) != NULL ) {
267305 //printf("RESP: %s\n", lteppp_trx_buffer);
268306 return true;
269307 }
270308 }
309+
271310 uart_get_buffered_data_len (LTE_UART_ID , & rx_len );
311+
272312 if ((len_count + rx_len ) >= (LTE_UART_BUFFER_SIZE - 2 ))
273313 {
274314 if (data_rem != NULL ) {
275315 * ((bool * )data_rem ) = true;
276316 return true;
277317 }
278318 }
319+ else if (rx_len == 0 )
320+ {
321+ uint8_t timeout_buff = 10 ;
322+ while ((!strstr (lteppp_trx_buffer ,"\r\nOK\r\n" )) && (!strstr (lteppp_trx_buffer ,"\r\nERROR\r\n" )) && (!strstr (lteppp_trx_buffer ,"+SYSSTART" )) && (!strstr (lteppp_trx_buffer ,"\r\nCONNECT\r\n" )) &&
323+ rx_len == 0 && timeout_buff > 0 )
324+ {
325+ #ifdef LTE_DEBUG_BUFF
326+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), "[Waiting]:\n" , strlen ("[Waiting]:\n" ));
327+ lteppp_log .ptr += strlen ("[Waiting]:\n" );
328+ #endif
329+
330+ uart_get_buffered_data_len (LTE_UART_ID , & rx_len );
331+
332+ if (from_mp ) {
333+ mp_hal_delay_ms (100 );
334+ }
335+ else {
336+ vTaskDelay (100 / portTICK_RATE_MS );
337+ }
338+ timeout_buff -- ;
339+ }
340+ //check size again
341+ if ((len_count + rx_len ) >= (LTE_UART_BUFFER_SIZE - 2 ))
342+ {
343+ if (data_rem != NULL ) {
344+ * ((bool * )data_rem ) = true;
345+ return true;
346+ }
347+ }
348+ }
279349 }
280350 else
281351 {
282- if (timeout_cnt > 0 && pause ) {
283- timeout_cnt -- ;
284- }
352+ // Do Nothing
285353 }
286354 }
287355 if (data_rem != NULL ) {
@@ -479,13 +547,42 @@ static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const cha
479547
480548 if (strstr (cmd , "Pycom_Dummy" ) != NULL )
481549 {
550+ #ifdef LTE_DEBUG_BUFF
551+ if (lteppp_log .ptr < (LTE_LOG_BUFF_SIZE - strlen ("[CMD]: Dummy" ) + 1 ))
552+ {
553+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), "[CMD]: Dummy" , strlen ("[CMD]: Dummy" ));
554+ lteppp_log .ptr += strlen ("[CMD]: Dummy" );
555+ lteppp_log .log [lteppp_log .ptr ] = '\n' ;
556+ lteppp_log .ptr ++ ;
557+ }
558+ else
559+ {
560+ lteppp_log .ptr = 0 ;
561+ lteppp_log .truncated = true;
562+ }
563+ #endif
482564 return lteppp_wait_at_rsp (expected_rsp , timeout , false, data_rem );
483565 }
484566 else
485567 {
486568 uint32_t cmd_len = strlen (cmd );
487569 // char tmp_buf[128];
488-
570+ #ifdef LTE_DEBUG_BUFF
571+ if (lteppp_log .ptr < (LTE_LOG_BUFF_SIZE - strlen ("[CMD]:" ) - cmd_len + 1 ))
572+ {
573+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), "[CMD]:" , strlen ("[CMD]:" ));
574+ lteppp_log .ptr += strlen ("[CMD]:" );
575+ memcpy (& (lteppp_log .log [lteppp_log .ptr ]), cmd , cmd_len );
576+ lteppp_log .ptr += cmd_len ;
577+ lteppp_log .log [lteppp_log .ptr ] = '\n' ;
578+ lteppp_log .ptr ++ ;
579+ }
580+ else
581+ {
582+ lteppp_log .ptr = 0 ;
583+ lteppp_log .truncated = true;
584+ }
585+ #endif
489586 // flush the rx buffer first
490587 uart_flush (LTE_UART_ID );
491588 // uart_read_bytes(LTE_UART_ID, (uint8_t *)tmp_buf, sizeof(tmp_buf), 5 / portTICK_RATE_MS);
0 commit comments