From bfa5cd9c13e308df5462653a53fe99eeb0543bc3 Mon Sep 17 00:00:00 2001 From: Daniel Pollard Date: Tue, 5 May 2020 15:23:38 +1000 Subject: [PATCH] refactor countio based on feedback --- locale/ID.po | 7 +- locale/circuitpython.pot | 7 +- locale/de_DE.po | 7 +- locale/en_US.po | 7 +- locale/en_x_pirate.po | 7 +- locale/es.po | 7 +- locale/fil.po | 7 +- locale/fr.po | 7 +- locale/it_IT.po | 7 +- locale/ko.po | 7 +- locale/pl.po | 7 +- locale/pt_BR.po | 7 +- locale/zh_Latn_pinyin.po | 7 +- ports/atmel-samd/common-hal/countio/Counter.c | 83 ++++++++++ ports/atmel-samd/common-hal/countio/Counter.h | 19 +++ .../atmel-samd/common-hal/countio/__init__.c | 1 + ports/atmel-samd/eic_handler.c | 7 + ports/atmel-samd/eic_handler.h | 1 + ports/cxd56/mpconfigport.mk | 2 +- ports/litex/mpconfigport.mk | 2 +- ports/mimxrt10xx/mpconfigport.mk | 2 +- ports/nrf/mpconfigport.mk | 2 + ports/stm/mpconfigport.mk | 3 + py/circuitpy_defns.mk | 5 + py/circuitpy_mpconfig.h | 8 + py/circuitpy_mpconfig.mk | 11 ++ shared-bindings/countio/Counter.c | 152 ++++++++++++++++++ shared-bindings/countio/Counter.h | 18 +++ shared-bindings/countio/__init__.c | 48 ++++++ shared-bindings/countio/__init__.h | 9 ++ 30 files changed, 448 insertions(+), 16 deletions(-) create mode 100644 ports/atmel-samd/common-hal/countio/Counter.c create mode 100644 ports/atmel-samd/common-hal/countio/Counter.h create mode 100644 ports/atmel-samd/common-hal/countio/__init__.c create mode 100644 shared-bindings/countio/Counter.c create mode 100644 shared-bindings/countio/Counter.h create mode 100644 shared-bindings/countio/__init__.c create mode 100644 shared-bindings/countio/__init__.h diff --git a/locale/ID.po b/locale/ID.po index ac10df5bf8..e303c7ca56 100644 --- a/locale/ID.po +++ b/locale/ID.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -245,6 +245,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Sebuah channel hardware interrupt sedang digunakan" @@ -1209,6 +1210,10 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "Pin tidak mempunya kemampuan untuk ADC (Analog Digital Converter)" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 4b18bfd478..aa82bfe29c 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -244,6 +244,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "" @@ -1197,6 +1198,10 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/de_DE.po b/locale/de_DE.po index 6cf4b7d479..f7ef9bfeb8 100644 --- a/locale/de_DE.po +++ b/locale/de_DE.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: Pascal Deneaux\n" "Language-Team: Sebastian Plamauer, Pascal Deneaux\n" @@ -246,6 +246,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "3-arg pow() wird nicht unterstützt" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Ein Hardware Interrupt Kanal wird schon benutzt" @@ -1212,6 +1213,10 @@ msgstr "Zugang verweigert" msgid "Pin does not have ADC capabilities" msgstr "Pin hat keine ADC Funktionalität" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/en_US.po b/locale/en_US.po index 428a9c4754..c72e6d0c98 100644 --- a/locale/en_US.po +++ b/locale/en_US.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: \n" @@ -244,6 +244,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "" @@ -1197,6 +1198,10 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/en_x_pirate.po b/locale/en_x_pirate.po index aacc176f5c..1c523bb61e 100644 --- a/locale/en_x_pirate.po +++ b/locale/en_x_pirate.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: @sommersoft, @MrCertainly\n" @@ -246,6 +246,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Avast! A hardware interrupt channel be used already" @@ -1201,6 +1202,10 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "Belay that! Th' Pin be not ADC capable" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/es.po b/locale/es.po index db60c9d37a..fe5086a99a 100644 --- a/locale/es.po +++ b/locale/es.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-08-24 22:56-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -246,6 +246,7 @@ msgstr "0.0 a una potencia compleja" msgid "3-arg pow() not supported" msgstr "pow() con 3 argumentos no soportado" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "El canal EXTINT ya está siendo utilizado" @@ -1211,6 +1212,10 @@ msgstr "Permiso denegado" msgid "Pin does not have ADC capabilities" msgstr "Pin no tiene capacidad ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/fil.po b/locale/fil.po index 6d577823fa..f1178c8cad 100644 --- a/locale/fil.po +++ b/locale/fil.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-12-20 22:15-0800\n" "Last-Translator: Timothy \n" "Language-Team: fil\n" @@ -247,6 +247,7 @@ msgstr "0.0 para sa complex power" msgid "3-arg pow() not supported" msgstr "3-arg pow() hindi suportado" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Isang channel ng hardware interrupt ay ginagamit na" @@ -1217,6 +1218,10 @@ msgstr "Walang pahintulot" msgid "Pin does not have ADC capabilities" msgstr "Ang pin ay walang kakayahan sa ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/fr.po b/locale/fr.po index 4d07eda19b..ab12be1b6e 100644 --- a/locale/fr.po +++ b/locale/fr.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2019-04-14 20:05+0100\n" "Last-Translator: Pierrick Couturier \n" "Language-Team: fr\n" @@ -248,6 +248,7 @@ msgstr "0.0 à une puissance complexe" msgid "3-arg pow() not supported" msgstr "pow() non supporté avec 3 arguments" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Un canal d'interruptions matérielles est déjà utilisé" @@ -1231,6 +1232,10 @@ msgstr "Permission refusée" msgid "Pin does not have ADC capabilities" msgstr "La broche ne peut être utilisée pour l'ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/it_IT.po b/locale/it_IT.po index 0c0825e5ec..826b378891 100644 --- a/locale/it_IT.po +++ b/locale/it_IT.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-10-02 16:27+0200\n" "Last-Translator: Enrico Paganin \n" "Language-Team: \n" @@ -246,6 +246,7 @@ msgstr "0.0 elevato alla potenza di un numero complesso" msgid "3-arg pow() not supported" msgstr "pow() con tre argmomenti non supportata" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Un canale di interrupt hardware è già in uso" @@ -1226,6 +1227,10 @@ msgstr "Permesso negato" msgid "Pin does not have ADC capabilities" msgstr "Il pin non ha capacità di ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/ko.po b/locale/ko.po index 3e3ac1cf60..f6bff27cec 100644 --- a/locale/ko.po +++ b/locale/ko.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2019-05-06 14:22-0700\n" "Last-Translator: \n" "Language-Team: LANGUAGE \n" @@ -246,6 +246,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "" @@ -1201,6 +1202,10 @@ msgstr "" msgid "Pin does not have ADC capabilities" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/pl.po b/locale/pl.po index 8cc3c30b7a..fa19bfc194 100644 --- a/locale/pl.po +++ b/locale/pl.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2019-03-19 18:37-0700\n" "Last-Translator: Radomir Dopieralski \n" "Language-Team: pl\n" @@ -245,6 +245,7 @@ msgstr "0.0 do potęgi zespolonej" msgid "3-arg pow() not supported" msgstr "3-argumentowy pow() jest niewspierany" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Kanał przerwań sprzętowych w użyciu" @@ -1202,6 +1203,10 @@ msgstr "Odmowa dostępu" msgid "Pin does not have ADC capabilities" msgstr "Nóżka nie obsługuje ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/pt_BR.po b/locale/pt_BR.po index d833169c0f..bf56405c64 100644 --- a/locale/pt_BR.po +++ b/locale/pt_BR.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2018-10-02 21:14-0000\n" "Last-Translator: \n" "Language-Team: \n" @@ -246,6 +246,7 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Um canal de interrupção de hardware já está em uso" @@ -1212,6 +1213,10 @@ msgstr "Permissão negada" msgid "Pin does not have ADC capabilities" msgstr "O pino não tem recursos de ADC" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/locale/zh_Latn_pinyin.po b/locale/zh_Latn_pinyin.po index e7d6df02fa..52337cdb43 100644 --- a/locale/zh_Latn_pinyin.po +++ b/locale/zh_Latn_pinyin.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: circuitpython-cn\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-04-27 18:34-0700\n" +"POT-Creation-Date: 2020-05-05 14:57+1000\n" "PO-Revision-Date: 2019-04-13 10:10-0700\n" "Last-Translator: hexthat\n" "Language-Team: Chinese Hanyu Pinyin\n" @@ -251,6 +251,7 @@ msgstr "0.0 dào fùzá diànyuán" msgid "3-arg pow() not supported" msgstr "bù zhīchí 3-arg pow ()" +#: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c msgid "A hardware interrupt channel is already in use" msgstr "Yìngjiàn zhōngduàn tōngdào yǐ zài shǐyòng zhōng" @@ -1216,6 +1217,10 @@ msgstr "Quánxiàn bèi jùjué" msgid "Pin does not have ADC capabilities" msgstr "Pin méiyǒu ADC nénglì" +#: ports/atmel-samd/common-hal/countio/Counter.c +msgid "Pin must support hardware interrupts" +msgstr "" + #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin number already reserved by EXTI" msgstr "" diff --git a/ports/atmel-samd/common-hal/countio/Counter.c b/ports/atmel-samd/common-hal/countio/Counter.c new file mode 100644 index 0000000000..db5b325b8f --- /dev/null +++ b/ports/atmel-samd/common-hal/countio/Counter.c @@ -0,0 +1,83 @@ + +#include "common-hal/countio/Counter.h" + +#include "atmel_start_pins.h" + +#include "eic_handler.h" +#include "samd/external_interrupts.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_a) { + if (!pin_a->has_extint) { + mp_raise_RuntimeError(translate("Pin must support hardware interrupts")); + } + + + if (eic_get_enable()) { + if (!eic_channel_free(pin_a->extint_channel)) { + mp_raise_RuntimeError(translate("A hardware interrupt channel is already in use")); + } + } else { + turn_on_external_interrupt_controller(); + } + + // These default settings apply when the EIC isn't yet enabled. + self->eic_channel_a = pin_a->extint_channel; + + self->pin_a = pin_a->number; + + gpio_set_pin_function(self->pin_a, GPIO_PIN_FUNCTION_A); + gpio_set_pin_pull_mode(self->pin_a, GPIO_PULL_UP); + + set_eic_channel_data(self->eic_channel_a, (void*) self); + + self->count = 0; + + + claim_pin(pin_a); + + + set_eic_handler(self->eic_channel_a, EIC_HANDLER_COUNTER); + turn_on_eic_channel(self->eic_channel_a, EIC_CONFIG_SENSE0_FALL_Val); + +} + +bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) { + return self->pin_a == NO_PIN; +} + +void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { + if (common_hal_countio_counter_deinited(self)) { + return; + } + + set_eic_handler(self->eic_channel_a, EIC_HANDLER_NO_INTERRUPT); + turn_off_eic_channel(self->eic_channel_a); + + + reset_pin_number(self->pin_a); + self->pin_a = NO_PIN; + +} + +mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { + return self->count; +} + +void common_hal_countio_counter_set_count(countio_counter_obj_t* self, + mp_int_t new_count) { + self->count = new_count; +} + +void common_hal_countio_counter_reset(countio_counter_obj_t* self){ + self->count = 0; +} + +void counter_interrupt_handler(uint8_t channel) { + countio_counter_obj_t* self = get_eic_channel_data(channel); + + self->count += 1; + +} diff --git a/ports/atmel-samd/common-hal/countio/Counter.h b/ports/atmel-samd/common-hal/countio/Counter.h new file mode 100644 index 0000000000..724c734152 --- /dev/null +++ b/ports/atmel-samd/common-hal/countio/Counter.h @@ -0,0 +1,19 @@ + +#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H +#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNTER_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin_a; + uint8_t eic_channel_a:4; + mp_int_t count; +} countio_counter_obj_t; + + +void counter_interrupt_handler(uint8_t channel); + +#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_COUNTIO_COUNT_H diff --git a/ports/atmel-samd/common-hal/countio/__init__.c b/ports/atmel-samd/common-hal/countio/__init__.c new file mode 100644 index 0000000000..b95b20d153 --- /dev/null +++ b/ports/atmel-samd/common-hal/countio/__init__.c @@ -0,0 +1 @@ +//No countio module functions diff --git a/ports/atmel-samd/eic_handler.c b/ports/atmel-samd/eic_handler.c index 30ecaa7cae..7f26bdefbf 100644 --- a/ports/atmel-samd/eic_handler.c +++ b/ports/atmel-samd/eic_handler.c @@ -27,6 +27,7 @@ #include "common-hal/pulseio/PulseIn.h" #include "common-hal/ps2io/Ps2.h" #include "common-hal/rotaryio/IncrementalEncoder.h" +#include "common-hal/countio/Counter.h" #include "shared-bindings/microcontroller/__init__.h" //#include "samd/external_interrupts.h" #include "eic_handler.h" @@ -59,6 +60,12 @@ void shared_eic_handler(uint8_t channel) { break; #endif +#if CIRCUITPY_COUNTIO + case EIC_HANDLER_COUNTER: + counter_interrupt_handler(channel); + break; +#endif + default: break; } diff --git a/ports/atmel-samd/eic_handler.h b/ports/atmel-samd/eic_handler.h index 71b0fce678..08da7ea34d 100644 --- a/ports/atmel-samd/eic_handler.h +++ b/ports/atmel-samd/eic_handler.h @@ -30,6 +30,7 @@ #define EIC_HANDLER_PULSEIN 0x1 #define EIC_HANDLER_INCREMENTAL_ENCODER 0x2 #define EIC_HANDLER_PS2 0x3 +#define EIC_HANDLER_COUNTER 0x04 void set_eic_handler(uint8_t channel, uint8_t eic_handler); void shared_eic_handler(uint8_t channel); diff --git a/ports/cxd56/mpconfigport.mk b/ports/cxd56/mpconfigport.mk index ad3d0e72df..8aeae1de56 100644 --- a/ports/cxd56/mpconfigport.mk +++ b/ports/cxd56/mpconfigport.mk @@ -18,5 +18,5 @@ CIRCUITPY_NEOPIXEL_WRITE = 0 CIRCUITPY_NVM = 0 CIRCUITPY_DISPLAYIO = 0 CIRCUITPY_FREQUENCYIO = 0 - +CIRCUITPY_COUNTIO = 0 INTERNAL_LIBM = 1 diff --git a/ports/litex/mpconfigport.mk b/ports/litex/mpconfigport.mk index 0a82f41619..004930ba70 100644 --- a/ports/litex/mpconfigport.mk +++ b/ports/litex/mpconfigport.mk @@ -25,7 +25,7 @@ CIRCUITPY_NVM = 0 CIRCUITPY_PULSEIO = 0 CIRCUITPY_ROTARYIO = 0 CIRCUITPY_RTC = 0 - +CIRCUITPY_COUNTIO = 0 # Enable USB support CIRCUITPY_USB_HID = 1 CIRCUITPY_USB_MIDI = 1 diff --git a/ports/mimxrt10xx/mpconfigport.mk b/ports/mimxrt10xx/mpconfigport.mk index 5881ac3020..e9e554142c 100644 --- a/ports/mimxrt10xx/mpconfigport.mk +++ b/ports/mimxrt10xx/mpconfigport.mk @@ -26,5 +26,5 @@ CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_I2CSLAVE = 0 CIRCUITPY_NVM = 0 CIRCUITPY_ROTARYIO = 0 - +CIRCUITPY_COUNTIO = 0 LONGINT_IMPL = MPZ diff --git a/ports/nrf/mpconfigport.mk b/ports/nrf/mpconfigport.mk index 410d3750e5..7939a175bd 100644 --- a/ports/nrf/mpconfigport.mk +++ b/ports/nrf/mpconfigport.mk @@ -54,6 +54,8 @@ CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_RGBMATRIX ?= 1 CIRCUITPY_FRAMEBUFFERIO ?= 1 +CIRCUITPY_COUNTIO = 0 + # nRF52840-specific ifeq ($(MCU_CHIP),nrf52840) diff --git a/ports/stm/mpconfigport.mk b/ports/stm/mpconfigport.mk index 5ef1e8e7b7..ee8b2e1601 100644 --- a/ports/stm/mpconfigport.mk +++ b/ports/stm/mpconfigport.mk @@ -8,6 +8,7 @@ ifeq ($(MCU_SERIES),F4) CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOIO = 0 CIRCUITPY_ROTARYIO = 0 + CIRCUITPY_COUNTIO = 0 CIRCUITPY_RTC = 0 CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_I2CSLAVE = 0 @@ -26,6 +27,7 @@ ifeq ($(MCU_SERIES),H7) CIRCUITPY_NVM = 0 CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOIO = 0 + CIRCUITPY_COUNTIO = 0 CIRCUITPY_ROTARYIO = 0 CIRCUITPY_RTC = 0 CIRCUITPY_FREQUENCYIO = 0 @@ -46,6 +48,7 @@ ifeq ($(MCU_SERIES),F7) CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOIO = 0 CIRCUITPY_ROTARYIO = 0 + CIRCUITPY_COUNTIO = 0 CIRCUITPY_RTC = 0 CIRCUITPY_FREQUENCYIO = 0 CIRCUITPY_I2CSLAVE = 0 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index ec8e6c3e98..a253f2fa82 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -136,6 +136,9 @@ endif ifeq ($(CIRCUITPY_BUSIO),1) SRC_PATTERNS += busio/% bitbangio/OneWire.% endif +ifeq ($(CIRCUITPY_COUNTIO),1) +SRC_PATTERNS += countio/% +endif ifeq ($(CIRCUITPY_DIGITALIO),1) SRC_PATTERNS += digitalio/% endif @@ -263,6 +266,8 @@ SRC_COMMON_HAL_ALL = \ busio/SPI.c \ busio/UART.c \ busio/__init__.c \ + countio/Counter.c \ + countio/__init__.c \ digitalio/DigitalInOut.c \ digitalio/__init__.c \ displayio/ParallelBus.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 29a4d524ca..a3ab4da941 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -314,6 +314,13 @@ extern const struct _mp_obj_module_t busio_module; #define BUSIO_MODULE #endif +#if CIRCUITPY_COUNTIO +extern const struct _mp_obj_module_t countio_module; +#define COUNTIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_countio), (mp_obj_t)&countio_module }, +#else +#define COUNTIO_MODULE +#endif + #if CIRCUITPY_DIGITALIO extern const struct _mp_obj_module_t digitalio_module; #define DIGITALIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, @@ -630,6 +637,7 @@ extern const struct _mp_obj_module_t ustack_module; BLEIO_MODULE \ BOARD_MODULE \ BUSIO_MODULE \ + COUNTIO_MODULE \ DIGITALIO_MODULE \ DISPLAYIO_MODULE \ FONTIO_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 30ab720d70..7ef87a7ea2 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -109,6 +109,12 @@ CIRCUITPY_DIGITALIO = 1 endif CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO) +ifndef CIRCUITPY_COUNTIO +CIRCUITPY_COUNTIO = 1 +endif +CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO) + + ifndef CIRCUITPY_DISPLAYIO CIRCUITPY_DISPLAYIO = $(CIRCUITPY_FULL_BUILD) endif @@ -207,6 +213,11 @@ CIRCUITPY_ROTARYIO = 1 endif CFLAGS += -DCIRCUITPY_ROTARYIO=$(CIRCUITPY_ROTARYIO) +ifndef CIRCUITPY_COUNTIO +CIRCUITPY_COUNTIO = 1 +endif +CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO) + ifndef CIRCUITPY_RTC CIRCUITPY_RTC = 1 endif diff --git a/shared-bindings/countio/Counter.c b/shared-bindings/countio/Counter.c new file mode 100644 index 0000000000..c10294199a --- /dev/null +++ b/shared-bindings/countio/Counter.c @@ -0,0 +1,152 @@ + +#include + +#include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/runtime0.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/countio/Counter.h" +#include "shared-bindings/util.h" + +//| .. currentmodule:: countio +//| +//| :class:`Counter` -- Track the count of edge transistions (pulses) on a given pin +//| ==================================================================================== +//| +//| Counter will keep track of the number of edge transistions (pulses) on a given pin +//| +//| .. class:: Counter(pin_a) +//| +//| Create an Counter object associated with the given pin. It tracks the number of +//| pulses relative when the object is contructed. +//| +//| :param ~microcontroller.Pin pin_a: Pin to read pulses from. +//| +//| +//| For example:: +//| +//| import countio +//| import time +//| from board import * +//| +//| pin_counter = countio.Counter(board.D1) +//| +//| while True: +//| count = pin_counter.count +//| if count == None 10: +//| pin_counter.reset() +//| print(pin_counter.count) +//| +STATIC mp_obj_t countio_counter_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_pin_a }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin_a, MP_ARG_REQUIRED | MP_ARG_OBJ } + + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + const mcu_pin_obj_t* pin_a = validate_obj_is_free_pin(args[ARG_pin_a].u_obj); + + + countio_counter_obj_t *self = m_new_obj(countio_counter_obj_t); + self->base.type = &countio_counter_type; + + common_hal_countio_counter_construct(self, pin_a); + + return MP_OBJ_FROM_PTR(self); +} + +//| .. method:: deinit() +//| +//| Deinitializes the Counter and releases any hardware resources for reuse. +//| +STATIC mp_obj_t countio_counter_deinit(mp_obj_t self_in) { + countio_counter_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_countio_counter_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(countio_counter_deinit_obj, countio_counter_deinit); + +STATIC void check_for_deinit(countio_counter_obj_t *self) { + if (common_hal_countio_counter_deinited(self)) { + raise_deinited_error(); + } +} + +//| .. method:: __enter__() +//| +//| No-op used by Context Managers. +//| +// Provided by context manager helper. + +//| .. method:: __exit__() +//| +//| Automatically deinitializes the hardware when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| +STATIC mp_obj_t countio_counter_obj___exit__(size_t n_args, const mp_obj_t *args) { + (void)n_args; + common_hal_countio_counter_deinit(args[0]); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(countio_counter___exit___obj, 4, 4, countio_counter_obj___exit__); + + +//| .. attribute:: count +//| +//| The current count in terms of pulses. +//| +//| +STATIC mp_obj_t countio_counter_obj_get_count(mp_obj_t self_in) { + countio_counter_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + + return mp_obj_new_int(common_hal_countio_counter_get_count(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(countio_counter_get_count_obj, countio_counter_obj_get_count); + +STATIC mp_obj_t countio_counter_obj_set_count(mp_obj_t self_in, mp_obj_t new_count) { + countio_counter_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + + common_hal_countio_counter_set_count(self, mp_obj_get_int(new_count)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(countio_counter_set_count_obj, countio_counter_obj_set_count); + +const mp_obj_property_t countio_counter_count_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&countio_counter_get_count_obj, + (mp_obj_t)&countio_counter_set_count_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC mp_obj_t countio_counter_reset(mp_obj_t self_in){ + countio_counter_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + //set the position to zero for reset + common_hal_countio_counter_reset(self); + return mp_const_none; +} + + +MP_DEFINE_CONST_FUN_OBJ_1(countio_counter_reset_obj, countio_counter_reset); + +STATIC const mp_rom_map_elem_t countio_counter_locals_dict_table[] = { + // Methods + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&countio_counter_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&countio_counter___exit___obj) }, + { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&countio_counter_count_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&countio_counter_reset_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(countio_counter_locals_dict, countio_counter_locals_dict_table); + +const mp_obj_type_t countio_counter_type = { + { &mp_type_type }, + .name = MP_QSTR_Counter, + .make_new = countio_counter_make_new, + .locals_dict = (mp_obj_dict_t*)&countio_counter_locals_dict, +}; diff --git a/shared-bindings/countio/Counter.h b/shared-bindings/countio/Counter.h new file mode 100644 index 0000000000..15adbbe2ac --- /dev/null +++ b/shared-bindings/countio/Counter.h @@ -0,0 +1,18 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO_COUNTER_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO_COUNTER_H + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/countio/Counter.h" + +extern const mp_obj_type_t countio_counter_type; + +extern void common_hal_countio_counter_construct(countio_counter_obj_t* self, + const mcu_pin_obj_t* pin_a); +extern void common_hal_countio_counter_deinit(countio_counter_obj_t* self); +extern bool common_hal_countio_counter_deinited(countio_counter_obj_t* self); +extern mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self); +extern void common_hal_countio_counter_set_count(countio_counter_obj_t* self, + mp_int_t new_count); +extern void common_hal_countio_counter_reset(countio_counter_obj_t* self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO_COUNTER_H diff --git a/shared-bindings/countio/__init__.c b/shared-bindings/countio/__init__.c new file mode 100644 index 0000000000..8929bf7edb --- /dev/null +++ b/shared-bindings/countio/__init__.c @@ -0,0 +1,48 @@ + +#include + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/countio/__init__.h" +#include "shared-bindings/countio/Counter.h" + +//| :mod:`countio` --- Support for edge counting +//| ======================================================== +//| +//| .. module:: countio +//| :synopsis: Support for edge counting +//| :platform: SAMD +//| +//| The `countio` module contains logic to read and count edge transistions +//| +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| Counter +//| + +//| .. warning:: This module is not available in some SAMD21 (aka M0) builds. See the +//| :ref:`module-support-matrix` for more info. +//| + +//| All classes change hardware state and should be deinitialized when they +//| are no longer needed if the program continues after use. To do so, either +//| call :py:meth:`!deinit` or use a context manager. See +//| :ref:`lifetime-and-contextmanagers` for more info. +//| + +STATIC const mp_rom_map_elem_t countio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_countio) }, + { MP_ROM_QSTR(MP_QSTR_Counter), MP_ROM_PTR(&countio_counter_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(countio_module_globals, countio_module_globals_table); + +const mp_obj_module_t countio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&countio_module_globals, +}; diff --git a/shared-bindings/countio/__init__.h b/shared-bindings/countio/__init__.h new file mode 100644 index 0000000000..35ae9f0354 --- /dev/null +++ b/shared-bindings/countio/__init__.h @@ -0,0 +1,9 @@ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO___INIT___H + +#include "py/obj.h" + +// Nothing now. + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_COUNTIO___INIT___H