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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * Fast I/O Routines
  24. * Use direct port manipulation to save scads of processor time.
  25. * Contributed by Triffid_Hunter. Modified by Kliment and the Marlin team.
  26. */
  27. #ifndef _FASTIO_ARDUINO_H
  28. #define _FASTIO_ARDUINO_H
  29. #include <avr/io.h>
  30. #include "macros.h"
  31. #define AVR_AT90USB1286_FAMILY (defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1286P__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB646P__) || defined(__AVR_AT90USB647__))
  32. #define AVR_ATmega1284_FAMILY (defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__))
  33. #define AVR_ATmega2560_FAMILY (defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__))
  34. #define AVR_ATmega2561_FAMILY (defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__))
  35. #define AVR_ATmega328_FAMILY (defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328p__))
  36. /**
  37. * Include Ports and Functions
  38. */
  39. #if AVR_ATmega328_FAMILY
  40. #include "fastio_168.h"
  41. #elif AVR_ATmega1284_FAMILY
  42. #include "fastio_644.h"
  43. #elif AVR_ATmega2560_FAMILY
  44. #include "fastio_1280.h"
  45. #elif AVR_AT90USB1286_FAMILY
  46. #include "fastio_AT90USB.h"
  47. #elif AVR_ATmega2561_FAMILY
  48. #include "fastio_1281.h"
  49. #else
  50. #error "Pins for this chip not defined in Arduino.h! If you have a working pins definition, please contribute!"
  51. #endif
  52. #ifndef _BV
  53. #define _BV(PIN) (1UL << PIN)
  54. #endif
  55. /**
  56. * Magic I/O routines
  57. *
  58. * Now you can simply SET_OUTPUT(PIN); WRITE(PIN, HIGH); WRITE(PIN, LOW);
  59. *
  60. * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
  61. */
  62. #define _READ(IO) ((bool)(DIO ## IO ## _RPORT & _BV(DIO ## IO ## _PIN)))
  63. // On some boards pins > 0x100 are used. These are not converted to atomic actions. A critical section is needed.
  64. #define _WRITE_NC(IO, v) do { if (v) {DIO ## IO ## _WPORT |= _BV(DIO ## IO ## _PIN); } else {DIO ## IO ## _WPORT &= ~_BV(DIO ## IO ## _PIN); }; } while (0)
  65. #define _WRITE_C(IO, v) do { if (v) { \
  66. CRITICAL_SECTION_START; \
  67. {DIO ## IO ## _WPORT |= _BV(DIO ## IO ## _PIN); } \
  68. CRITICAL_SECTION_END; \
  69. } \
  70. else { \
  71. CRITICAL_SECTION_START; \
  72. {DIO ## IO ## _WPORT &= ~_BV(DIO ## IO ## _PIN); } \
  73. CRITICAL_SECTION_END; \
  74. } \
  75. } \
  76. while (0)
  77. #define _WRITE(IO, v) do { if (&(DIO ## IO ## _RPORT) >= (uint8_t *)0x100) {_WRITE_C(IO, v); } else {_WRITE_NC(IO, v); }; } while (0)
  78. #define _TOGGLE(IO) do {DIO ## IO ## _RPORT ^= _BV(DIO ## IO ## _PIN); } while (0)
  79. #define _SET_INPUT(IO) do {DIO ## IO ## _DDR &= ~_BV(DIO ## IO ## _PIN); } while (0)
  80. #define _SET_OUTPUT(IO) do {DIO ## IO ## _DDR |= _BV(DIO ## IO ## _PIN); } while (0)
  81. #define _GET_INPUT(IO) ((DIO ## IO ## _DDR & _BV(DIO ## IO ## _PIN)) == 0)
  82. #define _GET_OUTPUT(IO) ((DIO ## IO ## _DDR & _BV(DIO ## IO ## _PIN)) != 0)
  83. #define _GET_TIMER(IO) (DIO ## IO ## _PWM)
  84. #define READ(IO) _READ(IO)
  85. #define WRITE(IO,V) _WRITE(IO,V)
  86. #define TOGGLE(IO) _TOGGLE(IO)
  87. #define SET_INPUT(IO) _SET_INPUT(IO)
  88. #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _WRITE(IO, HIGH); }while(0)
  89. #define SET_OUTPUT(IO) _SET_OUTPUT(IO)
  90. #define GET_INPUT(IO) _GET_INPUT(IO)
  91. #define GET_OUTPUT(IO) _GET_OUTPUT(IO)
  92. #define GET_TIMER(IO) _GET_TIMER(IO)
  93. #define OUT_WRITE(IO, v) do{ SET_OUTPUT(IO); WRITE(IO, v); }while(0)
  94. /**
  95. * Timer and Interrupt Control
  96. */
  97. // Waveform Generation Modes
  98. typedef enum {
  99. WGM_NORMAL, // 0
  100. WGM_PWM_PC_8, // 1
  101. WGM_PWM_PC_9, // 2
  102. WGM_PWM_PC_10, // 3
  103. WGM_CTC_OCRnA, // 4 COM OCnx
  104. WGM_FAST_PWM_8, // 5
  105. WGM_FAST_PWM_9, // 6
  106. WGM_FAST_PWM_10, // 7
  107. WGM_PWM_PC_FC_ICRn, // 8
  108. WGM_PWM_PC_FC_OCRnA, // 9 COM OCnA
  109. WGM_PWM_PC_ICRn, // 10
  110. WGM_PWM_PC_OCRnA, // 11 COM OCnA
  111. WGM_CTC_ICRn, // 12 COM OCnx
  112. WGM_reserved, // 13
  113. WGM_FAST_PWM_ICRn, // 14 COM OCnA
  114. WGM_FAST_PWM_OCRnA // 15 COM OCnA
  115. } WaveGenMode;
  116. // Compare Modes
  117. typedef enum {
  118. COM_NORMAL, // 0
  119. COM_TOGGLE, // 1 Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
  120. COM_CLEAR_SET, // 2 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  121. COM_SET_CLEAR // 3 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  122. } CompareMode;
  123. // Clock Sources
  124. typedef enum {
  125. CS_NONE, // 0
  126. CS_PRESCALER_1, // 1
  127. CS_PRESCALER_8, // 2
  128. CS_PRESCALER_64, // 3
  129. CS_PRESCALER_256, // 4
  130. CS_PRESCALER_1024, // 5
  131. CS_EXT_FALLING, // 6
  132. CS_EXT_RISING // 7
  133. } ClockSource;
  134. // Clock Sources (Timer 2 only)
  135. typedef enum {
  136. CS2_NONE, // 0
  137. CS2_PRESCALER_1, // 1
  138. CS2_PRESCALER_8, // 2
  139. CS2_PRESCALER_32, // 3
  140. CS2_PRESCALER_64, // 4
  141. CS2_PRESCALER_128, // 5
  142. CS2_PRESCALER_256, // 6
  143. CS2_PRESCALER_1024 // 7
  144. } ClockSource2;
  145. // Get interrupt bits in an orderly way
  146. #define GET_WGM(T) (((TCCR##T##A >> WGM##T##0) & 0x3) | ((TCCR##T##B >> WGM##T##2 << 2) & 0xC))
  147. #define GET_CS(T) ((TCCR##T##B >> CS##T##0) & 0x7)
  148. #define GET_COM(T,Q) ((TCCR##T##Q >> COM##T##Q##0) & 0x3)
  149. #define GET_COMA(T) GET_COM(T,A)
  150. #define GET_COMB(T) GET_COM(T,B)
  151. #define GET_COMC(T) GET_COM(T,C)
  152. #define GET_ICNC(T) (!!(TCCR##T##B & _BV(ICNC##T)))
  153. #define GET_ICES(T) (!!(TCCR##T##B & _BV(ICES##T)))
  154. #define GET_FOC(T,Q) (!!(TCCR##T##C & _BV(FOC##T##Q)))
  155. #define GET_FOCA(T) GET_FOC(T,A)
  156. #define GET_FOCB(T) GET_FOC(T,B)
  157. #define GET_FOCC(T) GET_FOC(T,C)
  158. // Set Wave Generation Mode bits
  159. #define _SET_WGM(T,V) do{ \
  160. TCCR##T##A = (TCCR##T##A & ~(0x3 << WGM##T##0)) | (( int(V) & 0x3) << WGM##T##0); \
  161. TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
  162. }while(0)
  163. #define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
  164. // Set Clock Select bits
  165. #define _SET_CS(T,V) (TCCR##T##B = (TCCR##T##B & ~(0x7 << CS##T##0)) | ((int(V) & 0x7) << CS##T##0))
  166. #define _SET_CS0(V) _SET_CS(0,V)
  167. #define _SET_CS1(V) _SET_CS(1,V)
  168. #ifdef TCCR2
  169. #define _SET_CS2(V) (TCCR2 = (TCCR2 & ~(0x7 << CS20)) | (int(V) << CS20))
  170. #else
  171. #define _SET_CS2(V) _SET_CS(2,V)
  172. #endif
  173. #define _SET_CS3(V) _SET_CS(3,V)
  174. #define _SET_CS4(V) _SET_CS(4,V)
  175. #define _SET_CS5(V) _SET_CS(5,V)
  176. #define SET_CS0(V) _SET_CS0(CS_##V)
  177. #define SET_CS1(V) _SET_CS1(CS_##V)
  178. #ifdef TCCR2
  179. #define SET_CS2(V) _SET_CS2(CS2_##V)
  180. #else
  181. #define SET_CS2(V) _SET_CS2(CS_##V)
  182. #endif
  183. #define SET_CS3(V) _SET_CS3(CS_##V)
  184. #define SET_CS4(V) _SET_CS4(CS_##V)
  185. #define SET_CS5(V) _SET_CS5(CS_##V)
  186. #define SET_CS(T,V) SET_CS##T(V)
  187. // Set Compare Mode bits
  188. #define _SET_COM(T,Q,V) (TCCR##T##Q = (TCCR##T##Q & ~(0x3 << COM##T##Q##0)) | (int(V) << COM##T##Q##0))
  189. #define SET_COM(T,Q,V) _SET_COM(T,Q,COM_##V)
  190. #define SET_COMA(T,V) SET_COM(T,A,V)
  191. #define SET_COMB(T,V) SET_COM(T,B,V)
  192. #define SET_COMC(T,V) SET_COM(T,C,V)
  193. #define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
  194. // Set Noise Canceler bit
  195. #define SET_ICNC(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICNC##T) : TCCR##T##B & ~_BV(ICNC##T))
  196. // Set Input Capture Edge Select bit
  197. #define SET_ICES(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICES##T) : TCCR##T##B & ~_BV(ICES##T))
  198. // Set Force Output Compare bit
  199. #define SET_FOC(T,Q,V) (TCCR##T##C = (V) ? TCCR##T##C | _BV(FOC##T##Q) : TCCR##T##C & ~_BV(FOC##T##Q))
  200. #define SET_FOCA(T,V) SET_FOC(T,A,V)
  201. #define SET_FOCB(T,V) SET_FOC(T,B,V)
  202. #define SET_FOCC(T,V) SET_FOC(T,C,V)
  203. /**
  204. * PWM availability macros
  205. */
  206. //find out which harware PWMs are already in use
  207. #if PIN_EXISTS(CONTROLLER_FAN)
  208. #define PWM_CHK_FAN_B(p) (p == CONTROLLER_FAN_PIN || p == E0_AUTO_FAN_PIN || p == E1_AUTO_FAN_PIN || p == E2_AUTO_FAN_PIN || p == E3_AUTO_FAN_PIN || p == E4_AUTO_FAN_PIN)
  209. #else
  210. #define PWM_CHK_FAN_B(p) (p == E0_AUTO_FAN_PIN || p == E1_AUTO_FAN_PIN || p == E2_AUTO_FAN_PIN || p == E3_AUTO_FAN_PIN || p == E4_AUTO_FAN_PIN)
  211. #endif
  212. #if PIN_EXISTS(FAN) || PIN_EXISTS(FAN1) || PIN_EXISTS(FAN2)
  213. #if PIN_EXISTS(FAN2)
  214. #define PWM_CHK_FAN_A(p) (p == FAN_PIN || p == FAN1_PIN || p == FAN2_PIN)
  215. #elif PIN_EXISTS(FAN1)
  216. #define PWM_CHK_FAN_A(p) (p == FAN_PIN || p == FAN1_PIN)
  217. #else
  218. #define PWM_CHK_FAN_A(p) p == FAN_PIN
  219. #endif
  220. #else
  221. #define PWM_CHK_FAN_A(p) false
  222. #endif
  223. #if HAS_MOTOR_CURRENT_PWM
  224. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  225. #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E || p == MOTOR_CURRENT_PWM_Z || p == MOTOR_CURRENT_PWM_XY)
  226. #elif PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  227. #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E || p == MOTOR_CURRENT_PWM_Z)
  228. #else
  229. #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E)
  230. #endif
  231. #else
  232. #define PWM_CHK_MOTOR_CURRENT(p) false
  233. #endif
  234. #if defined(NUM_SERVOS)
  235. #if AVR_ATmega2560_FAMILY
  236. #define PWM_CHK_SERVO(p) ( p == 5 || NUM_SERVOS > 12 && p == 6 || NUM_SERVOS > 24 && p == 46) //PWMS 3A, 4A & 5A
  237. #elif AVR_ATmega2561_FAMILY
  238. #define PWM_CHK_SERVO(p) p == 5 //PWM3A
  239. #elif AVR_ATmega1284_FAMILY
  240. #define PWM_CHK_SERVO(p) false
  241. #elif AVR_AT90USB1286_FAMILY
  242. #define PWM_CHK_SERVO(p) p == 16 //PWM3A
  243. #elif AVR_ATmega328_FAMILY
  244. #define PWM_CHK_SERVO(p) false
  245. #endif
  246. #else
  247. #define PWM_CHK_SERVO(p) false
  248. #endif
  249. #if ENABLED(BARICUDA)
  250. #if HAS_HEATER_1 && HAS_HEATER_2
  251. #define PWM_CHK_HEATER(p) (p == HEATER_1_PIN || p == HEATER_2_PIN)
  252. #elif HAS_HEATER_1
  253. #define PWM_CHK_HEATER(p) (p == HEATER_1_PIN)
  254. #endif
  255. #else
  256. #define PWM_CHK_HEATER(p) false
  257. #endif
  258. #define PWM_CHK(p) (PWM_CHK_HEATER(p) || PWM_CHK_SERVO(p) || PWM_CHK_MOTOR_CURRENT(p)\
  259. || PWM_CHK_FAN_A(p) || PWM_CHK_FAN_B(p))
  260. // define which hardware PWMs are available for the current CPU
  261. // all timer 1 PWMS deleted from this list because they are never available
  262. #if AVR_ATmega2560_FAMILY
  263. #define PWM_PINS(p) ((p >= 2 && p <= 10 ) || p == 13 || p == 44 || p == 45 || p == 46 )
  264. #elif AVR_ATmega2561_FAMILY
  265. #define PWM_PINS(p) ((p >= 2 && p <= 6 ) || p == 9)
  266. #elif AVR_ATmega1284_FAMILY
  267. #define PWM_PINS(p) (p == 3 || p == 4 || p == 14 || p == 15)
  268. #elif AVR_AT90USB1286_FAMILY
  269. #define PWM_PINS(p) (p == 0 || p == 1 || p == 14 || p == 15 || p == 16 || p == 24)
  270. #elif AVR_ATmega328_FAMILY
  271. #define PWM_PINS(p) (p == 3 || p == 5 || p == 6 || p == 11)
  272. #else
  273. #error "unknown CPU"
  274. #endif
  275. // finally - the macro that tells us if a pin is an available hardware PWM
  276. #define USEABLE_HARDWARE_PWM(p) (PWM_PINS(p) && !PWM_CHK(p))
  277. #endif // _FASTIO_ARDUINO_H