|
27 | 27 | #include "common-hal/pulseio/PulseIn.h"
|
28 | 28 | #include "py/runtime.h"
|
29 | 29 |
|
30 |
| -// STATIC void pulsein_handler(uint8_t num) { |
31 |
| -// } |
| 30 | +STATIC uint8_t refcount = 0; |
| 31 | +STATIC pulseio_pulsein_obj_t * handles[RMT_CHANNEL_MAX]; |
| 32 | + |
| 33 | +// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset |
| 34 | + |
| 35 | +STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) { |
| 36 | + //mp_printf(&mp_plat_print, "Update internal Buffer\n"); |
| 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 | + //mp_printf(&mp_plat_print, "Length%d\n",length); |
| 42 | + // TODO: If the size of the recieve is larger than the buffer, reset it? |
| 43 | + |
| 44 | + for (size_t i=0; i < length; i++) { |
| 45 | + //mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), |
| 46 | + // items[i].level0,(items[i].duration1 * 3),items[i].level1); |
| 47 | + uint16_t pos = (self->start + self->len) % self->maxlen; |
| 48 | + self->buffer[pos] = items[i].duration0 * 3; |
| 49 | + // Check if second item exists before incrementing |
| 50 | + if (items[i].duration1) { |
| 51 | + self->buffer[pos+1] = items[i].duration1 * 3; |
| 52 | + if (self->len < (self->maxlen - 1)) { |
| 53 | + self->len += 2; |
| 54 | + } else { |
| 55 | + self->start += 2; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if (self->len < self->maxlen) { |
| 59 | + self->len++; |
| 60 | + } else { |
| 61 | + self->start++; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + vRingbufferReturnItem(self->buf_handle, (void *) items); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// We can't access the RMT interrupt, so we need a global service to prevent |
| 70 | +// the ringbuffer from overflowing and crashing the peripheral |
| 71 | +void pulsein_background(void) { |
| 72 | + //mp_printf(&mp_plat_print, "BG Task!\n"); |
| 73 | + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { |
| 74 | + if (handles[i]) { |
| 75 | + //mp_printf(&mp_plat_print, "Located viable handle:%d\n",i); |
| 76 | + update_internal_buffer(handles[i]); |
| 77 | + UBaseType_t items_waiting; |
| 78 | + vRingbufferGetInfo(handles[i]->buf_handle, NULL, NULL, NULL, NULL, &items_waiting); |
| 79 | + //mp_printf(&mp_plat_print, "items waiting:%d\n",items_waiting); |
| 80 | + // if (items_waiting > handles[i]->maxlen) { |
| 81 | + // mp_printf(&mp_plat_print, "Overage!\n"); |
| 82 | + // mp_printf(&mp_plat_print, "Items waiting detected:%d\n", items_waiting); |
| 83 | + // update_internal_buffer(handles[i]); |
| 84 | + // } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
32 | 88 |
|
33 | 89 | void pulsein_reset(void) {
|
| 90 | + mp_printf(&mp_plat_print, "Pulsein Reset called\n"); |
| 91 | + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { |
| 92 | + handles[i] = NULL; |
| 93 | + } |
| 94 | + supervisor_disable_tick(); |
| 95 | + refcount = 0; |
34 | 96 | }
|
35 | 97 |
|
36 | 98 | void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin,
|
37 | 99 | uint16_t maxlen, bool idle_state) {
|
38 |
| - mp_raise_NotImplementedError(translate("PulseIn not supported on this chip")); |
| 100 | + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); |
| 101 | + if (self->buffer == NULL) { |
| 102 | + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); |
| 103 | + } |
| 104 | + self->pin = pin; |
| 105 | + self->maxlen = maxlen; |
| 106 | + // self->idle_state = idle_state; |
| 107 | + self->start = 0; |
| 108 | + self->len = 0; |
| 109 | + // self->first_edge = true; |
| 110 | + self->paused = false; |
| 111 | + // self->last_overflow = 0; |
| 112 | + // self->last_count = 0; |
| 113 | + |
| 114 | + rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); |
| 115 | + mp_printf(&mp_plat_print, "Selected Channel:%d!\n",channel); |
| 116 | + |
| 117 | + // Configure Channel |
| 118 | + rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel); |
| 119 | + config.rx_config.filter_en = true; |
| 120 | + config.rx_config.idle_threshold = 30000; |
| 121 | + config.clk_div = 240; |
| 122 | + |
| 123 | + rmt_config(&config); |
| 124 | + size_t len = 1000; //TODO: pick a reasonable number for this? |
| 125 | + // size_t len = maxlen * 4; |
| 126 | + rmt_driver_install(channel, len, 0); |
| 127 | + |
| 128 | + self->channel = channel; |
| 129 | + |
| 130 | + // Store handle for background updates |
| 131 | + handles[channel] = self; |
| 132 | + |
| 133 | + rmt_get_ringbuf_handle(channel, &(self->buf_handle)); |
| 134 | + rmt_rx_start(channel, true); |
| 135 | + |
| 136 | + supervisor_enable_tick(); |
| 137 | + refcount++; |
39 | 138 | }
|
40 | 139 |
|
41 | 140 | bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
|
42 |
| - return false; |
| 141 | + return handles[self->channel] ? true : false; |
43 | 142 | }
|
44 | 143 |
|
45 | 144 | void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
| 145 | + handles[self->channel] = NULL; |
| 146 | + esp32s2_peripherals_free_rmt(self->channel); |
| 147 | + reset_pin_number(self->pin); |
| 148 | + refcount--; |
| 149 | + if (refcount == 0) { |
| 150 | + supervisor_disable_tick(); |
| 151 | + } |
46 | 152 | }
|
47 | 153 |
|
48 | 154 | void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
| 155 | + self->paused = true; |
| 156 | + rmt_rx_stop(); |
49 | 157 | }
|
50 | 158 |
|
51 | 159 | void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
|
| 160 | + // Make sure we're paused. |
| 161 | + if ( !self->paused ) { |
| 162 | + common_hal_pulseio_pulsein_pause(self); |
| 163 | + } |
| 164 | + |
| 165 | + self->paused = false; |
| 166 | + rmt_rx_start(); |
52 | 167 | }
|
53 | 168 |
|
54 | 169 | void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
|
| 170 | + self->start = 0; |
| 171 | + self->len = 0; |
55 | 172 | }
|
56 | 173 |
|
57 | 174 | uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
|
58 |
| - return false; |
| 175 | + //mp_printf(&mp_plat_print, "Call GetItem\n"); |
| 176 | + update_internal_buffer(self); |
| 177 | + if (index < 0) { |
| 178 | + index += self->len; |
| 179 | + } |
| 180 | + if (index < 0 || index >= self->len) { |
| 181 | + mp_raise_IndexError(translate("index out of range")); |
| 182 | + } |
| 183 | + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; |
| 184 | + return value; |
59 | 185 | }
|
60 | 186 |
|
61 | 187 | uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
|
62 |
| - return false; |
| 188 | + mp_printf(&mp_plat_print, "Call PopLeft\n"); |
| 189 | + update_internal_buffer(self); |
| 190 | + |
| 191 | + if (self->len == 0) { |
| 192 | + mp_raise_IndexError(translate("pop from an empty PulseIn")); |
| 193 | + } |
| 194 | + |
| 195 | + uint16_t value = self->buffer[self->start]; |
| 196 | + self->start = (self->start + 1) % self->maxlen; |
| 197 | + self->len--; |
| 198 | + |
| 199 | + return value; |
| 200 | + |
| 201 | + |
| 202 | + // uint32_t length = 0; |
| 203 | + // rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 10); |
| 204 | + // mp_printf(&mp_plat_print, "Length%d\n",length); |
| 205 | + // if (items) { |
| 206 | + // length /= 4; |
| 207 | + // mp_printf(&mp_plat_print, "Length%d\n",length); |
| 208 | + // for (size_t i=0; i < length; i++) { |
| 209 | + // mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), |
| 210 | + // items[i].level0,(items[i].duration1 * 3),items[i].level1); |
| 211 | + // } |
| 212 | + // vRingbufferReturnItem(self->buf_handle, (void *) items); |
| 213 | + // } |
| 214 | + // // while(items) { |
| 215 | + // // mp_printf(&mp_plat_print, "Length%d",length); |
| 216 | + // // mp_printf(&mp_plat_print, "Item val0:%d, val1:%d, val:%d",items->duration0, |
| 217 | + // // items->duration1,items->val); |
| 218 | + // // vRingbufferReturnItem(self->buf_handle, (void *) items); |
| 219 | + // // items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 1); |
| 220 | + // // } |
| 221 | + // return items ? items[0].duration0 : false; |
63 | 222 | }
|
64 | 223 |
|
65 | 224 | uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
|
66 |
| - return false; |
| 225 | + return self->maxlen; |
67 | 226 | }
|
68 | 227 |
|
69 | 228 | bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
|
70 |
| - return false; |
| 229 | + return self->paused; |
71 | 230 | }
|
72 | 231 |
|
73 | 232 | uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
|
74 |
| - return false; |
| 233 | + return self->len; |
75 | 234 | }
|
0 commit comments