Skip to content

Commit c67f589

Browse files
authored
Merge pull request #3704 from microDev1/frequencyio-S2
ESP32S2: Support for FrequencyIO
2 parents 16fe2de + f3b5ca5 commit c67f589

File tree

8 files changed

+372
-10
lines changed

8 files changed

+372
-10
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ SRC_C += \
188188
lib/utils/pyexec.c \
189189
lib/utils/stdout_helpers.c \
190190
lib/utils/sys_stdio_mphal.c \
191+
peripherals/timer.c \
191192
peripherals/pcnt.c \
192193
peripherals/pins.c \
193194
peripherals/rmt.c \
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/frequencyio/FrequencyIn.h"
28+
29+
#include "py/runtime.h"
30+
31+
static void IRAM_ATTR pcnt_overflow_handler(void *self_in) {
32+
frequencyio_frequencyin_obj_t * self = self_in;
33+
// reset counter
34+
pcnt_counter_clear(self->unit);
35+
36+
// increase multiplier
37+
self->multiplier++;
38+
39+
// reset interrupt
40+
PCNT.int_clr.val = BIT(self->unit);
41+
}
42+
43+
static void IRAM_ATTR timer_interrupt_handler(void *self_in) {
44+
frequencyio_frequencyin_obj_t * self = self_in;
45+
// get counter value
46+
int16_t count;
47+
pcnt_get_counter_value(self->unit, &count);
48+
self->frequency = ((count / 2.0) + (self->multiplier * INT16_MAX / 4.0)) / (self->capture_period);
49+
50+
// reset multiplier
51+
self->multiplier = 0;
52+
53+
// reset counter
54+
pcnt_counter_clear(self->unit);
55+
56+
// reset interrupt
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;
64+
}
65+
66+
static void init_pcnt(frequencyio_frequencyin_obj_t* self) {
67+
// Prepare configuration for the PCNT unit
68+
const pcnt_config_t pcnt_config = {
69+
// Set PCNT input signal and control GPIOs
70+
.pulse_gpio_num = self->pin,
71+
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
72+
.channel = PCNT_CHANNEL_0,
73+
// What to do on the positive / negative edge of pulse input?
74+
.pos_mode = PCNT_COUNT_INC, // count both rising and falling edges
75+
.neg_mode = PCNT_COUNT_INC,
76+
// Set counter limit
77+
.counter_h_lim = INT16_MAX,
78+
.counter_l_lim = 0,
79+
};
80+
81+
// initialize PCNT
82+
const int8_t unit = peripherals_pcnt_init(pcnt_config);
83+
if (unit == -1) {
84+
mp_raise_RuntimeError(translate("All PCNT units in use"));
85+
}
86+
87+
// set the GPIO back to high-impedance, as pcnt_unit_config sets it as pull-up
88+
gpio_set_pull_mode(self->pin, GPIO_FLOATING);
89+
90+
self->unit = (pcnt_unit_t)unit;
91+
92+
// enable pcnt interrupt
93+
pcnt_event_enable(self->unit, PCNT_EVT_H_LIM);
94+
pcnt_isr_register(pcnt_overflow_handler, (void *)self, ESP_INTR_FLAG_IRAM, &self->handle);
95+
pcnt_intr_enable(self->unit);
96+
}
97+
98+
static void init_timer(frequencyio_frequencyin_obj_t* self) {
99+
// Prepare configuration for the timer module
100+
const timer_config_t config = {
101+
.alarm_en = true,
102+
.counter_en = false,
103+
.intr_type = TIMER_INTR_LEVEL,
104+
.counter_dir = TIMER_COUNT_UP,
105+
.auto_reload = true,
106+
.divider = 80 // 1 us per tick
107+
};
108+
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);
122+
123+
// start timer
124+
timer_start(self->timer.group, self->timer.idx);
125+
}
126+
127+
void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self,
128+
const mcu_pin_obj_t* pin, const uint16_t capture_period) {
129+
if ((capture_period == 0) || (capture_period > 500)) {
130+
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
131+
}
132+
133+
self->pin = pin->number;
134+
self->handle = NULL;
135+
self->multiplier = 0;
136+
self->capture_period = capture_period;
137+
138+
// initialize pcnt and timer
139+
init_pcnt(self);
140+
init_timer(self);
141+
142+
claim_pin(pin);
143+
}
144+
145+
bool common_hal_frequencyio_frequencyin_deinited(frequencyio_frequencyin_obj_t* self) {
146+
return self->unit == PCNT_UNIT_MAX;
147+
}
148+
149+
void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* self) {
150+
if (common_hal_frequencyio_frequencyin_deinited(self)) {
151+
return;
152+
}
153+
reset_pin_number(self->pin);
154+
peripherals_pcnt_deinit(&self->unit);
155+
peripherals_timer_deinit(&self->timer);
156+
if (self->handle) {
157+
esp_intr_free(self->handle);
158+
self->handle = NULL;
159+
}
160+
}
161+
162+
uint32_t common_hal_frequencyio_frequencyin_get_item(frequencyio_frequencyin_obj_t* self) {
163+
return self->frequency;
164+
}
165+
166+
void common_hal_frequencyio_frequencyin_pause(frequencyio_frequencyin_obj_t* self) {
167+
pcnt_counter_pause(self->unit);
168+
timer_pause(self->timer.group, self->timer.idx);
169+
}
170+
171+
void common_hal_frequencyio_frequencyin_resume(frequencyio_frequencyin_obj_t* self) {
172+
pcnt_counter_resume(self->unit);
173+
timer_start(self->timer.group, self->timer.idx);
174+
}
175+
176+
void common_hal_frequencyio_frequencyin_clear(frequencyio_frequencyin_obj_t* self) {
177+
self->frequency = 0;
178+
pcnt_counter_clear(self->unit);
179+
timer_set_counter_value(self->timer.group, self->timer.idx, 0);
180+
}
181+
182+
uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequencyin_obj_t *self) {
183+
return self->capture_period;
184+
}
185+
186+
void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) {
187+
if ((capture_period == 0) || (capture_period > 500)) {
188+
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
189+
}
190+
self->capture_period = capture_period;
191+
common_hal_frequencyio_frequencyin_clear(self);
192+
timer_set_alarm_value(self->timer.group, self->timer.idx, capture_period * 1000000);
193+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H
29+
30+
#include "py/obj.h"
31+
#include "peripherals/pcnt.h"
32+
#include "peripherals/timer.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
pcnt_unit_t unit;
37+
timer_index_t timer;
38+
intr_handle_t handle;
39+
uint8_t pin;
40+
uint8_t multiplier;
41+
uint32_t frequency;
42+
uint16_t capture_period;
43+
} frequencyio_frequencyin_obj_t;
44+
45+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_FREQUENCYIO_FREQUENCYIN_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No ferquencyio module functions.

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CIRCUITPY_AUDIOBUSIO = 0
1818
CIRCUITPY_AUDIOIO = 0
1919
CIRCUITPY_CANIO = 1
2020
CIRCUITPY_COUNTIO = 1
21-
CIRCUITPY_FREQUENCYIO = 0
21+
CIRCUITPY_FREQUENCYIO = 1
2222
CIRCUITPY_I2CPERIPHERAL = 0
2323
CIRCUITPY_ROTARYIO = 1
2424
CIRCUITPY_NVM = 0

ports/esp32s2/peripherals/timer.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "peripherals/timer.h"
28+
29+
#define TIMER_FREE 1
30+
#define TIMER_BUSY 0
31+
32+
static uint8_t timer_state[2][2];
33+
34+
void peripherals_timer_reset(void) {
35+
timer_index_t timer;
36+
for (uint8_t i = 0; i < 2; i++) {
37+
for (uint8_t j = 0; j < 2; j++) {
38+
if (timer_state[i][j] == TIMER_BUSY) {
39+
timer.idx = (timer_idx_t)j;
40+
timer.group = (timer_group_t)i;
41+
timer_state[i][j] = TIMER_FREE;
42+
peripherals_timer_deinit(&timer);
43+
}
44+
}
45+
}
46+
}
47+
48+
void peripherals_timer_init(const timer_config_t * config, timer_index_t * timer) {
49+
bool break_loop = false;
50+
51+
// get free timer
52+
for (uint8_t i = 0; i < 2; i++) {
53+
for (uint8_t j = 0; j < 2; j++) {
54+
if (timer_state[i][j] == TIMER_FREE) {
55+
timer->idx = (timer_idx_t)j;
56+
timer->group = (timer_group_t)i;
57+
timer_state[i][j] = TIMER_BUSY;
58+
break_loop = true;
59+
break;
60+
} else if (i == 1 && j == 1) {
61+
timer->idx = TIMER_MAX;
62+
timer->group = TIMER_GROUP_MAX;
63+
return;
64+
}
65+
}
66+
if (break_loop) {break;}
67+
}
68+
69+
// initialize timer module
70+
timer_init(timer->group, timer->idx, config);
71+
timer_set_counter_value(timer->group, timer->idx, 0);
72+
}
73+
74+
void peripherals_timer_deinit(timer_index_t * timer) {
75+
timer_deinit(timer->group, timer->idx);
76+
}

ports/esp32s2/peripherals/timer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H
28+
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H
29+
30+
#include "driver/timer.h"
31+
32+
typedef struct {
33+
timer_idx_t idx;
34+
timer_group_t group;
35+
} timer_index_t;
36+
37+
extern void peripherals_timer_init(const timer_config_t * config, timer_index_t * timer);
38+
extern void peripherals_timer_deinit(timer_index_t * timer);
39+
extern void peripherals_timer_reset(void);
40+
41+
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H

0 commit comments

Comments
 (0)