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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /**
  31. * Enable this option to use Teensy++ 2.0 assignments for AT90USB processors.
  32. */
  33. //#define AT90USBxx_TEENSYPP_ASSIGNMENTS
  34. /**
  35. * Include Ports and Functions
  36. */
  37. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
  38. #include "fastio_168.h"
  39. #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284P__)
  40. #include "fastio_644.h"
  41. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  42. #include "fastio_1280.h"
  43. #elif defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__)
  44. #ifdef AT90USBxx_TEENSYPP_ASSIGNMENTS
  45. #include "fastio_AT90USB-Teensy.h"
  46. #else
  47. #include "fastio_AT90USB-Marlin.h"
  48. #endif
  49. #elif defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__)
  50. #include "fastio_1281.h"
  51. #else
  52. #error "Pins for this chip not defined in Arduino.h! If you have a working pins definition, please contribute!"
  53. #endif
  54. #ifndef _BV
  55. #define _BV(PIN) (1UL << PIN)
  56. #endif
  57. /**
  58. * Magic I/O routines
  59. *
  60. * Now you can simply SET_OUTPUT(PIN); WRITE(PIN, HIGH); WRITE(PIN, LOW);
  61. *
  62. * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
  63. */
  64. #define _READ(IO) ((bool)(DIO ## IO ## _RPORT & _BV(DIO ## IO ## _PIN)))
  65. // On some boards pins > 0x100 are used. These are not converted to atomic actions. A critical section is needed.
  66. #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)
  67. #define _WRITE_C(IO, v) do { if (v) { \
  68. CRITICAL_SECTION_START; \
  69. {DIO ## IO ## _WPORT |= _BV(DIO ## IO ## _PIN); } \
  70. CRITICAL_SECTION_END; \
  71. } \
  72. else { \
  73. CRITICAL_SECTION_START; \
  74. {DIO ## IO ## _WPORT &= ~_BV(DIO ## IO ## _PIN); } \
  75. CRITICAL_SECTION_END; \
  76. } \
  77. } \
  78. while (0)
  79. #define _WRITE(IO, v) do { if (&(DIO ## IO ## _RPORT) >= (uint8_t *)0x100) {_WRITE_C(IO, v); } else {_WRITE_NC(IO, v); }; } while (0)
  80. #define _TOGGLE(IO) do {DIO ## IO ## _RPORT ^= _BV(DIO ## IO ## _PIN); } while (0)
  81. #define _SET_INPUT(IO) do {DIO ## IO ## _DDR &= ~_BV(DIO ## IO ## _PIN); } while (0)
  82. #define _SET_OUTPUT(IO) do {DIO ## IO ## _DDR |= _BV(DIO ## IO ## _PIN); } while (0)
  83. #define _GET_INPUT(IO) ((DIO ## IO ## _DDR & _BV(DIO ## IO ## _PIN)) == 0)
  84. #define _GET_OUTPUT(IO) ((DIO ## IO ## _DDR & _BV(DIO ## IO ## _PIN)) != 0)
  85. #define _GET_TIMER(IO) (DIO ## IO ## _PWM)
  86. #define READ(IO) _READ(IO)
  87. #define WRITE(IO,V) _WRITE(IO,V)
  88. #define TOGGLE(IO) _TOGGLE(IO)
  89. #define SET_INPUT(IO) _SET_INPUT(IO)
  90. #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _WRITE(IO, HIGH); }while(0)
  91. #define SET_OUTPUT(IO) _SET_OUTPUT(IO)
  92. #define GET_INPUT(IO) _GET_INPUT(IO)
  93. #define GET_OUTPUT(IO) _GET_OUTPUT(IO)
  94. #define GET_TIMER(IO) _GET_TIMER(IO)
  95. #define OUT_WRITE(IO, v) do{ SET_OUTPUT(IO); WRITE(IO, v); }while(0)
  96. /**
  97. * Timer and Interrupt Control
  98. */
  99. // Waveform Generation Modes
  100. typedef enum {
  101. WGM_NORMAL, // 0
  102. WGM_PWM_PC_8, // 1
  103. WGM_PWM_PC_9, // 2
  104. WGM_PWM_PC_10, // 3
  105. WGM_CTC_OCRnA, // 4 COM OCnx
  106. WGM_FAST_PWM_8, // 5
  107. WGM_FAST_PWM_9, // 6
  108. WGM_FAST_PWM_10, // 7
  109. WGM_PWM_PC_FC_ICRn, // 8
  110. WGM_PWM_PC_FC_OCRnA, // 9 COM OCnA
  111. WGM_PWM_PC_ICRn, // 10
  112. WGM_PWM_PC_OCRnA, // 11 COM OCnA
  113. WGM_CTC_ICRn, // 12 COM OCnx
  114. WGM_reserved, // 13
  115. WGM_FAST_PWM_ICRn, // 14 COM OCnA
  116. WGM_FAST_PWM_OCRnA // 15 COM OCnA
  117. } WaveGenMode;
  118. // Compare Modes
  119. typedef enum {
  120. COM_NORMAL, // 0
  121. COM_TOGGLE, // 1 Non-PWM: OCnx ... Both PWM (WGM 9,11,14,15): OCnA only ... else NORMAL
  122. COM_CLEAR_SET, // 2 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  123. COM_SET_CLEAR // 3 Non-PWM: OCnx ... Fast PWM: OCnx/Bottom ... PF-FC: OCnx Up/Down
  124. } CompareMode;
  125. // Clock Sources
  126. typedef enum {
  127. CS_NONE, // 0
  128. CS_PRESCALER_1, // 1
  129. CS_PRESCALER_8, // 2
  130. CS_PRESCALER_64, // 3
  131. CS_PRESCALER_256, // 4
  132. CS_PRESCALER_1024, // 5
  133. CS_EXT_FALLING, // 6
  134. CS_EXT_RISING // 7
  135. } ClockSource;
  136. // Clock Sources (Timer 2 only)
  137. typedef enum {
  138. CS2_NONE, // 0
  139. CS2_PRESCALER_1, // 1
  140. CS2_PRESCALER_8, // 2
  141. CS2_PRESCALER_32, // 3
  142. CS2_PRESCALER_64, // 4
  143. CS2_PRESCALER_128, // 5
  144. CS2_PRESCALER_256, // 6
  145. CS2_PRESCALER_1024 // 7
  146. } ClockSource2;
  147. // Get interrupt bits in an orderly way
  148. #define GET_WGM(T) (((TCCR##T##A >> WGM##T##0) & 0x3) | ((TCCR##T##B >> WGM##T##2 << 2) & 0xC))
  149. #define GET_CS(T) ((TCCR##T##B >> CS##T##0) & 0x7)
  150. #define GET_COM(T,Q) ((TCCR##T##Q >> COM##T##Q##0) & 0x3)
  151. #define GET_COMA(T) GET_COM(T,A)
  152. #define GET_COMB(T) GET_COM(T,B)
  153. #define GET_COMC(T) GET_COM(T,C)
  154. #define GET_ICNC(T) (!!(TCCR##T##B & _BV(ICNC##T)))
  155. #define GET_ICES(T) (!!(TCCR##T##B & _BV(ICES##T)))
  156. #define GET_FOC(T,Q) (!!(TCCR##T##C & _BV(FOC##T##Q)))
  157. #define GET_FOCA(T) GET_FOC(T,A)
  158. #define GET_FOCB(T) GET_FOC(T,B)
  159. #define GET_FOCC(T) GET_FOC(T,C)
  160. // Set Wave Generation Mode bits
  161. #define _SET_WGM(T,V) do{ \
  162. TCCR##T##A = (TCCR##T##A & ~(0x3 << WGM##T##0)) | (( int(V) & 0x3) << WGM##T##0); \
  163. TCCR##T##B = (TCCR##T##B & ~(0x3 << WGM##T##2)) | (((int(V) >> 2) & 0x3) << WGM##T##2); \
  164. }while(0)
  165. #define SET_WGM(T,V) _SET_WGM(T,WGM_##V)
  166. // Set Clock Select bits
  167. #define _SET_CS(T,V) (TCCR##T##B = (TCCR##T##B & ~(0x7 << CS##T##0)) | ((int(V) & 0x7) << CS##T##0))
  168. #define _SET_CS0(V) _SET_CS(0,V)
  169. #define _SET_CS1(V) _SET_CS(1,V)
  170. #ifdef TCCR2
  171. #define _SET_CS2(V) (TCCR2 = (TCCR2 & ~(0x7 << CS20)) | (int(V) << CS20))
  172. #else
  173. #define _SET_CS2(V) _SET_CS(2,V)
  174. #endif
  175. #define _SET_CS3(V) _SET_CS(3,V)
  176. #define _SET_CS4(V) _SET_CS(4,V)
  177. #define _SET_CS5(V) _SET_CS(5,V)
  178. #define SET_CS0(V) _SET_CS0(CS_##V)
  179. #define SET_CS1(V) _SET_CS1(CS_##V)
  180. #ifdef TCCR2
  181. #define SET_CS2(V) _SET_CS2(CS2_##V)
  182. #else
  183. #define SET_CS2(V) _SET_CS2(CS_##V)
  184. #endif
  185. #define SET_CS3(V) _SET_CS3(CS_##V)
  186. #define SET_CS4(V) _SET_CS4(CS_##V)
  187. #define SET_CS5(V) _SET_CS5(CS_##V)
  188. #define SET_CS(T,V) SET_CS##T(V)
  189. // Set Compare Mode bits
  190. #define _SET_COM(T,Q,V) (TCCR##T##Q = (TCCR##T##Q & ~(0x3 << COM##T##Q##0)) | (int(V) << COM##T##Q##0))
  191. #define _SET_COMA(T,V) _SET_COM(T,A,V)
  192. #define _SET_COMB(T,V) _SET_COM(T,B,V)
  193. #define _SET_COMC(T,V) _SET_COM(T,C,V)
  194. #define _SET_COMS(T,V1,V2,V3) do{ _SET_COMA(T,V1); _SET_COMB(T,V2); _SET_COMC(T,V3); }while(0)
  195. #define SET_COM(T,Q,V) _SET_COM(T,Q,COM_##V)
  196. #define SET_COMA(T,V) SET_COM(T,A,V)
  197. #define SET_COMB(T,V) SET_COM(T,B,V)
  198. #define SET_COMC(T,V) SET_COM(T,C,V)
  199. #define SET_COMS(T,V1,V2,V3) do{ SET_COMA(T,V1); SET_COMB(T,V2); SET_COMC(T,V3); }while(0)
  200. // Set Noise Canceler bit
  201. #define SET_ICNC(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICNC##T) : TCCR##T##B & ~_BV(ICNC##T))
  202. // Set Input Capture Edge Select bit
  203. #define SET_ICES(T,V) (TCCR##T##B = (V) ? TCCR##T##B | _BV(ICES##T) : TCCR##T##B & ~_BV(ICES##T))
  204. // Set Force Output Compare bit
  205. #define SET_FOC(T,Q,V) (TCCR##T##C = (V) ? TCCR##T##C | _BV(FOC##T##Q) : TCCR##T##C & ~_BV(FOC##T##Q))
  206. #define SET_FOCA(T,V) SET_FOC(T,A,V)
  207. #define SET_FOCB(T,V) SET_FOC(T,B,V)
  208. #define SET_FOCC(T,V) SET_FOC(T,C,V)
  209. #endif // _FASTIO_ARDUINO_H