OneWire_direct_gpio.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #ifndef OneWire_Direct_GPIO_h
  2. #define OneWire_Direct_GPIO_h
  3. // This header should ONLY be included by OneWire.cpp. These defines are
  4. // meant to be private, used within OneWire.cpp, but not exposed to Arduino
  5. // sketches or other libraries which may include OneWire.h.
  6. #include <stdint.h>
  7. // Platform specific I/O definitions
  8. #if defined(__AVR__)
  9. #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
  10. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  11. #define IO_REG_TYPE uint8_t
  12. #define IO_REG_BASE_ATTR asm("r30")
  13. #define IO_REG_MASK_ATTR
  14. #if defined(__AVR_ATmega4809__)
  15. #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  16. #define DIRECT_MODE_INPUT(base, mask) ((*((base)-8)) &= ~(mask))
  17. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)-8)) |= (mask))
  18. #define DIRECT_WRITE_LOW(base, mask) ((*((base)-4)) &= ~(mask))
  19. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)-4)) |= (mask))
  20. #else
  21. #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
  22. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
  23. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
  24. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
  25. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
  26. #endif
  27. #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
  28. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  29. #define PIN_TO_BITMASK(pin) (1)
  30. #define IO_REG_TYPE uint8_t
  31. #define IO_REG_BASE_ATTR
  32. #define IO_REG_MASK_ATTR __attribute__ ((unused))
  33. #define DIRECT_READ(base, mask) (*((base)+512))
  34. #define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
  35. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
  36. #define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
  37. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
  38. #elif defined(__MKL26Z64__)
  39. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  40. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  41. #define IO_REG_TYPE uint8_t
  42. #define IO_REG_BASE_ATTR
  43. #define IO_REG_MASK_ATTR
  44. #define DIRECT_READ(base, mask) ((*((base)+16) & (mask)) ? 1 : 0)
  45. #define DIRECT_MODE_INPUT(base, mask) (*((base)+20) &= ~(mask))
  46. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+20) |= (mask))
  47. #define DIRECT_WRITE_LOW(base, mask) (*((base)+8) = (mask))
  48. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+4) = (mask))
  49. #elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
  50. #define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
  51. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  52. #define IO_REG_TYPE uint32_t
  53. #define IO_REG_BASE_ATTR
  54. #define IO_REG_MASK_ATTR
  55. #define DIRECT_READ(base, mask) ((*((base)+2) & (mask)) ? 1 : 0)
  56. #define DIRECT_MODE_INPUT(base, mask) (*((base)+1) &= ~(mask))
  57. #define DIRECT_MODE_OUTPUT(base, mask) (*((base)+1) |= (mask))
  58. #define DIRECT_WRITE_LOW(base, mask) (*((base)+34) = (mask))
  59. #define DIRECT_WRITE_HIGH(base, mask) (*((base)+33) = (mask))
  60. #elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__)
  61. // Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
  62. // http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
  63. // If you have trouble with OneWire on Arduino Due, please check the
  64. // status of delayMicroseconds() before reporting a bug in OneWire!
  65. #define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
  66. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  67. #define IO_REG_TYPE uint32_t
  68. #define IO_REG_BASE_ATTR
  69. #define IO_REG_MASK_ATTR
  70. #define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
  71. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
  72. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
  73. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
  74. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
  75. #ifndef PROGMEM
  76. #define PROGMEM
  77. #endif
  78. #ifndef pgm_read_byte
  79. #define pgm_read_byte(addr) (*(const uint8_t *)(addr))
  80. #endif
  81. #elif defined(__PIC32MX__)
  82. #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
  83. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  84. #define IO_REG_TYPE uint32_t
  85. #define IO_REG_BASE_ATTR
  86. #define IO_REG_MASK_ATTR
  87. #define DIRECT_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) //PORTX + 0x10
  88. #define DIRECT_MODE_INPUT(base, mask) ((*(base+2)) = (mask)) //TRISXSET + 0x08
  89. #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) = (mask)) //TRISXCLR + 0x04
  90. #define DIRECT_WRITE_LOW(base, mask) ((*(base+8+1)) = (mask)) //LATXCLR + 0x24
  91. #define DIRECT_WRITE_HIGH(base, mask) ((*(base+8+2)) = (mask)) //LATXSET + 0x28
  92. #elif defined(ARDUINO_ARCH_ESP8266)
  93. // Special note: I depend on the ESP community to maintain these definitions and
  94. // submit good pull requests. I can not answer any ESP questions or help you
  95. // resolve any problems related to ESP chips. Please do not contact me and please
  96. // DO NOT CREATE GITHUB ISSUES for ESP support. All ESP questions must be asked
  97. // on ESP community forums.
  98. #define PIN_TO_BASEREG(pin) ((volatile uint32_t*) GPO)
  99. #define PIN_TO_BITMASK(pin) (1 << pin)
  100. #define IO_REG_TYPE uint32_t
  101. #define IO_REG_BASE_ATTR
  102. #define IO_REG_MASK_ATTR
  103. #define DIRECT_READ(base, mask) ((GPI & (mask)) ? 1 : 0) //GPIO_IN_ADDRESS
  104. #define DIRECT_MODE_INPUT(base, mask) (GPE &= ~(mask)) //GPIO_ENABLE_W1TC_ADDRESS
  105. #define DIRECT_MODE_OUTPUT(base, mask) (GPE |= (mask)) //GPIO_ENABLE_W1TS_ADDRESS
  106. #define DIRECT_WRITE_LOW(base, mask) (GPOC = (mask)) //GPIO_OUT_W1TC_ADDRESS
  107. #define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
  108. #elif defined(ARDUINO_ARCH_ESP32)
  109. #include <driver/rtc_io.h>
  110. #define PIN_TO_BASEREG(pin) (0)
  111. #define PIN_TO_BITMASK(pin) (pin)
  112. #define IO_REG_TYPE uint32_t
  113. #define IO_REG_BASE_ATTR
  114. #define IO_REG_MASK_ATTR
  115. static inline __attribute__((always_inline))
  116. IO_REG_TYPE directRead(IO_REG_TYPE pin)
  117. {
  118. #if CONFIG_IDF_TARGET_ESP32C3
  119. return (GPIO.in.val >> pin) & 0x1;
  120. #else // plain ESP32
  121. if ( pin < 32 )
  122. return (GPIO.in >> pin) & 0x1;
  123. else if ( pin < 46 )
  124. return (GPIO.in1.val >> (pin - 32)) & 0x1;
  125. #endif
  126. return 0;
  127. }
  128. static inline __attribute__((always_inline))
  129. void directWriteLow(IO_REG_TYPE pin)
  130. {
  131. #if CONFIG_IDF_TARGET_ESP32C3
  132. GPIO.out_w1tc.val = ((uint32_t)1 << pin);
  133. #else // plain ESP32
  134. if ( pin < 32 )
  135. GPIO.out_w1tc = ((uint32_t)1 << pin);
  136. else if ( pin < 46 )
  137. GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32));
  138. #endif
  139. }
  140. static inline __attribute__((always_inline))
  141. void directWriteHigh(IO_REG_TYPE pin)
  142. {
  143. #if CONFIG_IDF_TARGET_ESP32C3
  144. GPIO.out_w1ts.val = ((uint32_t)1 << pin);
  145. #else // plain ESP32
  146. if ( pin < 32 )
  147. GPIO.out_w1ts = ((uint32_t)1 << pin);
  148. else if ( pin < 46 )
  149. GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32));
  150. #endif
  151. }
  152. static inline __attribute__((always_inline))
  153. void directModeInput(IO_REG_TYPE pin)
  154. {
  155. #if CONFIG_IDF_TARGET_ESP32C3
  156. GPIO.enable_w1tc.val = ((uint32_t)1 << (pin));
  157. #else
  158. if ( digitalPinIsValid(pin) )
  159. {
  160. #if ESP_IDF_VERSION_MAJOR < 4 // IDF 3.x ESP32/PICO-D4
  161. uint32_t rtc_reg(rtc_gpio_desc[pin].reg);
  162. if ( rtc_reg ) // RTC pins PULL settings
  163. {
  164. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
  165. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
  166. }
  167. #endif
  168. // Input
  169. if ( pin < 32 )
  170. GPIO.enable_w1tc = ((uint32_t)1 << pin);
  171. else
  172. GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32));
  173. }
  174. #endif
  175. }
  176. static inline __attribute__((always_inline))
  177. void directModeOutput(IO_REG_TYPE pin)
  178. {
  179. #if CONFIG_IDF_TARGET_ESP32C3
  180. GPIO.enable_w1ts.val = ((uint32_t)1 << (pin));
  181. #else
  182. if ( digitalPinIsValid(pin) && pin <= 33 ) // pins above 33 can be only inputs
  183. {
  184. #if ESP_IDF_VERSION_MAJOR < 4 // IDF 3.x ESP32/PICO-D4
  185. uint32_t rtc_reg(rtc_gpio_desc[pin].reg);
  186. if ( rtc_reg ) // RTC pins PULL settings
  187. {
  188. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
  189. ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
  190. }
  191. #endif
  192. // Output
  193. if ( pin < 32 )
  194. GPIO.enable_w1ts = ((uint32_t)1 << pin);
  195. else // already validated to pins <= 33
  196. GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32));
  197. }
  198. #endif
  199. }
  200. #define DIRECT_READ(base, pin) directRead(pin)
  201. #define DIRECT_WRITE_LOW(base, pin) directWriteLow(pin)
  202. #define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(pin)
  203. #define DIRECT_MODE_INPUT(base, pin) directModeInput(pin)
  204. #define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(pin)
  205. // https://github.com/PaulStoffregen/OneWire/pull/47
  206. // https://github.com/stickbreaker/OneWire/commit/6eb7fc1c11a15b6ac8c60e5671cf36eb6829f82c
  207. #ifdef interrupts
  208. #undef interrupts
  209. #endif
  210. #ifdef noInterrupts
  211. #undef noInterrupts
  212. #endif
  213. #define noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux)
  214. #define interrupts() portEXIT_CRITICAL(&mux);}
  215. //#warning "ESP32 OneWire testing"
  216. #elif defined(ARDUINO_ARCH_STM32)
  217. #define PIN_TO_BASEREG(pin) (0)
  218. #define PIN_TO_BITMASK(pin) ((uint32_t)digitalPinToPinName(pin))
  219. #define IO_REG_TYPE uint32_t
  220. #define IO_REG_BASE_ATTR
  221. #define IO_REG_MASK_ATTR
  222. #define DIRECT_READ(base, pin) digitalReadFast((PinName)pin)
  223. #define DIRECT_WRITE_LOW(base, pin) digitalWriteFast((PinName)pin, LOW)
  224. #define DIRECT_WRITE_HIGH(base, pin) digitalWriteFast((PinName)pin, HIGH)
  225. #define DIRECT_MODE_INPUT(base, pin) pin_function((PinName)pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0))
  226. #define DIRECT_MODE_OUTPUT(base, pin) pin_function((PinName)pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0))
  227. #elif defined(__SAMD21G18A__)
  228. #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin))
  229. #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
  230. #define IO_REG_TYPE uint32_t
  231. #define IO_REG_BASE_ATTR
  232. #define IO_REG_MASK_ATTR
  233. #define DIRECT_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0)
  234. #define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) = (mask))
  235. #define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+2)) = (mask))
  236. #define DIRECT_WRITE_LOW(base, mask) ((*((base)+5)) = (mask))
  237. #define DIRECT_WRITE_HIGH(base, mask) ((*((base)+6)) = (mask))
  238. #elif defined(__ASR6501__)
  239. #define PIN_IN_PORT(pin) (pin % PIN_NUMBER_IN_PORT)
  240. #define PORT_FROM_PIN(pin) (pin / PIN_NUMBER_IN_PORT)
  241. #define PORT_OFFSET(port) (PORT_REG_SHFIT * port)
  242. #define PORT_ADDRESS(pin) (CYDEV_GPIO_BASE + PORT_OFFSET(PORT_FROM_PIN(pin)))
  243. #define PIN_TO_BASEREG(pin) (0)
  244. #define PIN_TO_BITMASK(pin) (pin)
  245. #define IO_REG_TYPE uint32_t
  246. #define IO_REG_BASE_ATTR
  247. #define IO_REG_MASK_ATTR
  248. #define DIRECT_READ(base, pin) CY_SYS_PINS_READ_PIN(PORT_ADDRESS(pin)+4, PIN_IN_PORT(pin))
  249. #define DIRECT_WRITE_LOW(base, pin) CY_SYS_PINS_CLEAR_PIN(PORT_ADDRESS(pin), PIN_IN_PORT(pin))
  250. #define DIRECT_WRITE_HIGH(base, pin) CY_SYS_PINS_SET_PIN(PORT_ADDRESS(pin), PIN_IN_PORT(pin))
  251. #define DIRECT_MODE_INPUT(base, pin) CY_SYS_PINS_SET_DRIVE_MODE(PORT_ADDRESS(pin)+8, PIN_IN_PORT(pin), CY_SYS_PINS_DM_DIG_HIZ)
  252. #define DIRECT_MODE_OUTPUT(base, pin) CY_SYS_PINS_SET_DRIVE_MODE(PORT_ADDRESS(pin)+8, PIN_IN_PORT(pin), CY_SYS_PINS_DM_STRONG)
  253. #elif defined(RBL_NRF51822)
  254. #define PIN_TO_BASEREG(pin) (0)
  255. #define PIN_TO_BITMASK(pin) (pin)
  256. #define IO_REG_TYPE uint32_t
  257. #define IO_REG_BASE_ATTR
  258. #define IO_REG_MASK_ATTR
  259. #define DIRECT_READ(base, pin) nrf_gpio_pin_read(pin)
  260. #define DIRECT_WRITE_LOW(base, pin) nrf_gpio_pin_clear(pin)
  261. #define DIRECT_WRITE_HIGH(base, pin) nrf_gpio_pin_set(pin)
  262. #define DIRECT_MODE_INPUT(base, pin) nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL)
  263. #define DIRECT_MODE_OUTPUT(base, pin) nrf_gpio_cfg_output(pin)
  264. #elif defined(__arc__) /* Arduino101/Genuino101 specifics */
  265. #include "scss_registers.h"
  266. #include "portable.h"
  267. #include "avr/pgmspace.h"
  268. #define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId)
  269. #define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType)
  270. #define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase)
  271. #define DIR_OFFSET_SS 0x01
  272. #define DIR_OFFSET_SOC 0x04
  273. #define EXT_PORT_OFFSET_SS 0x0A
  274. #define EXT_PORT_OFFSET_SOC 0x50
  275. /* GPIO registers base address */
  276. #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase)
  277. #define PIN_TO_BITMASK(pin) pin
  278. #define IO_REG_TYPE uint32_t
  279. #define IO_REG_BASE_ATTR
  280. #define IO_REG_MASK_ATTR
  281. static inline __attribute__((always_inline))
  282. IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  283. {
  284. IO_REG_TYPE ret;
  285. if (SS_GPIO == GPIO_TYPE(pin)) {
  286. ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS));
  287. } else {
  288. ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC);
  289. }
  290. return ((ret >> GPIO_ID(pin)) & 0x01);
  291. }
  292. static inline __attribute__((always_inline))
  293. void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  294. {
  295. if (SS_GPIO == GPIO_TYPE(pin)) {
  296. WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)),
  297. ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
  298. } else {
  299. MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin));
  300. }
  301. }
  302. static inline __attribute__((always_inline))
  303. void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  304. {
  305. if (SS_GPIO == GPIO_TYPE(pin)) {
  306. WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)),
  307. ((IO_REG_TYPE)(base) + DIR_OFFSET_SS));
  308. } else {
  309. MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin));
  310. }
  311. }
  312. static inline __attribute__((always_inline))
  313. void directWriteLow(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  314. {
  315. if (SS_GPIO == GPIO_TYPE(pin)) {
  316. WRITE_ARC_REG(READ_ARC_REG(base) & ~(0x01 << GPIO_ID(pin)), base);
  317. } else {
  318. MMIO_REG_VAL(base) &= ~(0x01 << GPIO_ID(pin));
  319. }
  320. }
  321. static inline __attribute__((always_inline))
  322. void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin)
  323. {
  324. if (SS_GPIO == GPIO_TYPE(pin)) {
  325. WRITE_ARC_REG(READ_ARC_REG(base) | (0x01 << GPIO_ID(pin)), base);
  326. } else {
  327. MMIO_REG_VAL(base) |= (0x01 << GPIO_ID(pin));
  328. }
  329. }
  330. #define DIRECT_READ(base, pin) directRead(base, pin)
  331. #define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin)
  332. #define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin)
  333. #define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin)
  334. #define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin)
  335. #elif defined(__riscv)
  336. /*
  337. * Tested on highfive1
  338. *
  339. * Stable results are achieved operating in the
  340. * two high speed modes of the highfive1. It
  341. * seems to be less reliable in slow mode.
  342. */
  343. #define PIN_TO_BASEREG(pin) (0)
  344. #define PIN_TO_BITMASK(pin) digitalPinToBitMask(pin)
  345. #define IO_REG_TYPE uint32_t
  346. #define IO_REG_BASE_ATTR
  347. #define IO_REG_MASK_ATTR
  348. static inline __attribute__((always_inline))
  349. IO_REG_TYPE directRead(IO_REG_TYPE mask)
  350. {
  351. return ((GPIO_REG(GPIO_INPUT_VAL) & mask) != 0) ? 1 : 0;
  352. }
  353. static inline __attribute__((always_inline))
  354. void directModeInput(IO_REG_TYPE mask)
  355. {
  356. GPIO_REG(GPIO_OUTPUT_XOR) &= ~mask;
  357. GPIO_REG(GPIO_IOF_EN) &= ~mask;
  358. GPIO_REG(GPIO_INPUT_EN) |= mask;
  359. GPIO_REG(GPIO_OUTPUT_EN) &= ~mask;
  360. }
  361. static inline __attribute__((always_inline))
  362. void directModeOutput(IO_REG_TYPE mask)
  363. {
  364. GPIO_REG(GPIO_OUTPUT_XOR) &= ~mask;
  365. GPIO_REG(GPIO_IOF_EN) &= ~mask;
  366. GPIO_REG(GPIO_INPUT_EN) &= ~mask;
  367. GPIO_REG(GPIO_OUTPUT_EN) |= mask;
  368. }
  369. static inline __attribute__((always_inline))
  370. void directWriteLow(IO_REG_TYPE mask)
  371. {
  372. GPIO_REG(GPIO_OUTPUT_VAL) &= ~mask;
  373. }
  374. static inline __attribute__((always_inline))
  375. void directWriteHigh(IO_REG_TYPE mask)
  376. {
  377. GPIO_REG(GPIO_OUTPUT_VAL) |= mask;
  378. }
  379. #define DIRECT_READ(base, mask) directRead(mask)
  380. #define DIRECT_WRITE_LOW(base, mask) directWriteLow(mask)
  381. #define DIRECT_WRITE_HIGH(base, mask) directWriteHigh(mask)
  382. #define DIRECT_MODE_INPUT(base, mask) directModeInput(mask)
  383. #define DIRECT_MODE_OUTPUT(base, mask) directModeOutput(mask)
  384. #else
  385. #define PIN_TO_BASEREG(pin) (0)
  386. #define PIN_TO_BITMASK(pin) (pin)
  387. #define IO_REG_TYPE unsigned int
  388. #define IO_REG_BASE_ATTR
  389. #define IO_REG_MASK_ATTR
  390. #define DIRECT_READ(base, pin) digitalRead(pin)
  391. #define DIRECT_WRITE_LOW(base, pin) digitalWrite(pin, LOW)
  392. #define DIRECT_WRITE_HIGH(base, pin) digitalWrite(pin, HIGH)
  393. #define DIRECT_MODE_INPUT(base, pin) pinMode(pin,INPUT)
  394. #define DIRECT_MODE_OUTPUT(base, pin) pinMode(pin,OUTPUT)
  395. #warning "OneWire. Fallback mode. Using API calls for pinMode,digitalRead and digitalWrite. Operation of this library is not guaranteed on this architecture."
  396. #endif
  397. #endif