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

Commit d6c1ccb

Browse files
authored
Merge pull request #80 from husigeza/Minor_fixes_BLE_Lora
Minor fixes in BLE and LoRa
2 parents f63954f + 9493536 commit d6c1ccb

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

esp32/lora/timer-board.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,11 @@ static TimerTime_t TimerTickCounterContext = 0;
5656
* Value trigging the IRQ
5757
*/
5858
DRAM_ATTR volatile TimerTime_t TimeoutCntValue = 0;
59-
extern TaskHandle_t xLoRaTimerTaskHndl;
6059

6160
static IRAM_ATTR void TimerCallback (void) {
62-
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
63-
6461
TimerTickCounter++;
6562
if (TimeoutCntValue > 0 && TimerTickCounter == TimeoutCntValue) {
6663
TimerIrqHandler();
67-
// Notify the thread so it will wake up when the ISR is complete
68-
vTaskNotifyGiveFromISR(xLoRaTimerTaskHndl, &xHigherPriorityTaskWoken);
69-
portYIELD_FROM_ISR();
7064
}
7165
}
7266

esp32/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ TaskHandle_t svTaskHandle;
5858
TaskHandle_t SmartConfTaskHandle;
5959
#if defined(LOPY) || defined (LOPY4) || defined (FIPY)
6060
TaskHandle_t xLoRaTaskHndl;
61-
TaskHandle_t xLoRaTimerTaskHndl;
61+
DRAM_ATTR TaskHandle_t xLoRaTimerTaskHndl;
6262
#endif
6363
#if defined(SIPY) || defined (LOPY4) || defined (FIPY)
6464
TaskHandle_t xSigfoxTaskHndl;

esp32/mods/modbt.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ static void gap_events_handler (esp_gap_ble_cb_event_t event, esp_ble_gap_cb_par
659659
xQueueSend(xScanQueue, (void *)&bt_event_result, (TickType_t)0);
660660
bt_obj.events |= MOD_BT_GATTC_ADV_EVT;
661661
if (bt_obj.trigger & MOD_BT_GATTC_ADV_EVT) {
662-
mp_irq_queue_interrupt(bluetooth_callback_handler, (void *)&bt_obj);
662+
mp_irq_queue_interrupt_non_ISR(bluetooth_callback_handler, (void *)&bt_obj);
663663
}
664664
break;
665665
case ESP_GAP_SEARCH_DISC_RES_EVT:
@@ -765,7 +765,7 @@ static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
765765
char_obj->events |= MOD_BT_GATTC_INDICATE_EVT;
766766
}
767767
if ((char_obj->trigger & MOD_BT_GATTC_NOTIFY_EVT) || (char_obj->trigger & MOD_BT_GATTC_INDICATE_EVT)) {
768-
mp_irq_queue_interrupt(gattc_char_callback_handler, char_obj);
768+
mp_irq_queue_interrupt_non_ISR(gattc_char_callback_handler, char_obj);
769769
}
770770
}
771771
break;
@@ -867,7 +867,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
867867
if (char_obj->trigger & MOD_BT_GATTS_READ_EVT) {
868868
char_obj->read_request = true;
869869
char_obj->trans_id = p->read.trans_id;
870-
mp_irq_queue_interrupt(gatts_char_callback_handler, char_obj);
870+
mp_irq_queue_interrupt_non_ISR(gatts_char_callback_handler, char_obj);
871871
break;
872872
}
873873
}
@@ -894,7 +894,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
894894
bt_gatts_char_obj_t *char_obj = (bt_gatts_char_obj_t *)attr_obj;
895895
char_obj->events |= MOD_BT_GATTS_WRITE_EVT;
896896
if (char_obj->trigger & MOD_BT_GATTS_WRITE_EVT) {
897-
mp_irq_queue_interrupt(gatts_char_callback_handler, char_obj);
897+
mp_irq_queue_interrupt_non_ISR(gatts_char_callback_handler, char_obj);
898898
}
899899
} else { // descriptor
900900
if (attr_obj->uuid.len == ESP_UUID_LEN_16 && attr_obj->uuid.uuid.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) {
@@ -903,7 +903,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
903903
char_obj->config = value;
904904
char_obj->events |= MOD_BT_GATTS_SUBSCRIBE_EVT;
905905
if (char_obj->trigger & MOD_BT_GATTS_SUBSCRIBE_EVT) {
906-
mp_irq_queue_interrupt(gatts_char_callback_handler, char_obj);
906+
mp_irq_queue_interrupt_non_ISR(gatts_char_callback_handler, char_obj);
907907
}
908908

909909
if (value == 0x0001) { // notifications enabled
@@ -961,7 +961,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
961961
bt_obj.gatts_conn_id = p->connect.conn_id;
962962
bt_obj.events |= MOD_BT_GATTS_CONN_EVT;
963963
if (bt_obj.trigger & MOD_BT_GATTS_CONN_EVT) {
964-
mp_irq_queue_interrupt(bluetooth_callback_handler, (void *)&bt_obj);
964+
mp_irq_queue_interrupt_non_ISR(bluetooth_callback_handler, (void *)&bt_obj);
965965
}
966966
if (bt_obj.secure){
967967
esp_ble_set_encryption(p->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
@@ -981,7 +981,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
981981
bt_obj.events |= MOD_BT_GATTS_DISCONN_EVT;
982982
xEventGroupSetBits(bt_event_group, MOD_BT_GATTS_DISCONN_EVT);
983983
if (bt_obj.trigger & MOD_BT_GATTS_DISCONN_EVT) {
984-
mp_irq_queue_interrupt(bluetooth_callback_handler, (void *)&bt_obj);
984+
mp_irq_queue_interrupt_non_ISR(bluetooth_callback_handler, (void *)&bt_obj);
985985
}
986986
break;
987987
case ESP_GATTS_CLOSE_EVT:

esp32/mods/modlora.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ static const char *modlora_nvs_data_key[E_LORA_NVS_NUM_KEYS] = { "JOINED", "UPLN
262262
"MACPARAMS", "CHANNELS", "SRVACK", "MACNXTTX",
263263
"MACBUFIDX", "MACRPTIDX", "MACBUF", "MACRPTBUF",
264264
"REGION", "CHANMASK", "CHANMASKREM" };
265-
DRAM_ATTR static modlora_timerCallback modlora_timer_cb;
266265
/******************************************************************************
267266
DECLARE PUBLIC DATA
268267
******************************************************************************/
@@ -394,10 +393,16 @@ bool modlora_is_module_sleep(void)
394393

395394
IRAM_ATTR void modlora_set_timer_callback(modlora_timerCallback cb)
396395
{
397-
modlora_timer_cb = cb;
398396
if(cb != NULL)
399397
{
400-
xQueueSendFromISR(xCbQueue, &cb, NULL);
398+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
399+
400+
xQueueSendFromISR(xCbQueue, &cb, &xHigherPriorityTaskWoken);
401+
402+
if( xHigherPriorityTaskWoken)
403+
{
404+
portYIELD_FROM_ISR();
405+
}
401406
}
402407
}
403408

@@ -1185,20 +1190,14 @@ static void TASK_LoRa (void *pvParameters) {
11851190

11861191
static void TASK_LoRa_Timer (void *pvParameters) {
11871192

1188-
static uint32_t thread_notification;
1189-
11901193
for(;;)
11911194
{
1192-
thread_notification = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
1193-
if (thread_notification) {
1194-
1195-
modlora_timerCallback cb;
1196-
while (pdTRUE == xQueueReceive(xCbQueue, &cb, 0))
1195+
modlora_timerCallback cb;
1196+
while (pdTRUE == xQueueReceive(xCbQueue, &cb, portMAX_DELAY))
1197+
{
1198+
if(cb != NULL)
11971199
{
1198-
if(cb != NULL)
1199-
{
1200-
cb();
1201-
}
1200+
cb();
12021201
}
12031202
}
12041203
}

esp32/mods/modlora.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ typedef void ( *modlora_timerCallback )( void );
144144
/******************************************************************************
145145
EXPORTED DATA
146146
******************************************************************************/
147-
extern TaskHandle_t xLoRaTimerTaskHndl;
147+
extern DRAM_ATTR TaskHandle_t xLoRaTimerTaskHndl;
148148
/******************************************************************************
149149
DECLARE FUNCTIONS
150150
******************************************************************************/

esp32/util/mpirq.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,20 @@ mp_obj_tuple_t *mp_irq_find (mp_obj_t parent) {
148148

149149
void IRAM_ATTR mp_irq_queue_interrupt(void (* handler)(void *), void *arg) {
150150
mp_callback_obj_t cb = {.handler = handler, .arg = arg};
151-
xQueueSendFromISR(InterruptsQueue, &cb, NULL);
151+
152+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
153+
154+
xQueueSendFromISR(InterruptsQueue, &cb, &xHigherPriorityTaskWoken);
155+
156+
if( xHigherPriorityTaskWoken)
157+
{
158+
portYIELD_FROM_ISR();
159+
}
160+
}
161+
162+
void mp_irq_queue_interrupt_non_ISR(void (* handler)(void *), void *arg) {
163+
mp_callback_obj_t cb = {.handler = handler, .arg = arg};
164+
(void)xQueueSend(InterruptsQueue, &cb, 0);
152165
}
153166

154167
void IRAM_ATTR mp_irq_queue_interrupt_immediate_thread_delete(TaskHandle_t id) {

esp32/util/mpirq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void mp_irq_add (mp_obj_t parent, mp_obj_t handler);
4949
void mp_irq_remove (mp_obj_t parent);
5050
mp_obj_tuple_t *mp_irq_find (mp_obj_t parent);
5151
void mp_irq_queue_interrupt(void (* handler)(void *), void *arg);
52+
void mp_irq_queue_interrupt_non_ISR(void (* handler)(void *), void *arg);
5253
void mp_irq_queue_interrupt_immediate_thread_delete(TaskHandle_t id);
5354
void mp_irq_kill(void);
5455
#endif /* MPIRQ_H_ */

lib/lora/system/timer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ IRAM_ATTR void TimerIrqHandler( void )
228228

229229
if( elapsedTimer->Callback != NULL )
230230
{
231+
// Callback will be processed out of the Interrupt context in a Thread
231232
modlora_set_timer_callback(elapsedTimer->Callback);
232233
}
233234
}

0 commit comments

Comments
 (0)