Add pcnt handler

This commit is contained in:
microDev 2020-11-01 18:00:07 +05:30
parent 5e3bfa1956
commit 4438050f79
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
6 changed files with 177 additions and 36 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-21 20:13-0500\n"
"POT-Creation-Date: 2020-11-01 11:11+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -1109,7 +1109,8 @@ msgid "Invalid phase"
msgstr ""
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
#: ports/atmel-samd/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
#: shared-module/rgbmatrix/RGBMatrix.c
msgid "Invalid pin"
msgstr ""
@ -1271,6 +1272,10 @@ msgstr ""
msgid "No MOSI Pin"
msgstr ""
#: ports/esp32s2/pcnt_handler.c
msgid "No PCNT unit free"
msgstr ""
#: ports/atmel-samd/common-hal/busio/UART.c
#: ports/esp32s2/common-hal/busio/UART.c
#: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/UART.c

View File

@ -173,6 +173,7 @@ SRC_C += \
background.c \
fatfs_port.c \
mphalport.c \
pcnt_handler.c \
bindings/espidf/__init__.c \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \

View File

@ -1,67 +1,75 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "common-hal/countio/Counter.h"
#include "common-hal/microcontroller/Pin.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
const mcu_pin_obj_t* pin) {
claim_pin(pin);
#include "driver/pcnt.h"
static void pcnt_init(countio_counter_obj_t* self) {
int unit = PCNT_UNIT_0;
// Prepare configuration for the PCNT unit
pcnt_config_t pcnt_config = {
// Set PCNT input signal and control GPIOs
.pulse_gpio_num = self->pin->number,
.pulse_gpio_num = pin->number,
.ctrl_gpio_num = PCNT_PIN_NOT_USED,
.channel = PCNT_CHANNEL_0,
.unit = unit,
// What to do on the positive / negative edge of pulse input?
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
};
// Initialize PCNT unit
pcnt_unit_config(&pcnt_config);
pcnt_handler_init(&pcnt_config);
// Configure and enable the input filter
pcnt_set_filter_value(unit, 100);
pcnt_filter_enable(unit);
// Initialize PCNT's counter
pcnt_counter_pause(unit);
pcnt_counter_clear(unit);
// Everything is set up, now go to counting
pcnt_counter_resume(unit);
}
void common_hal_countio_counter_construct(countio_counter_obj_t* self,
const mcu_pin_obj_t* pin) {
claim_pin(pin);
self->pin = pin;
pcnt_init(self);
self->pin = pin->number;
self->unit = pcnt_config.unit;
}
bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) {
return self->pin == NULL;
return self->unit == PCNT_UNIT_MAX;
}
void common_hal_countio_counter_deinit(countio_counter_obj_t* self) {
if (common_hal_countio_counter_deinited(self)) {
return;
}
reset_pin_number(self->pin->number);
self->pin = NULL;
reset_pin_number(self->pin);
pcnt_handler_deinit(&self->unit);
}
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) {
int16_t count;
pcnt_get_counter_value(PCNT_UNIT_0, &count);
pcnt_get_counter_value(self->unit, &count);
return count+self->count;
}
void common_hal_countio_counter_set_count(countio_counter_obj_t* self,
mp_int_t new_count) {
self->count = new_count;
pcnt_counter_clear(PCNT_UNIT_0);
pcnt_counter_clear(self->unit);
}
void common_hal_countio_counter_reset(countio_counter_obj_t* self) {

View File

@ -1,15 +1,40 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNTER_H
#include "common-hal/microcontroller/Pin.h"
#include "py/obj.h"
#include "pcnt_handler.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t * pin;
uint8_t pin;
mp_int_t count;
pcnt_unit_t unit;
} countio_counter_obj_t;
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_COUNTIO_COUNT_H

View File

@ -0,0 +1,67 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "pcnt_handler.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
#define PCNT_UNIT_ACTIVE 1
#define PCNT_UNIT_INACTIVE 0
static uint8_t pcnt_state[4];
void pcnt_handler_init(pcnt_config_t* pcnt_config) {
// Look for available pcnt unit
for (uint8_t i = 0; i<=3; i++) {
if (pcnt_state[i] == PCNT_UNIT_INACTIVE) {
pcnt_config->unit = (pcnt_unit_t)i;
pcnt_state[i] = PCNT_UNIT_ACTIVE;
break;
} else if (i == 3) {
mp_raise_RuntimeError(translate("No PCNT unit free"));
}
}
// Initialize PCNT unit
pcnt_unit_config(pcnt_config);
// Configure and enable the input filter
pcnt_set_filter_value(pcnt_config->unit, 100);
pcnt_filter_enable(pcnt_config->unit);
// Initialize PCNT's counter
pcnt_counter_pause(pcnt_config->unit);
pcnt_counter_clear(pcnt_config->unit);
// Everything is set up, now go to counting
pcnt_counter_resume(pcnt_config->unit);
}
void pcnt_handler_deinit(pcnt_unit_t* unit) {
pcnt_state[*unit] = PCNT_UNIT_INACTIVE;
*unit = PCNT_UNIT_MAX;
}

View File

@ -0,0 +1,35 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_PCNT_HANDLER_H
#define MICROPY_INCLUDED_ESP32S2_PCNT_HANDLER_H
#include "driver/pcnt.h"
extern void pcnt_handler_init(pcnt_config_t* pcnt_config);
extern void pcnt_handler_deinit(pcnt_unit_t* unit);
#endif // MICROPY_INCLUDED_ESP32S2_PCNT_HANDLER_H