2929#include "py/runtime.h"
3030
3131static 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 ;
3333 // reset counter
3434 pcnt_counter_clear (self -> unit );
3535
@@ -41,18 +41,26 @@ static void IRAM_ATTR pcnt_overflow_handler(void *self_in) {
4141}
4242
4343static 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 ;
4545 // get counter value
4646 int16_t count ;
4747 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 ;
4952
5053 // reset counter
5154 pcnt_counter_clear (self -> unit );
5255
5356 // 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 ;
5664}
5765
5866static void init_pcnt (frequencyio_frequencyin_obj_t * self ) {
@@ -70,7 +78,7 @@ static void init_pcnt(frequencyio_frequencyin_obj_t* self) {
7078 .counter_l_lim = 0 ,
7179 };
7280
73- // Initialize PCNT unit
81+ // initialize PCNT
7482 const int8_t unit = peripherals_pcnt_init (pcnt_config );
7583 if (unit == -1 ) {
7684 mp_raise_RuntimeError (translate ("All PCNT units in use" ));
@@ -98,15 +106,22 @@ static void init_timer(frequencyio_frequencyin_obj_t* self) {
98106 .divider = 80 // 1 us per tick
99107 };
100108
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 );
107122
108- // Start timer
109- timer_start (TIMER_GROUP_0 , TIMER_0 );
123+ // start timer
124+ timer_start (self -> timer . group , self -> timer . idx );
110125}
111126
112127void 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
137152 }
138153 reset_pin_number (self -> pin );
139154 peripherals_pcnt_deinit (& self -> unit );
140- timer_deinit ( TIMER_GROUP_0 , TIMER_0 );
155+ peripherals_timer_deinit ( & self -> timer );
141156 if (self -> handle ) {
142157 esp_intr_free (self -> handle );
143158 self -> handle = NULL ;
144159 }
145160}
146161
147162uint32_t common_hal_frequencyio_frequencyin_get_item (frequencyio_frequencyin_obj_t * self ) {
148- return ( self -> frequency + ( self -> multiplier * INT16_MAX )) ;
163+ return self -> frequency ;
149164}
150165
151166void common_hal_frequencyio_frequencyin_pause (frequencyio_frequencyin_obj_t * self ) {
152167 pcnt_counter_pause (self -> unit );
153- timer_pause (TIMER_GROUP_0 , TIMER_0 );
168+ timer_pause (self -> timer . group , self -> timer . idx );
154169}
155170
156171void common_hal_frequencyio_frequencyin_resume (frequencyio_frequencyin_obj_t * self ) {
157172 pcnt_counter_resume (self -> unit );
158- timer_start (TIMER_GROUP_0 , TIMER_0 );
173+ timer_start (self -> timer . group , self -> timer . idx );
159174}
160175
161176void common_hal_frequencyio_frequencyin_clear (frequencyio_frequencyin_obj_t * self ) {
162177 self -> frequency = 0 ;
163178 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 );
165180}
166181
167182uint16_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
174189 }
175190 self -> capture_period = capture_period ;
176191 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 );
178193}
0 commit comments