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.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  25. */
  26. /**
  27. * Fast IO functions for SAMD51
  28. */
  29. #include "SAMD51.h"
  30. /**
  31. * Utility functions
  32. */
  33. #ifndef MASK
  34. #define MASK(PIN) _BV(PIN)
  35. #endif
  36. /**
  37. * Magic I/O routines
  38. *
  39. * Now you can simply SET_OUTPUT(IO); WRITE(IO, HIGH); WRITE(IO, LOW);
  40. */
  41. // Read a pin
  42. #define READ(IO) ((PORT->Group[(EPortType)GET_SAMD_PORT(IO)].IN.reg & MASK(GET_SAMD_PIN(IO))) != 0)
  43. // Write to a pin
  44. #define WRITE(IO,V) do{ \
  45. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  46. const uint32_t mask = MASK(GET_SAMD_PIN(IO)); \
  47. \
  48. if (V) PORT->Group[port].OUTSET.reg = mask; \
  49. else PORT->Group[port].OUTCLR.reg = mask; \
  50. }while(0)
  51. // Toggle a pin
  52. #define TOGGLE(IO) PORT->Group[(EPortType)GET_SAMD_PORT(IO)].OUTTGL.reg = MASK(GET_SAMD_PIN(IO));
  53. // Set pin as input
  54. #define SET_INPUT(IO) do{ \
  55. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  56. const uint32_t pin = GET_SAMD_PIN(IO); \
  57. \
  58. PORT->Group[port].PINCFG[pin].reg = (uint8_t)(PORT_PINCFG_INEN); \
  59. PORT->Group[port].DIRCLR.reg = MASK(pin); \
  60. }while(0)
  61. // Set pin as input with pullup
  62. #define SET_INPUT_PULLUP(IO) do{ \
  63. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  64. const uint32_t pin = GET_SAMD_PIN(IO); \
  65. const uint32_t mask = MASK(pin); \
  66. \
  67. PORT->Group[port].PINCFG[pin].reg = (uint8_t)(PORT_PINCFG_INEN | PORT_PINCFG_PULLEN); \
  68. PORT->Group[port].DIRCLR.reg = mask; \
  69. PORT->Group[port].OUTSET.reg = mask; \
  70. }while(0)
  71. // Set pin as input with pulldown
  72. #define SET_INPUT_PULLDOWN(IO) do{ \
  73. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  74. const uint32_t pin = GET_SAMD_PIN(IO); \
  75. const uint32_t mask = MASK(pin); \
  76. \
  77. PORT->Group[port].PINCFG[pin].reg = (uint8_t)(PORT_PINCFG_INEN | PORT_PINCFG_PULLEN); \
  78. PORT->Group[port].DIRCLR.reg = mask; \
  79. PORT->Group[port].OUTCLR.reg = mask; \
  80. }while(0)
  81. // Set pin as output (push pull)
  82. #define SET_OUTPUT(IO) do{ \
  83. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  84. const uint32_t pin = GET_SAMD_PIN(IO); \
  85. \
  86. PORT->Group[port].DIRSET.reg = MASK(pin); \
  87. PORT->Group[port].PINCFG[pin].reg = 0; \
  88. }while(0)
  89. // Set pin as output (open drain)
  90. #define SET_OUTPUT_OD(IO) do{ \
  91. const EPortType port = (EPortType)GET_SAMD_PORT(IO); \
  92. const uint32_t pin = GET_SAMD_PIN(IO); \
  93. \
  94. PORT->Group[port].PINCFG[pin].reg = (uint8_t)(PORT_PINCFG_PULLEN); \
  95. PORT->Group[port].DIRCLR.reg = MASK(pin); \
  96. }while(0)
  97. // Set pin as PWM (push pull)
  98. #define SET_PWM SET_OUTPUT
  99. // Set pin as PWM (open drain)
  100. #define SET_PWM_OD SET_OUTPUT_OD
  101. // check if pin is an output
  102. #define IS_OUTPUT(IO) ((PORT->Group[(EPortType)GET_SAMD_PORT(IO)].DIR.reg & MASK(GET_SAMD_PIN(IO))) \
  103. || (PORT->Group[(EPortType)GET_SAMD_PORT(IO)].PINCFG[GET_SAMD_PIN(IO)].reg & (PORT_PINCFG_INEN | PORT_PINCFG_PULLEN)) == PORT_PINCFG_PULLEN)
  104. // check if pin is an input
  105. #define IS_INPUT(IO) !IS_OUTPUT(IO)
  106. // Shorthand
  107. #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0)
  108. #define OUT_WRITE_OD(IO,V) do{ SET_OUTPUT_OD(IO); WRITE(IO,V); }while(0)
  109. // digitalRead/Write wrappers
  110. #define extDigitalRead(IO) digitalRead(IO)
  111. #define extDigitalWrite(IO,V) digitalWrite(IO,V)
  112. /**
  113. * Ports and functions
  114. * Added as necessary or if I feel like it- not a comprehensive list!
  115. */
  116. #ifdef ADAFRUIT_GRAND_CENTRAL_M4
  117. /*
  118. * Adafruit Grand Central M4 has a lot of PWMs the availables are listed here.
  119. * Some of these share the same source and so can't be used in the same time
  120. */
  121. #define PWM_PIN(P) (WITHIN(P, 2, 13) || WITHIN(P, 22, 23) || WITHIN(P, 44, 45) || P == 48)
  122. // Return fulfilled ADCx->INPUTCTRL.reg
  123. #define PIN_TO_INPUTCTRL(P) ( (PIN_TO_AIN(P) == 0) ? ADC_INPUTCTRL_MUXPOS_AIN0 \
  124. : (PIN_TO_AIN(P) == 1) ? ADC_INPUTCTRL_MUXPOS_AIN1 \
  125. : (PIN_TO_AIN(P) == 2) ? ADC_INPUTCTRL_MUXPOS_AIN2 \
  126. : (PIN_TO_AIN(P) == 3) ? ADC_INPUTCTRL_MUXPOS_AIN3 \
  127. : (PIN_TO_AIN(P) == 4) ? ADC_INPUTCTRL_MUXPOS_AIN4 \
  128. : (PIN_TO_AIN(P) == 5) ? ADC_INPUTCTRL_MUXPOS_AIN5 \
  129. : (PIN_TO_AIN(P) == 6) ? ADC_INPUTCTRL_MUXPOS_AIN6 \
  130. : (PIN_TO_AIN(P) == 7) ? ADC_INPUTCTRL_MUXPOS_AIN7 \
  131. : (PIN_TO_AIN(P) == 8) ? ADC_INPUTCTRL_MUXPOS_AIN8 \
  132. : (PIN_TO_AIN(P) == 9) ? ADC_INPUTCTRL_MUXPOS_AIN9 \
  133. : (PIN_TO_AIN(P) == 10) ? ADC_INPUTCTRL_MUXPOS_AIN10 \
  134. : (PIN_TO_AIN(P) == 11) ? ADC_INPUTCTRL_MUXPOS_AIN11 \
  135. : (PIN_TO_AIN(P) == 12) ? ADC_INPUTCTRL_MUXPOS_AIN12 \
  136. : (PIN_TO_AIN(P) == 13) ? ADC_INPUTCTRL_MUXPOS_AIN13 \
  137. : (PIN_TO_AIN(P) == 14) ? ADC_INPUTCTRL_MUXPOS_AIN14 \
  138. : ADC_INPUTCTRL_MUXPOS_AIN15)
  139. #define ANAPIN_TO_SAMDPIN(P) ( (P == 0) ? PIN_TO_SAMD_PIN(67) \
  140. : (P == 1) ? PIN_TO_SAMD_PIN(68) \
  141. : (P == 2) ? PIN_TO_SAMD_PIN(69) \
  142. : (P == 3) ? PIN_TO_SAMD_PIN(70) \
  143. : (P == 4) ? PIN_TO_SAMD_PIN(71) \
  144. : (P == 5) ? PIN_TO_SAMD_PIN(72) \
  145. : (P == 6) ? PIN_TO_SAMD_PIN(73) \
  146. : (P == 7) ? PIN_TO_SAMD_PIN(74) \
  147. : (P == 8) ? PIN_TO_SAMD_PIN(54) \
  148. : (P == 9) ? PIN_TO_SAMD_PIN(55) \
  149. : (P == 10) ? PIN_TO_SAMD_PIN(56) \
  150. : (P == 11) ? PIN_TO_SAMD_PIN(57) \
  151. : (P == 12) ? PIN_TO_SAMD_PIN(58) \
  152. : (P == 13) ? PIN_TO_SAMD_PIN(59) \
  153. : (P == 14) ? PIN_TO_SAMD_PIN(60) \
  154. : (P == 15) ? PIN_TO_SAMD_PIN(61) \
  155. : (P == 16) ? PIN_TO_SAMD_PIN(12) \
  156. : (P == 17) ? PIN_TO_SAMD_PIN(13) \
  157. : PIN_TO_SAMD_PIN(9))
  158. #define digitalPinToAnalogInput(P) (WITHIN(P, 67, 74) ? (P) - 67 : WITHIN(P, 54, 61) ? 8 + (P) - 54 : WITHIN(P, 12, 13) ? 16 + (P) - 12 : P == 9 ? 18 : -1)
  159. /*
  160. * pins
  161. */
  162. // PORTA
  163. #define DIO67_PIN PIN_PA02 // A0
  164. #define DIO59_PIN PIN_PA04 // A13
  165. #define DIO68_PIN PIN_PA05 // A1
  166. #define DIO60_PIN PIN_PA06 // A14
  167. #define DIO61_PIN PIN_PA07 // A15
  168. #define DIO26_PIN PIN_PA12
  169. #define DIO27_PIN PIN_PA13
  170. #define DIO28_PIN PIN_PA14
  171. #define DIO23_PIN PIN_PA15
  172. #define DIO37_PIN PIN_PA16
  173. #define DIO36_PIN PIN_PA17
  174. #define DIO35_PIN PIN_PA18
  175. #define DIO34_PIN PIN_PA19
  176. #define DIO33_PIN PIN_PA20
  177. #define DIO32_PIN PIN_PA21
  178. #define DIO31_PIN PIN_PA22
  179. #define DIO30_PIN PIN_PA23
  180. // PORTB
  181. #define DIO12_PIN PIN_PB00 // A16
  182. #define DIO13_PIN PIN_PB01 // A17
  183. #define DIO9_PIN PIN_PB02 // A18
  184. #define DIO69_PIN PIN_PB03 // A2
  185. #define DIO74_PIN PIN_PB04 // A7
  186. #define DIO54_PIN PIN_PB05 // A8
  187. #define DIO55_PIN PIN_PB06 // A9
  188. #define DIO56_PIN PIN_PB07 // A10
  189. #define DIO57_PIN PIN_PB08 // A11
  190. #define DIO58_PIN PIN_PB09 // A12
  191. #define DIO18_PIN PIN_PB12
  192. #define DIO19_PIN PIN_PB13
  193. #define DIO39_PIN PIN_PB14
  194. #define DIO38_PIN PIN_PB15
  195. #define DIO14_PIN PIN_PB16
  196. #define DIO15_PIN PIN_PB17
  197. #define DIO8_PIN PIN_PB18
  198. #define DIO29_PIN PIN_PB19
  199. #define DIO20_PIN PIN_PB20
  200. #define DIO21_PIN PIN_PB21
  201. #define DIO10_PIN PIN_PB22
  202. #define DIO11_PIN PIN_PB23
  203. #define DIO1_PIN PIN_PB24
  204. #define DIO0_PIN PIN_PB25
  205. #define DIO83_PIN PIN_PB28 // SD_CS
  206. #define DIO95_PIN PIN_PB31 // SD_CD
  207. // PORTC
  208. #define DIO70_PIN PIN_PC00 // A3
  209. #define DIO71_PIN PIN_PC01 // A4
  210. #define DIO72_PIN PIN_PC02 // A5
  211. #define DIO73_PIN PIN_PC03 // A6
  212. #define DIO48_PIN PIN_PC04
  213. #define DIO49_PIN PIN_PC05
  214. #define DIO46_PIN PIN_PC06
  215. #define DIO47_PIN PIN_PC07
  216. #define DIO45_PIN PIN_PC10
  217. #define DIO44_PIN PIN_PC11
  218. #define DIO41_PIN PIN_PC12
  219. #define DIO40_PIN PIN_PC13
  220. #define DIO43_PIN PIN_PC14
  221. #define DIO42_PIN PIN_PC15
  222. #define DIO25_PIN PIN_PC16
  223. #define DIO24_PIN PIN_PC17
  224. #define DIO2_PIN PIN_PC18
  225. #define DIO3_PIN PIN_PC19
  226. #define DIO4_PIN PIN_PC20
  227. #define DIO5_PIN PIN_PC21
  228. #define DIO16_PIN PIN_PC22
  229. #define DIO17_PIN PIN_PC23
  230. #define DIO88_PIN PIN_PC24 // NEOPIXEL
  231. // PORTD
  232. #define DIO53_PIN PIN_PD10
  233. #define DIO22_PIN PIN_PD12
  234. #define DIO6_PIN PIN_PD20
  235. #define DIO7_PIN PIN_PD21
  236. #endif // ADAFRUIT_GRAND_CENTRAL_M4