|
26 | 26 |
|
27 | 27 | #include <stdint.h>
|
28 | 28 |
|
| 29 | +#include <user_interface.h> |
| 30 | +#include <eagle_soc.h> |
| 31 | +#include "esp_mphal.h" |
| 32 | + |
29 | 33 | #include "mpconfigport.h"
|
30 | 34 | #include "py/gc.h"
|
31 | 35 | #include "py/runtime.h"
|
32 | 36 | #include "shared-bindings/microcontroller/__init__.h"
|
33 | 37 | #include "shared-bindings/pulseio/PulseIn.h"
|
| 38 | +#include "common-hal/microcontroller/__init__.h" |
| 39 | + |
| 40 | +static void pulsein_set_interrupt(pulseio_pulsein_obj_t *self, bool rising, bool falling) { |
| 41 | + ETS_GPIO_INTR_DISABLE(); |
| 42 | + // Set interrupt mode |
| 43 | + GPIO_REG_WRITE( |
| 44 | + GPIO_PIN_ADDR(self->pin->gpio_number), |
| 45 | + (GPIO_REG_READ(GPIO_PIN_ADDR(self->pin->gpio_number) & ~GPIO_PIN_INT_TYPE_MASK)) | |
| 46 | + GPIO_PIN_INT_TYPE_SET( |
| 47 | + (rising ? GPIO_PIN_INTR_POSEDGE : 0) | (falling ? GPIO_PIN_INTR_NEGEDGE : 0) |
| 48 | + ) |
| 49 | + ); |
| 50 | + // Clear interrupt status |
| 51 | + GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << self->pin->gpio_number); |
| 52 | + ETS_GPIO_INTR_ENABLE(); |
| 53 | +} |
| 54 | + |
| 55 | +void pulseio_pulsein_interrupt_handler(void *data) { |
| 56 | + pulseio_pulsein_obj_t *self = data; |
| 57 | + uint32_t time_us = system_get_time(); |
| 58 | + if (self->first_edge) { |
| 59 | + self->first_edge = false; |
| 60 | + pulsein_set_interrupt(self, true, true); |
| 61 | + } else { |
| 62 | + uint16_t elapsed_us = (uint16_t)(time_us - self->last_us); |
| 63 | + uint16_t i = (self->start + self->len) % self->maxlen; |
| 64 | + self->buffer[i] = elapsed_us; |
| 65 | + if (self->len < self->maxlen) { |
| 66 | + self->len++; |
| 67 | + } else { |
| 68 | + self->start++; |
| 69 | + } |
| 70 | + } |
| 71 | + self->last_us = time_us; |
| 72 | +} |
34 | 73 |
|
35 | 74 | void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
|
36 | 75 | const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
|
37 |
| - mp_raise_NotImplementedError(""); |
| 76 | + if (pin->gpio_number == NO_GPIO || pin->gpio_function == SPECIAL_CASE) { |
| 77 | + mp_raise_msg_varg(&mp_type_ValueError, "No PulseIn support for %q", pin->name ); |
| 78 | + } |
| 79 | + PIN_FUNC_SELECT(pin->peripheral, pin->gpio_function); |
| 80 | + PIN_PULLUP_DIS(pin->peripheral); |
| 81 | + self->pin = pin; |
| 82 | + |
| 83 | + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); |
| 84 | + if (self->buffer == NULL) { |
| 85 | + mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t)); |
| 86 | + } |
| 87 | + |
| 88 | + self->maxlen = maxlen; |
| 89 | + self->idle_state = idle_state; |
| 90 | + self->start = 0; |
| 91 | + self->len = 0; |
| 92 | + self->first_edge = true; |
| 93 | + self->last_us = 0; |
| 94 | + self->paused = false; |
| 95 | + |
| 96 | + microcontroller_pin_register_intr_handler(self->pin->gpio_number, |
| 97 | + pulseio_pulsein_interrupt_handler, (void *)self); |
| 98 | + pulsein_set_interrupt(self, !idle_state, idle_state); |
38 | 99 | }
|
39 | 100 |
|
40 | 101 | bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
|
41 |
| - return true; |
| 102 | + return self->buffer == NULL; |
42 | 103 | }
|
43 | 104 |
|
44 | 105 | void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
| 106 | + pulsein_set_interrupt(self, false, false); |
| 107 | + microcontroller_pin_register_intr_handler(self->pin->gpio_number, NULL, NULL); |
| 108 | + PIN_FUNC_SELECT(self->pin->peripheral, 0); |
| 109 | + m_free(self->buffer); |
| 110 | + self->buffer = NULL; |
45 | 111 | }
|
46 | 112 |
|
47 | 113 | void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
| 114 | + pulsein_set_interrupt(self, false, false); |
| 115 | + self->paused = true; |
48 | 116 | }
|
49 | 117 |
|
50 | 118 | void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
|
51 | 119 | uint16_t trigger_duration) {
|
| 120 | + // Make sure we're paused. |
| 121 | + common_hal_pulseio_pulsein_pause(self); |
| 122 | + |
| 123 | + // Send the trigger pulse. |
| 124 | + if (trigger_duration > 0) { |
| 125 | + uint32_t mask = 1 << self->pin->gpio_number; |
| 126 | + // switch pin to an output with state opposite idle state |
| 127 | + gpio_output_set(self->idle_state ? 0 : mask, self->idle_state ? mask : 0, 0, 0); |
| 128 | + gpio_output_set(0, 0, mask, 0); |
| 129 | + common_hal_mcu_delay_us((uint32_t)trigger_duration); |
| 130 | + // switch pin back to an open input |
| 131 | + gpio_output_set(0, 0, 0, mask); |
| 132 | + } |
| 133 | + |
| 134 | + common_hal_mcu_disable_interrupts(); |
| 135 | + self->first_edge = true; |
| 136 | + pulsein_set_interrupt(self, !self->idle_state, self->idle_state); |
| 137 | + common_hal_mcu_enable_interrupts(); |
| 138 | + self->paused = false; |
52 | 139 | }
|
53 | 140 |
|
54 | 141 | void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
|
| 142 | + common_hal_mcu_disable_interrupts(); |
| 143 | + self->start = 0; |
| 144 | + self->len = 0; |
| 145 | + common_hal_mcu_enable_interrupts(); |
55 | 146 | }
|
56 | 147 |
|
57 | 148 | uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
|
58 |
| - return 0; |
| 149 | + if (self->len == 0) { |
| 150 | + mp_raise_IndexError("pop from an empty PulseIn"); |
| 151 | + } |
| 152 | + common_hal_mcu_disable_interrupts(); |
| 153 | + uint16_t value = self->buffer[self->start]; |
| 154 | + self->start = (self->start + 1) % self->maxlen; |
| 155 | + self->len--; |
| 156 | + common_hal_mcu_enable_interrupts(); |
| 157 | + |
| 158 | + return value; |
59 | 159 | }
|
60 | 160 |
|
61 | 161 | uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
|
62 |
| - return 0; |
| 162 | + return self->maxlen; |
63 | 163 | }
|
64 | 164 |
|
65 | 165 | bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
|
66 |
| - return false; |
| 166 | + return self->paused; |
67 | 167 | }
|
68 | 168 |
|
69 | 169 | uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
|
70 |
| - return 0; |
| 170 | + return self->len; |
71 | 171 | }
|
72 | 172 |
|
73 | 173 | uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self,
|
74 | 174 | int16_t index) {
|
75 |
| - return 0; |
| 175 | + common_hal_mcu_disable_interrupts(); |
| 176 | + if (index < 0) { |
| 177 | + index += self->len; |
| 178 | + } |
| 179 | + if (index < 0 || index >= self->len) { |
| 180 | + common_hal_mcu_enable_interrupts(); |
| 181 | + mp_raise_IndexError("index out of range"); |
| 182 | + } |
| 183 | + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; |
| 184 | + common_hal_mcu_enable_interrupts(); |
| 185 | + return value; |
76 | 186 | }
|
0 commit comments