diff --git a/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp new file mode 100644 index 0000000..9ca4d7f --- /dev/null +++ b/STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp @@ -0,0 +1,75 @@ +#include +#include "boards.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 + * before the start of the pulse. */ + + + /* + * Roger Clark + * + * Note. The API spec for this function published on http://www.arduino.cc/en/Reference/PulseIn + * doesn't reflect what either the AVR or SAM version of this function actualy do with regard to the timeout value + * + * "timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) " + * + * Because the timeout, is actually coded as the total time to both wait while the input is in the state requested + * then wait for the opposite state duration + * then count the length of the pulse when it has the value of state (HIGH or LOW) + * + * So I think the code for both the AVR and the Due is wrong in that it doesnt match the spec + * + * I have done basically the same as the AVR and Due code, except to make the timeout a bit more accurate I have put in a dummy volatile varable + * dummyWidth so that both the waiting while loops take the same number of clock cycles to execute as the acount width counting loop + * + * to be slighly more accurate the maxLoops variable really needs to take into account the loop setup code, but its probably as good as necessary + * + */ +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 + // 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); + + + uint32_t width = 0; // keep initialization out of time critical area + + // convert the timeout from microseconds to a number of times through + // the initial loop; it takes 16 clock cycles per iteration. + uint32_t numloops = 0; + uint32_t maxloops = timeout * ( F_CPU / 16000000); + volatile uint32_t dummyWidth=0; + + // wait for any previous pulse to end + while ( (dev->regs->IDR & bit) == bit) { + if (numloops++ == maxloops) { + return 0; + } + dummyWidth++; + } + + // wait for the pulse to start + while ((dev->regs->IDR & bit) != bit) { + if (numloops++ == maxloops) { + return 0; + } + dummyWidth++; + } + + // wait for the pulse to stop + while ((dev->regs->IDR & bit) == bit) { + if (numloops++ == maxloops) { + return 0; + } + width++; + } + + // Excluding time taking up by the interrupts, it needs 16 clock cycles to look through the last while loop + // 5 is added as a fiddle factor to correct for interrupts etc. But ultimately this would only be accurate if it was done ona hardware timer + + return (uint32_t)( ( (unsigned long long)(width+5) * (unsigned long long) 16000000.0) /(unsigned long long)F_CPU ) ; +} diff --git a/STM32F1/cores/maple/wiring_pulse.h b/STM32F1/cores/maple/wiring_pulse.h new file mode 100644 index 0000000..abc2463 --- /dev/null +++ b/STM32F1/cores/maple/wiring_pulse.h @@ -0,0 +1,37 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2015 Roger Clark + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ + + + +#ifndef _WIRISH_PULSE_H_ +#define _WIRISH_PULSE_H_ + +#include + +uint32_t pulseIn( uint32_t ulPin, uint32_t ulState, uint32_t ulTimeout = 1000000L ) ; + + +#endif diff --git a/STM32F1/cores/maple/wirish.h b/STM32F1/cores/maple/wirish.h index d925df6..c074528 100644 --- a/STM32F1/cores/maple/wirish.h +++ b/STM32F1/cores/maple/wirish.h @@ -59,6 +59,7 @@ #include #include #include +#include #if STM32_MCU_SERIES == STM32_SERIES_F1 /* FIXME [0.0.13?] port to F2 */ //#include