My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

fastio_Due.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * Fast I/O Routines for SAM3X8E
  25. * Use direct port manipulation to save scads of processor time.
  26. * Contributed by Triffid_Hunter and modified by Kliment, thinkyhead, Bob-the-Kuhn, et.al.
  27. */
  28. /**
  29. * Description: Fast IO functions for Arduino Due and compatible (SAM3X8E)
  30. *
  31. * For ARDUINO_ARCH_SAM
  32. * Note the code here was specifically crafted by disassembling what GCC produces
  33. * out of it, so GCC is able to optimize it out as much as possible to the least
  34. * amount of instructions. Be very carefull if you modify them, as "clean code"
  35. * leads to less efficient compiled code!!
  36. */
  37. #include <pins_arduino.h>
  38. /**
  39. * Utility functions
  40. */
  41. // Due has 12 PWMs assigned to logical pins 2-13.
  42. // 6, 7, 8 & 9 come from the PWM controller. The others come from the timers.
  43. #define PWM_PIN(P) WITHIN(P, 2, 13)
  44. #ifndef MASK
  45. #define MASK(PIN) (1 << PIN)
  46. #endif
  47. /**
  48. * Magic I/O routines
  49. *
  50. * Now you can simply SET_OUTPUT(STEP); WRITE(STEP, HIGH); WRITE(STEP, LOW);
  51. *
  52. * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
  53. */
  54. // Read a pin
  55. #define _READ(IO) bool(DIO ## IO ## _WPORT -> PIO_PDSR & MASK(DIO ## IO ## _PIN))
  56. // Write to a pin
  57. #define _WRITE_VAR(IO,V) do { \
  58. volatile Pio* port = digitalPinToPort(IO); \
  59. const uint32_t mask = digitalPinToBitMask(IO); \
  60. if (V) port->PIO_SODR = mask; \
  61. else port->PIO_CODR = mask; \
  62. } while(0)
  63. // Write to a pin
  64. #define _WRITE(IO,V) do { \
  65. volatile Pio* port = (DIO ## IO ## _WPORT); \
  66. const uint32_t mask = MASK(DIO ## IO ## _PIN); \
  67. if (V) port->PIO_SODR = mask; \
  68. else port->PIO_CODR = mask; \
  69. } while(0)
  70. // Toggle a pin
  71. #define _TOGGLE(IO) _WRITE(IO, !READ(IO))
  72. #if MB(PRINTRBOARD_G2)
  73. #include "G2_pins.h"
  74. // Set pin as input
  75. #define _SET_INPUT(IO) do{ \
  76. pmc_enable_periph_clk(G2_g_APinDescription[IO].ulPeripheralId); \
  77. PIO_Configure((DIO ## IO ## _WPORT), PIO_INPUT, MASK(DIO ## IO ## _PIN), 0); \
  78. }while(0)
  79. // Set pin as output
  80. #define _SET_OUTPUT(IO) do{ \
  81. uint32_t mask = MASK(G2_g_APinDescription[IO].ulPeripheralId); \
  82. if ((PMC->PMC_PCSR0 & mask) != (mask)) PMC->PMC_PCER0 = mask; \
  83. volatile Pio* port = (DIO ## IO ## _WPORT); \
  84. mask = MASK(DIO ## IO ## _PIN); \
  85. if (_READ(IO)) port->PIO_SODR = mask; \
  86. else port->PIO_CODR = mask; \
  87. port->PIO_IDR = mask; \
  88. const uint32_t pin_config = G2_g_APinDescription[IO].ulPinConfiguration; \
  89. if (pin_config & PIO_PULLUP) port->PIO_PUER = mask; \
  90. else port->PIO_PUDR = mask; \
  91. if (pin_config & PIO_OPENDRAIN) port->PIO_MDER = mask; \
  92. else port->PIO_MDDR = mask; \
  93. port->PIO_PER = mask; \
  94. port->PIO_OER = mask; \
  95. g_pinStatus[IO] = (g_pinStatus[IO] & 0xF0) | PIN_STATUS_DIGITAL_OUTPUT; \
  96. }while(0)
  97. /**
  98. * Set pin as output with comments
  99. * #define _SET_OUTPUT(IO) do{ \
  100. * uint32_t mask = MASK(G2_g_APinDescription[IO].ulPeripheralId); \
  101. * if ((PMC->PMC_PCSR0 & mask ) != (mask)) PMC->PMC_PCER0 = mask; \ // enable PIO clock if not already enabled
  102. *
  103. * volatile Pio* port = (DIO ## IO ## _WPORT); \
  104. * const uint32_t mask = MASK(DIO ## IO ## _PIN); \
  105. * if (_READ(IO)) port->PIO_SODR = mask; \ // set output to match input BEFORE setting direction or will glitch the output
  106. * else port->PIO_CODR = mask; \
  107. *
  108. * port->PIO_IDR = mask; \ // disable interrupt
  109. *
  110. * uint32_t pin_config = G2_g_APinDescription[IO].ulPinConfiguration; \
  111. * if (pin_config & PIO_PULLUP) pPio->PIO_PUER = mask; \ // enable pullup if necessary
  112. * else pPio->PIO_PUDR = mask; \
  113. *
  114. * if (pin_config & PIO_OPENDRAIN) port->PIO_MDER = mask; \ // Enable multi-drive if necessary
  115. * else port->PIO_MDDR = mask; \
  116. *
  117. * port->PIO_PER = mask; \
  118. * port->PIO_OER = mask; \ // set to output
  119. *
  120. * g_pinStatus[IO] = (g_pinStatus[IO] & 0xF0) | PIN_STATUS_DIGITAL_OUTPUT; \
  121. * }while(0)
  122. */
  123. #else
  124. // Set pin as input
  125. #define _SET_INPUT(IO) do{ \
  126. pmc_enable_periph_clk(g_APinDescription[IO].ulPeripheralId); \
  127. PIO_Configure(digitalPinToPort(IO), PIO_INPUT, digitalPinToBitMask(IO), 0); \
  128. }while(0)
  129. // Set pin as output
  130. #define _SET_OUTPUT(IO) do{ \
  131. pmc_enable_periph_clk(g_APinDescription[IO].ulPeripheralId); \
  132. PIO_Configure(digitalPinToPort(IO), _READ(IO) ? PIO_OUTPUT_1 : PIO_OUTPUT_0, digitalPinToBitMask(IO), g_APinDescription[IO].ulPinConfiguration); \
  133. g_pinStatus[IO] = (g_pinStatus[IO] & 0xF0) | PIN_STATUS_DIGITAL_OUTPUT; \
  134. }while(0)
  135. #endif
  136. // Set pin as input with pullup mode
  137. #define _PULLUP(IO,V) pinMode(IO, (V) ? INPUT_PULLUP : INPUT)
  138. // Read a pin (wrapper)
  139. #define READ(IO) _READ(IO)
  140. // Write to a pin (wrapper)
  141. #define WRITE_VAR(IO,V) _WRITE_VAR(IO,V)
  142. #define WRITE(IO,V) _WRITE(IO,V)
  143. // Toggle a pin (wrapper)
  144. #define TOGGLE(IO) _TOGGLE(IO)
  145. // Set pin as input (wrapper)
  146. #define SET_INPUT(IO) _SET_INPUT(IO)
  147. // Set pin as input with pullup (wrapper)
  148. #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0)
  149. // Set pin as output (wrapper) - reads the pin and sets the output to that value
  150. #define SET_OUTPUT(IO) _SET_OUTPUT(IO)
  151. // Set pin as PWM
  152. #define SET_PWM(IO) SET_OUTPUT(IO)
  153. // Check if pin is an input
  154. #define IS_INPUT(IO) ((digitalPinToPort(IO)->PIO_OSR & digitalPinToBitMask(IO)) == 0)
  155. // Check if pin is an output
  156. #define IS_OUTPUT(IO) ((digitalPinToPort(IO)->PIO_OSR & digitalPinToBitMask(IO)) != 0)
  157. // Shorthand
  158. #define OUT_WRITE(IO,V) { SET_OUTPUT(IO); WRITE(IO,V); }
  159. // digitalRead/Write wrappers
  160. #define extDigitalRead(IO) digitalRead(IO)
  161. #define extDigitalWrite(IO,V) digitalWrite(IO,V)
  162. /**
  163. * Ports and functions
  164. * Added as necessary or if I feel like it- not a comprehensive list!
  165. */
  166. // UART
  167. #define RXD DIO0
  168. #define TXD DIO1
  169. // TWI (I2C)
  170. #define SCL DIO21
  171. #define SDA DIO20
  172. /**
  173. * pins
  174. */
  175. #define DIO0_PIN 8
  176. #define DIO0_WPORT PIOA
  177. #define DIO1_PIN 9
  178. #define DIO1_WPORT PIOA
  179. #define DIO2_PIN 25
  180. #define DIO2_WPORT PIOB
  181. #define DIO3_PIN 28
  182. #define DIO3_WPORT PIOC
  183. #define DIO4_PIN 26
  184. #define DIO4_WPORT PIOC
  185. #define DIO5_PIN 25
  186. #define DIO5_WPORT PIOC
  187. #define DIO6_PIN 24
  188. #define DIO6_WPORT PIOC
  189. #define DIO7_PIN 23
  190. #define DIO7_WPORT PIOC
  191. #define DIO8_PIN 22
  192. #define DIO8_WPORT PIOC
  193. #define DIO9_PIN 21
  194. #define DIO9_WPORT PIOC
  195. #define DIO10_PIN 29
  196. #define DIO10_WPORT PIOC
  197. #define DIO11_PIN 7
  198. #define DIO11_WPORT PIOD
  199. #define DIO12_PIN 8
  200. #define DIO12_WPORT PIOD
  201. #define DIO13_PIN 27
  202. #define DIO13_WPORT PIOB
  203. #define DIO14_PIN 4
  204. #define DIO14_WPORT PIOD
  205. #define DIO15_PIN 5
  206. #define DIO15_WPORT PIOD
  207. #define DIO16_PIN 13
  208. #define DIO16_WPORT PIOA
  209. #define DIO17_PIN 12
  210. #define DIO17_WPORT PIOA
  211. #define DIO18_PIN 11
  212. #define DIO18_WPORT PIOA
  213. #define DIO19_PIN 10
  214. #define DIO19_WPORT PIOA
  215. #define DIO20_PIN 12
  216. #define DIO20_WPORT PIOB
  217. #define DIO21_PIN 13
  218. #define DIO21_WPORT PIOB
  219. #define DIO22_PIN 26
  220. #define DIO22_WPORT PIOB
  221. #define DIO23_PIN 14
  222. #define DIO23_WPORT PIOA
  223. #define DIO24_PIN 15
  224. #define DIO24_WPORT PIOA
  225. #define DIO25_PIN 0
  226. #define DIO25_WPORT PIOD
  227. #define DIO26_PIN 1
  228. #define DIO26_WPORT PIOD
  229. #define DIO27_PIN 2
  230. #define DIO27_WPORT PIOD
  231. #define DIO28_PIN 3
  232. #define DIO28_WPORT PIOD
  233. #define DIO29_PIN 6
  234. #define DIO29_WPORT PIOD
  235. #define DIO30_PIN 9
  236. #define DIO30_WPORT PIOD
  237. #define DIO31_PIN 7
  238. #define DIO31_WPORT PIOA
  239. #define DIO32_PIN 10
  240. #define DIO32_WPORT PIOD
  241. #define DIO33_PIN 1
  242. #define DIO33_WPORT PIOC
  243. #if !MB(PRINTRBOARD_G2) // normal DUE pin mapping
  244. #define DIO34_PIN 2
  245. #define DIO34_WPORT PIOC
  246. #define DIO35_PIN 3
  247. #define DIO35_WPORT PIOC
  248. #define DIO36_PIN 4
  249. #define DIO36_WPORT PIOC
  250. #define DIO37_PIN 5
  251. #define DIO37_WPORT PIOC
  252. #define DIO38_PIN 6
  253. #define DIO38_WPORT PIOC
  254. #define DIO39_PIN 7
  255. #define DIO39_WPORT PIOC
  256. #define DIO40_PIN 8
  257. #define DIO40_WPORT PIOC
  258. #define DIO41_PIN 9
  259. #define DIO41_WPORT PIOC
  260. #endif // !PRINTRBOARD_G2
  261. #define DIO42_PIN 19
  262. #define DIO42_WPORT PIOA
  263. #define DIO43_PIN 20
  264. #define DIO43_WPORT PIOA
  265. #define DIO44_PIN 19
  266. #define DIO44_WPORT PIOC
  267. #define DIO45_PIN 18
  268. #define DIO45_WPORT PIOC
  269. #define DIO46_PIN 17
  270. #define DIO46_WPORT PIOC
  271. #define DIO47_PIN 16
  272. #define DIO47_WPORT PIOC
  273. #define DIO48_PIN 15
  274. #define DIO48_WPORT PIOC
  275. #define DIO49_PIN 14
  276. #define DIO49_WPORT PIOC
  277. #define DIO50_PIN 13
  278. #define DIO50_WPORT PIOC
  279. #define DIO51_PIN 12
  280. #define DIO51_WPORT PIOC
  281. #define DIO52_PIN 21
  282. #define DIO52_WPORT PIOB
  283. #define DIO53_PIN 14
  284. #define DIO53_WPORT PIOB
  285. #define DIO54_PIN 16
  286. #define DIO54_WPORT PIOA
  287. #define DIO55_PIN 24
  288. #define DIO55_WPORT PIOA
  289. #define DIO56_PIN 23
  290. #define DIO56_WPORT PIOA
  291. #define DIO57_PIN 22
  292. #define DIO57_WPORT PIOA
  293. #define DIO58_PIN 6
  294. #define DIO58_WPORT PIOA
  295. #define DIO59_PIN 4
  296. #define DIO59_WPORT PIOA
  297. #define DIO60_PIN 3
  298. #define DIO60_WPORT PIOA
  299. #define DIO61_PIN 2
  300. #define DIO61_WPORT PIOA
  301. #define DIO62_PIN 17
  302. #define DIO62_WPORT PIOB
  303. #define DIO63_PIN 18
  304. #define DIO63_WPORT PIOB
  305. #define DIO64_PIN 19
  306. #define DIO64_WPORT PIOB
  307. #define DIO65_PIN 20
  308. #define DIO65_WPORT PIOB
  309. #define DIO66_PIN 15
  310. #define DIO66_WPORT PIOB
  311. #define DIO67_PIN 16
  312. #define DIO67_WPORT PIOB
  313. #define DIO68_PIN 1
  314. #define DIO68_WPORT PIOA
  315. #define DIO69_PIN 0
  316. #define DIO69_WPORT PIOA
  317. #define DIO70_PIN 17
  318. #define DIO70_WPORT PIOA
  319. #define DIO71_PIN 18
  320. #define DIO71_WPORT PIOA
  321. #define DIO72_PIN 30
  322. #define DIO72_WPORT PIOC
  323. #define DIO73_PIN 21
  324. #define DIO73_WPORT PIOA
  325. #define DIO74_PIN 25
  326. #define DIO74_WPORT PIOA
  327. #define DIO75_PIN 26
  328. #define DIO75_WPORT PIOA
  329. #define DIO76_PIN 27
  330. #define DIO76_WPORT PIOA
  331. #define DIO77_PIN 28
  332. #define DIO77_WPORT PIOA
  333. #define DIO78_PIN 23
  334. #define DIO78_WPORT PIOB
  335. #define DIO79_PIN 17
  336. #define DIO79_WPORT PIOA
  337. #define DIO80_PIN 12
  338. #define DIO80_WPORT PIOB
  339. #define DIO81_PIN 8
  340. #define DIO81_WPORT PIOA
  341. #define DIO82_PIN 11
  342. #define DIO82_WPORT PIOA
  343. #define DIO83_PIN 13
  344. #define DIO83_WPORT PIOA
  345. #define DIO84_PIN 4
  346. #define DIO84_WPORT PIOD
  347. #define DIO85_PIN 11
  348. #define DIO85_WPORT PIOB
  349. #define DIO86_PIN 21
  350. #define DIO86_WPORT PIOB
  351. #define DIO87_PIN 29
  352. #define DIO87_WPORT PIOA
  353. #define DIO88_PIN 15
  354. #define DIO88_WPORT PIOB
  355. #define DIO89_PIN 14
  356. #define DIO89_WPORT PIOB
  357. #define DIO90_PIN 1
  358. #define DIO90_WPORT PIOA
  359. #define DIO91_PIN 15
  360. #define DIO91_WPORT PIOB
  361. #if ARDUINO_SAM_ARCHIM
  362. #define DIO92_PIN 11
  363. #define DIO92_WPORT PIOC
  364. #define DIO93_PIN 2
  365. #define DIO93_WPORT PIOB
  366. #define DIO94_PIN 1
  367. #define DIO94_WPORT PIOB
  368. #define DIO95_PIN 0
  369. #define DIO95_WPORT PIOB
  370. #define DIO96_PIN 10
  371. #define DIO96_WPORT PIOC
  372. #define DIO97_PIN 24
  373. #define DIO97_WPORT PIOB
  374. #define DIO98_PIN 7
  375. #define DIO98_WPORT PIOB
  376. #define DIO99_PIN 6
  377. #define DIO99_WPORT PIOB
  378. #define DIO100_PIN 8
  379. #define DIO100_WPORT PIOB
  380. #define DIO101_PIN 5
  381. #define DIO101_WPORT PIOB
  382. #define DIO102_PIN 4
  383. #define DIO102_WPORT PIOB
  384. #define DIO103_PIN 3
  385. #define DIO103_WPORT PIOB
  386. #define DIO104_PIN 20
  387. #define DIO104_WPORT PIOC
  388. #define DIO105_PIN 22
  389. #define DIO105_WPORT PIOB
  390. #define DIO106_PIN 27
  391. #define DIO106_WPORT PIOC
  392. #define DIO107_PIN 10
  393. #define DIO107_WPORT PIOB
  394. #define DIO108_PIN 9
  395. #define DIO108_WPORT PIOB
  396. #else // !ARDUINO_SAM_ARCHIM
  397. #define DIO92_PIN 5
  398. #define DIO92_WPORT PIOA
  399. #define DIO93_PIN 12
  400. #define DIO93_WPORT PIOB
  401. #define DIO94_PIN 22
  402. #define DIO94_WPORT PIOB
  403. #define DIO95_PIN 23
  404. #define DIO95_WPORT PIOB
  405. #define DIO96_PIN 24
  406. #define DIO96_WPORT PIOB
  407. #define DIO97_PIN 20
  408. #define DIO97_WPORT PIOC
  409. #define DIO98_PIN 27
  410. #define DIO98_WPORT PIOC
  411. #define DIO99_PIN 10
  412. #define DIO99_WPORT PIOC
  413. #define DIO100_PIN 11
  414. #define DIO100_WPORT PIOC
  415. #endif // !ARDUINO_SAM_ARCHIM