diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c new file mode 100644 index 0000000..474d497 --- /dev/null +++ b/cores/arduino/wiring_pulse.c @@ -0,0 +1,53 @@ +/* + Copyright (c) 2011 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Arduino.h" +#include "wiring_private.h" +#include "sysctl.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. */ +extern uint32_t pulseIn(uint32_t pin, bool state, uint32_t timeout) +{ + uint32_t cpu_freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); + uint32_t numloops = 0; + uint32_t maxloops = timeout * (cpu_freq / 1000000L) / 16; // microsecondsToClockCycles(timeout) / 16; ??? + uint32_t start, end; + + // wait for any previous pulse to end + while (digitalRead(pin) == state) + if (numloops++ == maxloops) + return 0; + + // wait for the pulse to start + while (digitalRead(pin) != state) + if (numloops++ == maxloops) + return 0; + + // wait for the pulse to stop + start = micros(); + while (digitalRead(pin) == state) + if (numloops++ == maxloops) + return 0; + end = micros(); + + return (end - start); +} diff --git a/cores/arduino/wiring_pulse.h b/cores/arduino/wiring_pulse.h new file mode 100644 index 0000000..7226583 --- /dev/null +++ b/cores/arduino/wiring_pulse.h @@ -0,0 +1,40 @@ +/* + Copyright (c) 2011 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _WIRING_PULSE_ +#define _WIRING_PULSE_ + +#include + +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus + +/* + * \brief 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. + */ +extern uint32_t pulseIn(uint32_t ulPin, bool ulState, uint32_t ulTimeout = 1000000L); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif /* _WIRING_PULSE_ */ \ No newline at end of file diff --git a/platform.txt b/platform.txt index dded941..bf7b22b 100644 --- a/platform.txt +++ b/platform.txt @@ -98,5 +98,5 @@ tools.manual_openocd.program.pattern="{path}{cmd}" -d0 -f {program.config} -c "f tools.kflash.path={runtime.tools.kflash.path}/ tools.kflash.cmd={runtime.tools.kflash.path}/kflash tools.kflash.bootloader.path={runtime.tools.kflash.path}/bootloader.bin -tools.kflash.upload.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" -b 2000000 {build.path}/{build.project_name}.bin -tools.kflash.program.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" -b 2000000 {build.path}/{build.project_name}.bin \ No newline at end of file +tools.kflash.upload.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" {build.path}/{build.project_name}.bin +tools.kflash.program.pattern="{cmd}" -p {serial.port} -l "{bootloader.path}" {build.path}/{build.project_name}.bin \ No newline at end of file