8000 Merging Fixes and updates from dev/master #262 · pycom/pycom-micropython-sigfox@07a52e4 · 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 07a52e4

Browse files
author
Islam Wahdan
authored
Merging Fixes and updates from dev/master #262
* esp32/modlte: Added factory modem reset option , method to check UE coverage and Bug fixes (#17) * Esp32/frozen/MQTTlib: Gracefully handle EAGAIN return of Socket.write * esp32/mpthreadport.c: fix an issue in cleaning up user threads during of soft-reset (#14) Fixes issue [PYFW-197] # Conflicts: # esp32/mpthreadport.c * esp32/mphal: Fix issue causing Device crash when safe booting (CTRL-F) while BLE/Wlan enabled (#15) # Conflicts: # esp32/mods/modwlan.c * esp32/modlte: minor fix * esp32/make: Fix makefile bug excluding SIPY FIPY LOPY4 from secure Boot process
1 parent 42cd2ea commit 07a52e4

File tree

8 files changed

+161
-69
lines changed

8 files changed

+161
-69
lines changed

esp32/application.mk

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,7 @@ $(BUILD)/application.a: $(OBJ)
554554
$(ECHO) "AR $@"
555555
$(Q) rm -f $@
556556
$(Q) $(AR) cru $@ $^
557-
ifeq ($(BOARD), $(filter $(BOARD), SIPY FIPY LOPY4))
558-
$(BUILD)/application.elf: $(BUILD)/application.a $(BUILD)/esp32_out.ld
559-
$(ECHO) "LINK $@"
560-
$(Q) $(CC) $(APP_LDFLAGS) $(APP_LIBS) -o $@
561-
$(Q) $(SIZE) $@
562-
else
557+
563558
$(BUILD)/application.elf: $(BUILD)/application.a $(BUILD)/esp32_out.ld $(SECURE_BOOT_VERIFICATION_KEY)
564559
# $(ECHO) "COPY IDF LIBRARIES $@"
565560
# $(Q) $(PYTHON) get_idf_libs.py --idflibs $(IDF_PATH)/examples/wifi/scan/build
@@ -621,7 +616,6 @@ endif # feq ($(SECURE), on)
621616
$(BUILD)/esp32_out.ld: $(ESP_IDF_COMP_PATH)/esp32/ld/esp32.ld sdkconfig.h
622617
$(ECHO) "CPP $@"
623618
$(Q) $(CC) -I. -C -P -x c -E $< -o $@
624-
endif
625619

626620
flash: $(APP_BIN) $(BOOT_BIN)
627621
$(ECHO) "Entering flash mode"

esp32/frozen/Common/MQTTMsgHandler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ def _send_packet(self, packet):
222222
try:
223223
if self._sock:
224224
written = self._sock.write(packet)
225-
print('Packet sent. (Length: %d)' % written)
225+
if(written == None):
226+
written = -1
227+
else:
228+
print('Packet sent. (Length: %d)' % written)
226229
except socket.error as err:
227230
print('Socket send error {0}'.format(err))
228231
return False

esp32/hal/esp32_mphal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "modmachine.h"
3535
#include "updater.h"
3636
#include "bootloader.h"
37+
#include "modwlan.h"
38+
#include "modbt.h"
3739

3840
#include "freertos/FreeRTOS.h"
3941
#include "freertos/task.h"
@@ -190,6 +192,9 @@ void mp_hal_delay_ms(uint32_t delay) {
190192
void mp_hal_reset_safe_and_boot(bool reset) {
191193
boot_info_t boot_info;
192194
uint32_t boot_info_offset;
195+
/* Disable Wifi/BT to avoid cache region being accessed since it will be disabled when updating Safe boot flag in flash */
196+
wlan_deinit(NULL);
197+
bt_deinit(NULL);
193198
if (updater_read_boot_info (&boot_info, &boot_info_offset)) {
194199
boot_info.safeboot = SAFE_BOOT_SW;
195200
updater_write_boot_info (&boot_info, boot_info_offset);

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

9 F438 4-
// 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;
@@ -833,7 +851,7 @@ STATIC mp_obj_t lte_resume(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
833851
} else if (lteppp_get_state() == E_LTE_PPP) {
834852
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem already connected"));
835853
} else {
836-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem not attached"));
854+
//nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem not attached"));
837855
}
838856
return mp_const_none;
839857
}
@@ -973,6 +991,20 @@ STATIC mp_obj_t lte_iccid(mp_obj_t self_in) {
973991
}
974992
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_iccid_obj, lte_iccid);
975993

994+
STATIC mp_obj_t lte_ue_coverage(mp_obj_t self_in) {
995+
lte_check_init();
996+
lte_check_inppp();
997+
if(lte_check_attached(lte_legacyattach_flag))
998+
{
999+
return lte_ue_is_out_of_coverage?mp_const_false:mp_const_true;
1000+
}
1001+
else
1002+
{
1003+
return mp_const_false;
1004+
}
1005+
}
1006+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_ue_coverage_obj, lte_ue_coverage);
1007+
9761008
STATIC mp_obj_t lte_reset(mp_obj_t self_in) {
9771009
lte_check_init();
9781010
lte_disconnect(self_in);
@@ -990,6 +1022,28 @@ STATIC mp_obj_t lte_reset(mp_obj_t self_in) {
9901022
}
9911023
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_reset_obj, lte_reset);
9921024

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

9951049
if(lte_obj.init)
@@ -1094,8 +1148,10 @@ STATIC const mp_map_elem_t lte_locals_dict_table[] = {
7119
10941148
{ MP_OBJ_NEW_QSTR(MP_QSTR_iccid), (mp_obj_t)&lte_iccid_obj },
10951149
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_at_cmd), (mp_obj_t)&lte_send_at_cmd_obj },
10961150
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&lte_reset_obj },
1151+
{ MP_OBJ_NEW_QSTR(MP_QSTR_factory_reset), (mp_obj_t)&lte_factory_reset_obj },
10971152
{ MP_OBJ_NEW_QSTR(MP_QSTR_modem_upgrade_mode), (mp_obj_t)&lte_upgrade_mode_obj },
10981153
{ MP_OBJ_NEW_QSTR(MP_QSTR_reconnect_uart), (mp_obj_t)&lte_reconnect_uart_obj },
1154+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ue_coverage), (mp_obj_t)&lte_ue_coverage_obj },
10991155

11001156
// class constants
11011157
{ MP_OBJ_NEW_QSTR(MP_QSTR_IP), MP_OBJ_NEW_QSTR(MP_QSTR_IP) },

0 commit comments

Comments
 (0)
0