29
29
#include "py/runtime.h"
30
30
31
31
static void IRAM_ATTR pcnt_overflow_handler (void * self_in ) {
32
- frequencyio_frequencyin_obj_t * self = self_in ;
32
+ frequencyio_frequencyin_obj_t * self = self_in ;
33
33
// reset counter
34
34
pcnt_counter_clear (self -> unit );
35
35
@@ -41,18 +41,26 @@ static void IRAM_ATTR pcnt_overflow_handler(void *self_in) {
41
41
}
42
42
43
43
static void IRAM_ATTR timer_interrupt_handler (void * self_in ) {
44
- frequencyio_frequencyin_obj_t * self = self_in ;
44
+ frequencyio_frequencyin_obj_t * self = self_in ;
45
45
// get counter value
46
46
int16_t count ;
47
47
pcnt_get_counter_value (self -> unit , & count );
48
- self -> frequency = count / 2.0 / self -> capture_period ;
48
+ self -> frequency = ((count / 2.0 ) + (self -> multiplier * INT16_MAX / 4.0 )) / (self -> capture_period );
49
+
50
+ // reset multiplier
51
+ self -> multiplier = 0 ;
49
52
50
53
// reset counter
51
54
pcnt_counter_clear (self -> unit );
52
55
53
56
// reset interrupt
54
- TIMERG0 .int_clr .t0 = 1 ;
55
- TIMERG0 .hw_timer [0 ].config .alarm_en = 1 ;
57
+ timg_dev_t * device = self -> timer .group ? & (TIMERG1 ) : & (TIMERG0 );
58
+ if (self -> timer .idx ) {
59
+ device -> int_clr .t1 = 1 ;
60
+ } else {
61
+ device -> int_clr .t0 = 1 ;
62
+ }
63
+ device -> hw_timer [self -> timer .idx ].config .alarm_en = 1 ;
56
64
}
57
65
58
66
static void init_pcnt (frequencyio_frequencyin_obj_t * self ) {
@@ -70,7 +78,7 @@ static void init_pcnt(frequencyio_frequencyin_obj_t* self) {
70
78
.counter_l_lim = 0 ,
71
79
};
72
80
73
- // Initialize PCNT unit
81
+ // initialize PCNT
74
82
const int8_t unit = peripherals_pcnt_init (pcnt_config );
75
83
if (unit == -1 ) {
76
84
mp_raise_RuntimeError (translate ("All PCNT units in use" ));
@@ -98,15 +106,22 @@ static void init_timer(frequencyio_frequencyin_obj_t* self) {
98
106
.divider = 80 // 1 us per tick
99
107
};
100
108
101
- // Initialize timer module
102
- timer_init (TIMER_GROUP_0 , TIMER_0 , & config );
103
- timer_set_counter_value (TIMER_GROUP_0 , TIMER_0 , 0 );
104
- timer_set_alarm_value (TIMER_GROUP_0 , TIMER_0 , self -> capture_period * 1000000 );
105
- timer_isr_register (TIMER_GROUP_0 , TIMER_0 , timer_interrupt_handler , (void * )self , ESP_INTR_FLAG_IRAM , & self -> handle );
106
- timer_enable_intr (TIMER_GROUP_0 , TIMER_0 );
109
+ // initialize Timer
110
+ peripherals_timer_init (& config , & self -> timer );
111
+ if (self -> timer .idx == TIMER_MAX || self -> timer .group == TIMER_GROUP_MAX ) {
112
+ mp_raise_RuntimeError (translate ("All timers in use" ));
113
+ }
114
+
115
+ timer_idx_t idx = self -> timer .idx ;
116
+ timer_group_t group = self -> timer .group ;
117
+
118
+ // enable timer interrupt
119
+ timer_set_alarm_value (group , idx , self -> capture_period * 1000000 );
120
+ timer_isr_register (group , idx , timer_interrupt_handler , (void * )self , ESP_INTR_FLAG_IRAM , & self -> handle );
121
+ timer_enable_intr (group , idx );
107
122
108
- // Start timer
109
- timer_start (TIMER_GROUP_0 , TIMER_0 );
123
+ // start timer
124
+ timer_start (self -> timer . group , self -> timer . idx );
110
125
}
111
126
112
127
void common_hal_frequencyio_frequencyin_construct (frequencyio_frequencyin_obj_t * self ,
@@ -137,31 +152,31 @@ void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* se
137
152
}
138
153
reset_pin_number (self -> pin );
139
154
peripherals_pcnt_deinit (& self -> unit );
140
- timer_deinit ( TIMER_GROUP_0 , TIMER_0 );
155
+ peripherals_timer_deinit ( & self -> timer );
141
156
if (self -> handle ) {
142
157
esp_intr_free (self -> handle );
143
158
self -> handle = NULL ;
144
159
}
145
160
}
146
161
147
162
uint32_t common_hal_frequencyio_frequencyin_get_item (frequencyio_frequencyin_obj_t * self ) {
148
- return ( self -> frequency + ( self -> multiplier * INT16_MAX )) ;
163
+ return self -> frequency ;
149
164
}
150
165
151
166
void common_hal_frequencyio_frequencyin_pause (frequencyio_frequencyin_obj_t * self ) {
152
167
pcnt_counter_pause (self -> unit );
153
- timer_pause (TIMER_GROUP_0 , TIMER_0 );
168
+ timer_pause (self -> timer . group , self -> timer . idx );
154
169
}
155
170
156
171
void common_hal_frequencyio_frequencyin_resume (frequencyio_frequencyin_obj_t * self ) {
157
172
pcnt_counter_resume (self -> unit );
158
- timer_start (TIMER_GROUP_0 , TIMER_0 );
173
+ timer_start (self -> timer . group , self -> timer . idx );
159
174
}
160
175
161
176
void common_hal_frequencyio_frequencyin_clear (frequencyio_frequencyin_obj_t * self ) {
162
177
self -> frequency = 0 ;
163
178
pcnt_counter_clear (self -> unit );
164
- timer_set_counter_value (TIMER_GROUP_0 , TIMER_0 , 0 );
179
+ timer_set_counter_value (self -> timer . group , self -> timer . idx , 0 );
165
180
}
166
181
167
182
uint16_t common_hal_frequencyio_frequencyin_get_capture_period (frequencyio_frequencyin_obj_t * self ) {
@@ -174,5 +189,5 @@ void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequency
174
189
}
175
190
self -> capture_period = capture_period ;
176
191
common_hal_frequencyio_frequencyin_clear (self );
177
- timer_set_alarm_value (TIMER_GROUP_0 , TIMER_0 , capture_period * 1000000 );
192
+ timer_set_alarm_value (self -> timer . group , self -> timer . idx , capture_period * 1000000 );
178
193
}
0 commit comments