Skip to content

Commit 4438050

Browse files
committed
Add pcnt handler
1 parent 5e3bfa1 commit 4438050

File tree

6 files changed

+177
-36
lines changed

6 files changed

+177
-36
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-01 11:11+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"
@@ -1109,7 +1109,8 @@ msgid "Invalid phase"
11091109
msgstr ""
11101110

11111111
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
1112-
#: ports/atmel-samd/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
1112+
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
1113+
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
11131114
#: shared-module/rgbmatrix/RGBMatrix.c
11141115
msgid "Invalid pin"
11151116
msgstr ""
@@ -1271,6 +1272,10 @@ msgstr ""
12711272
msgid "No MOSI Pin"
12721273
msgstr ""
12731274

1275+
#: ports/esp32s2/pcnt_handler.c
1276+
msgid "No PCNT unit free"
1277+
msgstr ""
1278+
12741279
#: ports/atmel-samd/common-hal/busio/UART.c
12751280
#: ports/esp32s2/common-hal/busio/UART.c
12761281
#: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/UART.c

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ SRC_C += \
173173
background.c \
174174
fatfs_port.c \
175175
mphalport.c \
176+
pcnt_handler.c \
176177
bindings/espidf/__init__.c \
177178
boards/$(BOARD)/board.c \
178179
boards/$(BOARD)/pins.c \

ports/esp32s2/common-hal/countio/Counter.c

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
1-
#include "common-hal/countio/Counter.h"
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+
*/
226

3-
#include "py/runtime.h"
4-
#include "supervisor/shared/translate.h"
27+
#include "common-hal/countio/Counter.h"
28+
#include "common-hal/microcontroller/Pin.h"
529

6-
#include "driver/pcnt.h"
30+
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
31+
const mcu_pin_obj_t* pin) {
32+
claim_pin(pin);
733

8-
static void pcnt_init(countio_counter_obj_t* self) {
9-
int unit = PCNT_UNIT_0;
1034
// Prepare configuration for the PCNT unit
1135
pcnt_config_t pcnt_config = {
1236
// Set PCNT input signal and control GPIOs
13-
.pulse_gpio_num = self->pin->number,
37+
.pulse_gpio_num = pin->number,
1438
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
1539
.channel = PCNT_CHANNEL_0,
16-
.unit = unit,
1740
// What to do on the positive / negative edge of pulse input?
1841
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
1942
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
2043
};
2144
// Initialize PCNT unit
22-
pcnt_unit_config(&pcnt_config);
23-
24-
// Configure and enable the input filter
25-
pcnt_set_filter_value(unit, 100);
26-
pcnt_filter_enable(unit);
45+
pcnt_handler_init(&pcnt_config);
2746

28-
// Initialize PCNT's counter
29-
pcnt_counter_pause(unit);
30-
pcnt_counter_clear(unit);
31-
32-
// Everything is set up, now go to counting
33-
pcnt_counter_resume(unit);
34-
}
35-
36-
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
37-
const mcu_pin_obj_t* pin) {
38-
claim_pin(pin);
39-
self->pin = pin;
40-
pcnt_init(self);
47+
self->pin = pin->number;
48+
self->unit = pcnt_config.unit;
4149
}
4250

4351
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
44-
return self->pin == NULL;
52+
return self->unit == PCNT_UNIT_MAX;
4553
}
4654

4755
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
4856
if (common_hal_countio_counter_deinited(self)) {
4957
return;
5058
}
51-
reset_pin_number(self->pin->number);
52-
self->pin = NULL;
59+
reset_pin_number(self->pin);
60+
pcnt_handler_deinit(&self->unit);
5361
}
5462

5563
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
5664
int16_t count;
57-
pcnt_get_counter_value(PCNT_UNIT_0, &count);
65+
pcnt_get_counter_value(self->unit, &count);
5866
return count+self->count;
5967
}
6068

6169
void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
6270
mp_int_t new_count) {
6371
self->count = new_count;
64-
pcnt_counter_clear(PCNT_UNIT_0);
72+
pcnt_counter_clear(self->unit);
6573
}
6674

6775
void common_hal_countio_counter_reset(countio_counter_obj_t* self) {
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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+
*/
126

227
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
328
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
429

5-
#include "common-hal/microcontroller/Pin.h"
6-
730
#include "py/obj.h"
31+
#include "pcnt_handler.h"
832

933
typedef struct {
1034
mp_obj_base_t base;
11-
const mcu_pin_obj_t * pin;
35+
uint8_t pin;
1236
mp_int_t count;
37+
pcnt_unit_t unit;
1338
} countio_counter_obj_t;
1439

1540
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H

ports/esp32s2/pcnt_handler.c

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

ports/esp32s2/pcnt_handler.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_PCNT_HANDLER_H
28+
#define MICROPY_INCLUDED_ESP32S2_PCNT_HANDLER_H
29+
30+
#include "driver/pcnt.h"
31+
32+
extern void pcnt_handler_init(pcnt_config_t* pcnt_config);
33+
extern void pcnt_handler_deinit(pcnt_unit_t* unit);
34+
35+
#endif // MICROPY_INCLUDED_ESP32S2_PCNT_HANDLER_H

0 commit comments

Comments
 (0)