Merge pull request #6450 from mwisslead/rp2040_pulsein_common_hal_construct

Rp2040 pulsein improvements
This commit is contained in:
Scott Shawcroft 2022-06-08 08:15:29 -07:00 committed by GitHub
commit 9c20eb58e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 70 deletions

View File

@ -38,9 +38,9 @@
#define NO_PIN 0xff
#define MAX_PULSE 65535
#define MIN_PULSE 10
#define MIN_PULSE 0
uint16_t pulsein_program[] = {
static const uint16_t pulsein_program[] = {
0x4001, // 1: in pins, 1
};
@ -57,48 +57,29 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
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.
common_hal_rp2pio_statemachine_construct(&self->state_machine,
pulsein_program, MP_ARRAY_SIZE(pulsein_program),
1000000, // frequency
NULL, 0, // init, init_len
NULL, 0, 0, 0, // first out pin, # out pins, initial_out_pin_state
pin, 1, 0, 0, // first in pin, # in pins
NULL, 0, 0, 0, // first set pin
NULL, 0, 0, 0, // first sideset pin
false, // No sideset enable
NULL, PULL_NONE, // jump pin, jmp_pull
0, // wait gpio pins
true, // exclusive pin usage
false, 8, false, // TX, setting we don't use
false, // wait for TX stall
true, 32, true, // RX auto pull every 32 bits. shift left to output msb first
false, // Not user-interruptible.
0, -1); // wrap settings
if (!ok) {
mp_raise_RuntimeError(translate("All state machines in use"));
}
common_hal_pulseio_pulsein_pause(self);
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);
common_hal_pulseio_pulsein_resume(self, 0);
}
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
@ -119,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
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);
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;
self->paused = true;
}
void common_hal_pulseio_pulsein_interrupt(void *self_in) {
pulseio_pulsein_obj_t *self = self_in;
@ -140,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
} else {
uint32_t result = self->level_count;
self->last_level = level;
self->level_count = 0;
self->level_count = 1;
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
if (result > MAX_PULSE) {
result = MAX_PULSE;
@ -154,28 +136,15 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
} 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) {
common_hal_pulseio_pulsein_pause(self);
// Send the trigger pulse.
if (trigger_duration > 0) {
gpio_set_function(self->pin,GPIO_FUNC_SIO);
@ -183,11 +152,8 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
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);
@ -195,12 +161,11 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
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);
self->paused = false;
}
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) {
@ -210,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
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;
}
@ -228,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
}
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
return true;
return self->paused;
}
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self,

View File

@ -37,13 +37,13 @@ typedef struct {
mp_obj_base_t base;
uint8_t pin;
bool idle_state;
bool paused;
uint16_t maxlen;
uint16_t *buffer;
volatile bool last_level;
volatile uint32_t level_count;
volatile uint16_t len;
volatile uint16_t start;
volatile uint16_t buf_index;
rp2pio_statemachine_obj_t state_machine;
} pulseio_pulsein_obj_t;