aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/ports/raspberrypi/common-hal/countio/Counter.c
blob: 0ae773bdf3d6c22c9fbdca1a39311a82eec2e3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "shared-bindings/countio/Counter.h"

#include "py/runtime.h"
#include "py/mpstate.h"
#include "supervisor/shared/translate.h"

#include "shared-bindings/countio/Edge.h"
#include "shared-bindings/digitalio/Pull.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"


void common_hal_countio_counter_construct(countio_counter_obj_t *self,
    const mcu_pin_obj_t *pin, countio_edge_t edge, digitalio_pull_t pull) {

    if (pwm_gpio_to_channel(pin->number) != PWM_CHAN_B) {
        mp_raise_RuntimeError(translate("Pin must be on PWM Channel B"));
    }

    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);

    if (MP_STATE_PORT(counting)[self->slice_num] != NULL) {
        mp_raise_RuntimeError(translate("PWM slice already in use"));
    }

    uint8_t ab_channel = pwm_gpio_to_channel(self->pin);
    if (!pwmio_claim_slice_ab_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);
    irq_set_enabled(PWM_IRQ_WRAP, true);

    pwm_config cfg = pwm_get_default_config();
    pwm_config_set_clkdiv_mode(&cfg,  edge == EDGE_RISE ? PWM_DIV_B_RISING : PWM_DIV_B_FALLING);
    pwm_init(self->slice_num, &cfg, false);
    gpio_set_function(self->pin, GPIO_FUNC_PWM);
    gpio_set_pulls(self->pin, pull == PULL_UP, pull == PULL_DOWN);

    claim_pin(pin);

    MP_STATE_PORT(counting)[self->slice_num] = self;

    self->count = 0;
    pwm_set_enabled(self->slice_num, true);
}


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]);
        }
    }
}

bool common_hal_countio_counter_deinited(countio_counter_obj_t *self) {
    return self->pin == 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);

    pwmio_release_slice_ab_channels(self->slice_num);

    reset_pin_number(self->pin);

    MP_STATE_PORT(counting)[self->slice_num] = NULL;
    self->pin = 0;
    self->slice_num = 0;
}

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 counter_interrupt_handler(void) {
    uint32_t mask = pwm_get_irq_status_mask();

    uint8_t i = 1;
    uint8_t pos = 0;
    while (!(i & mask)) {
        i = i << 1;
        ++pos;
    }

    countio_counter_obj_t *self = MP_STATE_PORT(counting)[pos];
    if (self != NULL) {
        pwm_clear_irq(self->slice_num);
        self->count += 65536;
    }
}