Hopefully fix PulseIn
This commit is contained in:
parent
ed5cdd7e09
commit
00d5f63e7c
@ -29,6 +29,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "nrf/timers.h"
|
||||||
#include "py/mpconfig.h"
|
#include "py/mpconfig.h"
|
||||||
#include "py/gc.h"
|
#include "py/gc.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
@ -41,6 +42,20 @@
|
|||||||
// obj array to map pin -> self since nrfx hide the mapping
|
// obj array to map pin -> self since nrfx hide the mapping
|
||||||
static pulseio_pulsein_obj_t* _objs[GPIOTE_CH_NUM];
|
static pulseio_pulsein_obj_t* _objs[GPIOTE_CH_NUM];
|
||||||
|
|
||||||
|
// A single timer is shared amongst all PulseIn objects as a common high speed clock reference.
|
||||||
|
static uint8_t refcount = 0;
|
||||||
|
static nrfx_timer_t *timer = NULL;
|
||||||
|
|
||||||
|
static uint32_t overflow_count = 0;
|
||||||
|
|
||||||
|
static void timer_overflow_event_handler(nrf_timer_event_t event_type, void *p_context) {
|
||||||
|
if (event_type != NRF_TIMER_EVENT_COMPARE0) {
|
||||||
|
// Other event.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
overflow_count++;
|
||||||
|
}
|
||||||
|
|
||||||
// return index of the object in array
|
// return index of the object in array
|
||||||
static int _find_pulsein_obj(pulseio_pulsein_obj_t* obj) {
|
static int _find_pulsein_obj(pulseio_pulsein_obj_t* obj) {
|
||||||
for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) {
|
for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) {
|
||||||
@ -54,13 +69,8 @@ static int _find_pulsein_obj(pulseio_pulsein_obj_t* obj) {
|
|||||||
|
|
||||||
static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
|
static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
|
||||||
// Grab the current time first.
|
// Grab the current time first.
|
||||||
uint32_t current_us = 0;
|
uint32_t current_overflow = overflow_count;
|
||||||
uint64_t current_ms = 0;
|
uint32_t current_count = nrfx_timer_capture(timer, 1);
|
||||||
// FIXME! We need a higher resolution clock to measure against.
|
|
||||||
//current_tick(¤t_ms, ¤t_us);
|
|
||||||
|
|
||||||
// current_tick gives us the remaining us until the next tick but we want the number since the last ms.
|
|
||||||
current_us = 1000 - current_us;
|
|
||||||
|
|
||||||
pulseio_pulsein_obj_t* self = NULL;
|
pulseio_pulsein_obj_t* self = NULL;
|
||||||
for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) {
|
for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) {
|
||||||
@ -78,18 +88,16 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
|
|||||||
self->first_edge = false;
|
self->first_edge = false;
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
uint32_t ms_diff = current_ms - self->last_ms;
|
// Wrapped around a number of times.
|
||||||
uint16_t us_diff = current_us - self->last_us;
|
uint32_t total_diff = 0xffff;
|
||||||
uint32_t total_diff = us_diff;
|
// Wrapped around once so
|
||||||
|
if (self->last_overflow == current_overflow - 1) {
|
||||||
if (self->last_us > current_us) {
|
total_diff = current_count + (0xffffffff - self->last_count);
|
||||||
total_diff = 1000 + current_us - self->last_us;
|
} else if (self->last_overflow == current_overflow) {
|
||||||
if (ms_diff > 1) {
|
total_diff = current_count - self->last_count;
|
||||||
total_diff += (ms_diff - 1) * 1000;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
total_diff += ms_diff * 1000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cap duration at 16 bits.
|
||||||
uint16_t duration = 0xffff;
|
uint16_t duration = 0xffff;
|
||||||
if (total_diff < duration) {
|
if (total_diff < duration) {
|
||||||
duration = total_diff;
|
duration = total_diff;
|
||||||
@ -104,8 +112,8 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self->last_ms = current_ms;
|
self->last_overflow = current_overflow;
|
||||||
self->last_us = current_us;
|
self->last_count = current_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pulsein_reset(void) {
|
void pulsein_reset(void) {
|
||||||
@ -114,6 +122,11 @@ void pulsein_reset(void) {
|
|||||||
}
|
}
|
||||||
nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY);
|
nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY);
|
||||||
|
|
||||||
|
if (timer != NULL) {
|
||||||
|
nrf_peripherals_free_timer(timer);
|
||||||
|
}
|
||||||
|
refcount = 0;
|
||||||
|
|
||||||
memset(_objs, 0, sizeof(_objs));
|
memset(_objs, 0, sizeof(_objs));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,6 +142,27 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
|
|||||||
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
|
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (refcount == 0) {
|
||||||
|
timer = nrf_peripherals_allocate_timer();
|
||||||
|
if (timer == NULL) {
|
||||||
|
mp_raise_RuntimeError(translate("All timers in use"));
|
||||||
|
}
|
||||||
|
overflow_count = 0;
|
||||||
|
|
||||||
|
nrfx_timer_config_t timer_config = {
|
||||||
|
// PulseIn durations are in microseconds, so this is convenient.
|
||||||
|
.frequency = NRF_TIMER_FREQ_1MHz,
|
||||||
|
.mode = NRF_TIMER_MODE_TIMER,
|
||||||
|
.bit_width = NRF_TIMER_BIT_WIDTH_32,
|
||||||
|
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
|
||||||
|
};
|
||||||
|
|
||||||
|
nrfx_timer_init(timer, &timer_config, &timer_overflow_event_handler);
|
||||||
|
// Interrupt on overflow so we can track when it rolls over.
|
||||||
|
nrfx_timer_compare(timer, 0, 0, true);
|
||||||
|
}
|
||||||
|
refcount++;
|
||||||
|
|
||||||
self->pin = pin->number;
|
self->pin = pin->number;
|
||||||
self->maxlen = maxlen;
|
self->maxlen = maxlen;
|
||||||
self->idle_state = idle_state;
|
self->idle_state = idle_state;
|
||||||
@ -136,8 +170,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
|
|||||||
self->len = 0;
|
self->len = 0;
|
||||||
self->first_edge = true;
|
self->first_edge = true;
|
||||||
self->paused = false;
|
self->paused = false;
|
||||||
self->last_us = 0;
|
self->last_overflow = 0;
|
||||||
self->last_ms = 0;
|
self->last_count = 0;
|
||||||
|
|
||||||
claim_pin(pin);
|
claim_pin(pin);
|
||||||
|
|
||||||
@ -173,6 +207,11 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
|||||||
|
|
||||||
reset_pin_number(self->pin);
|
reset_pin_number(self->pin);
|
||||||
self->pin = NO_PIN;
|
self->pin = NO_PIN;
|
||||||
|
|
||||||
|
refcount--;
|
||||||
|
if (refcount == 0) {
|
||||||
|
nrf_peripherals_free_timer(timer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
||||||
@ -207,8 +246,8 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t tri
|
|||||||
|
|
||||||
self->first_edge = true;
|
self->first_edge = true;
|
||||||
self->paused = false;
|
self->paused = false;
|
||||||
self->last_ms = 0;
|
self->last_overflow = 0;
|
||||||
self->last_us = 0;
|
self->last_count = 0;
|
||||||
|
|
||||||
nrfx_gpiote_in_event_enable(self->pin, true);
|
nrfx_gpiote_in_event_enable(self->pin, true);
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,8 @@ typedef struct {
|
|||||||
|
|
||||||
volatile uint16_t start;
|
volatile uint16_t start;
|
||||||
volatile uint16_t len;
|
volatile uint16_t len;
|
||||||
volatile uint16_t last_us;
|
volatile size_t last_overflow;
|
||||||
volatile uint64_t last_ms;
|
volatile size_t last_count;
|
||||||
} pulseio_pulsein_obj_t;
|
} pulseio_pulsein_obj_t;
|
||||||
|
|
||||||
void pulsein_reset(void);
|
void pulsein_reset(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user