8000 Prevent freezing USB during high frequency PulseIn. by tannewt · Pull Request #901 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Prevent freezing USB during high frequency PulseIn. #901

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

Merged
merged 7 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

10000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ports/atmel-samd/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@
#include "background.h"

#include "audio_dma.h"
#include "tick.h"
#include "usb.h"
#include "usb_mass_storage.h"

volatile uint64_t last_finished_tick = 0;

void run_background_tasks(void) {
audio_dma_background();
usb_msc_background();
usb_cdc_background();
last_finished_tick = ticks_ms;
}

bool background_tasks_ok(void) {
return ticks_ms - last_finished_tick < 1000;
}
3 changes: 3 additions & 0 deletions ports/atmel-samd/background.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BACKGROUND_H
#define MICROPY_INCLUDED_ATMEL_SAMD_BACKGROUND_H

#include <stdbool.h>

void run_background_tasks(void);
bool background_tasks_ok(void);

#endif // MICROPY_INCLUDED_ATMEL_SAMD_BACKGROUND_H
10 changes: 1 addition & 9 deletions ports/atmel-samd/bindings/samd/Clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@
STATIC void samd_clock_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in);

mp_printf(print, "%q.%q.%s(", MP_QSTR_samd, MP_QSTR_clock, self->name);
if (clock_get_enabled(self->type, self->index)) {
mp_printf(print, "frequency=%u", clock_get_frequency(self->type, self->index));
uint32_t calibration = clock_get_calibration(self->type, self->index);
if (calibration) {
mp_printf(print, ", calibration=%u", calibration);
}
}
mp_printf(print, ")");
mp_printf(print, "%q.%q.%q", MP_QSTR_samd, MP_QSTR_clock, self->name);
}

//| .. attribute:: enabled
Expand Down
10 changes: 5 additions & 5 deletions ports/atmel-samd/bindings/samd/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@

typedef struct {
mp_obj_base_t base;
const char *name;
qstr name;
uint8_t type;
uint8_t index;
} samd_clock_obj_t;

#define CLOCK(_name, _type, _index) \
const samd_clock_obj_t clock_ ## _name = { \
{ &samd_clock_type }, \
.name = #_name, \
.name = MP_QSTR_ ## _name, \
.type = _type, \
.index = _index, \
}

#define CLOCK_SOURCE(_name) \
const samd_clock_obj_t clock_ ## _name = { \
{ &samd_clock_type }, \
.name = #_name, \
.name = MP_QSTR_ ## _name, \
.type = 0, \
.index = GCLK_SOURCE_ ## _name, \
}

#define CLOCK_GCLK(_name) \
const samd_clock_obj_t clock_ ## _name = { \
{ &samd_clock_type }, \
.name = #_name, \
.name = MP_QSTR_ ## _name, \
.type = 1, \
.index = _name ## _GCLK_ID, \
}

#define CLOCK_GCLK_(_name, _extra) \
const samd_clock_obj_t clock_ ## _name ## _ ## _extra = { \
{ &samd_clock_type }, \
.name = #_name "_" #_extra, \
.name = MP_QSTR_ ## _name ## _ ## _extra, \
.type = 1, \
.index = _name ## _GCLK_ID_ ## _extra, \
}
Expand Down
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/arduino_zero/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PB09)
#define DEFAULT_UART_BUS_TX (&pin_PB08)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PB09)
#define DEFAULT_UART_BUS_TX (&pin_PB08)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PB17)
#define DEFAULT_UART_BUS_TX (&pin_PB16)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/gemma_m0/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#define IGNORE_PIN_PA20 1
#define IGNORE_PIN_PA21 1
#define IGNORE_PIN_PA22 1
// USB is always used.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
#define IGNORE_PIN_PA27 1
#define IGNORE_PIN_PA28 1
#define IGNORE_PIN_PA30 1
Expand Down
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA16)
#define DEFAULT_UART_BUS_TX (&pin_PA17)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA11)
#define DEFAULT_UART_BUS_TX (&pin_PA10)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA23)
#define DEFAULT_UART_BUS_TX (&pin_PA22)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/trinket_m0/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#define IGNORE_PIN_PA21 1
#define IGNORE_PIN_PA22 1
#define IGNORE_PIN_PA23 1
// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
#define IGNORE_PIN_PA27 1
#define IGNORE_PIN_PA28 1
#define IGNORE_PIN_PA30 1
Expand Down
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@

#define DEFAULT_UART_BUS_RX (&pin_PA07)
#define DEFAULT_UART_BUS_TX (&pin_PA06)

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
4 changes: 4 additions & 0 deletions ports/atmel-samd/boards/ugame10/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@
{ MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module }

// USB is always used internally so skip the pin objects for it.
#define IGNORE_PIN_PA24 1
#define IGNORE_PIN_PA25 1
14 changes: 3 additions & 11 deletions ports/atmel-samd/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,10 @@ void common_hal_mcu_delay_us(uint32_t delay) {
mp_hal_delay_us(delay);
}

// Interrupt flags that will be saved and restored during disable/Enable
// interrupt functions below.

// ASF4's interrupt disable doesn't handle duplicate calls
volatile uint32_t interrupt_flags;
volatile uint32_t nesting_count = 0;
void common_hal_mcu_disable_interrupts(void) {
if (nesting_count == 0) {
interrupt_flags = __get_PRIMASK();
__disable_irq();
__DMB();
}
__disable_irq();
__DMB();
nesting_count++;
}

Expand All @@ -66,7 +58,7 @@ void common_hal_mcu_enable_interrupts(void) {
return;
}
__DMB();
__set_PRIMASK(interrupt_flags);
__enable_irq();
}

extern uint32_t _ezero;
Expand Down
16 changes: 16 additions & 0 deletions ports/atmel-samd/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "atmel_start_pins.h"
#include "hal/include/hal_gpio.h"

#include "background.h"
#include "mpconfigport.h"
#include "py/gc.h"
#include "py/runtime.h"
Expand Down Expand Up @@ -60,10 +61,16 @@ void pulsein_interrupt_handler(uint8_t channel) {
uint32_t current_us;
uint64_t current_ms;
current_tick(&current_ms, &current_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 = get_eic_channel_data(channel);
if (!background_tasks_ok() || self->errored_too_fast) {
self->errored_too_fast = true;
common_hal_pulseio_pulsein_pause(self);
return;
}
if (self->first_edge) {
self->first_edge = false;
pulsein_set_config(self, false);
Expand Down Expand Up @@ -118,6 +125,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
self->first_edge = true;
self->last_us = 0;
self->last_ms = 0;
self->errored_too_fast = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= false


set_eic_channel_data(pin->extint_channel, (void*) self);

Expand Down Expand Up @@ -157,6 +165,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
// Make sure we're paused.
common_hal_pulseio_pulsein_pause(self);

// Reset erroring
self->errored_too_fast = false;

// Send the trigger pulse.
if (trigger_duration > 0) {
gpio_set_pin_pull_mode(self->pin, GPIO_PULL_OFF);
Expand Down Expand Up @@ -207,6 +218,11 @@ 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) {
uint32_t mask = 1 << self->channel;
return (EIC->INTENSET.reg & (mask << EIC_INTENSET_EXTINT_Pos)) == 0;
}

uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self,
int16_t index) {
common_hal_mcu_disable_interrupts();
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/common-hal/pulseio/PulseIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef struct {
volatile bool first_edge;
volatile uint64_t last_ms;
volatile uint16_t last_us;
volatile bool errored_too_fast;
} pulseio_pulsein_obj_t;

void pulsein_reset(void);
Expand Down
3 changes: 3 additions & 0 deletions ports/atmel-samd/peripherals/samd21/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ static void init_clock_source_dfll48m_xosc(void) {
SYSCTRL_DFLLCTRL_ENABLE;
while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {}
while (GCLK->STATUS.bit.SYNCBUSY) {}

// Wait for the fine lock on the DFLL.
while (!SYSCTRL->PCLKSR.bit.DFLLLCKC || !SYSCTRL->PCLKSR.bit.DFLLLCKF) {}
}

static void init_clock_source_dfll48m_usb(void) {
Expand Down
16 changes: 16 additions & 0 deletions ports/atmel-samd/tick.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ void tick_init() {
uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000;
SysTick_Config(ticks_per_ms-1);
NVIC_EnableIRQ(SysTick_IRQn);
// Set all peripheral interrupt priorities to the lowest priority by default.
for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) {
NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL);
}
// Bump up the systick interrupt.
NVIC_SetPriority(SysTick_IRQn, 1);
#ifdef SAMD21
NVIC_SetPriority(USB_IRQn, 1);
#endif

#ifdef SAMD51
NVIC_SetPriority(USB_0_IRQn, 1);
NVIC_SetPriority(USB_1_IRQn, 1);
NVIC_SetPriority(USB_2_IRQn, 1);
NVIC_SetPriority(USB_3_IRQn, 1);
#endif
}

void tick_delay(uint32_t us) {
Expand Down
4 changes: 4 additions & 0 deletions ports/nrf/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
return 0xadaf;
}

bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
return true;
}

uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
return 0xadaf;
}
Expand Down
Loading
0