Skip to content

Commit 01c7a06

Browse files
authored
Merge pull request adafruit#3615 from microDev1/CountIO-S2
ESP32S2: Support for CountIO
2 parents ec12477 + ac8a0fa commit 01c7a06

File tree

8 files changed

+235
-3
lines changed

8 files changed

+235
-3
lines changed

locale/circuitpython.pot

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-10-21 20:13-0500\n"
11+
"POT-Creation-Date: 2020-11-04 21:18+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -296,6 +296,10 @@ msgstr ""
296296
msgid "All I2C peripherals are in use"
297297
msgstr ""
298298

299+
#: ports/esp32s2/peripherals/pcnt_handler.c
300+
msgid "All PCNT units in use"
301+
msgstr ""
302+
299303
#: ports/atmel-samd/common-hal/canio/Listener.c
300304
#: ports/esp32s2/common-hal/canio/Listener.c
301305
#: ports/stm/common-hal/canio/Listener.c
@@ -1109,7 +1113,8 @@ msgid "Invalid phase"
11091113
msgstr ""
11101114

11111115
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
1112-
#: ports/atmel-samd/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
1116+
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
1117+
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
11131118
#: shared-module/rgbmatrix/RGBMatrix.c
11141119
msgid "Invalid pin"
11151120
msgstr ""

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/pcnt.c \
191192
peripherals/pins.c \
192193
peripherals/rmt.c \
193194
supervisor/shared/memory.c
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 "common-hal/countio/Counter.h"
28+
#include "common-hal/microcontroller/Pin.h"
29+
30+
#include "py/runtime.h"
31+
#include "supervisor/shared/translate.h"
32+
33+
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
34+
const mcu_pin_obj_t* pin) {
35+
claim_pin(pin);
36+
37+
// Prepare configuration for the PCNT unit
38+
const pcnt_config_t pcnt_config = {
39+
// Set PCNT input signal and control GPIOs
40+
.pulse_gpio_num = pin->number,
41+
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
42+
.channel = PCNT_CHANNEL_0,
43+
// What to do on the positive / negative edge of pulse input?
44+
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
45+
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
46+
};
47+
48+
// Initialize PCNT unit
49+
const int8_t unit = peripherals_pcnt_init(pcnt_config);
50+
if (unit == -1) {
51+
mp_raise_RuntimeError(translate("All PCNT units in use"));
52+
}
53+
54+
self->pin = pin->number;
55+
self->unit = (pcnt_unit_t)unit;
56+
}
57+
58+
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
59+
return self->unit == PCNT_UNIT_MAX;
60+
}
61+
62+
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
63+
if (common_hal_countio_counter_deinited(self)) {
64+
return;
65+
}
66+
reset_pin_number(self->pin);
67+
peripherals_pcnt_deinit(&self->unit);
68+
}
69+
70+
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
71+
int16_t count;
72+
pcnt_get_counter_value(self->unit, &count);
73+
return count+self->count;
74+
}
75+
76+
void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
77+
mp_int_t new_count) {
78+
self->count = new_count;
79+
pcnt_counter_clear(self->unit);
80+
}
81+
82+
void common_hal_countio_counter_reset(countio_counter_obj_t* self) {
83+
common_hal_countio_counter_set_count(self, 0);
84+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_COUNTIO_COUNTER_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
29+
30+
#include "py/obj.h"
31+
#include "peripherals/pcnt.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
uint8_t pin;
36+
mp_int_t count;
37+
pcnt_unit_t unit;
38+
} countio_counter_obj_t;
39+
40+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//No countio module functions

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CIRCUITPY_FULL_BUILD = 1
1717
CIRCUITPY_AUDIOBUSIO = 0
1818
CIRCUITPY_AUDIOIO = 0
1919
CIRCUITPY_CANIO = 1
20-
CIRCUITPY_COUNTIO = 0
20+
CIRCUITPY_COUNTIO = 1
2121
CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_I2CPERIPHERAL = 0
2323
CIRCUITPY_ROTARYIO = 0

ports/esp32s2/peripherals/pcnt.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/pcnt.h"
28+
29+
#define PCNT_UNIT_ACTIVE 1
30+
#define PCNT_UNIT_INACTIVE 0
31+
32+
static uint8_t pcnt_state[4];
33+
34+
int peripherals_pcnt_init(pcnt_config_t pcnt_config) {
35+
// Look for available pcnt unit
36+
for (uint8_t i = 0; i<=3; i++) {
37+
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
38+
pcnt_config.unit = (pcnt_unit_t)i;
39+
pcnt_state[i] = PCNT_UNIT_ACTIVE;
40+
break;
41+
} else if (i == 3) {
42+
return -1;
43+
}
44+
}
45+
46+
// Initialize PCNT unit
47+
pcnt_unit_config(&pcnt_config);
48+
49+
// Configure and enable the input filter
50+
pcnt_set_filter_value(pcnt_config.unit, 100);
51+
pcnt_filter_enable(pcnt_config.unit);
52+
53+
// Initialize PCNT's counter
54+
pcnt_counter_pause(pcnt_config.unit);
55+
pcnt_counter_clear(pcnt_config.unit);
56+
57+
// Everything is set up, now go to counting
58+
pcnt_counter_resume(pcnt_config.unit);
59+
60+
return pcnt_config.unit;
61+
}
62+
63+
void peripherals_pcnt_deinit(pcnt_unit_t* unit) {
64+
pcnt_state[*unit] = PCNT_UNIT_INACTIVE;
65+
*unit = PCNT_UNIT_MAX;
66+
}

ports/esp32s2/peripherals/pcnt.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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_PCNT_HANDLER_H
28+
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H
29+
30+
#include "driver/pcnt.h"
31+
32+
extern int peripherals_pcnt_init(pcnt_config_t pcnt_config);
33+
extern void peripherals_pcnt_deinit(pcnt_unit_t* unit);
34+
35+
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_PCNT_HANDLER_H

0 commit comments

Comments
 (0)