DigitalPin.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Arduino DigitalIO Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the Arduino DigitalIO Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino DigitalIO Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * @file
  22. * @brief Fast Digital Pin functions
  23. *
  24. * @defgroup digitalPin Fast Pin I/O
  25. * @details Fast Digital I/O functions and template class.
  26. * @{
  27. */
  28. #ifndef DigitalPin_h
  29. #define DigitalPin_h
  30. #if defined(__AVR__) || defined(DOXYGEN)
  31. #include <avr/io.h>
  32. /** GpioPinMap type */
  33. struct GpioPinMap_t {
  34. volatile uint8_t* pin; /**< address of PIN for this pin */
  35. volatile uint8_t* ddr; /**< address of DDR for this pin */
  36. volatile uint8_t* port; /**< address of PORT for this pin */
  37. uint8_t mask; /**< bit mask for this pin */
  38. };
  39. /** Initializer macro. */
  40. #define GPIO_PIN(reg, bit) {&PIN##reg, &DDR##reg, &PORT##reg, 1 << bit}
  41. // Include pin map for current board.
  42. #include "boards/GpioPinMap.h"
  43. //------------------------------------------------------------------------------
  44. /** generate bad pin number error */
  45. void badPinNumber(void)
  46. __attribute__((error("Pin number is too large or not a constant")));
  47. //------------------------------------------------------------------------------
  48. /** Check for valid pin number
  49. * @param[in] pin Number of pin to be checked.
  50. */
  51. static inline __attribute__((always_inline))
  52. void badPinCheck(uint8_t pin) {
  53. if (!__builtin_constant_p(pin) || pin >= NUM_DIGITAL_PINS) {
  54. badPinNumber();
  55. }
  56. }
  57. //------------------------------------------------------------------------------
  58. /** DDR register address
  59. * @param[in] pin Arduino pin number
  60. * @return register address
  61. */
  62. static inline __attribute__((always_inline))
  63. volatile uint8_t* ddrReg(uint8_t pin) {
  64. badPinCheck(pin);
  65. return GpioPinMap[pin].ddr;
  66. }
  67. //------------------------------------------------------------------------------
  68. /** Bit mask for pin
  69. * @param[in] pin Arduino pin number
  70. * @return mask
  71. */
  72. static inline __attribute__((always_inline))
  73. uint8_t pinMask(uint8_t pin) {
  74. badPinCheck(pin);
  75. return GpioPinMap[pin].mask;
  76. }
  77. //------------------------------------------------------------------------------
  78. /** PIN register address
  79. * @param[in] pin Arduino pin number
  80. * @return register address
  81. */
  82. static inline __attribute__((always_inline))
  83. volatile uint8_t* pinReg(uint8_t pin) {
  84. badPinCheck(pin);
  85. return GpioPinMap[pin].pin;
  86. }
  87. //------------------------------------------------------------------------------
  88. /** PORT register address
  89. * @param[in] pin Arduino pin number
  90. * @return register address
  91. */
  92. static inline __attribute__((always_inline))
  93. volatile uint8_t* portReg(uint8_t pin) {
  94. badPinCheck(pin);
  95. return GpioPinMap[pin].port;
  96. }
  97. //------------------------------------------------------------------------------
  98. /** Fast write helper.
  99. * @param[in] address I/O register address
  100. * @param[in] mask bit mask for pin
  101. * @param[in] level value for bit
  102. */
  103. static inline __attribute__((always_inline))
  104. void fastBitWriteSafe(volatile uint8_t* address, uint8_t mask, bool level) {
  105. uint8_t s;
  106. if (address > reinterpret_cast<uint8_t*>(0X3F)) {
  107. s = SREG;
  108. cli();
  109. }
  110. if (level) {
  111. *address |= mask;
  112. } else {
  113. *address &= ~mask;
  114. }
  115. if (address > reinterpret_cast<uint8_t*>(0X3F)) {
  116. SREG = s;
  117. }
  118. }
  119. //------------------------------------------------------------------------------
  120. /** Read pin value.
  121. * @param[in] pin Arduino pin number
  122. * @return value read
  123. */
  124. static inline __attribute__((always_inline))
  125. bool fastDigitalRead(uint8_t pin) {
  126. return *pinReg(pin) & pinMask(pin);
  127. }
  128. //------------------------------------------------------------------------------
  129. /** Toggle a pin.
  130. * @param[in] pin Arduino pin number
  131. *
  132. * If the pin is in output mode toggle the pin level.
  133. * If the pin is in input mode toggle the state of the 20K pullup.
  134. */
  135. static inline __attribute__((always_inline))
  136. void fastDigitalToggle(uint8_t pin) {
  137. if (pinReg(pin) > reinterpret_cast<uint8_t*>(0X3F)) {
  138. // must write bit to high address port
  139. *pinReg(pin) = pinMask(pin);
  140. } else {
  141. // will compile to sbi and PIN register will not be read.
  142. *pinReg(pin) |= pinMask(pin);
  143. }
  144. }
  145. //------------------------------------------------------------------------------
  146. /** Set pin value.
  147. * @param[in] pin Arduino pin number
  148. * @param[in] level value to write
  149. */
  150. static inline __attribute__((always_inline))
  151. void fastDigitalWrite(uint8_t pin, bool level) {
  152. fastBitWriteSafe(portReg(pin), pinMask(pin), level);
  153. }
  154. //------------------------------------------------------------------------------
  155. /** Write the DDR register.
  156. * @param[in] pin Arduino pin number
  157. * @param[in] level value to write
  158. */
  159. static inline __attribute__((always_inline))
  160. void fastDdrWrite(uint8_t pin, bool level) {
  161. fastBitWriteSafe(ddrReg(pin), pinMask(pin), level);
  162. }
  163. //------------------------------------------------------------------------------
  164. /** Set pin mode.
  165. * @param[in] pin Arduino pin number
  166. * @param[in] mode INPUT, OUTPUT, or INPUT_PULLUP.
  167. *
  168. * The internal pullup resistors will be enabled if mode is INPUT_PULLUP
  169. * and disabled if the mode is INPUT.
  170. */
  171. static inline __attribute__((always_inline))
  172. void fastPinMode(uint8_t pin, uint8_t mode) {
  173. fastDdrWrite(pin, mode == OUTPUT);
  174. if (mode != OUTPUT) {
  175. fastDigitalWrite(pin, mode == INPUT_PULLUP);
  176. }
  177. }
  178. #else // defined(__AVR__)
  179. #if defined(CORE_TEENSY)
  180. //------------------------------------------------------------------------------
  181. /** read pin value
  182. * @param[in] pin Arduino pin number
  183. * @return value read
  184. */
  185. static inline __attribute__((always_inline))
  186. bool fastDigitalRead(uint8_t pin) {
  187. return *portInputRegister(pin);
  188. }
  189. //------------------------------------------------------------------------------
  190. /** Set pin value
  191. * @param[in] pin Arduino pin number
  192. * @param[in] level value to write
  193. */
  194. static inline __attribute__((always_inline))
  195. void fastDigitalWrite(uint8_t pin, bool value) {
  196. if (value) {
  197. *portSetRegister(pin) = 1;
  198. } else {
  199. *portClearRegister(pin) = 1;
  200. }
  201. }
  202. #elif defined(__SAM3X8E__) || defined(__SAM3X8H__)
  203. //------------------------------------------------------------------------------
  204. /** read pin value
  205. * @param[in] pin Arduino pin number
  206. * @return value read
  207. */
  208. static inline __attribute__((always_inline))
  209. bool fastDigitalRead(uint8_t pin) {
  210. return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin;
  211. }
  212. //------------------------------------------------------------------------------
  213. /** Set pin value
  214. * @param[in] pin Arduino pin number
  215. * @param[in] level value to write
  216. */
  217. static inline __attribute__((always_inline))
  218. void fastDigitalWrite(uint8_t pin, bool value) {
  219. if (value) {
  220. g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin;
  221. } else {
  222. g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin;
  223. }
  224. }
  225. #elif defined(ESP8266)
  226. //------------------------------------------------------------------------------
  227. /** Set pin value
  228. * @param[in] pin Arduino pin number
  229. * @param[in] val value to write
  230. */
  231. static inline __attribute__((always_inline))
  232. void fastDigitalWrite(uint8_t pin, uint8_t val) {
  233. if (pin < 16) {
  234. if (val) {
  235. GPOS = (1 << pin);
  236. } else {
  237. GPOC = (1 << pin);
  238. }
  239. } else if (pin == 16) {
  240. if (val) {
  241. GP16O |= 1;
  242. } else {
  243. GP16O &= ~1;
  244. }
  245. }
  246. }
  247. //------------------------------------------------------------------------------
  248. /** Read pin value
  249. * @param[in] pin Arduino pin number
  250. * @return value read
  251. */
  252. static inline __attribute__((always_inline))
  253. bool fastDigitalRead(uint8_t pin) {
  254. if (pin < 16) {
  255. return GPIP(pin);
  256. } else if (pin == 16) {
  257. return GP16I & 0x01;
  258. }
  259. return 0;
  260. }
  261. #else // CORE_TEENSY
  262. //------------------------------------------------------------------------------
  263. inline void fastDigitalWrite(uint8_t pin, bool value) {
  264. digitalWrite(pin, value);
  265. }
  266. //------------------------------------------------------------------------------
  267. inline bool fastDigitalRead(uint8_t pin) {
  268. return digitalRead(pin);
  269. }
  270. #endif // CORE_TEENSY
  271. //------------------------------------------------------------------------------
  272. inline void fastDigitalToggle(uint8_t pin) {
  273. fastDigitalWrite(pin, !fastDigitalRead(pin));
  274. }
  275. //------------------------------------------------------------------------------
  276. inline void fastPinMode(uint8_t pin, uint8_t mode) {
  277. pinMode(pin, mode);
  278. }
  279. #endif // __AVR__
  280. //------------------------------------------------------------------------------
  281. /** set pin configuration
  282. * @param[in] pin Arduino pin number
  283. * @param[in] mode mode INPUT or OUTPUT.
  284. * @param[in] level If mode is output, set level high/low.
  285. * If mode is input, enable or disable the pin's 20K pullup.
  286. */
  287. #define fastPinConfig(pin, mode, level)\
  288. {fastPinMode(pin, mode); fastDigitalWrite(pin, level);}
  289. //==============================================================================
  290. /**
  291. * @class DigitalPin
  292. * @brief Fast digital port I/O
  293. */
  294. template<uint8_t PinNumber>
  295. class DigitalPin {
  296. public:
  297. //----------------------------------------------------------------------------
  298. /** Constructor */
  299. DigitalPin() {}
  300. //----------------------------------------------------------------------------
  301. /** Asignment operator.
  302. * @param[in] value If true set the pin's level high else set the
  303. * pin's level low.
  304. *
  305. * @return This DigitalPin instance.
  306. */
  307. inline DigitalPin & operator = (bool value) __attribute__((always_inline)) {
  308. write(value);
  309. return *this;
  310. }
  311. //----------------------------------------------------------------------------
  312. /** Parenthesis operator.
  313. * @return Pin's level
  314. */
  315. inline operator bool () const __attribute__((always_inline)) {
  316. return read();
  317. }
  318. //----------------------------------------------------------------------------
  319. /** Set pin configuration.
  320. * @param[in] mode: INPUT or OUTPUT.
  321. * @param[in] level If mode is OUTPUT, set level high/low.
  322. * If mode is INPUT, enable or disable the pin's 20K pullup.
  323. */
  324. inline __attribute__((always_inline))
  325. void config(uint8_t mode, bool level) {
  326. fastPinConfig(PinNumber, mode, level);
  327. }
  328. //----------------------------------------------------------------------------
  329. /**
  330. * Set pin level high if output mode or enable 20K pullup if input mode.
  331. */
  332. inline __attribute__((always_inline))
  333. void high() {write(true);}
  334. //----------------------------------------------------------------------------
  335. /**
  336. * Set pin level low if output mode or disable 20K pullup if input mode.
  337. */
  338. inline __attribute__((always_inline))
  339. void low() {write(false);}
  340. //----------------------------------------------------------------------------
  341. /**
  342. * Set pin mode.
  343. * @param[in] mode: INPUT, OUTPUT, or INPUT_PULLUP.
  344. *
  345. * The internal pullup resistors will be enabled if mode is INPUT_PULLUP
  346. * and disabled if the mode is INPUT.
  347. */
  348. inline __attribute__((always_inline))
  349. void mode(uint8_t mode) {
  350. fastPinMode(PinNumber, mode);
  351. }
  352. //----------------------------------------------------------------------------
  353. /** @return Pin's level. */
  354. inline __attribute__((always_inline))
  355. bool read() const {
  356. return fastDigitalRead(PinNumber);
  357. }
  358. //----------------------------------------------------------------------------
  359. /** Toggle a pin.
  360. *
  361. * If the pin is in output mode toggle the pin's level.
  362. * If the pin is in input mode toggle the state of the 20K pullup.
  363. */
  364. inline __attribute__((always_inline))
  365. void toggle() {
  366. fastDigitalToggle(PinNumber);
  367. }
  368. //----------------------------------------------------------------------------
  369. /** Write the pin's level.
  370. * @param[in] value If true set the pin's level high else set the
  371. * pin's level low.
  372. */
  373. inline __attribute__((always_inline))
  374. void write(bool value) {
  375. fastDigitalWrite(PinNumber, value);
  376. }
  377. };
  378. #endif // DigitalPin_h
  379. /** @} */