2021-08-27 20:39:19 -04:00
|
|
|
#include "shared-bindings/countio/Counter.h"
|
2021-02-23 20:26:11 -05:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mpstate.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
#include "shared-bindings/countio/Edge.h"
|
|
|
|
#include "shared-bindings/digitalio/Pull.h"
|
2021-03-02 20:32:06 -05:00
|
|
|
#include "common-hal/pwmio/PWMOut.h"
|
|
|
|
|
2021-02-23 20:26:11 -05:00
|
|
|
#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"
|
|
|
|
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_countio_counter_construct(countio_counter_obj_t *self,
|
2021-12-30 21:49:52 -05:00
|
|
|
const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
if (pwm_gpio_to_channel(pin->number) != PWM_CHAN_B) {
|
2021-02-23 20:26:11 -05:00
|
|
|
mp_raise_RuntimeError(translate("Pin must be on PWM Channel B"));
|
|
|
|
}
|
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
if (edge == EDGE_RISE_AND_FALL) {
|
|
|
|
mp_raise_NotImplementedError(translate("RISE_AND_FALL not available on this chip"));
|
|
|
|
}
|
|
|
|
|
|
|
|
self->pin = pin->number;
|
|
|
|
self->slice_num = pwm_gpio_to_slice_num(self->pin);
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-02-27 16:17:27 -05:00
|
|
|
if (MP_STATE_PORT(counting)[self->slice_num] != NULL) {
|
|
|
|
mp_raise_RuntimeError(translate("PWM slice already in use"));
|
|
|
|
}
|
|
|
|
|
2021-12-05 21:16:46 -05:00
|
|
|
if (!pwmio_claim_slice_ab_channels(self->slice_num)) {
|
2021-03-02 20:32:06 -05:00
|
|
|
mp_raise_RuntimeError(translate("PWM slice channel A already in use"));
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:26:11 -05:00
|
|
|
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();
|
2021-12-30 21:49:52 -05:00
|
|
|
pwm_config_set_clkdiv_mode(&cfg, edge == EDGE_RISE ? PWM_DIV_B_RISING : PWM_DIV_B_FALLING);
|
2021-02-23 20:26:11 -05:00
|
|
|
pwm_init(self->slice_num, &cfg, false);
|
2021-12-30 21:49:52 -05:00
|
|
|
gpio_set_function(self->pin, GPIO_FUNC_PWM);
|
|
|
|
gpio_set_pulls(self->pin, pull == PULL_UP, pull == PULL_DOWN);
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
claim_pin(pin);
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-02-27 16:17:27 -05:00
|
|
|
MP_STATE_PORT(counting)[self->slice_num] = self;
|
2021-02-23 20:26:11 -05:00
|
|
|
|
2021-02-27 16:17:27 -05:00
|
|
|
self->count = 0;
|
2021-02-23 20:26:11 -05:00
|
|
|
pwm_set_enabled(self->slice_num, true);
|
|
|
|
}
|
|
|
|
|
2021-08-27 20:39:19 -04:00
|
|
|
|
|
|
|
void reset_countio(void) {
|
|
|
|
for (size_t i = 0; i < NUM_PWM_SLICES; i++) {
|
|
|
|
if (MP_STATE_PORT(counting)[i] != NULL) {
|
|
|
|
common_hal_countio_counter_deinit(MP_STATE_PORT(counting)[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
|
2021-12-30 21:49:52 -05:00
|
|
|
return self->pin == 0;
|
2021-02-23 20:26:11 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_countio_counter_deinit(countio_counter_obj_t *self) {
|
2021-02-23 20:26:11 -05:00
|
|
|
if (common_hal_countio_counter_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwm_set_enabled(self->slice_num, false);
|
|
|
|
pwm_set_irq_enabled(self->slice_num, false);
|
|
|
|
|
2021-12-05 21:16:46 -05:00
|
|
|
pwmio_release_slice_ab_channels(self->slice_num);
|
2021-03-02 20:32:06 -05:00
|
|
|
|
2021-12-30 21:49:52 -05:00
|
|
|
reset_pin_number(self->pin);
|
2021-02-27 16:17:27 -05:00
|
|
|
|
|
|
|
MP_STATE_PORT(counting)[self->slice_num] = NULL;
|
2021-12-30 21:49:52 -05:00
|
|
|
self->pin = 0;
|
2021-02-23 20:26:11 -05:00
|
|
|
self->slice_num = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t *self) {
|
2021-02-23 20:26:11 -05:00
|
|
|
self->count += pwm_get_counter(self->slice_num);
|
|
|
|
pwm_set_counter(self->slice_num, 0);
|
|
|
|
return self->count;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_countio_counter_set_count(countio_counter_obj_t *self,
|
|
|
|
mp_int_t new_count) {
|
2021-02-23 20:26:11 -05:00
|
|
|
pwm_set_counter(self->slice_num, 0);
|
|
|
|
self->count = new_count;
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
void counter_interrupt_handler(void) {
|
2021-02-27 16:17:27 -05:00
|
|
|
uint32_t mask = pwm_get_irq_status_mask();
|
|
|
|
|
2021-12-05 21:16:46 -05:00
|
|
|
uint8_t i = 1;
|
|
|
|
uint8_t pos = 0;
|
2021-02-27 16:17:27 -05:00
|
|
|
while (!(i & mask)) {
|
|
|
|
i = i << 1;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2021-12-05 21:16:46 -05:00
|
|
|
countio_counter_obj_t *self = MP_STATE_PORT(counting)[pos];
|
2021-02-27 16:17:27 -05:00
|
|
|
if (self != NULL) {
|
|
|
|
pwm_clear_irq(self->slice_num);
|
|
|
|
self->count += 65536;
|
|
|
|
}
|
2021-02-23 20:26:11 -05:00
|
|
|
}
|