From 4a84203917deb692d65175c7c07c83893732861b Mon Sep 17 00:00:00 2001 From: lacklustrlabs Date: Sat, 18 Nov 2017 21:08:20 +0100 Subject: [PATCH 1/2] Modified pulseIn() to use the state parameter --- .../cores/maple/stm32f1/wiring_pulse_f1.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp index 9ca4d7f..32ecc07 100644 --- a/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp +++ b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp @@ -1,5 +1,6 @@ #include #include "boards.h" +#include "variant.h" /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds @@ -28,13 +29,13 @@ */ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) { - // cache the port and bit of the pin in order to speed up the + // cache the reg map and bit of the pin in order to speed up the // pulse width measuring loop and achieve finer resolution. calling // digitalRead() instead yields much coarser resolution. - gpio_dev *dev=PIN_MAP[pin].gpio_device; - uint32_t bit = (1U << PIN_MAP[pin].gpio_bit); - + const gpio_reg_map * const regs = digitalPinToPort(pin)->regs; + const uint32_t bit = digitalPinToBitMask(pin); + const uint32_t stateMask = (state ? bit:0); uint32_t width = 0; // keep initialization out of time critical area @@ -45,23 +46,23 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) volatile uint32_t dummyWidth=0; // wait for any previous pulse to end - while ( (dev->regs->IDR & bit) == bit) { + while ((regs->IDR & bit) == stateMask) { if (numloops++ == maxloops) { return 0; } - dummyWidth++; + dummyWidth++; } // wait for the pulse to start - while ((dev->regs->IDR & bit) != bit) { + while ((regs->IDR & bit) != stateMask) { if (numloops++ == maxloops) { return 0; } - dummyWidth++; + dummyWidth++; } // wait for the pulse to stop - while ((dev->regs->IDR & bit) == bit) { + while ((regs->IDR & bit) == stateMask) { if (numloops++ == maxloops) { return 0; } From 0f6d90943adcfe12ddd69a998d47e5dc2dabb566 Mon Sep 17 00:00:00 2001 From: lacklustrlabs Date: Sun, 19 Nov 2017 09:39:29 +0100 Subject: [PATCH 2/2] Caching IDR pointer --- STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp index 32ecc07..5a5d835 100644 --- a/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp +++ b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp @@ -29,11 +29,11 @@ */ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) { - // cache the reg map and bit of the pin in order to speed up the + // cache the IDR address and bit of the pin in order to speed up the // pulse width measuring loop and achieve finer resolution. calling // digitalRead() instead yields much coarser resolution. - const gpio_reg_map * const regs = digitalPinToPort(pin)->regs; + __io uint32_t * const idr = portInputRegister(digitalPinToPort(pin)); const uint32_t bit = digitalPinToBitMask(pin); const uint32_t stateMask = (state ? bit:0); @@ -46,7 +46,7 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) volatile uint32_t dummyWidth=0; // wait for any previous pulse to end - while ((regs->IDR & bit) == stateMask) { + while ((*idr & bit) == stateMask) { if (numloops++ == maxloops) { return 0; } @@ -54,7 +54,7 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) } // wait for the pulse to start - while ((regs->IDR & bit) != stateMask) { + while ((*idr & bit) != stateMask) { if (numloops++ == maxloops) { return 0; } @@ -62,7 +62,7 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) } // wait for the pulse to stop - while ((regs->IDR & bit) == stateMask) { + while ((*idr & bit) == stateMask) { if (numloops++ == maxloops) { return 0; }