|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 microDev |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/frequencyio/FrequencyIn.h" |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +static void IRAM_ATTR pcnt_overflow_handler(void *self_in) { |
| 32 | + frequencyio_frequencyin_obj_t * self = self_in; |
| 33 | + // reset counter |
| 34 | + pcnt_counter_clear(self->unit); |
| 35 | + |
| 36 | + // increase multiplier |
| 37 | + self->multiplier++; |
| 38 | + |
| 39 | + // reset interrupt |
| 40 | + PCNT.int_clr.val = BIT(self->unit); |
| 41 | +} |
| 42 | + |
| 43 | +static void IRAM_ATTR timer_interrupt_handler(void *self_in) { |
| 44 | + frequencyio_frequencyin_obj_t * self = self_in; |
| 45 | + // get counter value |
| 46 | + int16_t count; |
| 47 | + pcnt_get_counter_value(self->unit, &count); |
| 48 | + self->frequency = ((count / 2.0) + (self->multiplier * INT16_MAX / 4.0)) / (self->capture_period); |
| 49 | + |
| 50 | + // reset multiplier |
| 51 | + self->multiplier = 0; |
| 52 | + |
| 53 | + // reset counter |
| 54 | + pcnt_counter_clear(self->unit); |
| 55 | + |
| 56 | + // reset interrupt |
| 57 | + timg_dev_t *device = self->timer.group ? &(TIMERG1) : &(TIMERG0); |
| 58 | + if (self->timer.idx) { |
| 59 | + device->int_clr.t1 = 1; |
| 60 | + } else { |
| 61 | + device->int_clr.t0 = 1; |
| 62 | + } |
| 63 | + device->hw_timer[self->timer.idx].config.alarm_en = 1; |
| 64 | +} |
| 65 | + |
| 66 | +static void init_pcnt(frequencyio_frequencyin_obj_t* self) { |
| 67 | + // Prepare configuration for the PCNT unit |
| 68 | + const pcnt_config_t pcnt_config = { |
| 69 | + // Set PCNT input signal and control GPIOs |
| 70 | + .pulse_gpio_num = self->pin, |
| 71 | + .ctrl_gpio_num = PCNT_PIN_NOT_USED, |
| 72 | + .channel = PCNT_CHANNEL_0, |
| 73 | + // What to do on the positive / negative edge of pulse input? |
| 74 | + .pos_mode = PCNT_COUNT_INC, // count both rising and falling edges |
| 75 | + .neg_mode = PCNT_COUNT_INC, |
| 76 | + // Set counter limit |
| 77 | + .counter_h_lim = INT16_MAX, |
| 78 | + .counter_l_lim = 0, |
| 79 | + }; |
| 80 | + |
| 81 | + // initialize PCNT |
| 82 | + const int8_t unit = peripherals_pcnt_init(pcnt_config); |
| 83 | + if (unit == -1) { |
| 84 | + mp_raise_RuntimeError(translate("All PCNT units in use")); |
| 85 | + } |
| 86 | + |
| 87 | + // set the GPIO back to high-impedance, as pcnt_unit_config sets it as pull-up |
| 88 | + gpio_set_pull_mode(self->pin, GPIO_FLOATING); |
| 89 | + |
| 90 | + self->unit = (pcnt_unit_t)unit; |
| 91 | + |
| 92 | + // enable pcnt interrupt |
| 93 | + pcnt_event_enable(self->unit, PCNT_EVT_H_LIM); |
| 94 | + pcnt_isr_register(pcnt_overflow_handler, (void *)self, ESP_INTR_FLAG_IRAM, &self->handle); |
| 95 | + pcnt_intr_enable(self->unit); |
| 96 | +} |
| 97 | + |
| 98 | +static void init_timer(frequencyio_frequencyin_obj_t* self) { |
| 99 | + // Prepare configuration for the timer module |
| 100 | + const timer_config_t config = { |
| 101 | + .alarm_en = true, |
| 102 | + .counter_en = false, |
| 103 | + .intr_type = TIMER_INTR_LEVEL, |
| 104 | + .counter_dir = TIMER_COUNT_UP, |
| 105 | + .auto_reload = true, |
| 106 | + .divider = 80 // 1 us per tick |
| 107 | + }; |
| 108 | + |
| 109 | + // initialize Timer |
| 110 | + peripherals_timer_init(&config, &self->timer); |
| 111 | + if (self->timer.idx == TIMER_MAX || self->timer.group == TIMER_GROUP_MAX) { |
| 112 | + mp_raise_RuntimeError(translate("All timers in use")); |
| 113 | + } |
| 114 | + |
| 115 | + timer_idx_t idx = self->timer.idx; |
| 116 | + timer_group_t group = self->timer.group; |
| 117 | + |
| 118 | + // enable timer interrupt |
| 119 | + timer_set_alarm_value(group, idx, self->capture_period * 1000000); |
| 120 | + timer_isr_register(group, idx, timer_interrupt_handler, (void *)self, ESP_INTR_FLAG_IRAM, &self->handle); |
| 121 | + timer_enable_intr(group, idx); |
| 122 | + |
| 123 | + // start timer |
| 124 | + timer_start(self->timer.group, self->timer.idx); |
| 125 | +} |
| 126 | + |
| 127 | +void common_hal_frequencyio_frequencyin_construct(frequencyio_frequencyin_obj_t* self, |
| 128 | + const mcu_pin_obj_t* pin, const uint16_t capture_period) { |
| 129 | + if ((capture_period == 0) || (capture_period > 500)) { |
| 130 | + mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500")); |
| 131 | + } |
| 132 | + |
| 133 | + self->pin = pin->number; |
| 134 | + self->handle = NULL; |
| 135 | + self->multiplier = 0; |
| 136 | + self->capture_period = capture_period; |
| 137 | + |
| 138 | + // initialize pcnt and timer |
| 139 | + init_pcnt(self); |
| 140 | + init_timer(self); |
| 141 | + |
| 142 | + claim_pin(pin); |
| 143 | +} |
| 144 | + |
| 145 | +bool common_hal_frequencyio_frequencyin_deinited(frequencyio_frequencyin_obj_t* self) { |
| 146 | + return self->unit == PCNT_UNIT_MAX; |
| 147 | +} |
| 148 | + |
| 149 | +void common_hal_frequencyio_frequencyin_deinit(frequencyio_frequencyin_obj_t* self) { |
| 150 | + if (common_hal_frequencyio_frequencyin_deinited(self)) { |
| 151 | + return; |
| 152 | + } |
| 153 | + reset_pin_number(self->pin); |
| 154 | + peripherals_pcnt_deinit(&self->unit); |
| 155 | + peripherals_timer_deinit(&self->timer); |
| 156 | + if (self->handle) { |
| 157 | + esp_intr_free(self->handle); |
| 158 | + self->handle = NULL; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +uint32_t common_hal_frequencyio_frequencyin_get_item(frequencyio_frequencyin_obj_t* self) { |
| 163 | + return self->frequency; |
| 164 | +} |
| 165 | + |
| 166 | +void common_hal_frequencyio_frequencyin_pause(frequencyio_frequencyin_obj_t* self) { |
| 167 | + pcnt_counter_pause(self->unit); |
| 168 | + timer_pause(self->timer.group, self->timer.idx); |
| 169 | +} |
| 170 | + |
| 171 | +void common_hal_frequencyio_frequencyin_resume(frequencyio_frequencyin_obj_t* self) { |
| 172 | + pcnt_counter_resume(self->unit); |
| 173 | + timer_start(self->timer.group, self->timer.idx); |
| 174 | +} |
| 175 | + |
| 176 | +void common_hal_frequencyio_frequencyin_clear(frequencyio_frequencyin_obj_t* self) { |
| 177 | + self->frequency = 0; |
| 178 | + pcnt_counter_clear(self->unit); |
| 179 | + timer_set_counter_value(self->timer.group, self->timer.idx, 0); |
| 180 | +} |
| 181 | + |
| 182 | +uint16_t common_hal_frequencyio_frequencyin_get_capture_period(frequencyio_frequencyin_obj_t *self) { |
| 183 | + return self->capture_period; |
| 184 | +} |
| 185 | + |
| 186 | +void common_hal_frequencyio_frequencyin_set_capture_period(frequencyio_frequencyin_obj_t *self, uint16_t capture_period) { |
| 187 | + if ((capture_period == 0) || (capture_period > 500)) { |
| 188 | + mp_raise_ValueError(translate("Invalid capture period. Valid range: 1 - 500")); |
| 189 | + } |
| 190 | + self->capture_period = capture_period; |
| 191 | + common_hal_frequencyio_frequencyin_clear(self); |
| 192 | + timer_set_alarm_value(self->timer.group, self->timer.idx, capture_period * 1000000); |
| 193 | +} |
0 commit comments