37
37
#include "modwlan.h"
38
38
#include "modbt.h"
39
39
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"
47
41
42
+ typedef void (* HAL_tick_user_cb_t )(void );
48
43
#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
+
50
50
#endif
51
51
52
- #define TIMER_TICKS 160000 // 1 ms @160MHz
53
52
54
53
#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
+
57
58
HAL_tick_user_cb ();
58
59
}
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
+
59
65
}
60
66
61
67
void HAL_set_tick_cb (void * cb ) {
62
- HAL_tick_user_cb = cb ;
68
+ HAL_tick_user_cb = ( HAL_tick_user_cb_t ) cb ;
63
69
}
64
70
#endif
65
71
@@ -68,8 +74,30 @@ void mp_hal_init(bool soft_reset) {
68
74
#if defined (LOPY ) || defined(LOPY4 ) || defined(FIPY )
69
75
// setup the HAL timer for LoRa
70
76
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
+
73
101
#endif
74
102
}
75
103
}
0 commit comments