Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. #include <Arduino.h>
  31. #ifdef __arm__
  32. #ifdef CORE_TEENSY
  33. //------------------------------------------------------------------------------
  34. /** read pin value
  35. * @param[in] pin Arduino pin number
  36. * @return value read
  37. */
  38. static inline __attribute__((always_inline))
  39. bool fastDigitalRead(uint8_t pin) {
  40. return *portInputRegister(pin);
  41. }
  42. //------------------------------------------------------------------------------
  43. /** Set pin value
  44. * @param[in] pin Arduino pin number
  45. * @param[in] level value to write
  46. */
  47. static inline __attribute__((always_inline))
  48. void fastDigitalWrite(uint8_t pin, bool value) {
  49. if (value) {
  50. *portSetRegister(pin) = 1;
  51. } else {
  52. *portClearRegister(pin) = 1;
  53. }
  54. }
  55. #else // CORE_TEENSY
  56. //------------------------------------------------------------------------------
  57. /** read pin value
  58. * @param[in] pin Arduino pin number
  59. * @return value read
  60. */
  61. static inline __attribute__((always_inline))
  62. bool fastDigitalRead(uint8_t pin){
  63. return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin;
  64. }
  65. //------------------------------------------------------------------------------
  66. /** Set pin value
  67. * @param[in] pin Arduino pin number
  68. * @param[in] level value to write
  69. */
  70. static inline __attribute__((always_inline))
  71. void fastDigitalWrite(uint8_t pin, bool value){
  72. if(value) {
  73. g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin;
  74. } else {
  75. g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin;
  76. }
  77. }
  78. #endif // CORE_TEENSY
  79. //------------------------------------------------------------------------------
  80. inline void fastDigitalToggle(uint8_t pin) {
  81. fastDigitalWrite(pin, !fastDigitalRead(pin));
  82. }
  83. //------------------------------------------------------------------------------
  84. inline void fastPinMode(uint8_t pin, bool mode) {pinMode(pin, mode);}
  85. #else // __arm__
  86. #include <avr/io.h>
  87. #include <util/atomic.h>
  88. //------------------------------------------------------------------------------
  89. /**
  90. * @class pin_map_t
  91. * @brief struct for mapping digital pins
  92. */
  93. struct pin_map_t {
  94. volatile uint8_t* ddr; /**< address of DDR for this pin */
  95. volatile uint8_t* pin; /**< address of PIN for this pin */
  96. volatile uint8_t* port; /**< address of PORT for this pin */
  97. uint8_t bit; /**< bit number for this pin */
  98. };
  99. //------------------------------------------------------------------------------
  100. #if defined(__AVR_ATmega168__)\
  101. ||defined(__AVR_ATmega168P__)\
  102. ||defined(__AVR_ATmega328P__)
  103. // 168 and 328 Arduinos
  104. const static pin_map_t pinMap[] = {
  105. {&DDRD, &PIND, &PORTD, 0}, // D0 0
  106. {&DDRD, &PIND, &PORTD, 1}, // D1 1
  107. {&DDRD, &PIND, &PORTD, 2}, // D2 2
  108. {&DDRD, &PIND, &PORTD, 3}, // D3 3
  109. {&DDRD, &PIND, &PORTD, 4}, // D4 4
  110. {&DDRD, &PIND, &PORTD, 5}, // D5 5
  111. {&DDRD, &PIND, &PORTD, 6}, // D6 6
  112. {&DDRD, &PIND, &PORTD, 7}, // D7 7
  113. {&DDRB, &PINB, &PORTB, 0}, // B0 8
  114. {&DDRB, &PINB, &PORTB, 1}, // B1 9
  115. {&DDRB, &PINB, &PORTB, 2}, // B2 10
  116. {&DDRB, &PINB, &PORTB, 3}, // B3 11
  117. {&DDRB, &PINB, &PORTB, 4}, // B4 12
  118. {&DDRB, &PINB, &PORTB, 5}, // B5 13
  119. {&DDRC, &PINC, &PORTC, 0}, // C0 14
  120. {&DDRC, &PINC, &PORTC, 1}, // C1 15
  121. {&DDRC, &PINC, &PORTC, 2}, // C2 16
  122. {&DDRC, &PINC, &PORTC, 3}, // C3 17
  123. {&DDRC, &PINC, &PORTC, 4}, // C4 18
  124. {&DDRC, &PINC, &PORTC, 5} // C5 19
  125. };
  126. //------------------------------------------------------------------------------
  127. #elif defined(__AVR_ATmega1280__)\
  128. || defined(__AVR_ATmega2560__)
  129. // Mega
  130. static const pin_map_t pinMap[] = {
  131. {&DDRE, &PINE, &PORTE, 0}, // E0 0
  132. {&DDRE, &PINE, &PORTE, 1}, // E1 1
  133. {&DDRE, &PINE, &PORTE, 4}, // E4 2
  134. {&DDRE, &PINE, &PORTE, 5}, // E5 3
  135. {&DDRG, &PING, &PORTG, 5}, // G5 4
  136. {&DDRE, &PINE, &PORTE, 3}, // E3 5
  137. {&DDRH, &PINH, &PORTH, 3}, // H3 6
  138. {&DDRH, &PINH, &PORTH, 4}, // H4 7
  139. {&DDRH, &PINH, &PORTH, 5}, // H5 8
  140. {&DDRH, &PINH, &PORTH, 6}, // H6 9
  141. {&DDRB, &PINB, &PORTB, 4}, // B4 10
  142. {&DDRB, &PINB, &PORTB, 5}, // B5 11
  143. {&DDRB, &PINB, &PORTB, 6}, // B6 12
  144. {&DDRB, &PINB, &PORTB, 7}, // B7 13
  145. {&DDRJ, &PINJ, &PORTJ, 1}, // J1 14
  146. {&DDRJ, &PINJ, &PORTJ, 0}, // J0 15
  147. {&DDRH, &PINH, &PORTH, 1}, // H1 16
  148. {&DDRH, &PINH, &PORTH, 0}, // H0 17
  149. {&DDRD, &PIND, &PORTD, 3}, // D3 18
  150. {&DDRD, &PIND, &PORTD, 2}, // D2 19
  151. {&DDRD, &PIND, &PORTD, 1}, // D1 20
  152. {&DDRD, &PIND, &PORTD, 0}, // D0 21
  153. {&DDRA, &PINA, &PORTA, 0}, // A0 22
  154. {&DDRA, &PINA, &PORTA, 1}, // A1 23
  155. {&DDRA, &PINA, &PORTA, 2}, // A2 24
  156. {&DDRA, &PINA, &PORTA, 3}, // A3 25
  157. {&DDRA, &PINA, &PORTA, 4}, // A4 26
  158. {&DDRA, &PINA, &PORTA, 5}, // A5 27
  159. {&DDRA, &PINA, &PORTA, 6}, // A6 28
  160. {&DDRA, &PINA, &PORTA, 7}, // A7 29
  161. {&DDRC, &PINC, &PORTC, 7}, // C7 30
  162. {&DDRC, &PINC, &PORTC, 6}, // C6 31
  163. {&DDRC, &PINC, &PORTC, 5}, // C5 32
  164. {&DDRC, &PINC, &PORTC, 4}, // C4 33
  165. {&DDRC, &PINC, &PORTC, 3}, // C3 34
  166. {&DDRC, &PINC, &PORTC, 2}, // C2 35
  167. {&DDRC, &PINC, &PORTC, 1}, // C1 36
  168. {&DDRC, &PINC, &PORTC, 0}, // C0 37
  169. {&DDRD, &PIND, &PORTD, 7}, // D7 38
  170. {&DDRG, &PING, &PORTG, 2}, // G2 39
  171. {&DDRG, &PING, &PORTG, 1}, // G1 40
  172. {&DDRG, &PING, &PORTG, 0}, // G0 41
  173. {&DDRL, &PINL, &PORTL, 7}, // L7 42
  174. {&DDRL, &PINL, &PORTL, 6}, // L6 43
  175. {&DDRL, &PINL, &PORTL, 5}, // L5 44
  176. {&DDRL, &PINL, &PORTL, 4}, // L4 45
  177. {&DDRL, &PINL, &PORTL, 3}, // L3 46
  178. {&DDRL, &PINL, &PORTL, 2}, // L2 47
  179. {&DDRL, &PINL, &PORTL, 1}, // L1 48
  180. {&DDRL, &PINL, &PORTL, 0}, // L0 49
  181. {&DDRB, &PINB, &PORTB, 3}, // B3 50
  182. {&DDRB, &PINB, &PORTB, 2}, // B2 51
  183. {&DDRB, &PINB, &PORTB, 1}, // B1 52
  184. {&DDRB, &PINB, &PORTB, 0}, // B0 53
  185. {&DDRF, &PINF, &PORTF, 0}, // F0 54
  186. {&DDRF, &PINF, &PORTF, 1}, // F1 55
  187. {&DDRF, &PINF, &PORTF, 2}, // F2 56
  188. {&DDRF, &PINF, &PORTF, 3}, // F3 57
  189. {&DDRF, &PINF, &PORTF, 4}, // F4 58
  190. {&DDRF, &PINF, &PORTF, 5}, // F5 59
  191. {&DDRF, &PINF, &PORTF, 6}, // F6 60
  192. {&DDRF, &PINF, &PORTF, 7}, // F7 61
  193. {&DDRK, &PINK, &PORTK, 0}, // K0 62
  194. {&DDRK, &PINK, &PORTK, 1}, // K1 63
  195. {&DDRK, &PINK, &PORTK, 2}, // K2 64
  196. {&DDRK, &PINK, &PORTK, 3}, // K3 65
  197. {&DDRK, &PINK, &PORTK, 4}, // K4 66
  198. {&DDRK, &PINK, &PORTK, 5}, // K5 67
  199. {&DDRK, &PINK, &PORTK, 6}, // K6 68
  200. {&DDRK, &PINK, &PORTK, 7} // K7 69
  201. };
  202. //------------------------------------------------------------------------------
  203. #elif defined(__AVR_ATmega1284P__)\
  204. || defined(__AVR_ATmega1284__)\
  205. || defined(__AVR_ATmega644P__)\
  206. || defined(__AVR_ATmega644__)\
  207. || defined(__AVR_ATmega64__)\
  208. || defined(__AVR_ATmega32__)\
  209. || defined(__AVR_ATmega324__)\
  210. || defined(__AVR_ATmega16__)
  211. #ifdef defined(VARIANT_MIGHTY)
  212. // Mighty Layout
  213. static const pin_map_t pinMap[] = {
  214. {&DDRB, &PINB, &PORTB, 0}, // B0 0
  215. {&DDRB, &PINB, &PORTB, 1}, // B1 1
  216. {&DDRB, &PINB, &PORTB, 2}, // B2 2
  217. {&DDRB, &PINB, &PORTB, 3}, // B3 3
  218. {&DDRB, &PINB, &PORTB, 4}, // B4 4
  219. {&DDRB, &PINB, &PORTB, 5}, // B5 5
  220. {&DDRB, &PINB, &PORTB, 6}, // B6 6
  221. {&DDRB, &PINB, &PORTB, 7}, // B7 7
  222. {&DDRD, &PIND, &PORTD, 0}, // D0 8
  223. {&DDRD, &PIND, &PORTD, 1}, // D1 9
  224. {&DDRD, &PIND, &PORTD, 2}, // D2 10
  225. {&DDRD, &PIND, &PORTD, 3}, // D3 11
  226. {&DDRD, &PIND, &PORTD, 4}, // D4 12
  227. {&DDRD, &PIND, &PORTD, 5}, // D5 13
  228. {&DDRD, &PIND, &PORTD, 6}, // D6 14
  229. {&DDRD, &PIND, &PORTD, 7}, // D7 15
  230. {&DDRC, &PINC, &PORTC, 0}, // C0 16
  231. {&DDRC, &PINC, &PORTC, 1}, // C1 17
  232. {&DDRC, &PINC, &PORTC, 2}, // C2 18
  233. {&DDRC, &PINC, &PORTC, 3}, // C3 19
  234. {&DDRC, &PINC, &PORTC, 4}, // C4 20
  235. {&DDRC, &PINC, &PORTC, 5}, // C5 21
  236. {&DDRC, &PINC, &PORTC, 6}, // C6 22
  237. {&DDRC, &PINC, &PORTC, 7}, // C7 23
  238. {&DDRA, &PINA, &PORTA, 0}, // A0 24
  239. {&DDRA, &PINA, &PORTA, 1}, // A1 25
  240. {&DDRA, &PINA, &PORTA, 2}, // A2 26
  241. {&DDRA, &PINA, &PORTA, 3}, // A3 27
  242. {&DDRA, &PINA, &PORTA, 4}, // A4 28
  243. {&DDRA, &PINA, &PORTA, 5}, // A5 29
  244. {&DDRA, &PINA, &PORTA, 6}, // A6 30
  245. {&DDRA, &PINA, &PORTA, 7} // A7 31
  246. };
  247. #elif defined(VARIANT_BOBUINO)
  248. // Bobuino Layout
  249. static const pin_map_t pinMap[] = {
  250. {&DDRD, &PIND, &PORTD, 0}, // D0 0
  251. {&DDRD, &PIND, &PORTD, 1}, // D1 1
  252. {&DDRD, &PIND, &PORTD, 2}, // D2 2
  253. {&DDRD, &PIND, &PORTD, 3}, // D3 3
  254. {&DDRB, &PINB, &PORTB, 0}, // B0 4
  255. {&DDRB, &PINB, &PORTB, 1}, // B1 5
  256. {&DDRB, &PINB, &PORTB, 2}, // B2 6
  257. {&DDRB, &PINB, &PORTB, 3}, // B3 7
  258. {&DDRD, &PIND, &PORTD, 5}, // D5 8
  259. {&DDRD, &PIND, &PORTD, 6}, // D6 9
  260. {&DDRB, &PINB, &PORTB, 4}, // B4 10
  261. {&DDRB, &PINB, &PORTB, 5}, // B5 11
  262. {&DDRB, &PINB, &PORTB, 6}, // B6 12
  263. {&DDRB, &PINB, &PORTB, 7}, // B7 13
  264. {&DDRA, &PINA, &PORTA, 7}, // A7 14
  265. {&DDRA, &PINA, &PORTA, 6}, // A6 15
  266. {&DDRA, &PINA, &PORTA, 5}, // A5 16
  267. {&DDRA, &PINA, &PORTA, 4}, // A4 17
  268. {&DDRA, &PINA, &PORTA, 3}, // A3 18
  269. {&DDRA, &PINA, &PORTA, 2}, // A2 19
  270. {&DDRA, &PINA, &PORTA, 1}, // A1 20
  271. {&DDRA, &PINA, &PORTA, 0}, // A0 21
  272. {&DDRC, &PINC, &PORTC, 0}, // C0 22
  273. {&DDRC, &PINC, &PORTC, 1}, // C1 23
  274. {&DDRC, &PINC, &PORTC, 2}, // C2 24
  275. {&DDRC, &PINC, &PORTC, 3}, // C3 25
  276. {&DDRC, &PINC, &PORTC, 4}, // C4 26
  277. {&DDRC, &PINC, &PORTC, 5}, // C5 27
  278. {&DDRC, &PINC, &PORTC, 6}, // C6 28
  279. {&DDRC, &PINC, &PORTC, 7}, // C7 29
  280. {&DDRD, &PIND, &PORTD, 4}, // D4 30
  281. {&DDRD, &PIND, &PORTD, 7} // D7 31
  282. };
  283. #elif defined(VARIANT_STANDARD)
  284. // Standard Layout
  285. static const pin_map_t pinMap[] = {
  286. {&DDRB, &PINB, &PORTB, 0}, // B0 0
  287. {&DDRB, &PINB, &PORTB, 1}, // B1 1
  288. {&DDRB, &PINB, &PORTB, 2}, // B2 2
  289. {&DDRB, &PINB, &PORTB, 3}, // B3 3
  290. {&DDRB, &PINB, &PORTB, 4}, // B4 4
  291. {&DDRB, &PINB, &PORTB, 5}, // B5 5
  292. {&DDRB, &PINB, &PORTB, 6}, // B6 6
  293. {&DDRB, &PINB, &PORTB, 7}, // B7 7
  294. {&DDRD, &PIND, &PORTD, 0}, // D0 8
  295. {&DDRD, &PIND, &PORTD, 1}, // D1 9
  296. {&DDRD, &PIND, &PORTD, 2}, // D2 10
  297. {&DDRD, &PIND, &PORTD, 3}, // D3 11
  298. {&DDRD, &PIND, &PORTD, 4}, // D4 12
  299. {&DDRD, &PIND, &PORTD, 5}, // D5 13
  300. {&DDRD, &PIND, &PORTD, 6}, // D6 14
  301. {&DDRD, &PIND, &PORTD, 7}, // D7 15
  302. {&DDRC, &PINC, &PORTC, 0}, // C0 16
  303. {&DDRC, &PINC, &PORTC, 1}, // C1 17
  304. {&DDRC, &PINC, &PORTC, 2}, // C2 18
  305. {&DDRC, &PINC, &PORTC, 3}, // C3 19
  306. {&DDRC, &PINC, &PORTC, 4}, // C4 20
  307. {&DDRC, &PINC, &PORTC, 5}, // C5 21
  308. {&DDRC, &PINC, &PORTC, 6}, // C6 22
  309. {&DDRC, &PINC, &PORTC, 7}, // C7 23
  310. {&DDRA, &PINA, &PORTA, 7}, // A7 24
  311. {&DDRA, &PINA, &PORTA, 6}, // A6 25
  312. {&DDRA, &PINA, &PORTA, 5}, // A5 26
  313. {&DDRA, &PINA, &PORTA, 4}, // A4 27
  314. {&DDRA, &PINA, &PORTA, 3}, // A3 28
  315. {&DDRA, &PINA, &PORTA, 2}, // A2 29
  316. {&DDRA, &PINA, &PORTA, 1}, // A1 30
  317. {&DDRA, &PINA, &PORTA, 0} // A0 31
  318. };
  319. #else // VARIANT_MIGHTY
  320. #error Undefined variant 1284, 644, 324, 64, 32
  321. #endif // VARIANT_MIGHTY
  322. //------------------------------------------------------------------------------
  323. #elif defined(__AVR_ATmega32U4__)
  324. #ifdef CORE_TEENSY
  325. // Teensy 2.0
  326. static const pin_map_t pinMap[] = {
  327. {&DDRB, &PINB, &PORTB, 0}, // B0 0
  328. {&DDRB, &PINB, &PORTB, 1}, // B1 1
  329. {&DDRB, &PINB, &PORTB, 2}, // B2 2
  330. {&DDRB, &PINB, &PORTB, 3}, // B3 3
  331. {&DDRB, &PINB, &PORTB, 7}, // B7 4
  332. {&DDRD, &PIND, &PORTD, 0}, // D0 5
  333. {&DDRD, &PIND, &PORTD, 1}, // D1 6
  334. {&DDRD, &PIND, &PORTD, 2}, // D2 7
  335. {&DDRD, &PIND, &PORTD, 3}, // D3 8
  336. {&DDRC, &PINC, &PORTC, 6}, // C6 9
  337. {&DDRC, &PINC, &PORTC, 7}, // C7 10
  338. {&DDRD, &PIND, &PORTD, 6}, // D6 11
  339. {&DDRD, &PIND, &PORTD, 7}, // D7 12
  340. {&DDRB, &PINB, &PORTB, 4}, // B4 13
  341. {&DDRB, &PINB, &PORTB, 5}, // B5 14
  342. {&DDRB, &PINB, &PORTB, 6}, // B6 15
  343. {&DDRF, &PINF, &PORTF, 7}, // F7 16
  344. {&DDRF, &PINF, &PORTF, 6}, // F6 17
  345. {&DDRF, &PINF, &PORTF, 5}, // F5 18
  346. {&DDRF, &PINF, &PORTF, 4}, // F4 19
  347. {&DDRF, &PINF, &PORTF, 1}, // F1 20
  348. {&DDRF, &PINF, &PORTF, 0}, // F0 21
  349. {&DDRD, &PIND, &PORTD, 4}, // D4 22
  350. {&DDRD, &PIND, &PORTD, 5}, // D5 23
  351. {&DDRE, &PINE, &PORTE, 6} // E6 24
  352. };
  353. //------------------------------------------------------------------------------
  354. #else // CORE_TEENSY
  355. // Leonardo
  356. static const pin_map_t pinMap[] = {
  357. {&DDRD, &PIND, &PORTD, 2}, // D2 0
  358. {&DDRD, &PIND, &PORTD, 3}, // D3 1
  359. {&DDRD, &PIND, &PORTD, 1}, // D1 2
  360. {&DDRD, &PIND, &PORTD, 0}, // D0 3
  361. {&DDRD, &PIND, &PORTD, 4}, // D4 4
  362. {&DDRC, &PINC, &PORTC, 6}, // C6 5
  363. {&DDRD, &PIND, &PORTD, 7}, // D7 6
  364. {&DDRE, &PINE, &PORTE, 6}, // E6 7
  365. {&DDRB, &PINB, &PORTB, 4}, // B4 8
  366. {&DDRB, &PINB, &PORTB, 5}, // B5 9
  367. {&DDRB, &PINB, &PORTB, 6}, // B6 10
  368. {&DDRB, &PINB, &PORTB, 7}, // B7 11
  369. {&DDRD, &PIND, &PORTD, 6}, // D6 12
  370. {&DDRC, &PINC, &PORTC, 7}, // C7 13
  371. {&DDRB, &PINB, &PORTB, 3}, // B3 14
  372. {&DDRB, &PINB, &PORTB, 1}, // B1 15
  373. {&DDRB, &PINB, &PORTB, 2}, // B2 16
  374. {&DDRB, &PINB, &PORTB, 0}, // B0 17
  375. {&DDRF, &PINF, &PORTF, 7}, // F7 18
  376. {&DDRF, &PINF, &PORTF, 6}, // F6 19
  377. {&DDRF, &PINF, &PORTF, 5}, // F5 20
  378. {&DDRF, &PINF, &PORTF, 4}, // F4 21
  379. {&DDRF, &PINF, &PORTF, 1}, // F1 22
  380. {&DDRF, &PINF, &PORTF, 0}, // F0 23
  381. {&DDRD, &PIND, &PORTD, 4}, // D4 24
  382. {&DDRD, &PIND, &PORTD, 7}, // D7 25
  383. {&DDRB, &PINB, &PORTB, 4}, // B4 26
  384. {&DDRB, &PINB, &PORTB, 5}, // B5 27
  385. {&DDRB, &PINB, &PORTB, 6}, // B6 28
  386. {&DDRD, &PIND, &PORTD, 6} // D6 29
  387. };
  388. #endif // CORE_TEENSY
  389. //------------------------------------------------------------------------------
  390. #elif defined(__AVR_AT90USB646__)\
  391. || defined(__AVR_AT90USB1286__)
  392. // Teensy++ 1.0 & 2.0
  393. static const pin_map_t pinMap[] = {
  394. {&DDRD, &PIND, &PORTD, 0}, // D0 0
  395. {&DDRD, &PIND, &PORTD, 1}, // D1 1
  396. {&DDRD, &PIND, &PORTD, 2}, // D2 2
  397. {&DDRD, &PIND, &PORTD, 3}, // D3 3
  398. {&DDRD, &PIND, &PORTD, 4}, // D4 4
  399. {&DDRD, &PIND, &PORTD, 5}, // D5 5
  400. {&DDRD, &PIND, &PORTD, 6}, // D6 6
  401. {&DDRD, &PIND, &PORTD, 7}, // D7 7
  402. {&DDRE, &PINE, &PORTE, 0}, // E0 8
  403. {&DDRE, &PINE, &PORTE, 1}, // E1 9
  404. {&DDRC, &PINC, &PORTC, 0}, // C0 10
  405. {&DDRC, &PINC, &PORTC, 1}, // C1 11
  406. {&DDRC, &PINC, &PORTC, 2}, // C2 12
  407. {&DDRC, &PINC, &PORTC, 3}, // C3 13
  408. {&DDRC, &PINC, &PORTC, 4}, // C4 14
  409. {&DDRC, &PINC, &PORTC, 5}, // C5 15
  410. {&DDRC, &PINC, &PORTC, 6}, // C6 16
  411. {&DDRC, &PINC, &PORTC, 7}, // C7 17
  412. {&DDRE, &PINE, &PORTE, 6}, // E6 18
  413. {&DDRE, &PINE, &PORTE, 7}, // E7 19
  414. {&DDRB, &PINB, &PORTB, 0}, // B0 20
  415. {&DDRB, &PINB, &PORTB, 1}, // B1 21
  416. {&DDRB, &PINB, &PORTB, 2}, // B2 22
  417. {&DDRB, &PINB, &PORTB, 3}, // B3 23
  418. {&DDRB, &PINB, &PORTB, 4}, // B4 24
  419. {&DDRB, &PINB, &PORTB, 5}, // B5 25
  420. {&DDRB, &PINB, &PORTB, 6}, // B6 26
  421. {&DDRB, &PINB, &PORTB, 7}, // B7 27
  422. {&DDRA, &PINA, &PORTA, 0}, // A0 28
  423. {&DDRA, &PINA, &PORTA, 1}, // A1 29
  424. {&DDRA, &PINA, &PORTA, 2}, // A2 30
  425. {&DDRA, &PINA, &PORTA, 3}, // A3 31
  426. {&DDRA, &PINA, &PORTA, 4}, // A4 32
  427. {&DDRA, &PINA, &PORTA, 5}, // A5 33
  428. {&DDRA, &PINA, &PORTA, 6}, // A6 34
  429. {&DDRA, &PINA, &PORTA, 7}, // A7 35
  430. {&DDRE, &PINE, &PORTE, 4}, // E4 36
  431. {&DDRE, &PINE, &PORTE, 5}, // E5 37
  432. {&DDRF, &PINF, &PORTF, 0}, // F0 38
  433. {&DDRF, &PINF, &PORTF, 1}, // F1 39
  434. {&DDRF, &PINF, &PORTF, 2}, // F2 40
  435. {&DDRF, &PINF, &PORTF, 3}, // F3 41
  436. {&DDRF, &PINF, &PORTF, 4}, // F4 42
  437. {&DDRF, &PINF, &PORTF, 5}, // F5 43
  438. {&DDRF, &PINF, &PORTF, 6}, // F6 44
  439. {&DDRF, &PINF, &PORTF, 7} // F7 45
  440. };
  441. //------------------------------------------------------------------------------
  442. #else // CPU type
  443. #error unknown CPU type
  444. #endif // CPU type
  445. //------------------------------------------------------------------------------
  446. /** count of pins */
  447. static const uint8_t digitalPinCount = sizeof(pinMap)/sizeof(pin_map_t);
  448. //==============================================================================
  449. /** generate bad pin number error */
  450. void badPinNumber(void)
  451. __attribute__((error("Pin number is too large or not a constant")));
  452. //------------------------------------------------------------------------------
  453. /** Check for valid pin number
  454. * @param[in] pin Number of pin to be checked.
  455. */
  456. static inline __attribute__((always_inline))
  457. void badPinCheck(uint8_t pin) {
  458. if (!__builtin_constant_p(pin) || pin >= digitalPinCount) {
  459. badPinNumber();
  460. }
  461. }
  462. //------------------------------------------------------------------------------
  463. /** fast write helper
  464. * @param[in] address I/O register address
  465. * @param[in] bit bit number to write
  466. * @param[in] level value for bit
  467. */
  468. static inline __attribute__((always_inline))
  469. void fastBitWriteSafe(volatile uint8_t* address, uint8_t bit, bool level) {
  470. uint8_t oldSREG;
  471. if (address > (uint8_t*)0X5F) {
  472. oldSREG = SREG;
  473. cli();
  474. }
  475. if (level) {
  476. *address |= 1 << bit;
  477. } else {
  478. *address &= ~(1 << bit);
  479. }
  480. if (address > (uint8_t*)0X5F) {
  481. SREG = oldSREG;
  482. }
  483. }
  484. //------------------------------------------------------------------------------
  485. /** read pin value
  486. * @param[in] pin Arduino pin number
  487. * @return value read
  488. */
  489. static inline __attribute__((always_inline))
  490. bool fastDigitalRead(uint8_t pin) {
  491. badPinCheck(pin);
  492. return (*pinMap[pin].pin >> pinMap[pin].bit) & 1;
  493. }
  494. //------------------------------------------------------------------------------
  495. /** toggle a pin
  496. * @param[in] pin Arduino pin number
  497. *
  498. * If the pin is in output mode toggle the pin level.
  499. * If the pin is in input mode toggle the state of the 20K pullup.
  500. */
  501. static inline __attribute__((always_inline))
  502. void fastDigitalToggle(uint8_t pin) {
  503. badPinCheck(pin);
  504. if (pinMap[pin].pin > (uint8_t*)0X5F) {
  505. // must write bit to high address port
  506. *pinMap[pin].pin = 1 << pinMap[pin].bit;
  507. } else {
  508. // will compile to sbi and PIN register will not be read.
  509. *pinMap[pin].pin |= 1 << pinMap[pin].bit;
  510. }
  511. }
  512. //------------------------------------------------------------------------------
  513. /** Set pin value
  514. * @param[in] pin Arduino pin number
  515. * @param[in] level value to write
  516. */
  517. static inline __attribute__((always_inline))
  518. void fastDigitalWrite(uint8_t pin, bool level) {
  519. badPinCheck(pin);
  520. fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level);
  521. }
  522. //------------------------------------------------------------------------------
  523. /** set pin mode
  524. * @param[in] pin Arduino pin number
  525. * @param[in] mode if true set output mode else input mode
  526. *
  527. * fastPinMode does not enable or disable the 20K pullup for input mode.
  528. */
  529. static inline __attribute__((always_inline))
  530. void fastPinMode(uint8_t pin, bool mode) {
  531. badPinCheck(pin);
  532. fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode);
  533. }
  534. #endif // __arm__
  535. //------------------------------------------------------------------------------
  536. /** set pin configuration
  537. * @param[in] pin Arduino pin number
  538. * @param[in] mode If true set output mode else input mode
  539. * @param[in] level If mode is output, set level high/low.
  540. * If mode is input, enable or disable the pin's 20K pullup.
  541. */
  542. static inline __attribute__((always_inline))
  543. void fastPinConfig(uint8_t pin, bool mode, bool level) {
  544. fastPinMode(pin, mode);
  545. fastDigitalWrite(pin, level);
  546. }
  547. //==============================================================================
  548. /**
  549. * @class DigitalPin
  550. * @brief Fast digital port I/O
  551. */
  552. template<uint8_t PinNumber>
  553. class DigitalPin {
  554. public:
  555. //----------------------------------------------------------------------------
  556. /** Constructor */
  557. DigitalPin() {}
  558. //----------------------------------------------------------------------------
  559. /** Constructor
  560. * @param[in] pinMode if true set output mode else input mode.
  561. */
  562. explicit DigitalPin(bool pinMode) {
  563. mode(pinMode);
  564. }
  565. //----------------------------------------------------------------------------
  566. /** Constructor
  567. * @param[in] mode If true set output mode else input mode
  568. * @param[in] level If mode is output, set level high/low.
  569. * If mode is input, enable or disable the pin's 20K pullup.
  570. */
  571. DigitalPin(bool mode, bool level) {
  572. config(mode, level);
  573. }
  574. //----------------------------------------------------------------------------
  575. /** Asignment operator
  576. * @param[in] value If true set the pin's level high else set the
  577. * pin's level low.
  578. *
  579. * @return This DigitalPin instance.
  580. */
  581. inline DigitalPin & operator = (bool value) __attribute__((always_inline)) {
  582. write(value);
  583. return *this;
  584. }
  585. //----------------------------------------------------------------------------
  586. /** Parenthesis operator
  587. * @return Pin's level
  588. */
  589. inline operator bool () const __attribute__((always_inline)) {
  590. return read();
  591. }
  592. //----------------------------------------------------------------------------
  593. /** set pin configuration
  594. * @param[in] mode If true set output mode else input mode
  595. * @param[in] level If mode is output, set level high/low.
  596. * If mode is input, enable or disable the pin's 20K pullup.
  597. */
  598. inline __attribute__((always_inline))
  599. void config(bool mode, bool level) {
  600. fastPinConfig(PinNumber, mode, level);
  601. }
  602. //----------------------------------------------------------------------------
  603. /**
  604. * Set pin level high if output mode or enable 20K pullup if input mode.
  605. */
  606. inline __attribute__((always_inline))
  607. void high() {write(true);}
  608. //----------------------------------------------------------------------------
  609. /**
  610. * Set pin level low if output mode or disable 20K pullup if input mode.
  611. */
  612. inline __attribute__((always_inline))
  613. void low() {write(false);}
  614. //----------------------------------------------------------------------------
  615. /**
  616. * Set pin mode
  617. * @param[in] pinMode if true set output mode else input mode.
  618. *
  619. * mode() does not enable or disable the 20K pullup for input mode.
  620. */
  621. inline __attribute__((always_inline))
  622. void mode(bool pinMode) {
  623. fastPinMode(PinNumber, pinMode);
  624. }
  625. //----------------------------------------------------------------------------
  626. /** @return Pin's level */
  627. inline __attribute__((always_inline))
  628. bool read() const {
  629. return fastDigitalRead(PinNumber);
  630. }
  631. //----------------------------------------------------------------------------
  632. /** toggle a pin
  633. *
  634. * If the pin is in output mode toggle the pin's level.
  635. * If the pin is in input mode toggle the state of the 20K pullup.
  636. */
  637. inline __attribute__((always_inline))
  638. void toggle() {
  639. fastDigitalToggle(PinNumber);
  640. }
  641. //----------------------------------------------------------------------------
  642. /** Write the pin's level.
  643. * @param[in] value If true set the pin's level high else set the
  644. * pin's level low.
  645. */
  646. inline __attribute__((always_inline))
  647. void write(bool value) {
  648. fastDigitalWrite(PinNumber, value);
  649. }
  650. };
  651. #endif // DigitalPin_h
  652. /** @} */