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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. * Fast I/O Routines for AVR
  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. #include <avr/io.h>
  29. #define AVR_AT90USB1286_FAMILY (defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1286P__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB646P__) || defined(__AVR_AT90USB647__))
  30. #define AVR_ATmega1284_FAMILY (defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__))
  31. #define AVR_ATmega2560_FAMILY (defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__))
  32. #define AVR_ATmega2561_FAMILY (defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__))
  33. #define AVR_ATmega328_FAMILY (defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__))
  34. /**
  35. * Include Ports and Functions
  36. */
  37. #if AVR_ATmega328_FAMILY
  38. #include "fastio/fastio_168.h"
  39. #elif AVR_ATmega1284_FAMILY
  40. #include "fastio/fastio_644.h"
  41. #elif AVR_ATmega2560_FAMILY
  42. #include "fastio/fastio_1280.h"
  43. #elif AVR_AT90USB1286_FAMILY
  44. #include "fastio/fastio_AT90USB.h"
  45. #elif AVR_ATmega2561_FAMILY
  46. #include "fastio/fastio_1281.h"
  47. #else
  48. #error "No FastIO definition for the selected AVR Board."
  49. #endif
  50. /**
  51. * Magic I/O routines
  52. *
  53. * Now you can simply SET_OUTPUT(PIN); WRITE(PIN, HIGH); WRITE(PIN, LOW);
  54. *
  55. * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
  56. */
  57. #define _READ(IO) TEST(DIO ## IO ## _RPORT, DIO ## IO ## _PIN)
  58. #define _WRITE_NC(IO,V) do{ \
  59. if (V) SBI(DIO ## IO ## _WPORT, DIO ## IO ## _PIN); \
  60. else CBI(DIO ## IO ## _WPORT, DIO ## IO ## _PIN); \
  61. }while(0)
  62. #define _WRITE_C(IO,V) do{ \
  63. uint8_t port_bits = DIO ## IO ## _WPORT; /* Get a mask from the current port bits */ \
  64. if (V) port_bits = ~port_bits; /* For setting bits, invert the mask */ \
  65. DIO ## IO ## _RPORT = port_bits & _BV(DIO ## IO ## _PIN); /* Atomically toggle the output port bits */ \
  66. }while(0)
  67. #define _WRITE(IO,V) do{ if (&(DIO ## IO ## _RPORT) < (uint8_t*)0x100) _WRITE_NC(IO,V); else _WRITE_C(IO,V); }while(0)
  68. #define _TOGGLE(IO) (DIO ## IO ## _RPORT = _BV(DIO ## IO ## _PIN))
  69. #define _SET_INPUT(IO) CBI(DIO ## IO ## _DDR, DIO ## IO ## _PIN)
  70. #define _SET_OUTPUT(IO) SBI(DIO ## IO ## _DDR, DIO ## IO ## _PIN)
  71. #define _IS_INPUT(IO) !TEST(DIO ## IO ## _DDR, DIO ## IO ## _PIN)
  72. #define _IS_OUTPUT(IO) TEST(DIO ## IO ## _DDR, DIO ## IO ## _PIN)
  73. // digitalRead/Write wrappers
  74. #ifdef FASTIO_EXT_START
  75. void extDigitalWrite(const int8_t pin, const uint8_t state);
  76. uint8_t extDigitalRead(const int8_t pin);
  77. #else
  78. #define extDigitalWrite(IO,V) digitalWrite(IO,V)
  79. #define extDigitalRead(IO) digitalRead(IO)
  80. #endif
  81. #define READ(IO) _READ(IO)
  82. #define WRITE(IO,V) _WRITE(IO,V)
  83. #define TOGGLE(IO) _TOGGLE(IO)
  84. #define SET_INPUT(IO) _SET_INPUT(IO)
  85. #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _WRITE(IO, HIGH); }while(0)
  86. #define SET_INPUT_PULLDOWN SET_INPUT
  87. #define SET_OUTPUT(IO) _SET_OUTPUT(IO)
  88. #define SET_PWM SET_OUTPUT
  89. #define IS_INPUT(IO) _IS_INPUT(IO)
  90. #define IS_OUTPUT(IO) _IS_OUTPUT(IO)
  91. #define OUT_WRITE(IO,V) do{ SET_OUTPUT(IO); WRITE(IO,V); }while(0)
  92. /**
  93. * Timer and Interrupt Control
  94. */
  95. // Waveform Generation Modes
  96. enum WaveGenMode : char {
  97. WGM_NORMAL, // 0
  98. WGM_PWM_PC_8, // 1
  99. WGM_PWM_PC_9, // 2
  100. WGM_PWM_PC_10, // 3
  101. WGM_CTC_OCRnA, // 4 COM OCnx
  102. WGM_FAST_PWM_8, // 5
  103. WGM_FAST_PWM_9, // 6
  104. WGM_FAST_PWM_10, // 7
  105. WGM_PWM_PC_FC_ICRn, // 8
  106. WGM_PWM_PC_FC_OCRnA, // 9 COM OCnA
  107. WGM_PWM_PC_ICRn, // 10
  108. WGM_PWM_PC_OCRnA, // 11 COM OCnA
  109. WGM_CTC_ICRn, // 12 COM OCnx
  110. WGM_reserved, // 13
  111. WGM_FAST_PWM_ICRn, // 14 COM OCnA
  112. WGM_FAST_PWM_OCRnA // 15 COM OCnA
  113. };
  114. // Wavefore Generation Modes (Timer 2 only)
  115. enum WaveGenMode2 : char {
  116. WGM2_NORMAL, // 0
  117. WGM2_PWM_PC, // 1
  118. WGM2_CTC_OCR2A, // 2
  119. WGM2_FAST_PWM, // 3
  120. WGM2_reserved_1, // 4
  121. WGM2_PWM_PC_OCR2A, // 5
  122. WGM2_reserved_2, // 6
  123. WGM2_FAST_PWM_OCR2A, // 7
  124. };
  125. // Compare Modes
  126. enum CompareMode : char {
  127. COM_NORMAL, // 0
  128. COM_TOGGLE, // 1 Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
  129. COM_CLEAR_SET, // 2 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  130. COM_SET_CLEAR // 3 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  131. };
  132. // Clock Sources
  133. enum ClockSource : char {
  134. CS_NONE, // 0
  135. CS_PRESCALER_1, // 1
  136. CS_PRESCALER_8, // 2
  137. CS_PRESCALER_64, // 3
  138. CS_PRESCALER_256, // 4
  139. CS_PRESCALER_1024, // 5
  140. CS_EXT_FALLING, // 6
  141. CS_EXT_RISING // 7
  142. };
  143. // Clock Sources (Timer 2 only)
  144. enum ClockSource2 : char {
  145. CS2_NONE, // 0
  146. CS2_PRESCALER_1, // 1
  147. CS2_PRESCALER_8, // 2
  148. CS2_PRESCALER_32, // 3
  149. CS2_PRESCALER_64, // 4
  150. CS2_PRESCALER_128, // 5
  151. CS2_PRESCALER_256, // 6
  152. CS2_PRESCALER_1024 // 7
  153. };
  154. // Get interrupt bits in an orderly way
  155. // Ex: cs = GET_CS(0); coma1 = GET_COM(A,1);
  156. #define GET_WGM(T) (((TCCR##T##A >> WGM##T##0) & 0x3) | ((TCCR##T##B >> WGM##T##2 << 2) & 0xC))
  157. #define GET_CS(T) ((TCCR##T##B >> CS##T##0) & 0x7)
  158. #define GET_COM(T,Q) ((TCCR##T##Q >> COM##T##Q##0) & 0x3)
  159. #define GET_COMA(T) GET_COM(T,A)
  160. #define GET_COMB(T) GET_COM(T,B)
  161. #define GET_COMC(T) GET_COM(T,C)
  162. #define GET_ICNC(T) (!!(TCCR##T##B & _BV(ICNC##T)))
  163. #define GET_ICES(T) (!!(TCCR##T##B & _BV(ICES##T)))
  164. #define GET_FOC(T,Q) (!!(TCCR##T##C & _BV(FOC##T##Q)))
  165. #define GET_FOCA(T) GET_FOC(T,A)
  166. #define GET_FOCB(T) GET_FOC(T,B)
  167. #define GET_FOCC(T) GET_FOC(T,C)
  168. // Set Wave Generation Mode bits
  169. // Ex: SET_WGM(5,CTC_ICRn);
  170. #define _SET_WGM(T,V) do{ \
  171. TCCR##T##A = (TCCR##T##A & ~(0x3 << WGM##T##0)) | (( int(V) & 0x3) << WGM##T##0); \
  172. TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
  173. }while(0)
  174. #define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
  175. // Runtime (see set_pwm_frequency):
  176. #define _SET_WGMnQ(TCCRnQ, V) do{ \
  177. *(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V) & 0x3) << 0); \
  178. *(TCCRnQ)[1] = (*(TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
  179. }while(0)
  180. // Set Clock Select bits
  181. // Ex: SET_CS3(PRESCALER_64);
  182. #define _SET_CS(T,V) (TCCR##T##B = (TCCR##T##B & ~(0x7 << CS##T##0)) | ((int(V) & 0x7) << CS##T##0))
  183. #define _SET_CS0(V) _SET_CS(0,V)
  184. #define _SET_CS1(V) _SET_CS(1,V)
  185. #ifdef TCCR2
  186. #define _SET_CS2(V) (TCCR2 = (TCCR2 & ~(0x7 << CS20)) | (int(V) << CS20))
  187. #else
  188. #define _SET_CS2(V) _SET_CS(2,V)
  189. #endif
  190. #define _SET_CS3(V) _SET_CS(3,V)
  191. #define _SET_CS4(V) _SET_CS(4,V)
  192. #define _SET_CS5(V) _SET_CS(5,V)
  193. #define SET_CS0(V) _SET_CS0(CS_##V)
  194. #define SET_CS1(V) _SET_CS1(CS_##V)
  195. #ifdef TCCR2
  196. #define SET_CS2(V) _SET_CS2(CS2_##V)
  197. #else
  198. #define SET_CS2(V) _SET_CS2(CS_##V)
  199. #endif
  200. #define SET_CS3(V) _SET_CS3(CS_##V)
  201. #define SET_CS4(V) _SET_CS4(CS_##V)
  202. #define SET_CS5(V) _SET_CS5(CS_##V)
  203. #define SET_CS(T,V) SET_CS##T(V)
  204. // Runtime (see set_pwm_frequency)
  205. #define _SET_CSn(TCCRnQ, V) do{ \
  206. (*(TCCRnQ)[1] = (*(TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0)); \
  207. }while(0)
  208. // Set Compare Mode bits
  209. // Ex: SET_COMS(4,CLEAR_SET,CLEAR_SET,CLEAR_SET);
  210. #define _SET_COM(T,Q,V) (TCCR##T##Q = (TCCR##T##Q & ~(0x3 << COM##T##Q##0)) | (int(V) << COM##T##Q##0))
  211. #define SET_COM(T,Q,V) _SET_COM(T,Q,COM_##V)
  212. #define SET_COMA(T,V) SET_COM(T,A,V)
  213. #define SET_COMB(T,V) SET_COM(T,B,V)
  214. #define SET_COMC(T,V) SET_COM(T,C,V)
  215. #define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
  216. // Runtime (see set_pwm_duty)
  217. #define _SET_COMnQ(TCCRnQ, Q, V) do{ \
  218. (*(TCCRnQ)[0] = (*(TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q)))); \
  219. }while(0)
  220. // Set OCRnQ register
  221. // Runtime (see set_pwm_duty):
  222. #define _SET_OCRnQ(OCRnQ, Q, V) do{ \
  223. (*(OCRnQ)[(Q)] = (0x0000) | (int(V) & 0xFFFF)); \
  224. }while(0)
  225. // Set ICRn register (one per timer)
  226. // Runtime (see set_pwm_frequency)
  227. #define _SET_ICRn(ICRn, V) do{ \
  228. (*(ICRn) = (0x0000) | (int(V) & 0xFFFF)); \
  229. }while(0)
  230. // Set Noise Canceler bit
  231. // Ex: SET_ICNC(2,1)
  232. #define SET_ICNC(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICNC##T) : TCCR##T##B & ~_BV(ICNC##T))
  233. // Set Input Capture Edge Select bit
  234. // Ex: SET_ICES(5,0)
  235. #define SET_ICES(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICES##T) : TCCR##T##B & ~_BV(ICES##T))
  236. // Set Force Output Compare bit
  237. // Ex: SET_FOC(3,A,1)
  238. #define SET_FOC(T,Q,V) (TCCR##T##C = (V) ? TCCR##T##C | _BV(FOC##T##Q) : TCCR##T##C & ~_BV(FOC##T##Q))
  239. #define SET_FOCA(T,V) SET_FOC(T,A,V)
  240. #define SET_FOCB(T,V) SET_FOC(T,B,V)
  241. #define SET_FOCC(T,V) SET_FOC(T,C,V)
  242. #if 0
  243. /**
  244. * PWM availability macros
  245. */
  246. // Determine which harware PWMs are already in use
  247. #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 || P == E5_AUTO_FAN_PIN || P == E6_AUTO_FAN_PIN || P == E7_AUTO_FAN_PIN || P == CHAMBER_AUTO_FAN_PIN)
  248. #if PIN_EXISTS(CONTROLLER_FAN)
  249. #define PWM_CHK_FAN_B(P) (_PWM_CHK_FAN_B(P) || P == CONTROLLER_FAN_PIN)
  250. #else
  251. #define PWM_CHK_FAN_B(P) _PWM_CHK_FAN_B(P)
  252. #endif
  253. #if ANY_PIN(FAN, FAN1, FAN2, FAN3, FAN4, FAN5, FAN6, FAN7)
  254. #if PIN_EXISTS(FAN7)
  255. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN || P == FAN3_PIN || P == FAN4_PIN || P == FAN5_PIN || P == FAN6_PIN || P == FAN7_PIN)
  256. #elif PIN_EXISTS(FAN6)
  257. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN || P == FAN3_PIN || P == FAN4_PIN || P == FAN5_PIN || P == FAN6_PIN)
  258. #elif PIN_EXISTS(FAN5)
  259. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN || P == FAN3_PIN || P == FAN4_PIN || P == FAN5_PIN)
  260. #elif PIN_EXISTS(FAN4)
  261. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN || P == FAN3_PIN || P == FAN4_PIN)
  262. #elif PIN_EXISTS(FAN3)
  263. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN || P == FAN3_PIN)
  264. #elif PIN_EXISTS(FAN2)
  265. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN || P == FAN2_PIN)
  266. #elif PIN_EXISTS(FAN1)
  267. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN || P == FAN1_PIN)
  268. #else
  269. #define PWM_CHK_FAN_A(P) (P == FAN0_PIN)
  270. #endif
  271. #else
  272. #define PWM_CHK_FAN_A(P) false
  273. #endif
  274. #if HAS_MOTOR_CURRENT_PWM
  275. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  276. #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E || P == MOTOR_CURRENT_PWM_Z || P == MOTOR_CURRENT_PWM_XY)
  277. #elif PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  278. #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E || P == MOTOR_CURRENT_PWM_Z)
  279. #else
  280. #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E)
  281. #endif
  282. #else
  283. #define PWM_CHK_MOTOR_CURRENT(P) false
  284. #endif
  285. #ifdef NUM_SERVOS
  286. #if AVR_ATmega2560_FAMILY
  287. #define PWM_CHK_SERVO(P) (P == 5 || (NUM_SERVOS > 12 && P == 6) || (NUM_SERVOS > 24 && P == 46)) // PWMS 3A, 4A & 5A
  288. #elif AVR_ATmega2561_FAMILY
  289. #define PWM_CHK_SERVO(P) (P == 5) // PWM3A
  290. #elif AVR_ATmega1284_FAMILY
  291. #define PWM_CHK_SERVO(P) false
  292. #elif AVR_AT90USB1286_FAMILY
  293. #define PWM_CHK_SERVO(P) (P == 16) // PWM3A
  294. #elif AVR_ATmega328_FAMILY
  295. #define PWM_CHK_SERVO(P) false
  296. #endif
  297. #else
  298. #define PWM_CHK_SERVO(P) false
  299. #endif
  300. #if ENABLED(BARICUDA)
  301. #if HAS_HEATER_1 && HAS_HEATER_2
  302. #define PWM_CHK_HEATER(P) (P == HEATER_1_PIN || P == HEATER_2_PIN)
  303. #elif HAS_HEATER_1
  304. #define PWM_CHK_HEATER(P) (P == HEATER_1_PIN)
  305. #endif
  306. #else
  307. #define PWM_CHK_HEATER(P) false
  308. #endif
  309. #define PWM_CHK(P) (PWM_CHK_HEATER(P) || PWM_CHK_SERVO(P) || PWM_CHK_MOTOR_CURRENT(P) || PWM_CHK_FAN_A(P) || PWM_CHK_FAN_B(P))
  310. #endif // PWM_CHK is not used in Marlin
  311. // define which hardware PWMs are available for the current CPU
  312. // all timer 1 PWMS deleted from this list because they are never available
  313. #if AVR_ATmega2560_FAMILY
  314. #define PWM_PIN(P) ((P >= 2 && P <= 10) || P == 13 || P == 44 || P == 45 || P == 46)
  315. #elif AVR_ATmega2561_FAMILY
  316. #define PWM_PIN(P) ((P >= 2 && P <= 6) || P == 9)
  317. #elif AVR_ATmega1284_FAMILY
  318. #define PWM_PIN(P) (P == 3 || P == 4 || P == 14 || P == 15)
  319. #elif AVR_AT90USB1286_FAMILY
  320. #define PWM_PIN(P) (P == 0 || P == 1 || P == 14 || P == 15 || P == 16 || P == 24)
  321. #elif AVR_ATmega328_FAMILY
  322. #define PWM_PIN(P) (P == 3 || P == 5 || P == 6 || P == 11)
  323. #else
  324. #error "unknown CPU"
  325. #endif