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