diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-04-20 15:40:00 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-20 15:40:00 -0400 |
| commit | 58ba74194e120fd990cf9832b09a7067ddb32968 (patch) | |
| tree | a239e714e4f73dcb48dfda64fe4184f333f66bb8 | |
| parent | 77092f23802787f006783d37aad9c5d396bce3d9 (diff) | |
| parent | bef05ffbf183225563d087de8b48e80dbeb7dd17 (diff) | |
Merge pull request #765 from jerryneedell/jerryn_tick
modify tick_delay to handle SysTick->VAL rollover
| -rw-r--r-- | ports/atmel-samd/common-hal/pulseio/PulseIn.c | 5 | ||||
| -rw-r--r-- | ports/atmel-samd/tick.c | 18 |
2 files changed, 10 insertions, 13 deletions
diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index 2dca52913..6e3aa3abb 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -283,12 +283,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, if (trigger_duration > 0) { gpio_set_pin_pull_mode(self->pin, GPIO_PULL_OFF); gpio_set_pin_direction(self->pin, GPIO_DIRECTION_OUT); - - common_hal_mcu_disable_interrupts(); gpio_set_pin_level(self->pin, !self->idle_state); - common_hal_mcu_delay_us(trigger_duration); + common_hal_mcu_delay_us((uint32_t)trigger_duration); gpio_set_pin_level(self->pin, self->idle_state); - common_hal_mcu_enable_interrupts(); } // Reconfigure the pin and make sure its set to detect the first edge. diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index f5fd57f5d..27b5f05b4 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -52,21 +52,21 @@ void SysTick_Handler(void) { void tick_init() { uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000; - SysTick_Config(ticks_per_ms); + SysTick_Config(ticks_per_ms-1); NVIC_EnableIRQ(SysTick_IRQn); } void tick_delay(uint32_t us) { uint32_t ticks_per_us = common_hal_mcu_processor_get_frequency() / 1000 / 1000; - uint32_t us_between_ticks = SysTick->VAL / ticks_per_us; - uint64_t start_ms = ticks_ms; - while (us > 1000) { - while (ticks_ms == start_ms) {} - us -= us_between_ticks; - start_ms = ticks_ms; - us_between_ticks = 1000; + uint32_t us_until_next_tick = SysTick->VAL / ticks_per_us; + uint32_t start_tick; + while (us >= us_until_next_tick) { + start_tick=SysTick->VAL; // wait for SysTick->VAL to RESET + while (SysTick->VAL < start_tick) {} + us -= us_until_next_tick; + us_until_next_tick = 1000; } - while (SysTick->VAL > ((us_between_ticks - us) * ticks_per_us)) {} + while (SysTick->VAL > ((us_until_next_tick - us) * ticks_per_us)) {} } // us counts down! |
