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

Commit 1c79bea

Browse files
author
iwahdan88
committed
[PYFW-325] Use Hardware timers for Lora lib ticks #comment Hardware timer working, moved callback functions execution out of ISR context
# Conflicts: # esp32/mods/modlora.c # esp32/mods/modlora.h
1 parent 9ac68c2 commit 1c79bea

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

esp32/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ TaskHandle_t mpTaskHandle;
5757
TaskHandle_t svTaskHandle;
5858
#if defined(LOPY) || defined (LOPY4) || defined (FIPY)
5959
TaskHandle_t xLoRaTaskHndl;
60+
TaskHandle_t xLoRaTimerTaskHndl;
6061
#endif
6162
#if defined(SIPY) || defined (LOPY4) || defined (FIPY)
6263
TaskHandle_t xSigfoxTaskHndl;

esp32/mods/modlora.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ typedef struct {
230230
******************************************************************************/
231231
static QueueHandle_t xCmdQueue;
232232
static QueueHandle_t xRxQueue;
233+
static QueueHandle_t xCbQueue;
233234
static EventGroupHandle_t LoRaEvents;
234235

235236
static RadioEvents_t RadioEvents;
@@ -249,7 +250,7 @@ static const char *modlora_nvs_data_key[E_LORA_NVS_NUM_KEYS] = { "JOINED", "UPLN
249250
"MACPARAMS", "CHANNELS", "SRVACK", "MACNXTTX",
250251
"MACBUFIDX", "MACRPTIDX", "MACBUF", "MACRPTBUF",
251252
"REGION", "CHANMASK", "CHANMASKREM" };
252-
253+
DRAM_ATTR static modlora_timerCallback modlora_timer_cb;
253254
/******************************************************************************
254255
DECLARE PUBLIC DATA
255256
******************************************************************************/
@@ -259,6 +260,7 @@ extern TaskHandle_t xLoRaTaskHndl;
259260
DECLARE PRIVATE FUNCTIONS
260261
******************************************************************************/
261262
static void TASK_LoRa (void *pvParameters);
263+
static void TASK_LoRa_Timer (void *pvParameters);
262264
static void OnTxDone (void);
263265
static void OnRxDone (uint8_t *payload, uint32_t timestamp, uint16_t size, int16_t rssi, int8_t snr, uint8_t sf);
264266
static void OnTxTimeout (void);
@@ -306,6 +308,7 @@ SemaphoreHandle_t xLoRaSigfoxSem;
306308
void modlora_init0(void) {
307309
xCmdQueue = xQueueCreate(LORA_CMD_QUEUE_SIZE_MAX, sizeof(lora_cmd_data_t));
308310
xRxQueue = xQueueCreate(LORA_DATA_QUEUE_SIZE_MAX, sizeof(lora_rx_data_t));
311+
xCbQueue = xQueueCreate(LORA_CB_QUEUE_SIZE_MAX, sizeof(modlora_timerCallback));
309312
LoRaEvents = xEventGroupCreate();
310313
#if defined(FIPY) || defined(LOPY4)
311314
xLoRaSigfoxSem = xSemaphoreCreateMutex();
@@ -320,6 +323,7 @@ void modlora_init0(void) {
320323
BoardInitPeriph();
321324

322325
xTaskCreatePinnedToCore(TASK_LoRa, "LoRa", LORA_STACK_SIZE / sizeof(StackType_t), NULL, LORA_TASK_PRIORITY, &xLoRaTaskHndl, 1);
326+
xTaskCreatePinnedToCore(TASK_LoRa_Timer, "LoRa_Timer_callback", LORA_TIMER_STACK_SIZE / sizeof(StackType_t), NULL, LORA_TIMER_TASK_PRIORITY, &xLoRaTimerTaskHndl, 1);
323327
}
324328

325329
bool modlora_nvs_set_uint(uint32_t key_idx, uint32_t value) {
@@ -352,6 +356,14 @@ bool modlora_nvs_get_blob(uint32_t key_idx, void *value, uint32_t *length) {
352356
return false;
353357
}
354358

359+
IRAM_ATTR void modlora_set_timer_callback(modlora_timerCallback cb)
360+
{
361+
modlora_timer_cb = cb;
362+
{
363+
if(cb != NULL)
364+
xQueueSendFromISR(xCbQueue, &cb, NULL);
365+
}
366+
}
355367
/******************************************************************************
356368
DEFINE PRIVATE FUNCTIONS
357369
******************************************************************************/
@@ -1030,6 +1042,25 @@ static void TASK_LoRa (void *pvParameters) {
10301042
}
10311043
}
10321044

1045+
static void TASK_LoRa_Timer (void *pvParameters) {
1046+
1047+
static uint32_t thread_notification;
1048+
1049+
for(;;)
1050+
{
1051+
thread_notification = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
1052+
if (thread_notification) {
1053+
1054+
modlora_timerCallback cb;
1055+
xQueueReceive(xCbQueue, &cb, 0);
1056+
if(cb != NULL)
1057+
{
1058+
cb();
1059+
}
1060+
}
1061+
}
1062+
}
1063+
10331064
static void lora_callback_handler(void *arg) {
10341065
lora_obj_t *self = arg;
10351066

esp32/mods/modlora.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
#define LORA_PAYLOAD_SIZE_MAX (255)
2020
#define LORA_CMD_QUEUE_SIZE_MAX (2)
2121
#define LORA_DATA_QUEUE_SIZE_MAX (3)
22+
#define LORA_CB_QUEUE_SIZE_MAX (7)
2223
#define LORA_STACK_SIZE (2560)
24+
#define LORA_TIMER_STACK_SIZE (2048)
2325
#define LORA_TASK_PRIORITY (6)
26+
#define LORA_TIMER_TASK_PRIORITY (8)
2427

2528
#define LORA_STATUS_COMPLETED (0x01)
2629
#define LORA_STATUS_ERROR (0x02)
@@ -136,10 +139,11 @@ typedef struct {
136139
uint8_t port;
137140
} lora_rx_data_t;
138141

142+
typedef void ( *modlora_timerCallback )( void );
139143
/******************************************************************************
140144
EXPORTED DATA
141145
******************************************************************************/
142-
146+
extern TaskHandle_t xLoRaTimerTaskHndl;
143147
/******************************************************************************
144148
DECLARE FUNCTIONS
145149
******************************************************************************/
@@ -148,5 +152,6 @@ extern bool modlora_nvs_set_uint(uint32_t key_idx, uint32_t value);
148152
extern bool modlora_nvs_set_blob(uint32_t key_idx, const void *value, uint32_t length);
149153
extern bool modlora_nvs_get_uint(uint32_t key_idx, uint32_t *value);
150154
extern bool modlora_nvs_get_blob(uint32_t key_idx, void *value, uint32_t *length);
155+
IRAM_ATTR extern void modlora_set_timer_callback(modlora_timerCallback cb);
151156

152157
#endif // MODLORA_H_

lib/lora/system/timer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Maintainer: Miguel Luis and Gregory Cristian
1414
*/
1515
#include "board.h"
1616
#include "timer-board.h"
17+
#include "modlora.h"
1718

1819
/*!
1920
* This flag is used to make sure we have looped through the main several time to avoid race issues
@@ -69,6 +70,7 @@ static bool TimerExists( TimerEvent_t *obj );
6970
*/
7071
TimerTime_t TimerGetValue( void );
7172

73+
7274
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) )
7375
{
7476
obj->Timestamp = 0;
@@ -226,7 +228,12 @@ IRAM_ATTR void TimerIrqHandler( void )
226228

227229
if( elapsedTimer->Callback != NULL )
228230
{
229-
elapsedTimer->Callback( );
231+
modlora_set_timer_callback(elapsedTimer->Callback);
232+
233+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
234+
// Notify the thread so it will wake up when the ISR is complete
235+
vTaskNotifyGiveFromISR(xLoRaTimerTaskHndl, &xHigherPriorityTaskWoken);
236+
portYIELD_FROM_ISR();
230237
}
231238
}
232239

0 commit comments

Comments
 (0)