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

Commit 9ac68c2

Browse files
author
iwahdan88
committed
[PYFW-325] Use Hardware timers for Lora lib ticks #comment Initial Fix implementation
1 parent d85484d commit 9ac68c2

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

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: 2 additions & 2 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,7 +55,7 @@ static TimerTime_t TimerTickCounterContext = 0;
5555
/*!
5656
* Value trigging the IRQ
5757
*/
58-
volatile TimerTime_t TimeoutCntValue = 0;
58+
DRAM_ATTR volatile TimerTime_t TimeoutCntValue = 0;
5959

6060

6161
static IRAM_ATTR void TimerCallback (void) {

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