|
25 | 25 | */
|
26 | 26 |
|
27 | 27 | #include "common-hal/pulseio/PulseIn.h"
|
| 28 | +#include "shared-bindings/microcontroller/__init__.h" |
28 | 29 | #include "py/runtime.h"
|
29 | 30 |
|
30 |
| -// STATIC void pulsein_handler(uint8_t num) { |
31 |
| -// } |
| 31 | +STATIC uint8_t refcount = 0; |
| 32 | +STATIC pulseio_pulsein_obj_t * handles[RMT_CHANNEL_MAX]; |
| 33 | + |
| 34 | +// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset |
| 35 | + |
| 36 | +STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) { |
| 37 | + uint32_t length = 0; |
| 38 | + rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 0); |
| 39 | + if (items) { |
| 40 | + length /= 4; |
| 41 | + for (size_t i=0; i < length; i++) { |
| 42 | + uint16_t pos = (self->start + self->len) % self->maxlen; |
| 43 | + self->buffer[pos] = items[i].duration0 * 3; |
| 44 | + // Check if second item exists before incrementing |
| 45 | + if (items[i].duration1) { |
| 46 | + self->buffer[pos+1] = items[i].duration1 * 3; |
| 47 | + if (self->len < (self->maxlen - 1)) { |
| 48 | + self->len += 2; |
| 49 | + } else { |
| 50 | + self->start += 2; |
| 51 | + } |
| 52 | + } else { |
| 53 | + if (self->len < self->maxlen) { |
| 54 | + self->len++; |
| 55 | + } else { |
| 56 | + self->start++; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + vRingbufferReturnItem(self->buf_handle, (void *) items); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// We can't access the RMT interrupt, so we need a global service to prevent |
| 65 | +// the ringbuffer from overflowing and crashing the peripheral |
| 66 | +void pulsein_background(void) { |
| 67 | + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { |
| 68 | + if (handles[i]) { |
| 69 | + update_internal_buffer(handles[i]); |
| 70 | + UBaseType_t items_waiting; |
| 71 | + vRingbufferGetInfo(handles[i]->buf_handle, NULL, NULL, NULL, NULL, &items_waiting); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
32 | 75 |
|
33 | 76 | void pulsein_reset(void) {
|
| 77 | + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { |
| 78 | + handles[i] = NULL; |
| 79 | + } |
| 80 | + supervisor_disable_tick(); |
| 81 | + refcount = 0; |
34 | 82 | }
|
35 | 83 |
|
36 | 84 | void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin,
|
37 | 85 | uint16_t maxlen, bool idle_state) {
|
38 |
| - mp_raise_NotImplementedError(translate("PulseIn not supported on this chip")); |
| 86 | + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); |
| 87 | + if (self->buffer == NULL) { |
| 88 | + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); |
| 89 | + } |
| 90 | + self->pin = pin; |
| 91 | + self->maxlen = maxlen; |
| 92 | + self->idle_state = idle_state; |
| 93 | + self->start = 0; |
| 94 | + self->len = 0; |
| 95 | + self->paused = false; |
| 96 | + |
| 97 | + // Set pull settings |
| 98 | + gpio_pullup_dis(pin->number); |
| 99 | + gpio_pulldown_dis(pin->number); |
| 100 | + if (idle_state) { |
| 101 | + gpio_pullup_en(pin->number); |
| 102 | + } else { |
| 103 | + gpio_pulldown_en(pin->number); |
| 104 | + } |
| 105 | + |
| 106 | + // Find a free RMT Channel and configure it |
| 107 | + rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); |
| 108 | + if (channel == RMT_CHANNEL_MAX) { |
| 109 | + mp_raise_RuntimeError(translate("All timers in use")); |
| 110 | + } |
| 111 | + rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel); |
| 112 | + config.rx_config.filter_en = true; |
| 113 | + config.rx_config.idle_threshold = 30000; // 30*3=90ms idle required to register a sequence |
| 114 | + config.clk_div = 240; // All measurements are divided by 3 to accomodate 65ms pulses |
| 115 | + rmt_config(&config); |
| 116 | + rmt_driver_install(channel, 1000, 0); //TODO: pick a more specific buffer size? |
| 117 | + |
| 118 | + // Store this object and the buffer handle for background updates |
| 119 | + self->channel = channel; |
| 120 | + handles[channel] = self; |
| 121 | + rmt_get_ringbuf_handle(channel, &(self->buf_handle)); |
| 122 | + |
| 123 | + // start RMT RX, and enable ticks so the core doesn't turn off. |
| 124 | + rmt_rx_start(channel, true); |
| 125 | + supervisor_enable_tick(); |
| 126 | + refcount++; |
39 | 127 | }
|
40 | 128 |
|
41 | 129 | bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
|
42 |
| - return false; |
| 130 | + return handles[self->channel] ? false : true; |
43 | 131 | }
|
44 | 132 |
|
45 | 133 | void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
| 134 | + handles[self->channel] = NULL; |
| 135 | + esp32s2_peripherals_free_rmt(self->channel); |
| 136 | + reset_pin_number(self->pin->number); |
| 137 | + refcount--; |
| 138 | + if (refcount == 0) { |
| 139 | + supervisor_disable_tick(); |
| 140 | + } |
46 | 141 | }
|
47 | 142 |
|
48 | 143 | void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
| 144 | + self->paused = true; |
| 145 | + rmt_rx_stop(self->channel); |
49 | 146 | }
|
50 | 147 |
|
51 | 148 | void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
|
| 149 | + // Make sure we're paused. |
| 150 | + if ( !self->paused ) { |
| 151 | + common_hal_pulseio_pulsein_pause(self); |
| 152 | + } |
| 153 | + |
| 154 | + if (trigger_duration > 0) { |
| 155 | + gpio_set_direction(self->pin->number, GPIO_MODE_DEF_OUTPUT); |
| 156 | + gpio_set_level(self->pin->number, !self->idle_state); |
| 157 | + common_hal_mcu_delay_us((uint32_t)trigger_duration); |
| 158 | + gpio_set_level(self->pin->number, self->idle_state); |
| 159 | + gpio_set_direction(self->pin->number, GPIO_MODE_INPUT); // should revert to pull direction |
| 160 | + } |
| 161 | + |
| 162 | + self->paused = false; |
| 163 | + rmt_rx_start(self->channel, false); |
52 | 164 | }
|
53 | 165 |
|
54 | 166 | void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
|
| 167 | + // Buffer only updates in BG tasks or fetches, so no extra protection is needed |
| 168 | + self->start = 0; |
| 169 | + self->len = 0; |
55 | 170 | }
|
56 | 171 |
|
57 | 172 | uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
|
58 |
| - return false; |
| 173 | + update_internal_buffer(self); |
| 174 | + if (index < 0) { |
| 175 | + index += self->len; |
| 176 | + } |
| 177 | + if (index < 0 || index >= self->len) { |
| 178 | + mp_raise_IndexError(translate("index out of range")); |
| 179 | + } |
| 180 | + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; |
| 181 | + return value; |
59 | 182 | }
|
60 | 183 |
|
61 | 184 | uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
|
62 |
| - return false; |
| 185 | + update_internal_buffer(self); |
| 186 | + |
| 187 | + if (self->len == 0) { |
| 188 | + mp_raise_IndexError(translate("pop from an empty PulseIn")); |
| 189 | + } |
| 190 | + |
| 191 | + uint16_t value = self->buffer[self->start]; |
| 192 | + self->start = (self->start + 1) % self->maxlen; |
| 193 | + self->len--; |
| 194 | + |
| 195 | + return value; |
63 | 196 | }
|
64 | 197 |
|
65 | 198 | uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
|
66 |
| - return false; |
| 199 | + return self->maxlen; |
67 | 200 | }
|
68 | 201 |
|
69 | 202 | bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
|
70 |
| - return false; |
| 203 | + return self->paused; |
71 | 204 | }
|
72 | 205 |
|
73 | 206 | uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
|
74 |
| - return false; |
| 207 | + return self->len; |
75 | 208 | }
|
0 commit comments