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.5KB

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