Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rp2040 pulsein improvements #6450

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 29 additions & 70 deletions ports/raspberrypi/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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.
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;
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

common_hal_pulseio_pulsein_pause(self);

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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -154,53 +136,36 @@ 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);
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);
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) {
Expand All @@ -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;
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/pulseio/PulseIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down