Skip to content

Commit c4917cd

Browse files
committed
frequencyio implementation for esp32s2
1 parent f2e911a commit c4917cd

File tree

4 files changed

+203
-1
lines changed

4 files changed

+203
-1
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 <stdint.h>
30+
31+
#include "py/runtime.h"
32+
33+
#include "supervisor/shared/tick.h"
34+
#include "shared-bindings/time/__init__.h"
35+
36+
#include "common-hal/microcontroller/Pin.h"
37+
38+
static void frequencyin_interrupt_handler(void *self_in) {
39+
frequencyio_frequencyin_obj_t* self = self_in;
40+
// get counter value
41+
int16_t count;
42+
pcnt_get_counter_value(self->unit, &count);
43+
self->frequency = count / 2.0 / self->capture_period;
44+
45+
// reset counter
46+
pcnt_counter_clear(self->unit);
47+
}
48+
49+
static void init_timer(frequencyio_frequencyin_obj_t* self, uint16_t capture_period) {
50+
// Prepare configuration for the timer module
51+
timer_config_t config = {
52+
.alarm_en = true,
53+
.counter_en = false,
54+
.intr_type = TIMER_INTR_LEVEL,
55+
.counter_dir = TIMER_COUNT_UP,
56+
.auto_reload = true,
57+
.divider = 80 // 1 us per tick
58+
};
59+
60+
// Initialize timer module
61+
timer_init(TIMER_GROUP_0, TIMER_0, &config);
62+
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
63+
timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, capture_period * 1000000);
64+
timer_enable_intr(TIMER_GROUP_0, TIMER_0);
65+
timer_isr_register(TIMER_GROUP_0, TIMER_0, &frequencyin_interrupt_handler, (void *)self, 0, &self->handle);
66+
67+
// Start timer
68+
timer_start(TIMER_GROUP_0, TIMER_0);
69+
}
70+
71+
void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self,
72+
const mcu_pin_obj_t* pin, const uint16_t capture_period) {
73+
if ((capture_period == 0) || (capture_period > 500)) {
74+
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
75+
}
76+
77+
// Prepare configuration for the PCNT unit
78+
const pcnt_config_t pcnt_config = {
79+
// Set PCNT input signal and control GPIOs
80+
.pulse_gpio_num = pin->number,
81+
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
82+
.channel = PCNT_CHANNEL_0,
83+
.lctrl_mode = PCNT_MODE_DISABLE,
84+
.hctrl_mode = PCNT_MODE_KEEP,
85+
.pos_mode = PCNT_COUNT_INC, // count both rising and falling edges
86+
.neg_mode = PCNT_COUNT_INC,
87+
.counter_h_lim = 0,
88+
.counter_l_lim = 0,
89+
};
90+
91+
// Initialize PCNT unit
92+
const int8_t unit = peripherals_pcnt_init(pcnt_config);
93+
if (unit == -1) {
94+
mp_raise_RuntimeError(translate("All PCNT units in use"));
95+
}
96+
97+
// set the GPIO back to high-impedance, as pcnt_unit_config sets it as pull-up
98+
gpio_set_pull_mode(pin->number, GPIO_FLOATING);
99+
100+
// initialize timer
101+
init_timer(self, capture_period);
102+
103+
self->pin = pin->number;
104+
self->unit = (pcnt_unit_t)unit;
105+
self->capture_period = capture_period;
106+
107+
claim_pin(pin);
108+
}
109+
110+
bool common_hal_frequencyio_frequencyin_deinited(frequencyio_frequencyin_obj_t* self) {
111+
return self->unit == PCNT_UNIT_MAX;
112+
}
113+
114+
void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* self) {
115+
if (common_hal_frequencyio_frequencyin_deinited(self)) {
116+
return;
117+
}
118+
reset_pin_number(self->pin);
119+
peripherals_pcnt_deinit(&self->unit);
120+
if (self->handle) {
121+
timer_deinit(TIMER_GROUP_0, TIMER_0);
122+
esp_intr_free(self->handle);
123+
self->handle = NULL;
124+
}
125+
}
126+
127+
uint32_t common_hal_frequencyio_frequencyin_get_item(frequencyio_frequencyin_obj_t* self) {
128+
return (1000 / (self->capture_period / self->frequency));
129+
}
130+
131+
void common_hal_frequencyio_frequencyin_pause(frequencyio_frequencyin_obj_t* self) {
132+
pcnt_counter_pause(self->unit);
133+
timer_pause(TIMER_GROUP_0, TIMER_0);
134+
}
135+
136+
void common_hal_frequencyio_frequencyin_resume(frequencyio_frequencyin_obj_t* self) {
137+
pcnt_counter_resume(self->unit);
138+
timer_start(TIMER_GROUP_0, TIMER_0);
139+
}
140+
141+
void common_hal_frequencyio_frequencyin_clear(frequencyio_frequencyin_obj_t* self) {
142+
self->frequency = 0;
143+
pcnt_counter_clear(self->unit);
144+
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
145+
}
146+
147+
uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequencyin_obj_t *self) {
148+
return self->capture_period;
149+
}
150+
151+
void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) {
152+
if ((capture_period == 0) || (capture_period > 500)) {
153+
mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500"));
154+
}
155+
self->capture_period = capture_period;
156+
common_hal_frequencyio_frequencyin_clear(self);
157+
timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, capture_period * 1000000);
158+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 "driver/timer.h"
32+
#include "peripherals/pcnt.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
pcnt_unit_t unit;
37+
intr_handle_t handle;
38+
uint8_t pin;
39+
uint32_t frequency;
40+
uint16_t capture_period;
41+
} frequencyio_frequencyin_obj_t;
42+
43+
#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

0 commit comments

Comments
 (0)