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

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