summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
Diffstat (limited to 'ports')
-rw-r--r--ports/atmel-samd/common-hal/countio/Counter.c83
-rw-r--r--ports/atmel-samd/common-hal/countio/Counter.h19
-rw-r--r--ports/atmel-samd/common-hal/countio/__init__.c1
-rw-r--r--ports/atmel-samd/eic_handler.c7
-rw-r--r--ports/atmel-samd/eic_handler.h1
-rw-r--r--ports/cxd56/mpconfigport.mk2
-rw-r--r--ports/litex/mpconfigport.mk2
-rw-r--r--ports/mimxrt10xx/mpconfigport.mk2
-rw-r--r--ports/nrf/mpconfigport.mk2
-rw-r--r--ports/stm/mpconfigport.mk3
10 files changed, 119 insertions, 3 deletions
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 000000000..db5b325b8
--- /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 000000000..724c73415
--- /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 000000000..b95b20d15
--- /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 30ecaa7ca..7f26bdefb 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 71b0fce67..08da7ea34 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 ad3d0e72d..8aeae1de5 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 0a82f4161..004930ba7 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 5881ac302..e9e554142 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 410d3750e..7939a175b 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 5ef1e8e7b..ee8b2e160 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