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

Commit e0c3cd8

Browse files
author
Islam Wahdan
authored
Bug fixes + Fix for LoRa Timer ticks drifting (#24) #278
* esp32/modussl: Releasing GIL when handshake is performed. * esp32/machspi: enabling 2nd SPI peref. On GPY boards * esp32/modwlan: Remove defaulting of wifi max_tx_pwr to 20 in init * esp32/modlte: Allow PDN list update when carrier is defined + updated sqns upgrade scripts to v1.2.4 * esp32/lte: add support for modem responses greater than UART buff size * esp32/modlte: Restrict scanning of and 5,8 for old Fipy/Gpy v1.0 * [PYFW-325] Use Hardware timers for Lora lib ticks #comment Initial Fix implementation * [PYFW-325] Use Hardware timers for Lora lib ticks #comment Hardware timer working, moved callback functions execution out of ISR context * [PYFW-325] Hardware timer for Lora ticks fully implemented #close #comment Fix tested
1 parent 815e27a commit e0c3cd8

File tree

13 files changed

+246
-90
lines changed

13 files changed

+246
-90
lines changed

esp32/frozen/LTE/sqnsupgrade.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
VERSION = "1.2.3"
2+
VERSION = "1.2.4"
33

44
# Copyright (c) 2019, Pycom Limited.
55
#
@@ -345,8 +345,10 @@ def __run(self, file_path=None, baudrate=921600, port=None, resume=False, load_f
345345
resume = True if mirror or recover or atneg_only or info_only else resume
346346
verbose = True if debug else verbose
347347
load_fff = False if bootrom and switch_ffh else load_fff
348+
target_baudrate = baudrate
348349
baudrate = self.__modem_speed if self.__speed_detected else baudrate
349350
if debug: print('mirror? {} recover? {} resume? {} direct? {} atneg_only? {} bootrom? {} load_fff? {}'.format(mirror, recover, resume, direct, atneg_only, bootrom, load_fff))
351+
if debug: print('baudrate: {} target_baudrate: {}'.format(baudrate, target_baudrate))
350352
abort = True
351353
external = False
352354
self.__serial = None
@@ -512,8 +514,11 @@ def __run(self, file_path=None, baudrate=921600, port=None, resume=False, load_f
512514
self.__serial.read()
513515
elif recover and (not direct):
514516
if atneg:
515-
result = self.at_negotiation(baudrate, port, max_try, mirror, atneg_only, debug)
517+
result = self.at_negotiation(baudrate, port, max_try, mirror, atneg_only, debug, target_baudrate)
516518
if result:
519+
baudrate = target_baudrate
520+
self.__modem_speed = target_baudrate
521+
self.__speed_detected = True
517522
if atneg_only:
518523
return True
519524
if mirror:
@@ -712,7 +717,11 @@ def wakeup_modem(self, baudrate, port, max_try, delay, debug, msg='Attempting AT
712717
MAX_TRY = max_try
713718
count = 0
714719
if msg is not None:
715-
print(msg)
720+
if debug:
721+
print(msg + 'with baudrate {}'.format(baudrate))
722+
else:
723+
print(msg)
724+
716725
self.__serial.read()
717726
self.__serial.write(b"AT\r\n")
718727
response = self.read_rsp(size=25)
@@ -729,10 +738,13 @@ def wakeup_modem(self, baudrate, port, max_try, delay, debug, msg='Attempting AT
729738
self.__serial = UART(1, baudrate=baudrate, pins=self.__pins, timeout_chars=100)
730739
return count < MAX_TRY
731740

732-
def at_negotiation(self, baudrate, port, max_try, mirror, atneg_only, debug):
741+
def at_negotiation(self, baudrate, port, max_try, mirror, atneg_only, debug, target_baudrate):
733742
MAX_TRY = max_try
734743
count = 0
735-
print('Attempting AT auto-negotiation...')
744+
if debug:
745+
print('Attempting AT auto-negotiation... with baudrate {} and target_baudrate {}'.format(baudrate, target_baudrate))
746+
else:
747+
print('Attempting AT auto-negotiation...')
736748
self.__serial.write(b"AT\r\n")
737749
response = self.read_rsp(size=20)
738750
if debug: print('{}'.format(response))
@@ -746,19 +758,21 @@ def at_negotiation(self, baudrate, port, max_try, mirror, atneg_only, debug):
746758
if debug: print('{}'.format(response))
747759
if b'OK' in response:
748760
self.__serial.read()
749-
cmd = "AT+IPR=%d\n"%baudrate
750-
if debug: print('Setting baudrate to {}'.format(baudrate))
761+
cmd = "AT+IPR=%d\n"%target_baudrate
762+
if debug: print('Setting baudrate to {}'.format(target_baudrate))
751763
self.__serial.write(cmd.encode())
752764
response = self.read_rsp(size=6)
753765
if debug: print('{}'.format(response))
754766
if b'OK' in response:
767+
self.__modem_speed = target_baudrate
768+
self.__speed_detected = True
755769
if atneg_only:
756770
return True
757771
if 'FiPy' in self.__sysname or 'GPy' in self.__sysname:
758-
self.__serial = UART(1, baudrate=baudrate, pins=self.__pins, timeout_chars=100)
772+
self.__serial = UART(1, baudrate=target_baudrate, pins=self.__pins, timeout_chars=100)
759773
else:
760774
self.__serial = None
761-
self.__serial = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, timeout=0.1)
775+
self.__serial = serial.Serial(port, target_baudrate, bytesize=serial.EIGHTBITS, timeout=0.1)
762776
self.__serial.reset_input_buffer()
763777
self.__serial.reset_output_buffer()
764778
self.__serial.flush()
@@ -774,7 +788,7 @@ def at_negotiation(self, baudrate, port, max_try, mirror, atneg_only, debug):
774788
print('ERROR in AT+SMOD returned {}'.format(response))
775789
return False
776790
else:
777-
print('ERROR in AT+IPR={} returned {}'.format(baudrate, response))
791+
print('ERROR in AT+IPR={} returned {}'.format(target_baudrate, response))
778792
return False
779793
else:
780794
print('ERROR sending AT command... no response? {}'.format(response))
@@ -839,8 +853,15 @@ def upgrade_uart(self, ffh_mode=False, mfile=None, retry=False, resume=False, co
839853
print('Preparing modem for upgrade...')
840854
if not retry and ffh_mode:
841855
success = False
842-
success = self.__run(bootrom=True, resume=resume, switch_ffh=True, direct=False, debug=debug, pkgdebug=pkgdebug, verbose=verbose)
843-
time.sleep(1)
856+
if self.__check_br(verbose=verbose, debug=debug):
857+
success = self.__run(bootrom=True, resume=resume, switch_ffh=True, direct=False, debug=debug, pkgdebug=pkgdebug, verbose=verbose)
858+
time.sleep(1)
859+
else:
860+
print('FFH mode is not necessary... ignoring!')
861+
print('Do not specify updater.elf when updating!')
862+
mfile = None
863+
ffh_mode = False
864+
success = True
844865
if success:
845866
if mfile is not None:
846867
success = False

esp32/hal/esp32_mphal.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,35 @@
3737
#include "modwlan.h"
3838
#include "modbt.h"
3939

40-
#include "freertos/FreeRTOS.h"
41-
#include "freertos/task.h"
42-
#include "freertos/semphr.h"
43-
#include "freertos/queue.h"
44-
#include "freertos/timers.h"
45-
#include "freertos/xtensa_api.h"
46-
40+
#include "driver/timer.h"
4741

42+
typedef void (*HAL_tick_user_cb_t)(void);
4843
#if defined (LOPY) || defined(LOPY4) || defined(FIPY)
49-
static void (*HAL_tick_user_cb)(void);
44+
DRAM_ATTR static HAL_tick_user_cb_t HAL_tick_user_cb;
45+
46+
#define TIMER1_ALARM_TIME_MS 1U
47+
#define TIMER1_DIVIDER 16U
48+
#define TIMER1_ALARM_COUNT ((uint64_t)TIMER_BASE_CLK * (uint64_t)TIMER1_ALARM_TIME_MS) / ((uint64_t)TIMER1_DIVIDER * (uint64_t)1000)
49+
5050
#endif
5151

52-
#define TIMER_TICKS 160000 // 1 ms @160MHz
5352

5453
#if defined (LOPY) || defined(LOPY4) || defined(FIPY)
55-
IRAM_ATTR static void HAL_TimerCallback (TimerHandle_t xTimer) {
56-
if (HAL_tick_user_cb) {
54+
IRAM_ATTR static void HAL_TimerCallback (void* arg) {
55+
56+
if (HAL_tick_user_cb != NULL) {
57+
5758
HAL_tick_user_cb();
5859
}
60+
61+
TIMERG0.int_clr_timers.t1 = 1;
62+
TIMERG0.hw_timer[1].update=1;
63+
TIMERG0.hw_timer[1].config.alarm_en = 1;
64+
5965
}
6066

6167
void HAL_set_tick_cb (void *cb) {
62-
HAL_tick_user_cb = cb;
68+
HAL_tick_user_cb = (HAL_tick_user_cb_t)cb;
6369
}
6470
#endif
6571

@@ -68,8 +74,30 @@ void mp_hal_init(bool soft_reset) {
6874
#if defined (LOPY) || defined(LOPY4) || defined(FIPY)
6975
// setup the HAL timer for LoRa
7076
HAL_tick_user_cb = NULL;
71-
TimerHandle_t hal_timer = xTimerCreate("HAL_Timer", 1 / portTICK_PERIOD_MS, pdTRUE, (void *) 0, HAL_TimerCallback);
72-
xTimerStart (hal_timer, 0);
77+
78+
timer_config_t config;
79+
80+
config.alarm_en = 1;
81+
config.auto_reload = 1;
82+
config.counter_dir = TIMER_COUNT_UP;
83+
config.divider = TIMER1_DIVIDER;
84+
config.intr_type = TIMER_INTR_LEVEL;
85+
config.counter_en = TIMER_PAUSE;
86+
/*Configure timer*/
87+
timer_init(TIMER_GROUP_0, TIMER_1, &config);
88+
/*Stop timer counter*/
89+
timer_pause(TIMER_GROUP_0, TIMER_1);
90+
/*Load counter value */
91+
timer_set_counter_value(TIMER_GROUP_0, TIMER_1, 0x00000000ULL);
92+
/*Set alarm value*/
93+
timer_set_alarm_value(TIMER_GROUP_0, TIMER_1, (uint64_t)TIMER1_ALARM_COUNT);
94+
/*Enable timer interrupt*/
95+
timer_enable_intr(TIMER_GROUP_0, TIMER_1);
96+
/* Register Interrupt */
97+
timer_isr_register(TIMER_GROUP_0, TIMER_1, HAL_TimerCallback, NULL, ESP_INTR_FLAG_IRAM, NULL);
98+
/* Start Timer */
99+
timer_start(TIMER_GROUP_0, TIMER_1);
100+
73101
#endif
74102
}
75103
}

esp32/lora/timer-board.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Maintainer: Miguel Luis and Gregory Cristian
4545
/*!
4646
* Hardware Timer tick counter
4747
*/
48-
volatile TimerTime_t TimerTickCounter = 1;
48+
DRAM_ATTR volatile TimerTime_t TimerTickCounter = 1;
4949

5050
/*!
5151
* Saved value of the Tick counter at the start of the next event
@@ -55,13 +55,18 @@ static TimerTime_t TimerTickCounterContext = 0;
5555
/*!
5656
* Value trigging the IRQ
5757
*/
58-
volatile TimerTime_t TimeoutCntValue = 0;
59-
58+
DRAM_ATTR volatile TimerTime_t TimeoutCntValue = 0;
59+
extern TaskHandle_t xLoRaTimerTaskHndl;
6060

6161
static IRAM_ATTR void TimerCallback (void) {
62+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
63+
6264
TimerTickCounter++;
6365
if (TimeoutCntValue > 0 && TimerTickCounter == TimeoutCntValue) {
6466
TimerIrqHandler();
67+
// Notify the thread so it will wake up when the ISR is complete
68+
vTaskNotifyGiveFromISR(xLoRaTimerTaskHndl, &xHigherPriorityTaskWoken);
69+
portYIELD_FROM_ISR();
6570
}
6671
}
6772

esp32/lte/lteppp.c

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern TaskHandle_t xLTETaskHndl;
5252
/******************************************************************************
5353
DECLARE PRIVATE DATA
5454
******************************************************************************/
55-
static char lteppp_trx_buffer[LTE_UART_BUFFER_SIZE];
55+
static char lteppp_trx_buffer[LTE_UART_BUFFER_SIZE + 1];
5656
static char lteppp_queue_buffer[LTE_UART_BUFFER_SIZE];
5757
static uart_dev_t* lteppp_uart_reg;
5858
static QueueHandle_t xCmdQueue;
@@ -80,7 +80,7 @@ static ltepppconnstatus_t lteppp_connstatus = LTE_PPP_IDLE;
8080
DECLARE PRIVATE FUNCTIONS
8181
******************************************************************************/
8282
static void TASK_LTE (void *pvParameters);
83-
static bool lteppp_send_at_cmd_exp(const char *cmd, uint32_t timeout, const char *expected_rsp);
83+
static bool lteppp_send_at_cmd_exp(const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem);
8484
static bool lteppp_send_at_cmd(const char *cmd, uint32_t timeout);
8585
static bool lteppp_check_sim_present(void);
8686
static void lteppp_status_cb (ppp_pcb *pcb, int err_code, void *ctx);
@@ -154,7 +154,7 @@ void lteppp_init(void) {
154154
lteppp_lte_state = E_LTE_INIT;
155155

156156
xCmdQueue = xQueueCreate(LTE_CMD_QUEUE_SIZE_MAX, sizeof(lte_task_cmd_data_t));
157-
xRxQueue = xQueueCreate(LTE_RSP_QUEUE_SIZE_MAX, LTE_AT_RSP_SIZE_MAX);
157+
xRxQueue = xQueueCreate(LTE_RSP_QUEUE_SIZE_MAX, LTE_AT_RSP_SIZE_MAX + 1);
158158

159159
xLTESem = xSemaphoreCreateMutex();
160160

@@ -221,7 +221,7 @@ void lteppp_send_at_command_delay (lte_task_cmd_data_t *cmd, lte_task_rsp_data_t
221221
xQueueReceive(xRxQueue, rsp, (TickType_t)portMAX_DELAY);
222222
}
223223

224-
bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp) {
224+
bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp, void* data_rem) {
225225

226226
uint32_t rx_len = 0;
227227

@@ -240,10 +240,13 @@ bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_m
240240
}
241241
} while (timeout > 0 && 0 == rx_len);
242242

243-
memset(lteppp_trx_buffer, 0, sizeof(lteppp_trx_buffer));
243+
memset(lteppp_trx_buffer, 0, LTE_UART_BUFFER_SIZE);
244+
uint16_t len_count = 0;
244245
while (rx_len > 0) {
245246
// 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)
246-
rx_len = uart_read_bytes(LTE_UART_ID, (uint8_t *)lteppp_trx_buffer, sizeof(lteppp_trx_buffer) - 2, LTE_TRX_WAIT_MS(sizeof(lteppp_trx_buffer)) / portTICK_RATE_MS);
247+
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);
248+
len_count += rx_len;
249+
247250
if (rx_len > 0) {
248251
// NULL terminate the string
249252
lteppp_trx_buffer[rx_len] = '\0';
@@ -255,8 +258,18 @@ bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_m
255258
}
256259
}
257260
uart_get_buffered_data_len(LTE_UART_ID, &rx_len);
261+
if((len_count + rx_len) >= (LTE_UART_BUFFER_SIZE - 2))
262+
{
263+
if (data_rem != NULL) {
264+
*((bool *)data_rem) = true;
265+
return true;
266+
}
267+
}
258268
}
259269
}
270+
if (data_rem != NULL) {
271+
*((bool *)data_rem) = false;
272+
}
260273
return false;
261274
}
262275

@@ -318,6 +331,7 @@ static void TASK_LTE (void *pvParameters) {
318331
lte_task_cmd_data_t *lte_task_cmd = (lte_task_cmd_data_t *)lteppp_trx_buffer;
319332
lte_task_rsp_data_t *lte_task_rsp = (lte_task_rsp_data_t *)lteppp_trx_buffer;
320333

334+
321335
connect_lte_uart();
322336

323337
while (!lteppp_enabled)
@@ -405,7 +419,7 @@ static void TASK_LTE (void *pvParameters) {
405419
for (;;) {
406420
vTaskDelay(LTE_TASK_PERIOD_MS);
407421
if (xQueueReceive(xCmdQueue, lteppp_trx_buffer, 0)) {
408-
lteppp_send_at_cmd_exp(lte_task_cmd->data, lte_task_cmd->timeout, NULL);
422+
lteppp_send_at_cmd_exp(lte_task_cmd->data, lte_task_cmd->timeout, NULL, &(lte_task_rsp->data_remaining));
409423
xQueueSend(xRxQueue, (void *)lte_task_rsp, (TickType_t)portMAX_DELAY);
410424
} else {
411425
lte_state_t state = lteppp_get_state();
@@ -428,8 +442,8 @@ static void TASK_LTE (void *pvParameters) {
428442
uart_get_buffered_data_len(LTE_UART_ID, &rx_len);
429443
if (rx_len > 0) {
430444
// try to read up to the size of the buffer
431-
rx_len = uart_read_bytes(LTE_UART_ID, (uint8_t *)lteppp_trx_buffer, sizeof(lteppp_trx_buffer),
432-
LTE_TRX_WAIT_MS(sizeof(lteppp_trx_buffer)) / portTICK_RATE_MS);
445+
rx_len = uart_read_bytes(LTE_UART_ID, (uint8_t *)lteppp_trx_buffer, LTE_UART_BUFFER_SIZE,
446+
LTE_TRX_WAIT_MS(LTE_UART_BUFFER_SIZE) / portTICK_RATE_MS);
433447
if (rx_len > 0) {
434448
pppos_input_tcpip(lteppp_pcb, (uint8_t *)lteppp_trx_buffer, rx_len);
435449
}
@@ -444,26 +458,33 @@ static void TASK_LTE (void *pvParameters) {
444458
}
445459

446460

447-
static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const char *expected_rsp) {
448-
uint32_t cmd_len = strlen(cmd);
449-
// char tmp_buf[128];
450-
451-
// flush the rx buffer first
452-
uart_flush(LTE_UART_ID);
453-
// uart_read_bytes(LTE_UART_ID, (uint8_t *)tmp_buf, sizeof(tmp_buf), 5 / portTICK_RATE_MS);
454-
// then send the command
455-
uart_write_bytes(LTE_UART_ID, cmd, cmd_len);
456-
if (strcmp(cmd, "+++")) {
457-
uart_write_bytes(LTE_UART_ID, "\r\n", 2);
461+
static bool lteppp_send_at_cmd_exp (const char *cmd, uint32_t timeout, const char *expected_rsp, void* data_rem) {
462+
if(strstr(cmd, "Pycom_Dummy") != NULL)
463+
{
464+
return lteppp_wait_at_rsp(expected_rsp, timeout, false, data_rem);
458465
}
459-
uart_wait_tx_done(LTE_UART_ID, LTE_TRX_WAIT_MS(cmd_len) / portTICK_RATE_MS);
460-
vTaskDelay(2 / portTICK_RATE_MS);
466+
else
467+
{
468+
uint32_t cmd_len = strlen(cmd);
469+
// char tmp_buf[128];
470+
471+
// flush the rx buffer first
472+
uart_flush(LTE_UART_ID);
473+
// uart_read_bytes(LTE_UART_ID, (uint8_t *)tmp_buf, sizeof(tmp_buf), 5 / portTICK_RATE_MS);
474+
// then send the command
475+
uart_write_bytes(LTE_UART_ID, cmd, cmd_len);
476+
if (strcmp(cmd, "+++")) {
477+
uart_write_bytes(LTE_UART_ID, "\r\n", 2);
478+
}
479+
uart_wait_tx_done(LTE_UART_ID, LTE_TRX_WAIT_MS(cmd_len) / portTICK_RATE_MS);
480+
vTaskDelay(2 / portTICK_RATE_MS);
461481

462-
return lteppp_wait_at_rsp(expected_rsp, timeout, false);
482+
return lteppp_wait_at_rsp(expected_rsp, timeout, false, data_rem);
483+
}
463484
}
464485

465486
static bool lteppp_send_at_cmd(const char *cmd, uint32_t timeout) {
466-
return lteppp_send_at_cmd_exp (cmd, timeout, LTE_OK_RSP);
487+
return lteppp_send_at_cmd_exp (cmd, timeout, LTE_OK_RSP, NULL);
467488
}
468489

469490
static bool lteppp_check_sim_present(void) {

esp32/lte/lteppp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ typedef struct {
5858
uint32_t timeout;
5959
char data[LTE_AT_CMD_SIZE_MAX - 4];
6060
} lte_task_cmd_data_t;
61-
61+
#pragma pack(1)
6262
typedef struct {
6363
char data[LTE_UART_BUFFER_SIZE];
64+
bool data_remaining;
6465
} lte_task_rsp_data_t;
66+
#pragma pack()
6567

6668
/******************************************************************************
6769
DECLARE PUBLIC FUNCTIONS
@@ -91,7 +93,7 @@ extern void lteppp_send_at_command (lte_task_cmd_data_t *cmd, lte_task_rsp_data_
9193

9294
extern void lteppp_send_at_command_delay (lte_task_cmd_data_t *cmd, lte_task_rsp_data_t *rsp, TickType_t delay);
9395

94-
extern bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp);
96+
extern bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp, void* data_rem);
9597

9698
extern bool lteppp_task_ready(void);
9799

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy