1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Dave Putz for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
#include <stdint.h>
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/pulseio/PulseIn.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate.h"
#include "bindings/rp2pio/StateMachine.h"
#include "common-hal/pulseio/PulseIn.h"
#define NO_PIN 0xff
#define MAX_PULSE 65535
#define MIN_PULSE 10
uint16_t pulsein_program[] = {
0x4001, // 1: in pins, 1
};
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
if (self->buffer == NULL) {
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
}
self->pin = pin->number;
self->maxlen = maxlen;
self->idle_state = idle_state;
self->start = 0;
self->len = 0;
bool ok = rp2pio_statemachine_construct(&self->state_machine,
pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]),
1000000,
NULL, 0,
NULL, 0,
pin, 1,
0,0,
NULL, 0,
NULL, 0,
1, 0,
NULL, // jump pin
1 << self->pin, false, true,
false, 8, false, // TX, unused
false,
true, 32, true, // RX auto-push every 32 bits
false, // claim pins
false, // Not user-interruptible.
false, // No sideset enable
0, -1); // wrap settings
if (!ok) {
mp_raise_RuntimeError(translate("All state machines in use"));
}
pio_sm_set_enabled(self->state_machine.pio,self->state_machine.state_machine, false);
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
self->last_level = self->idle_state;
self->level_count = 0;
self->buf_index = 0;
pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number);
common_hal_rp2pio_statemachine_set_interrupt_handler(&(self->state_machine),&common_hal_pulseio_pulsein_interrupt,self,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS);
// exec a set pindirs to 0 for input
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080);
// exec the appropriate wait for pin
if (self->idle_state == true) {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
} else {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
}
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
}
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
return self->pin == NO_PIN;
}
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
if (common_hal_pulseio_pulsein_deinited(self)) {
return;
}
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
common_hal_rp2pio_statemachine_deinit(&self->state_machine);
m_free(self->buffer);
reset_pin_number(self->pin);
self->pin = NO_PIN;
}
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
self->last_level = self->idle_state;
self->level_count = 0;
self->buf_index = 0;
}
void common_hal_pulseio_pulsein_interrupt(void *self_in) {
pulseio_pulsein_obj_t *self = self_in;
uint32_t rxfifo = 0;
rxfifo = pio_sm_get_blocking(self->state_machine.pio, self->state_machine.state_machine);
// translate from fifo to buffer
if ((rxfifo == 0 && self->last_level == false) || (rxfifo == 0xffffffff && self->last_level == true)) {
self->level_count = self->level_count + 32;
} else {
for (uint i = 0; i < 32; i++) {
bool level = (rxfifo & (1 << i)) >> i;
if (level == self->last_level) {
self->level_count++;
} else {
uint32_t result = self->level_count;
self->last_level = level;
self->level_count = 0;
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
if (result > MAX_PULSE) {
result = MAX_PULSE;
}
// return pulses that are not too short
if (result > MIN_PULSE) {
size_t buf_index = (self->start + self->len) % self->maxlen;
self->buffer[buf_index] = (uint16_t)result;
if (self->len < self->maxlen) {
self->len++;
} else {
self->start = (self->start + 1) % self->maxlen;
}
if (self->buf_index < self->maxlen) {
self->buf_index++;
} else {
self->start = 0;
self->buf_index = 0;
}
}
}
}
}
// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
if ((self->level_count > MAX_PULSE) || (self->buf_index >= self->maxlen)) {
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
self->buf_index = 0;
}
}
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
uint16_t trigger_duration) {
// Send the trigger pulse.
if (trigger_duration > 0) {
gpio_set_function(self->pin,GPIO_FUNC_SIO);
gpio_set_dir(self->pin,true);
gpio_put(self->pin, !self->idle_state);
common_hal_mcu_delay_us((uint32_t)trigger_duration);
gpio_set_function(self->pin,GPIO_FUNC_PIO0);
common_hal_mcu_delay_us(125);
}
// Reconfigure the pin for PIO
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
// exec a wait for the selected pin to change state
if (self->idle_state == true) {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
} else {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
}
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
}
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
self->start = 0;
self->len = 0;
self->buf_index = 0;
}
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
if (self->len == 0) {
mp_raise_IndexError_varg(translate("pop from empty %q"), MP_QSTR_PulseIn);
}
uint16_t value = self->buffer[self->start];
self->start = (self->start + 1) % self->maxlen;
self->len--;
// if we are empty reset buffer pointer and counters
if (self->len == 0) {
self->start = 0;
self->buf_index = 0;
self->level_count = 0;
}
return value;
}
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
return self->maxlen;
}
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
return self->len;
}
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
return true;
}
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self,
int16_t index) {
if (index < 0) {
index += self->len;
}
if (index < 0 || index >= self->len) {
mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn);
}
uint16_t value = self->buffer[(self->start + index) % self->maxlen];
return value;
}
|