aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgamblor21 <mark.komus@gmail.com>2021-03-02 19:32:06 -0600
committergamblor21 <mark.komus@gmail.com>2021-03-02 19:32:06 -0600
commit4246cc3f6d718e2f28fbeb73fc8c3c39dfb9d7f0 (patch)
tree0c8f6401adf2a899e8f86967344501e3fe6c1c3f
parentd7bc8a46a5f244069c2bc9b62961bc43dcee03a7 (diff)
Counter and PWMOut slice conflict check
-rw-r--r--locale/circuitpython.pot4
-rw-r--r--ports/raspberrypi/common-hal/countio/Counter.c9
-rw-r--r--ports/raspberrypi/common-hal/pwmio/PWMOut.c23
-rw-r--r--ports/raspberrypi/common-hal/pwmio/PWMOut.h4
4 files changed, 40 insertions, 0 deletions
diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot
index 7414c3973..7a9d0ce4b 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 4a1ccad0c..69629a084 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 00b461880..348d99ebe 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 0070e188b..6c0dda2db 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