Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit fe8afc3

Browse files
author
iwahdan88
committed
esp32/modlte: Bug fixes + added AT command logging Buffer debug option
1 parent 396d71b commit fe8afc3

File tree

4 files changed

+161
-25
lines changed

4 files changed

+161
-25
lines changed

esp32/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ OEM ?= 0
2222
BTYPE ?= release
2323

2424
BUILD = build/$(BOARD)/$(BTYPE)
25+
LTE_LOG_BUFF ?= 0
2526

2627
# by default Secure Boot and Flash Encryption are disabled
2728
SECURE ?= off
@@ -94,6 +95,12 @@ endif
9495
ifeq ($(BOARD), $(filter $(BOARD), SIPY))
9596
LIBS += sigfox/modsigfox_sipy.a
9697
endif
98+
# Enable or Disable LTE_LOG_BUFF
99+
ifeq ($(BOARD), $(filter $(BOARD), GPY FIPY))
100+
ifeq ($(LTE_LOG_BUFF),1)
101+
CFLAGS += -DLTE_LOG
102+
endif #ifeq ($(LTE_LOG_BUFF),1)
103+
endif #ifeq ($(BOARD), $(filter $(BOARD), GPY FIPY))
97104

98105
B_LIBS = -Lbootloader/lib -Lbootloader -L$(BUILD)/bootloader -L$(ESP_IDF_COMP_PATH)/esp32/ld \
99106
-L$(ESP_IDF_COMP_PATH)/esp32/lib -llog -lcore -lbootloader_support \

esp32/lte/lteppp.c

+114-17
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ extern TaskHandle_t xLTETaskHndl;
5353
DECLARE PRIVATE DATA
5454
******************************************************************************/
5555
static char lteppp_trx_buffer[LTE_UART_BUFFER_SIZE + 1];
56+
#ifdef LTE_DEBUG_BUFF
57+
static lte_log_t lteppp_log;
58+
#endif
5659
static char lteppp_queue_buffer[LTE_UART_BUFFER_SIZE];
5760
static uart_dev_t* lteppp_uart_reg;
5861
static 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

171177
void 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
176200
void 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);

esp32/lte/lteppp.h

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#define LTE_MUTEX_TIMEOUT (5050 / portTICK_RATE_MS)
3131
#define LTE_TASK_STACK_SIZE (3072)
3232
#define LTE_TASK_PRIORITY (6)
33+
#ifdef LTE_DEBUG_BUFF
34+
#define LTE_LOG_BUFF_SIZE (20 * 1024)
35+
#endif
3336

3437
/******************************************************************************
3538
DEFINE TYPES
@@ -54,6 +57,13 @@ typedef enum {
5457
E_LTE_CMD_PPP_EXIT
5558
} lte_task_cmd_t;
5659

60+
#ifdef LTE_DEBUG_BUFF
61+
typedef struct {
62+
char* log;
63+
uint16_t ptr;
64+
bool truncated;
65+
} lte_log_t;
66+
#endif
5767
typedef struct {
5868
uint32_t timeout;
5969
char data[LTE_AT_CMD_SIZE_MAX - 4];
@@ -110,5 +120,8 @@ extern bool ltepp_is_ppp_conn_up(void);
110120
extern void lteppp_suspend(void);
111121

112122
extern void lteppp_resume(void);
123+
#ifdef LTE_DEBUG_BUFF
124+
extern char* lteppp_get_log_buff(void);
125+
#endif
113126

114127
#endif // _LTEPPP_H_

esp32/mods/modlte.c

+27-8
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,11 @@ STATIC mp_obj_t lte_attach(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
612612
memcpy(cmd.data, "Pycom_Dummy", strlen("Pycom_Dummy"));
613613
while(modlte_rsp.data_remaining)
614614
{
615-
if((strstr(modlte_rsp.data, "17 bands") != NULL) && !is_hw_new_band_support)
616-
{
617-
is_hw_new_band_support = true;
615+
if (!is_hw_new_band_support) {
616+
if(strstr(modlte_rsp.data, "17 bands") != NULL)
617+
{
618+
is_hw_new_band_support = true;
619+
}
618620
}
619621
lteppp_send_at_command(&cmd, &modlte_rsp);
620622
}
@@ -654,7 +656,7 @@ STATIC mp_obj_t lte_attach(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
654656
}
655657
else if(version < SQNS_SW_5_8_BAND_SUPPORT)
656658
{
657-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported by current modem Firmware, please upgrade!", band));
659+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported by current modem Firmware [%d], please upgrade!", band, version));
658660
}
659661
break;
660662
case 1:
@@ -668,7 +670,7 @@ STATIC mp_obj_t lte_attach(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
668670
case 66:
669671
if(!is_sw_new_band_support)
670672
{
671-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported by current modem Firmware, please upgrade!", band));
673+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported by current modem Firmware [%d], please upgrade!", band, version));
672674
}
673675
if(!is_hw_new_band_support)
674676
{
@@ -1151,8 +1153,13 @@ STATIC mp_obj_t lte_factory_reset(mp_obj_t self_in) {
11511153
lte_push_at_command("AT^RESET", LTE_RX_TIMEOUT_MAX_MS);
11521154
lteppp_set_state(E_LTE_IDLE);
11531155
mp_hal_delay_ms(LTE_RX_TIMEOUT_MIN_MS);
1154-
if (!lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true, NULL)) {
1155-
lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true, NULL);
1156+
uint8_t timeout = 0;
1157+
while (!lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true, NULL)) {
1158+
if(timeout > 3)
1159+
{
1160+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
1161+
}
1162+
timeout++;
11561163
}
11571164
lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS);
11581165
if (!lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS)) {
@@ -1241,7 +1248,16 @@ STATIC mp_obj_t lte_upgrade_mode(void) {
12411248
return mp_const_none;
12421249
}
12431250
STATIC MP_DEFINE_CONST_FUN_OBJ_0(lte_upgrade_mode_obj, lte_upgrade_mode);
1244-
1251+
#ifdef LTE_DEBUG_BUFF
1252+
STATIC mp_obj_t lte_debug_buff(void) {
1253+
vstr_t vstr;
1254+
char* str_log = lteppp_get_log_buff();
1255+
vstr_init_len(&vstr, strlen(str_log));
1256+
strcpy(vstr.buf, str_log);
1257+
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
1258+
}
1259+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(lte_debug_buff_obj, lte_debug_buff);
1260+
#endif
12451261
STATIC mp_obj_t lte_reconnect_uart (void) {
12461262
connect_lte_uart();
12471263
lteppp_disconnect();
@@ -1271,6 +1287,9 @@ STATIC const mp_map_elem_t lte_locals_dict_table[] = {
12711287
{ MP_OBJ_NEW_QSTR(MP_QSTR_modem_upgrade_mode), (mp_obj_t)&lte_upgrade_mode_obj },
12721288
{ MP_OBJ_NEW_QSTR(MP_QSTR_reconnect_uart), (mp_obj_t)&lte_reconnect_uart_obj },
12731289
{ MP_OBJ_NEW_QSTR(MP_QSTR_ue_coverage), (mp_obj_t)&lte_ue_coverage_obj },
1290+
#ifdef LTE_DEBUG_BUFF
1291+
{ MP_OBJ_NEW_QSTR(MP_QSTR_debug_buff), (mp_obj_t)&lte_debug_buff_obj },
1292+
#endif
12741293

12751294
// class constants
12761295
{ MP_OBJ_NEW_QSTR(MP_QSTR_IP), MP_OBJ_NEW_QSTR(MP_QSTR_IP) },

0 commit comments

Comments
 (0)