PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

direct_pin_read.h 3.8KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef direct_pin_read_h_
  2. #define direct_pin_read_h_
  3. #if defined(__AVR__)
  4. #define IO_REG_TYPE uint8_t
  5. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  6. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  7. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  8. #elif defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL))
  9. #define IO_REG_TYPE uint8_t
  10. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  11. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  12. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  13. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  14. #define IO_REG_TYPE uint32_t
  15. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  16. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  17. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  18. #elif defined(__SAM3X8E__) // || defined(ESP8266)
  19. #define IO_REG_TYPE uint32_t
  20. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  21. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  22. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  23. #elif defined(__PIC32MX__)
  24. #define IO_REG_TYPE uint32_t
  25. #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
  26. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  27. #define DIRECT_PIN_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0)
  28. /* ESP8266 v2.0.0 Arduino workaround for bug https://github.com/esp8266/Arduino/issues/1110 */
  29. #elif defined(ESP8266)
  30. #define IO_REG_TYPE uint32_t
  31. #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)(0x60000000+(0x318)))
  32. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  33. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  34. /* ESP32 Arduino (https://github.com/espressif/arduino-esp32) */
  35. #elif defined(ESP32)
  36. #define IO_REG_TYPE uint32_t
  37. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  38. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  39. #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  40. #elif defined(__SAMD21G18A__) || defined(__SAMD51__)
  41. #define IO_REG_TYPE uint32_t
  42. #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
  43. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  44. #define DIRECT_PIN_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0)
  45. #elif defined(RBL_NRF51822)
  46. #define IO_REG_TYPE uint32_t
  47. #define PIN_TO_BASEREG(pin) (0)
  48. #define PIN_TO_BITMASK(pin) (pin)
  49. #define DIRECT_PIN_READ(base, pin) nrf_gpio_pin_read(pin)
  50. #elif defined(__arc__) /* Arduino101/Genuino101 specifics */
  51. #include "scss_registers.h"
  52. #include "portable.h"
  53. #include "avr/pgmspace.h"
  54. #define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId)
  55. #define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType)
  56. #define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase)
  57. #define EXT_PORT_OFFSET_SS 0x0A
  58. #define EXT_PORT_OFFSET_SOC 0x50
  59. #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase)
  60. #define PIN_TO_BITMASK(pin) pin
  61. #define IO_REG_TYPE uint32_t
  62. static inline __attribute__((always_inline))
  63. IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  64. {
  65. IO_REG_TYPE ret;
  66. if (SS_GPIO == GPIO_TYPE(pin)) {
  67. ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS));
  68. } else {
  69. ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC);
  70. }
  71. return ((ret >> GPIO_ID(pin)) & 0x01);
  72. }
  73. #define DIRECT_PIN_READ(base, pin) directRead(base, pin)
  74. #endif
  75. #endif