Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

81 行
3.0KB

  1. /*
  2. wiring.c - Partial implementation of the Wiring API for the ATmega8.
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. Modified for Teensyduino by Paul Stoffregen, paul@pjrc.com
  6. http://www.pjrc.com/teensy/teensyduino.html
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General
  16. Public License along with this library; if not, write to the
  17. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. Boston, MA 02111-1307 USA
  19. */
  20. #include "wiring_private.h"
  21. #include "pins_arduino.h"
  22. #include "core_pins.h"
  23. #define PULSEIN_CYCLES_PER_LOOP 21
  24. #define PULSEIN_CYCLES_LATENCY 11
  25. /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
  26. * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
  27. * to 3 minutes in length, but must be called at least a few dozen microseconds
  28. * before the start of the pulse. */
  29. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
  30. {
  31. // cache the port and bit of the pin in order to speed up the
  32. // pulse width measuring loop and achieve finer resolution. calling
  33. // digitalRead() instead yields much coarser resolution.
  34. uint8_t bit = digitalPinToBitMask(pin);
  35. volatile uint8_t *reg = portInputRegister(digitalPinToPort(pin));
  36. uint8_t stateMask = (state ? bit : 0);
  37. unsigned long width = 0; // keep initialization out of time critical area
  38. // convert the timeout from microseconds to a number of times through
  39. // the initial loop
  40. unsigned long numloops = 0;
  41. //unsigned long maxloops = microsecondsToClockCycles(timeout) / PULSEIN_CYCLES_PER_LOOP;
  42. unsigned long maxloops = timeout * clockCyclesPerMicrosecond() / PULSEIN_CYCLES_PER_LOOP;
  43. // wait for any previous pulse to end
  44. while ((*reg & bit) == stateMask)
  45. if (numloops++ == maxloops)
  46. return 0;
  47. // wait for the pulse to start
  48. while ((*reg & bit) != stateMask)
  49. if (numloops++ == maxloops)
  50. return 0;
  51. // wait for the pulse to stop
  52. while ((*reg & bit) == stateMask) {
  53. width++;
  54. if (numloops++ == maxloops)
  55. return 0;
  56. }
  57. // convert the reading to microseconds. The loop has been determined
  58. // to be PULSEIN_CYCLES_LATENCY clock cycles long and have about
  59. // PULSEIN_CYCLES_PER_LOOP clocks between the edge and the start of
  60. // the loop. There will be some error introduced by the interrupt
  61. // handlers.
  62. //return clockCyclesToMicroseconds(PULSEIN_CYCLES_PER_LOOP * width + PULSEIN_CYCLES_LATENCY);
  63. return (width * PULSEIN_CYCLES_PER_LOOP + PULSEIN_CYCLES_LATENCY + (clockCyclesPerMicrosecond() / 2)) / clockCyclesPerMicrosecond();
  64. }