|
1 | | -#include "common-hal/countio/Counter.h" |
| 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 | + */ |
2 | 26 |
|
3 | | -#include "py/runtime.h" |
4 | | -#include "supervisor/shared/translate.h" |
| 27 | +#include "common-hal/countio/Counter.h" |
| 28 | +#include "common-hal/microcontroller/Pin.h" |
5 | 29 |
|
6 | | -#include "driver/pcnt.h" |
| 30 | +void common_hal_countio_counter_construct(countio_counter_obj_t* self, |
| 31 | + const mcu_pin_obj_t* pin) { |
| 32 | + claim_pin(pin); |
7 | 33 |
|
8 | | -static void pcnt_init(countio_counter_obj_t* self) { |
9 | | - int unit = PCNT_UNIT_0; |
10 | 34 | // Prepare configuration for the PCNT unit |
11 | 35 | pcnt_config_t pcnt_config = { |
12 | 36 | // Set PCNT input signal and control GPIOs |
13 | | - .pulse_gpio_num = self->pin->number, |
| 37 | + .pulse_gpio_num = pin->number, |
14 | 38 | .ctrl_gpio_num = PCNT_PIN_NOT_USED, |
15 | 39 | .channel = PCNT_CHANNEL_0, |
16 | | - .unit = unit, |
17 | 40 | // What to do on the positive / negative edge of pulse input? |
18 | 41 | .pos_mode = PCNT_COUNT_INC, // Count up on the positive edge |
19 | 42 | .neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge |
20 | 43 | }; |
21 | 44 | // Initialize PCNT unit |
22 | | - pcnt_unit_config(&pcnt_config); |
23 | | - |
24 | | - // Configure and enable the input filter |
25 | | - pcnt_set_filter_value(unit, 100); |
26 | | - pcnt_filter_enable(unit); |
| 45 | + pcnt_handler_init(&pcnt_config); |
27 | 46 |
|
28 | | - // Initialize PCNT's counter |
29 | | - pcnt_counter_pause(unit); |
30 | | - pcnt_counter_clear(unit); |
31 | | - |
32 | | - // Everything is set up, now go to counting |
33 | | - pcnt_counter_resume(unit); |
34 | | -} |
35 | | - |
36 | | -void common_hal_countio_counter_construct(countio_counter_obj_t* self, |
37 | | - const mcu_pin_obj_t* pin) { |
38 | | - claim_pin(pin); |
39 | | - self->pin = pin; |
40 | | - pcnt_init(self); |
| 47 | + self->pin = pin->number; |
| 48 | + self->unit = pcnt_config.unit; |
41 | 49 | } |
42 | 50 |
|
43 | 51 | bool common_hal_countio_counter_deinited(countio_counter_obj_t* self) { |
44 | | - return self->pin == NULL; |
| 52 | + return self->unit == PCNT_UNIT_MAX; |
45 | 53 | } |
46 | 54 |
|
47 | 55 | void common_hal_countio_counter_deinit(countio_counter_obj_t* self) { |
48 | 56 | if (common_hal_countio_counter_deinited(self)) { |
49 | 57 | return; |
50 | 58 | } |
51 | | - reset_pin_number(self->pin->number); |
52 | | - self->pin = NULL; |
| 59 | + reset_pin_number(self->pin); |
| 60 | + pcnt_handler_deinit(&self->unit); |
53 | 61 | } |
54 | 62 |
|
55 | 63 | mp_int_t common_hal_countio_counter_get_count(countio_counter_obj_t* self) { |
56 | 64 | int16_t count; |
57 | | - pcnt_get_counter_value(PCNT_UNIT_0, &count); |
| 65 | + pcnt_get_counter_value(self->unit, &count); |
58 | 66 | return count+self->count; |
59 | 67 | } |
60 | 68 |
|
61 | 69 | void common_hal_countio_counter_set_count(countio_counter_obj_t* self, |
62 | 70 | mp_int_t new_count) { |
63 | 71 | self->count = new_count; |
64 | | - pcnt_counter_clear(PCNT_UNIT_0); |
| 72 | + pcnt_counter_clear(self->unit); |
65 | 73 | } |
66 | 74 |
|
67 | 75 | void common_hal_countio_counter_reset(countio_counter_obj_t* self) { |
|
0 commit comments