8000 Update to LTE module (#12) · pycom/pycom-micropython-sigfox@42f4108 · GitHub
[go: up one dir, main page]

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

Commit 42f4108

Browse files
author
Islam Wahdan
authored
Update to LTE module (#12)
* Update modlte.c * esp32/modlte: Refactoring
1 parent 81573b3 commit 42f4108

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

esp32/mods/modlte.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ uart_dev_t* uart_driver_lte = &UART2;
9090
uart_config_t lte_uart_config0;
9191
uart_config_t lte_uart_config1;
9292

93-
static bool lte_legacyattach_flag = false;
93+
static bool lte_legacyattach_flag = true;
9494

9595
extern TaskHandle_t xLTEUpgradeTaskHndl;
9696
extern TaskHandle_t mpTaskHandle;
@@ -115,6 +115,7 @@ static bool lte_check_attached(bool legacy);
115115
static void lte_check_init(void);
116116
static bool lte_check_sim_present(void);
117117
STATIC mp_obj_t lte_suspend(mp_obj_t self_in);
118+
STATIC mp_obj_t lte_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
118119

119120
STATIC mp_obj_t lte_deinit(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
120121
STATIC mp_obj_t lte_disconnect(mp_obj_t self_in);
@@ -131,13 +132,11 @@ void modlte_init0(void) {
131132
// DEFINE STATIC FUNCTIONS
132133
//*****************************************************************************
133134

134-
static bool lte_push_at_command_ext (char *cmd_str, uint32_t timeout, const char *expected_rsp) {
135-
lte_task_cmd_data_t cmd = {
136-
.timeout = timeout
137-
};
135+
static bool lte_push_at_command_ext(char *cmd_str, uint32_t timeout, const char *expected_rsp) {
136+
lte_task_cmd_data_t cmd = { .timeout = timeout };
138137
memcpy(cmd.data, cmd_str, strlen(cmd_str));
139-
//printf("[CMD] %s\n", cmd_str);
140-
lteppp_send_at_command (&cmd, &modlte_rsp);
138+
//printf("[CMD] %s\n", cmd_str);
139+
lteppp_send_at_command(&cmd, &modlte_rsp);
141140
if (strstr(modlte_rsp.data, expected_rsp) != NULL) {
142141
//printf("[OK] %s\n", modlte_rsp.data);
143142
return true;
@@ -147,14 +146,12 @@ static bool lte_push_at_command_ext (char *cmd_str, uint32_t timeout, const char
147146
}
148147

149148
static bool lte_push_at_command_delay_ext (char *cmd_str, uint32_t timeout, const char *expected_rsp, TickType_t delay) {
150-
lte_task_cmd_data_t cmd = {
151-
.timeout = timeout
152-
};
149+
lte_task_cmd_data_t cmd = { .timeout = timeout };
153150
memcpy(cmd.data, cmd_str, strlen(cmd_str));
154-
//printf("[CMD] %s\n", cmd_str);
155-
lteppp_send_at_command_delay (&cmd, &modlte_rsp, delay);
151+
//printf("[CMD] %s\n", cmd_str);
152+
lteppp_send_at_command_delay(&cmd, &modlte_rsp, delay);
156153
if (strstr(modlte_rsp.data, expected_rsp) != NULL) {
157-
//printf("[OK] %s\n", modlte_rsp.data);
154+
//printf("[OK] %s\n", modlte_rsp.data);
158155
return true;
159156
}
160157
//printf("[FAIL] %s\n", modlte_rsp.data);
@@ -215,6 +212,10 @@ static bool lte_check_attached(bool legacy) {
215212
if (legacy) {
216213
lte_push_at_command("AT+CEREG?", LTE_RX_TIMEOUT_MIN_MS);
217214
if (!cgatt) {
215+
if (strstr(modlte_rsp.data, "ERROR")) {
216+
mp_hal_delay_ms(LTE_RX_TIMEOUT_MIN_MS);
217+
lte_push_at_command("AT+CEREG?", LTE_RX_TIMEOUT_MIN_MS);
218+
}
218219
if (((pos = strstr(modlte_rsp.data, "+CEREG: 2,1,")) || (pos = strstr(modlte_rsp.data, "+CEREG: 2,5,")))
219220
&& (strlen(pos) >= 31) && (pos[30] == '7' || pos[30] == '9')) {
220221
attached = true;
@@ -232,9 +233,9 @@ static bool lte_check_attached(bool legacy) {
232233
attached = cgatt;
233234
}
234235
lte_push_at_command("AT+CFUN?", LTE_RX_TIMEOUT_MIN_MS);
235-
if (lteppp_get_state() == E_LTE_ATTACHING) {
236-
// for some reason the modem has crashed, enabled the radios again...
236+
if (lteppp_get_state() >= E_LTE_ATTACHING) {
237237
if (!strstr(modlte_rsp.data, "+CFUN: 1")) {
238+
//for some reason the modem has crashed, enabled the radios again...
238239
lte_push_at_command("AT+CFUN=1", LTE_RX_TIMEOUT_MIN_MS);
239240
}
240241
} else {
@@ -248,16 +249,21 @@ static bool lte_check_attached(bool legacy) {
248249
if (attached && lteppp_get_state() < E_LTE_PPP) {
249250
lteppp_set_state(E_LTE_ATTACHED);
250251
}
251-
//printf("This is our current LTE state: %d\n", lteppp_get_state());
252-
//printf("This is check_attached returning %d\n", attached);
252+
if (!attached && lteppp_get_state() > E_LTE_IDLE) {
253+
lteppp_set_state(E_LTE_ATTACHING);
254+
}
253255
return attached;
254256
}
255257

256258
static bool lte_check_legacy_version(void) {
257259
if (lte_push_at_command("ATI1", LTE_RX_TIMEOUT_MAX_MS)) {
258260
return strstr(modlte_rsp.data, "LR5.1.1.0-33080");
259261
} else {
260-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "LTE modem version not read"));
262+
if (lte_push_at_command("ATI1", LTE_RX_TIMEOUT_MAX_MS)) {
263+
return strstr(modlte_rsp.data, "LR5.1.1.0-33080");
264+
} else {
265+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "LTE modem version not read"));
266+
}
261267
}
262268
return true;
263269
}
@@ -278,7 +284,12 @@ static void lte_check_inppp(void) {
278284
static bool lte_check_sim_present(void) {
279285
lte_push_at_command("AT+CPIN?", LTE_RX_TIMEOUT_MAX_MS);
280286
if (strstr(modlte_rsp.data, "ERROR")) {
281-
return false;
287+
lte_push_at_command("AT+CPIN?", LTE_RX_TIMEOUT_MAX_MS);
288+
if (strstr(modlte_rsp.data, "ERROR")) {
289+
return false;
290+
} else {
291+
return true;
292+
}
282293
} else {
283294
return true;
284295
}
@@ -390,9 +401,9 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
390401

391402
static const mp_arg_t lte_init_args[] = {
392403
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
393-
{ MP_QSTR_carrier, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
394-
{ MP_QSTR_cid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
395-
{ MP_QSTR_legacyattach, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false}}
404+
{ MP_QSTR_carrier, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
405+
{ MP_QSTR_cid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
406+
{ MP_QSTR_legacyattach, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }
396407
};
397408

398409
static mp_obj_t lte_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
@@ -524,12 +535,12 @@ STATIC mp_obj_t lte_attach(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
524535
lte_check_init();
525536

526537
STATIC const mp_arg_t allowed_args[] = {
527-
{ MP_QSTR_band, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
528-
{ MP_QSTR_apn, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
529-
{ MP_QSTR_log, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} },
530-
{ MP_QSTR_cid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = mp_const_none} },
531-
{ MP_QSTR_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
532-
{ MP_QSTR_legacyattach, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
538+
{ MP_QSTR_band, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
539+
{ MP_QSTR_apn, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
540+
{ MP_QSTR_log, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} },
541+
{ MP_QSTR_cid, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = mp_const_none} },
542+
{ MP_QSTR_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
543+
{ MP_QSTR_legacyattach, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
533544

534545
};
535546

@@ -813,7 +824,11 @@ STATIC mp_obj_t lte_resume(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
813824
lteppp_set_state(E_LTE_PPP);
814825
vTaskDelay(1500);
815826
} else {
816-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
827+
lteppp_disconnect();
828+
lteppp_set_state(E_LTE_ATTACHED);
829+
lte_check_attached(lte_legacyattach_flag);
830+
return lte_connect(n_args, pos_args, kw_args);
831+
//nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
817832
}
818833
} else if (lteppp_get_state() == E_LTE_PPP) {
819834
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem already connected"));
@@ -1080,7 +1095,7 @@ STATIC const mp_map_elem_t lte_locals_dict_table[] = {
10801095
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_at_cmd), (mp_obj_t)&lte_send_at_cmd_obj },
10811096
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&lte_reset_obj },
10821097
{ MP_OBJ_NEW_QSTR(MP_QSTR_modem_upgrade_mode), (mp_obj_t)&lte_upgrade_mode_obj },
1083-
{ MP_OBJ_NEW_QSTR(MP_QSTR_reconnect_uart), (mp_obj_t)&lte_reconnect_uart_obj },
1098+
{ MP_OBJ_NEW_QSTR(MP_QSTR_reconnect_uart), (mp_obj_t)&lte_reconnect_uart_obj },
10841099

10851100
// class constants
10861101
{ MP_OBJ_NEW_QSTR(MP_QSTR_IP), MP_OBJ_NEW_QSTR(MP_QSTR_IP) },

0 commit comments

Comments
 (0)
0