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

Commit ad55267

Browse files
author
Islam Wahdan
authored
esp32/modlte: Added factory modem reset option , method to check UE coverage and Bug fixes (#17)
1 parent 98000bb commit ad55267

File tree

3 files changed

+109
-53
lines changed

3 files changed

+109
-53
lines changed

esp32/lte/lteppp.c

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,29 @@ static uint32_t lteppp_output_callback(ppp_pcb *pcb, u8_t *data, u32_t len, void
8989
/******************************************************************************
9090
DEFINE PUBLIC FUNCTIONS
9191
******************************************************************************/
92-
void connect_lte_uart (void) {
9392

94-
// bool success = uart_driver_delete(LTE_UART_ID);
95-
// vTaskDelay(5 / portTICK_RATE_MS);
96-
// printf("Driver deleted? %d\n", success);
93+
void disconnect_lte_uart (void) {
94+
uart_driver_delete(LTE_UART_ID);
95+
vTaskDelay(5 / portTICK_RATE_MS);
96+
97+
//deassign LTE Uart pins
98+
pin_deassign(MICROPY_LTE_TX_PIN);
99+
gpio_pullup_dis(MICROPY_LTE_TX_PIN->pin_number);
100+
101+
pin_deassign(MICROPY_LTE_RX_PIN);
102+
gpio_pullup_dis(MICROPY_LTE_RX_PIN->pin_number);
103+
104+
pin_deassign(MICROPY_LTE_CTS_PIN);
105+
gpio_pullup_dis(MICROPY_LTE_CTS_PIN->pin_number);
106+
107+
pin_deassign(MICROPY_LTE_RTS_PIN);
108+
gpio_pullup_dis(MICROPY_LTE_RTS_PIN->pin_number);
109+
110+
vTaskDelay(5 / portTICK_RATE_MS);
111+
112+
}
113+
114+
void connect_lte_uart (void) {
97115

98116
// initialize the UART interface
99117
uart_config_t config;
@@ -105,21 +123,6 @@ void connect_lte_uart (void) {
105123
config.rx_flow_ctrl_thresh = 64;
106124
uart_param_config(LTE_UART_ID, &config);
107125

108-
// //deassign LTE Uart pins
109-
// pin_deassign(MICROPY_LTE_TX_PIN);
110-
// gpio_pullup_dis(MICROPY_LTE_TX_PIN->pin_number);
111-
//
112-
// pin_deassign(MICROPY_LTE_RX_PIN);
113-
// gpio_pullup_dis(MICROPY_LTE_RX_PIN->pin_number);
114-
//
115-
// pin_deassign(MICROPY_LTE_CTS_PIN);
116-
// gpio_pullup_dis(MICROPY_LTE_CTS_PIN->pin_number);
117-
//
118-
// pin_deassign(MICROPY_LTE_RTS_PIN);
119-
// gpio_pullup_dis(MICROPY_LTE_RTS_PIN->pin_number);
120-
//
121-
// vTaskDelay(5 / portTICK_RATE_MS);
122-
123126
// configure the UART pins
124127
pin_config(MICROPY_LTE_TX_PIN, -1, U2TXD_OUT_IDX, GPIO_MODE_OUTPUT, MACHPIN_PULL_NONE, 1);
125128
pin_config(MICROPY_LTE_RX_PIN, U2RXD_IN_IDX, -1, GPIO_MODE_INPUT, MACHPIN_PULL_NONE, 1);
@@ -222,8 +225,6 @@ bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_m
222225

223226
uint32_t rx_len = 0;
224227

225-
//printf("Expected Response: %s\n", expected_rsp);
226-
227228
// wait until characters start arriving
228229
do {
229230
// being called from the MicroPython interpreter
@@ -237,7 +238,6 @@ bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_m
237238
if (timeout > 0) {
238239
timeout--;
239240
}
240-
//printf("Timeout: %d, rx_len: %d\n", timeout, rx_len);
241241
} while (timeout > 0 && 0 == rx_len);
242242

243243
memset(lteppp_trx_buffer, 0, sizeof(lteppp_trx_buffer));
@@ -448,8 +448,6 @@ static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const cha
448448
uint32_t cmd_len = strlen(cmd);
449449
// char tmp_buf[128];
450450

451-
//printf("at_cmd_exp: %s\n", cmd);
452-
453451
// flush the rx buffer first
454452
uart_flush(LTE_UART_ID);
455453
// uart_read_bytes(LTE_UART_ID, (uint8_t *)tmp_buf, sizeof(tmp_buf), 5 / portTICK_RATE_MS);

esp32/lte/lteppp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ bool lteppp_is_modem_connected(void);
101101

102102
extern void connect_lte_uart (void);
103103

104+
extern void disconnect_lte_uart (void);
105+
104106
extern bool ltepp_is_ppp_conn_up(void);
105107

106108
extern void lteppp_suspend(void);

esp32/mods/modlte.c

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "driver/gpio.h"
6767

6868
#include "pycom_config.h"
69+
#include "modmachine.h"
6970

7071
/******************************************************************************
7172
DEFINE TYPES
@@ -92,6 +93,8 @@ uart_config_t lte_uart_config1;
9293

9394
static bool lte_legacyattach_flag = true;
9495

96+
static bool lte_ue_is_out_of_coverage = false;
97+
9598
extern TaskHandle_t xLTEUpgradeTaskHndl;
9699
extern TaskHandle_t mpTaskHandle;
97100
extern TaskHandle_t svTaskHandle;
@@ -221,8 +224,16 @@ static bool lte_check_attached(bool legacy) {
221224
attached = true;
222225
}
223226
} else {
224-
if ((pos = strstr(modlte_rsp.data, "+CEREG: 2,1,")) || (pos = strstr(modlte_rsp.data, "+CEREG: 2,5,"))) {
227+
if ((pos = strstr(modlte_rsp.data, "+CEREG: 2,1,")) || (pos = strstr(modlte_rsp.data, "+CEREG: 2,5,")) || (pos = strstr(modlte_rsp.data, "+CEREG: 2,4"))) {
225228
attached = true;
229+
if((pos = strstr(modlte_rsp.data, "+CEREG: 2,4")))
230+
{
231+
lte_ue_is_out_of_coverage = true;
232+
}
233+
else
234+
{
235+
lte_ue_is_out_of_coverage = false;
236+
}
226237
} else {
227238
attached = false;
228239
}
@@ -337,7 +348,6 @@ static void TASK_LTE_UPGRADE(void *pvParameters){
337348
// Micro Python bindings; LTE class
338349

339350
static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
340-
//printf("This is lte_init_helper()\n");
341351
char at_cmd[LTE_AT_CMD_SIZE_MAX - 4];
342352
mod_network_register_nic(&lte_obj);
343353
connect_lte_uart();
@@ -363,7 +373,7 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
363373

364374
lte_push_at_command("AT!=\"setlpm airplane=1 enable=1\"", LTE_RX_TIMEOUT_MIN_MS);
365375
lte_push_at_command("AT+CFUN?", LTE_RX_TIMEOUT_MIN_MS);
366-
if (strstr(modlte_rsp.data, "+CFUN: 0")) {
376+
if (strstr(modlte_rsp.data, "+CFUN: 0") || strstr(modlte_rsp.data, "+CFUN: 4")) {
367377
const char *carrier = "standard";
368378
lte_obj.carrier = false;
369379
if (args[0].u_obj != mp_const_none) {
@@ -384,6 +394,11 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
384394
if (!strstr(modlte_rsp.data, carrier)) {
385395
sprintf(at_cmd, "AT+SQNCTM=\"%s\"", carrier);
386396
lte_push_at_command(at_cmd, LTE_RX_TIMEOUT_MAX_MS);
397+
lteppp_wait_at_rsp("+S", LTE_RX_TIMEOUT_MAX_MS, true);
398+
mp_hal_delay_ms(LTE_RX_TIMEOUT_MIN_MS);
399+
if (!lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true)) {
400+
lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true);
401+
}
387402
lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS);
388403
lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS);
389404
}
@@ -473,6 +488,7 @@ STATIC mp_obj_t lte_deinit(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
473488
lteppp_deinit();
474489
lte_obj.init = false;
475490
vTaskDelay(100);
491+
disconnect_lte_uart();
476492
return mp_const_none;
477493
}
478494
}
@@ -522,6 +538,7 @@ STATIC mp_obj_t lte_deinit(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
522538
}
523539
lteppp_deinit();
524540
mod_network_deregister_nic(&lte_obj);
541+
disconnect_lte_uart();
525542
return mp_const_none;
526543

527544
error:
@@ -555,46 +572,47 @@ STATIC mp_obj_t lte_attach(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
555572
lte_check_attached(lte_legacyattach_flag);
556573

557574
if (lteppp_get_state() < E_LTE_ATTACHING) {
558-
// configuring scanning in all 6 bands
559-
lte_push_at_command("AT!=\"clearscanconfig\"", LTE_RX_TIMEOUT_MIN_MS);
560-
if (args[0].u_obj == mp_const_none) {
561-
if (!lte_obj.carrier) {
562-
lte_push_at_command("AT!=\"RRC::addScanBand band=3\"", LTE_RX_TIMEOUT_MIN_MS);
563-
lte_push_at_command("AT!=\"RRC::addScanBand band=4\"", LTE_RX_TIMEOUT_MIN_MS);
564-
lte_push_at_command("AT!=\"RRC::addScanBand band=5\"", LTE_RX_TIMEOUT_MIN_MS);
565-
lte_push_at_command("AT!=\"RRC::addScanBand band=8\"", LTE_RX_TIMEOUT_MIN_MS);
566-
lte_push_at_command("AT!=\"RRC::addScanBand band=12\"", LTE_RX_TIMEOUT_MIN_MS);
567-
lte_push_at_command("AT!=\"RRC::addScanBand band=13\"", LTE_RX_TIMEOUT_MIN_MS);
568-
lte_push_at_command("AT!=\"RRC::addScanBand band=20\"", LTE_RX_TIMEOUT_MIN_MS);
569-
lte_push_at_command("AT!=\"RRC::addScanBand band=28\"", LTE_RX_TIMEOUT_MIN_MS);
570-
}
571-
} else {
572-
uint32_t band = mp_obj_get_int(args[0].u_obj);
573-
if (band == 3) {
575+
576+
if (!lte_obj.carrier) {
577+
// configuring scanning in all bands
578+
lte_push_at_command("AT!=\"clearscanconfig\"", LTE_RX_TIMEOUT_MIN_MS);
579+
if (args[0].u_obj == mp_const_none) {
574580
lte_push_at_command("AT!=\"RRC::addScanBand band=3\"", LTE_RX_TIMEOUT_MIN_MS);
575-
} else if (band == 4) {
576581
lte_push_at_command("AT!=\"RRC::addScanBand band=4\"", LTE_RX_TIMEOUT_MIN_MS);
577-
} else if (band == 5) {
578582
lte_push_at_command("AT!=\"RRC::addScanBand band=5\"", LTE_RX_TIMEOUT_MIN_MS);
579-
} else if (band == 8) {
580583
lte_push_at_command("AT!=\"RRC::addScanBand band=8\"", LTE_RX_TIMEOUT_MIN_MS);
581-
} else if (band == 12) {
582584
lte_push_at_command("AT!=\"RRC::addScanBand band=12\"", LTE_RX_TIMEOUT_MIN_MS);
583-
} else if (band == 13) {
584585
lte_push_at_command("AT!=\"RRC::addScanBand band=13\"", LTE_RX_TIMEOUT_MIN_MS);
585-
} else if (band == 20) {
586586
lte_push_at_command("AT!=\"RRC::addScanBand band=20\"", LTE_RX_TIMEOUT_MIN_MS);
587-
} else if (band == 28) {
588587
lte_push_at_command("AT!=\"RRC::addScanBand band=28\"", LTE_RX_TIMEOUT_MIN_MS);
589588
} else {
590-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported", band));
589+
uint32_t band = mp_obj_get_int(args[0].u_obj);
590+
if (band == 3) {
591+
lte_push_at_command("AT!=\"RRC::addScanBand band=3\"", LTE_RX_TIMEOUT_MIN_MS);
592+
} else if (band == 4) {
593+
lte_push_at_command("AT!=\"RRC::addScanBand band=4\"", LTE_RX_TIMEOUT_MIN_MS);
594+
} else if (band == 5) {
595+
lte_push_at_command("AT!=\"RRC::addScanBand band=5\"", LTE_RX_TIMEOUT_MIN_MS);
596+
} else if (band == 8) {
597+
lte_push_at_command("AT!=\"RRC::addScanBand band=8\"", LTE_RX_TIMEOUT_MIN_MS);
598+
} else if (band == 12) {
599+
lte_push_at_command("AT!=\"RRC::addScanBand band=12\"", LTE_RX_TIMEOUT_MIN_MS);
600+
} else if (band == 13) {
601+
lte_push_at_command("AT!=\"RRC::addScanBand band=13\"", LTE_RX_TIMEOUT_MIN_MS);
602+
} else if (band == 20) {
603+
lte_push_at_command("AT!=\"RRC::addScanBand band=20\"", LTE_RX_TIMEOUT_MIN_MS);
604+
} else if (band == 28) {
605+
lte_push_at_command("AT!=\"RRC::addScanBand band=28\"", LTE_RX_TIMEOUT_MIN_MS);
606+
} else {
607+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "band %d not supported", band));
608+
}
591609
}
592610
}
593611
if (args[3].u_obj != mp_const_none) {
594612
lte_obj.cid = args[3].u_int;
595613
}
596614

597-
if (args[1].u_obj != mp_const_none || args[4].u_obj != mp_const_none) {
615+
if (!lte_obj.carrier && (args[1].u_obj != mp_const_none || args[4].u_obj != mp_const_none)) {
598616

599617
const char* strapn;
600618
const char* strtype;
@@ -837,7 +855,7 @@ STATIC mp_obj_t lte_resume(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
837855
} else if (lteppp_get_state() == E_LTE_PPP) {
838856
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem already connected"));
839857
} else {
840-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem not attached"));
858+
//nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem not attached"));
841859
}
842860
return mp_const_none;
843861
}
@@ -977,6 +995,20 @@ STATIC mp_obj_t lte_iccid(mp_obj_t self_in) {
977995
}
978996
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_iccid_obj, lte_iccid);
979997

998+
STATIC mp_obj_t lte_ue_coverage(mp_obj_t self_in) {
999+
lte_check_init();
1000+
lte_check_inppp();
1001+
if(lte_check_attached(lte_legacyattach_flag))
1002+
{
1003+
return lte_ue_is_out_of_coverage?mp_const_false:mp_const_true;
1004+
}
1005+
else
1006+
{
1007+
return mp_const_false;
1008+
}
1009+
}
1010+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_ue_coverage_obj, lte_ue_coverage);
1011+
9801012
STATIC mp_obj_t lte_reset(mp_obj_t self_in) {
9811013
lte_check_init();
9821014
lte_disconnect(self_in);
@@ -994,6 +1026,28 @@ STATIC mp_obj_t lte_reset(mp_obj_t self_in) {
9941026
}
9951027
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_reset_obj, lte_reset);
9961028

1029+
STATIC mp_obj_t lte_factory_reset(mp_obj_t self_in) {
1030+
lte_check_init();
1031+
lte_disconnect(self_in);
1032+
lte_obj.init = false;
1033+
if (!lte_push_at_command("AT&F", LTE_RX_TIMEOUT_MAX_MS * 2)) {
1034+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
1035+
}
1036+
lte_push_at_command("AT^RESET", LTE_RX_TIMEOUT_MAX_MS);
1037+
lteppp_set_state(E_LTE_IDLE);
1038+
mp_hal_delay_ms(LTE_RX_TIMEOUT_MIN_MS);
1039+
if (!lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true)) {
1040+
lteppp_wait_at_rsp("+SYSSTART", LTE_RX_TIMEOUT_MAX_MS, true);
1041+
}
1042+
lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS);
1043+
if (!lte_push_at_command("AT", LTE_RX_TIMEOUT_MAX_MS)) {
1044+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
1045+
}
1046+
machine_reset();
1047+
return mp_const_none;
1048+
}
1049+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_factory_reset_obj, lte_factory_reset);
1050+
9971051
STATIC mp_obj_t lte_upgrade_mode(void) {
9981052

9991053
if(lte_obj.init)
@@ -1098,8 +1152,10 @@ STATIC const mp_map_elem_t lte_locals_dict_table[] = {
10981152
{ MP_OBJ_NEW_QSTR(MP_QSTR_iccid), (mp_obj_t)&lte_iccid_obj },
10991153
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_at_cmd), (mp_obj_t)&lte_send_at_cmd_obj },
11001154
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&lte_reset_obj },
1155+
{ MP_OBJ_NEW_QSTR(MP_QSTR_factory_reset), (mp_obj_t)&lte_factory_reset_obj },
11011156
{ MP_OBJ_NEW_QSTR(MP_QSTR_modem_upgrade_mode), (mp_obj_t)&lte_upgrade_mode_obj },
11021157
{ MP_OBJ_NEW_QSTR(MP_QSTR_reconnect_uart), (mp_obj_t)&lte_reconnect_uart_obj },
1158+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ue_coverage), (mp_obj_t)&lte_ue_coverage_obj },
11031159

11041160
// class constants
11051161
{ MP_OBJ_NEW_QSTR(MP_QSTR_IP), MP_OBJ_NEW_QSTR(MP_QSTR_IP) },

0 commit comments

Comments
 (0)