From 379f454ce6480376eccc8ccf378524d30fdace7a Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Tue, 23 Feb 2021 19:26:11 -0600 Subject: [PATCH 1/4] Adding countio to rp2040 --- locale/circuitpython.pot | 150 +++++++++++++++++- .../raspberrypi/common-hal/countio/Counter.c | 81 ++++++++++ .../raspberrypi/common-hal/countio/Counter.h | 19 +++ .../raspberrypi/common-hal/countio/__init__.c | 1 + ports/raspberrypi/mpconfigport.h | 5 +- ports/raspberrypi/mpconfigport.mk | 1 - 6 files changed, 250 insertions(+), 7 deletions(-) create mode 100644 ports/raspberrypi/common-hal/countio/Counter.c create mode 100644 ports/raspberrypi/common-hal/countio/Counter.h create mode 100644 ports/raspberrypi/common-hal/countio/__init__.c diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index de902f62fc..4fd6e99296 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -339,6 +339,10 @@ msgstr "" msgid "All event channels in use" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "All state machines in use" +msgstr "" + #: ports/atmel-samd/audio_dma.c ports/atmel-samd/common-hal/audiobusio/PDMIn.c msgid "All sync event channels in use" msgstr "" @@ -387,6 +391,7 @@ msgstr "" #: ports/cxd56/common-hal/analogio/AnalogOut.c #: ports/mimxrt10xx/common-hal/analogio/AnalogOut.c #: ports/nrf/common-hal/analogio/AnalogOut.c +#: ports/raspberrypi/common-hal/analogio/AnalogOut.c msgid "AnalogOut functionality not supported" msgstr "" @@ -533,6 +538,7 @@ msgstr "" #: ports/atmel-samd/common-hal/displayio/ParallelBus.c #: ports/esp32s2/common-hal/displayio/ParallelBus.c #: ports/nrf/common-hal/displayio/ParallelBus.c +#: ports/raspberrypi/common-hal/displayio/ParallelBus.c #, c-format msgid "Bus pin %d is already in use" msgstr "" @@ -586,6 +592,7 @@ msgstr "" #: ports/atmel-samd/common-hal/digitalio/DigitalInOut.c #: ports/mimxrt10xx/common-hal/digitalio/DigitalInOut.c #: ports/nrf/common-hal/digitalio/DigitalInOut.c +#: ports/raspberrypi/common-hal/digitalio/DigitalInOut.c msgid "Cannot get pull while in output mode" msgstr "" @@ -863,7 +870,8 @@ msgstr "" msgid "Error: Failure to bind" msgstr "" -#: py/enum.c shared-bindings/_bleio/__init__.c shared-bindings/aesio/aes.c +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c py/enum.c +#: shared-bindings/_bleio/__init__.c shared-bindings/aesio/aes.c #: shared-bindings/busio/SPI.c shared-bindings/microcontroller/Pin.c #: shared-bindings/neopixel_write/__init__.c #: shared-bindings/terminalio/Terminal.c @@ -1045,6 +1053,10 @@ msgstr "" msgid "I2C Init Error" msgstr "" +#: ports/raspberrypi/common-hal/busio/I2C.c +msgid "I2C peripheral in use" +msgstr "" + #: shared-bindings/audiobusio/I2SOut.c msgid "I2SOut not available" msgstr "" @@ -1068,6 +1080,10 @@ msgstr "" msgid "Incorrect buffer size" msgstr "" +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Init program size invalid" +msgstr "" + #: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c msgid "Initialization failed due to lack of memory" msgstr "" @@ -1080,6 +1096,31 @@ msgstr "" msgid "Input/output error" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Instruction %d jumps on pin" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Instruction %d shifts in more bits than pin count" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Instruction %d shifts out more bits than pin count" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Instruction %d uses extra pin" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Instruction %d waits on input outside of count" +msgstr "" + #: ports/nrf/common-hal/_bleio/__init__.c msgid "Insufficient authentication" msgstr "" @@ -1131,7 +1172,7 @@ msgstr "" #: ports/atmel-samd/common-hal/pwmio/PWMOut.c #: ports/cxd56/common-hal/pwmio/PWMOut.c ports/nrf/common-hal/pwmio/PWMOut.c -#: shared-bindings/pwmio/PWMOut.c +#: ports/raspberrypi/common-hal/pwmio/PWMOut.c shared-bindings/pwmio/PWMOut.c msgid "Invalid PWM frequency" msgstr "" @@ -1225,6 +1266,8 @@ msgstr "" #: ports/mimxrt10xx/common-hal/busio/I2C.c #: ports/mimxrt10xx/common-hal/busio/SPI.c #: ports/mimxrt10xx/common-hal/busio/UART.c ports/nrf/common-hal/busio/I2C.c +#: ports/raspberrypi/common-hal/busio/I2C.c +#: ports/raspberrypi/common-hal/busio/SPI.c msgid "Invalid pins" msgstr "" @@ -1346,6 +1389,36 @@ msgstr "" msgid "Missing MISO or MOSI Pin" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_in_pin. Instruction %d reads pin(s)" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_in_pin. Instruction %d shifts in from pin(s)" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_in_pin. Instruction %d waits based on pin" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_out_pin. Instruction %d shifts out to pin(s)" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_out_pin. Instruction %d writes pin(s)" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +#, c-format +msgid "Missing first_set_pin. Instruction %d sets pin(s)" +msgstr "" + #: shared-bindings/displayio/Group.c msgid "Must be a %q subclass." msgstr "" @@ -1399,14 +1472,14 @@ 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 -#: ports/stm/common-hal/busio/UART.c +#: ports/raspberrypi/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c msgid "No RX pin" 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 -#: ports/stm/common-hal/busio/UART.c +#: ports/raspberrypi/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c msgid "No TX pin" msgstr "" @@ -1463,6 +1536,10 @@ msgstr "" msgid "No network with that ssid" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "No out in program" +msgstr "" + #: shared-module/touchio/TouchIn.c msgid "No pulldown on pin; 1Mohm recommended" msgstr "" @@ -1518,6 +1595,10 @@ msgstr "" msgid "Only 8 or 16 bit mono with " msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "Only IN/OUT of up to 8 supported" +msgstr "" + #: ports/esp32s2/common-hal/wifi/__init__.c msgid "Only IPv4 addresses supported" msgstr "" @@ -1602,11 +1683,20 @@ msgstr "" msgid "Permission denied" msgstr "" +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Pin count must be at least 1" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "Pin count too large" +msgstr "" + #: ports/atmel-samd/common-hal/analogio/AnalogIn.c #: ports/cxd56/common-hal/analogio/AnalogIn.c #: ports/esp32s2/common-hal/analogio/AnalogIn.c #: ports/mimxrt10xx/common-hal/analogio/AnalogIn.c #: ports/nrf/common-hal/analogio/AnalogIn.c +#: ports/raspberrypi/common-hal/analogio/AnalogIn.c #: ports/stm/common-hal/analogio/AnalogIn.c msgid "Pin does not have ADC capabilities" msgstr "" @@ -1616,6 +1706,10 @@ msgstr "" msgid "Pin is input only" msgstr "" +#: ports/raspberrypi/common-hal/countio/Counter.c +msgid "Pin must be on PWM Channel B" +msgstr "" + #: ports/atmel-samd/common-hal/countio/Counter.c msgid "Pin must support hardware interrupts" msgstr "" @@ -1667,6 +1761,26 @@ msgstr "" msgid "Pretending to deep sleep until alarm, CTRL-C or file write.\n" msgstr "" +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "Program does IN without loading ISR" +msgstr "" + +#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c +msgid "Program does OUT without loading OSR" +msgstr "" + +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Program must contain at least one 16-bit instruction." +msgstr "" + +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Program size invalid" +msgstr "" + +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Program too large" +msgstr "" + #: shared-bindings/digitalio/DigitalInOut.c msgid "Pull not used when direction is output." msgstr "" @@ -1753,6 +1867,7 @@ msgstr "" #: ports/atmel-samd/common-hal/busio/I2C.c ports/esp32s2/common-hal/busio/I2C.c #: ports/mimxrt10xx/common-hal/busio/I2C.c ports/nrf/common-hal/busio/I2C.c +#: ports/raspberrypi/common-hal/busio/I2C.c msgid "SDA or SCL needs a pull up" msgstr "" @@ -1774,6 +1889,10 @@ msgstr "" msgid "SPI Re-initialization error" msgstr "" +#: ports/raspberrypi/common-hal/busio/SPI.c +msgid "SPI peripheral in use" +msgstr "" + #: shared-bindings/audiomixer/Mixer.c msgid "Sample rate must be positive" msgstr "" @@ -1804,6 +1923,14 @@ msgstr "" msgid "Server side context cannot have hostname" msgstr "" +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Set pin count must be between 1 and 5" +msgstr "" + +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "Side set pin count must be between 1 and 5" +msgstr "" + #: ports/cxd56/common-hal/camera/Camera.c msgid "Size not supported" msgstr "" @@ -1968,6 +2095,10 @@ msgstr "" msgid "UART Re-init error" msgstr "" +#: ports/raspberrypi/common-hal/busio/UART.c +msgid "UART not yet supported" +msgstr "" + #: ports/stm/common-hal/busio/UART.c msgid "UART write error" msgstr "" @@ -2072,7 +2203,8 @@ msgid "" msgstr "" #: ports/atmel-samd/common-hal/busio/I2C.c ports/cxd56/common-hal/busio/I2C.c -#: ports/esp32s2/common-hal/busio/UART.c ports/stm/common-hal/busio/I2C.c +#: ports/esp32s2/common-hal/busio/UART.c +#: ports/raspberrypi/common-hal/busio/I2C.c ports/stm/common-hal/busio/I2C.c msgid "Unsupported baudrate" msgstr "" @@ -3521,6 +3653,14 @@ msgstr "" msgid "pressing both buttons at start up.\n" msgstr "" +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "pull_threshold must be between 1 and 32" +msgstr "" + +#: ports/raspberrypi/bindings/rp2pio/StateMachine.c +msgid "push_threshold must be between 1 and 32" +msgstr "" + #: extmod/modutimeq.c msgid "queue overflow" msgstr "" diff --git a/ports/raspberrypi/common-hal/countio/Counter.c b/ports/raspberrypi/common-hal/countio/Counter.c new file mode 100644 index 0000000000..c5a47d2a41 --- /dev/null +++ b/ports/raspberrypi/common-hal/countio/Counter.c @@ -0,0 +1,81 @@ +#include "common-hal/countio/Counter.h" + +#include "py/runtime.h" +#include "py/mpstate.h" +#include "supervisor/shared/translate.h" + +#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" +#include "src/rp2_common/hardware_pwm/include/hardware/pwm.h" +#include "src/rp2_common/hardware_irq/include/hardware/irq.h" + + +void common_hal_countio_counter_construct(countio_counter_obj_t* self, + const mcu_pin_obj_t* pin_a) { + + if (pwm_gpio_to_channel(pin_a->number) != PWM_CHAN_B) { + mp_raise_RuntimeError(translate("Pin must be on PWM Channel B")); + } + + self->pin_a = pin_a->number; + self->slice_num = pwm_gpio_to_slice_num(self->pin_a); + + pwm_clear_irq(self->slice_num); + pwm_set_irq_enabled(self->slice_num, true); + irq_set_exclusive_handler(PWM_IRQ_WRAP, counter_interrupt_handler); + irq_set_enabled(PWM_IRQ_WRAP, true); + + pwm_config cfg = pwm_get_default_config(); + pwm_config_set_clkdiv_mode(&cfg, PWM_DIV_B_RISING); + pwm_init(self->slice_num, &cfg, false); + gpio_set_function(self->pin_a, GPIO_FUNC_PWM); + + self->count = 0; + + claim_pin(pin_a); + + MP_STATE_PORT(counting) = self; + + pwm_set_enabled(self->slice_num, true); +} + +bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) { + return self->pin_a == 0; +} + +void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { + if (common_hal_countio_counter_deinited(self)) { + return; + } + + pwm_set_enabled(self->slice_num, false); + pwm_set_irq_enabled(self->slice_num, false); + + reset_pin_number(self->pin_a); + gpio_init(self->pin_a); + self->pin_a = 0; + self->slice_num = 0; + MP_STATE_PORT(counting) = NULL; +} + +mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { + self->count += pwm_get_counter(self->slice_num); + pwm_set_counter(self->slice_num, 0); + return self->count; +} + +void common_hal_countio_counter_set_count(countio_counter_obj_t* self, + mp_int_t new_count) { + pwm_set_counter(self->slice_num, 0); + self->count = new_count; +} + +void common_hal_countio_counter_reset(countio_counter_obj_t* self){ + pwm_set_counter(self->slice_num, 0); + self->count = 0; +} + +void counter_interrupt_handler() { + countio_counter_obj_t *self = MP_STATE_PORT(counting); + pwm_clear_irq(self->slice_num); + self->count += 65536; +} diff --git a/ports/raspberrypi/common-hal/countio/Counter.h b/ports/raspberrypi/common-hal/countio/Counter.h new file mode 100644 index 0000000000..201034cf46 --- /dev/null +++ b/ports/raspberrypi/common-hal/countio/Counter.h @@ -0,0 +1,19 @@ + +#ifndef MICROPY_INCLUDED_RASPBERRRYPI_COMMON_HAL_COUNTIO_COUNTER_H +#define MICROPY_INCLUDED_RASPBERRRYPI_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 slice_num; + mp_int_t count; +} countio_counter_obj_t; + + +void counter_interrupt_handler(); + +#endif // MICROPY_INCLUDED_RASPBERRRYPI_COMMON_HAL_COUNTIO_COUNTER_H diff --git a/ports/raspberrypi/common-hal/countio/__init__.c b/ports/raspberrypi/common-hal/countio/__init__.c new file mode 100644 index 0000000000..b95b20d153 --- /dev/null +++ b/ports/raspberrypi/common-hal/countio/__init__.c @@ -0,0 +1 @@ +//No countio module functions diff --git a/ports/raspberrypi/mpconfigport.h b/ports/raspberrypi/mpconfigport.h index 77d1ff0805..b1165a3ba9 100644 --- a/ports/raspberrypi/mpconfigport.h +++ b/ports/raspberrypi/mpconfigport.h @@ -40,7 +40,10 @@ // This also includes mpconfigboard.h. #include "py/circuitpy_mpconfig.h" +//#include "peripherals/samd/dma.h" + #define MICROPY_PORT_ROOT_POINTERS \ - CIRCUITPY_COMMON_ROOT_POINTERS; + CIRCUITPY_COMMON_ROOT_POINTERS \ + mp_obj_t counting; #endif // __INCLUDED_MPCONFIGPORT_H diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 138896d41c..15e17cd6f4 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -29,7 +29,6 @@ CIRCUITPY_PWMIO = 1 # Things that need to be implemented. CIRCUITPY_AUDIOBUSIO = 0 # Use PIO interally for I2S CIRCUITPY_AUDIOMP3 = 0 -CIRCUITPY_COUNTIO = 0 # Use PWM interally CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally CIRCUITPY_I2CPERIPHERAL = 0 CIRCUITPY_NVM = 0 From d7bc8a46a5f244069c2bc9b62961bc43dcee03a7 Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Sat, 27 Feb 2021 15:17:27 -0600 Subject: [PATCH 2/4] Added counters per slice --- locale/circuitpython.pot | 6 +++- .../raspberrypi/common-hal/countio/Counter.c | 29 ++++++++++++++----- ports/raspberrypi/mpconfigport.h | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index e5ed6149ad..7414c39737 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -539,7 +539,6 @@ msgstr "" #: ports/atmel-samd/common-hal/displayio/ParallelBus.c #: ports/esp32s2/common-hal/displayio/ParallelBus.c #: ports/nrf/common-hal/displayio/ParallelBus.c -#: ports/raspberrypi/common-hal/displayio/ParallelBus.c #, c-format msgid "Bus pin %d is already in use" msgstr "" @@ -1688,6 +1687,10 @@ msgid "" "PWM frequency not writable when variable_frequency is False on construction." msgstr "" +#: ports/raspberrypi/common-hal/countio/Counter.c +msgid "PWM slice already in use" +msgstr "" + #: ports/mimxrt10xx/common-hal/displayio/ParallelBus.c #: ports/raspberrypi/common-hal/displayio/ParallelBus.c #: ports/stm/common-hal/displayio/ParallelBus.c @@ -3687,6 +3690,7 @@ msgstr "" #: ports/esp32s2/boards/targett_module_clip_wrover/mpconfigboard.h #: ports/esp32s2/boards/unexpectedmaker_feathers2/mpconfigboard.h #: ports/esp32s2/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.h +#: ports/esp32s2/boards/unexpectedmaker_tinys2/mpconfigboard.h msgid "pressing boot button at start up.\n" msgstr "" diff --git a/ports/raspberrypi/common-hal/countio/Counter.c b/ports/raspberrypi/common-hal/countio/Counter.c index c5a47d2a41..4a1ccad0cd 100644 --- a/ports/raspberrypi/common-hal/countio/Counter.c +++ b/ports/raspberrypi/common-hal/countio/Counter.c @@ -19,6 +19,10 @@ void common_hal_countio_counter_construct(countio_counter_obj_t* self, self->pin_a = pin_a->number; self->slice_num = pwm_gpio_to_slice_num(self->pin_a); + if (MP_STATE_PORT(counting)[self->slice_num] != NULL) { + mp_raise_RuntimeError(translate("PWM slice already in use")); + } + pwm_clear_irq(self->slice_num); pwm_set_irq_enabled(self->slice_num, true); irq_set_exclusive_handler(PWM_IRQ_WRAP, counter_interrupt_handler); @@ -29,12 +33,11 @@ void common_hal_countio_counter_construct(countio_counter_obj_t* self, pwm_init(self->slice_num, &cfg, false); gpio_set_function(self->pin_a, GPIO_FUNC_PWM); - self->count = 0; - claim_pin(pin_a); - MP_STATE_PORT(counting) = self; + MP_STATE_PORT(counting)[self->slice_num] = self; + self->count = 0; pwm_set_enabled(self->slice_num, true); } @@ -51,10 +54,10 @@ void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { pwm_set_irq_enabled(self->slice_num, false); reset_pin_number(self->pin_a); - gpio_init(self->pin_a); + + MP_STATE_PORT(counting)[self->slice_num] = NULL; self->pin_a = 0; self->slice_num = 0; - MP_STATE_PORT(counting) = NULL; } mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { @@ -75,7 +78,17 @@ void common_hal_countio_counter_reset(countio_counter_obj_t* self){ } void counter_interrupt_handler() { - countio_counter_obj_t *self = MP_STATE_PORT(counting); - pwm_clear_irq(self->slice_num); - self->count += 65536; + uint32_t mask = pwm_get_irq_status_mask(); + + uint8_t i = 1, pos = 1; + while (!(i & mask)) { + i = i << 1; + ++pos; + } + + countio_counter_obj_t *self = MP_STATE_PORT(counting)[pos-1]; + if (self != NULL) { + pwm_clear_irq(self->slice_num); + self->count += 65536; + } } diff --git a/ports/raspberrypi/mpconfigport.h b/ports/raspberrypi/mpconfigport.h index eb0754e38d..ccc8e8b032 100644 --- a/ports/raspberrypi/mpconfigport.h +++ b/ports/raspberrypi/mpconfigport.h @@ -43,7 +43,7 @@ #include "py/circuitpy_mpconfig.h" #define MICROPY_PORT_ROOT_POINTERS \ - mp_obj_t counting; \ + mp_obj_t counting[NUM_PWM_SLICES]; \ mp_obj_t playing_audio[NUM_DMA_CHANNELS]; \ CIRCUITPY_COMMON_ROOT_POINTERS; From 4246cc3f6d718e2f28fbeb73fc8c3c39dfb9d7f0 Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Tue, 2 Mar 2021 19:32:06 -0600 Subject: [PATCH 3/4] Counter and PWMOut slice conflict check --- locale/circuitpython.pot | 4 ++++ .../raspberrypi/common-hal/countio/Counter.c | 9 ++++++++ ports/raspberrypi/common-hal/pwmio/PWMOut.c | 23 +++++++++++++++++++ ports/raspberrypi/common-hal/pwmio/PWMOut.h | 4 ++++ 4 files changed, 40 insertions(+) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 7414c39737..7a9d0ce4b0 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1691,6 +1691,10 @@ msgstr "" msgid "PWM slice already in use" msgstr "" +#: ports/raspberrypi/common-hal/countio/Counter.c +msgid "PWM slice channel A already in use" +msgstr "" + #: ports/mimxrt10xx/common-hal/displayio/ParallelBus.c #: ports/raspberrypi/common-hal/displayio/ParallelBus.c #: ports/stm/common-hal/displayio/ParallelBus.c diff --git a/ports/raspberrypi/common-hal/countio/Counter.c b/ports/raspberrypi/common-hal/countio/Counter.c index 4a1ccad0cd..69629a0848 100644 --- a/ports/raspberrypi/common-hal/countio/Counter.c +++ b/ports/raspberrypi/common-hal/countio/Counter.c @@ -4,6 +4,8 @@ #include "py/mpstate.h" #include "supervisor/shared/translate.h" +#include "common-hal/pwmio/PWMOut.h" + #include "src/rp2_common/hardware_gpio/include/hardware/gpio.h" #include "src/rp2_common/hardware_pwm/include/hardware/pwm.h" #include "src/rp2_common/hardware_irq/include/hardware/irq.h" @@ -23,6 +25,11 @@ void common_hal_countio_counter_construct(countio_counter_obj_t* self, mp_raise_RuntimeError(translate("PWM slice already in use")); } + uint8_t channel = pwm_gpio_to_channel(self->pin_a); + if (!pwmio_claim_slice_channels(self->slice_num)) { + mp_raise_RuntimeError(translate("PWM slice channel A already in use")); + } + pwm_clear_irq(self->slice_num); pwm_set_irq_enabled(self->slice_num, true); irq_set_exclusive_handler(PWM_IRQ_WRAP, counter_interrupt_handler); @@ -53,6 +60,8 @@ void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { pwm_set_enabled(self->slice_num, false); pwm_set_irq_enabled(self->slice_num, false); + pwmio_release_slice_channels(self->slice_num); + reset_pin_number(self->pin_a); MP_STATE_PORT(counting)[self->slice_num] = NULL; diff --git a/ports/raspberrypi/common-hal/pwmio/PWMOut.c b/ports/raspberrypi/common-hal/pwmio/PWMOut.c index 00b461880c..348d99ebe2 100644 --- a/ports/raspberrypi/common-hal/pwmio/PWMOut.c +++ b/ports/raspberrypi/common-hal/pwmio/PWMOut.c @@ -61,6 +61,29 @@ static uint32_t _mask(uint8_t slice, uint8_t channel) { return 1 << (slice * CHANNELS_PER_SLICE + channel); } +bool pwmio_claim_slice_channels(uint8_t slice) { + uint32_t channel_use_mask_a = _mask(slice, 0); + uint32_t channel_use_mask_b = _mask(slice, 1); + + if ((channel_use & channel_use_mask_a) != 0) { + return false; + } + if ((channel_use & channel_use_mask_b) != 0) { + return false; + } + + channel_use |= channel_use_mask_a; + channel_use |= channel_use_mask_b; + return true; +} + +void pwmio_release_slice_channels(uint8_t slice) { + uint32_t channel_mask = _mask(slice, 0); + channel_use &= ~channel_mask; + channel_mask = _mask(slice, 1); + channel_use &= ~channel_mask; +} + void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { never_reset_channel |= _mask(self->slice, self->channel); diff --git a/ports/raspberrypi/common-hal/pwmio/PWMOut.h b/ports/raspberrypi/common-hal/pwmio/PWMOut.h index 0070e188b5..6c0dda2dba 100644 --- a/ports/raspberrypi/common-hal/pwmio/PWMOut.h +++ b/ports/raspberrypi/common-hal/pwmio/PWMOut.h @@ -46,4 +46,8 @@ void pwmout_reset(void); // Private API for AudioPWMOut. void pwmio_pwmout_set_top(pwmio_pwmout_obj_t* self, uint16_t top); +// Private API for countio to claim both channels on a slice +bool pwmio_claim_slice_channels(uint8_t slice); +void pwmio_release_slice_channels(uint8_t slice); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H From 98075c525515f9104e93100c180cadc3b9e8fc69 Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Tue, 2 Mar 2021 22:32:59 -0600 Subject: [PATCH 4/4] Fixed merge --- ports/raspberrypi/common-hal/pwmio/PWMOut.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/raspberrypi/common-hal/pwmio/PWMOut.c b/ports/raspberrypi/common-hal/pwmio/PWMOut.c index 4343b9e5cc..fd85fbf55b 100644 --- a/ports/raspberrypi/common-hal/pwmio/PWMOut.c +++ b/ports/raspberrypi/common-hal/pwmio/PWMOut.c @@ -82,6 +82,7 @@ void pwmio_release_slice_channels(uint8_t slice) { channel_use &= ~channel_mask; channel_mask = _mask(slice, 1); channel_use &= ~channel_mask; +} void pwmout_never_reset(uint8_t slice, uint8_t channel) { never_reset_channel |= _mask(slice, channel);