My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

pinsDebug.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #pragma once
  20. /**
  21. * PWM print routines for Atmel 8 bit AVR CPUs
  22. */
  23. #include "../../inc/MarlinConfig.h"
  24. #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS
  25. #define AVR_ATmega2560_FAMILY_PLUS_70 MB(BQ_ZUM_MEGA_3D, MIGHTYBOARD_REVE, MINIRAMBO, SCOOVO_X9H)
  26. #if AVR_AT90USB1286_FAMILY
  27. // Working with Teensyduino extension so need to re-define some things
  28. #include "pinsDebug_Teensyduino.h"
  29. // Can't use the "digitalPinToPort" function from the Teensyduino type IDEs
  30. // portModeRegister takes a different argument
  31. #define digitalPinToTimer_DEBUG(p) digitalPinToTimer(p)
  32. #define digitalPinToBitMask_DEBUG(p) digitalPinToBitMask(p)
  33. #define digitalPinToPort_DEBUG(p) digitalPinToPort_Teensy(p)
  34. #define GET_PINMODE(pin) (*portModeRegister(pin) & digitalPinToBitMask_DEBUG(pin))
  35. #elif AVR_ATmega2560_FAMILY_PLUS_70 // So we can access/display all the pins on boards using more than 70
  36. #include "pinsDebug_plus_70.h"
  37. #define digitalPinToTimer_DEBUG(p) digitalPinToTimer_plus_70(p)
  38. #define digitalPinToBitMask_DEBUG(p) digitalPinToBitMask_plus_70(p)
  39. #define digitalPinToPort_DEBUG(p) digitalPinToPort_plus_70(p)
  40. bool GET_PINMODE(int8_t pin) {return *portModeRegister(digitalPinToPort_DEBUG(pin)) & digitalPinToBitMask_DEBUG(pin); }
  41. #else
  42. #define digitalPinToTimer_DEBUG(p) digitalPinToTimer(p)
  43. #define digitalPinToBitMask_DEBUG(p) digitalPinToBitMask(p)
  44. #define digitalPinToPort_DEBUG(p) digitalPinToPort(p)
  45. bool GET_PINMODE(int8_t pin) {return *portModeRegister(digitalPinToPort_DEBUG(pin)) & digitalPinToBitMask_DEBUG(pin); }
  46. #define GET_ARRAY_PIN(p) pgm_read_byte(&pin_array[p].pin)
  47. #endif
  48. #define VALID_PIN(pin) (pin >= 0 && pin < NUM_DIGITAL_PINS ? 1 : 0)
  49. #if AVR_ATmega1284_FAMILY
  50. #define DIGITAL_PIN_TO_ANALOG_PIN(P) int(analogInputToDigitalPin(0) - (P))
  51. #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(7) && (P) <= analogInputToDigitalPin(0))
  52. #else
  53. #define DIGITAL_PIN_TO_ANALOG_PIN(P) int((P) - analogInputToDigitalPin(0))
  54. #define IS_ANALOG(P) ((P) >= analogInputToDigitalPin(0) && ((P) <= analogInputToDigitalPin(15) || (P) <= analogInputToDigitalPin(7)))
  55. #endif
  56. #define GET_ARRAY_PIN(p) pgm_read_byte(&pin_array[p].pin)
  57. #define MULTI_NAME_PAD 26 // space needed to be pretty if not first name assigned to a pin
  58. void PRINT_ARRAY_NAME(uint8_t x) {
  59. char *name_mem_pointer = (char*)pgm_read_ptr(&pin_array[x].name);
  60. for (uint8_t y = 0; y < MAX_NAME_LENGTH; y++) {
  61. char temp_char = pgm_read_byte(name_mem_pointer + y);
  62. if (temp_char != 0)
  63. SERIAL_CHAR(temp_char);
  64. else {
  65. for (uint8_t i = 0; i < MAX_NAME_LENGTH - y; i++) SERIAL_CHAR(' ');
  66. break;
  67. }
  68. }
  69. }
  70. #define GET_ARRAY_IS_DIGITAL(x) pgm_read_byte(&pin_array[x].is_digital)
  71. #if defined(__AVR_ATmega1284P__) // 1284 IDE extensions set this to the number of
  72. #undef NUM_DIGITAL_PINS // digital only pins while all other CPUs have it
  73. #define NUM_DIGITAL_PINS 32 // set to digital only + digital/analog
  74. #endif
  75. #define PWM_PRINT(V) do{ sprintf_P(buffer, PSTR("PWM: %4d"), V); SERIAL_ECHO(buffer); }while(0)
  76. #define PWM_CASE(N,Z) \
  77. case TIMER##N##Z: \
  78. if (TCCR##N##A & (_BV(COM##N##Z##1) | _BV(COM##N##Z##0))) { \
  79. PWM_PRINT(OCR##N##Z); \
  80. return true; \
  81. } else return false
  82. /**
  83. * Print a pin's PWM status.
  84. * Return true if it's currently a PWM pin.
  85. */
  86. static bool pwm_status(uint8_t pin) {
  87. char buffer[20]; // for the sprintf statements
  88. switch (digitalPinToTimer_DEBUG(pin)) {
  89. #if defined(TCCR0A) && defined(COM0A1)
  90. #ifdef TIMER0A
  91. #if !AVR_AT90USB1286_FAMILY // not available in Teensyduino type IDEs
  92. PWM_CASE(0, A);
  93. #endif
  94. #endif
  95. PWM_CASE(0, B);
  96. #endif
  97. #if defined(TCCR1A) && defined(COM1A1)
  98. PWM_CASE(1, A);
  99. PWM_CASE(1, B);
  100. #if defined(COM1C1) && defined(TIMER1C)
  101. PWM_CASE(1, C);
  102. #endif
  103. #endif
  104. #if defined(TCCR2A) && defined(COM2A1)
  105. PWM_CASE(2, A);
  106. PWM_CASE(2, B);
  107. #endif
  108. #if defined(TCCR3A) && defined(COM3A1)
  109. PWM_CASE(3, A);
  110. PWM_CASE(3, B);
  111. #ifdef COM3C1
  112. PWM_CASE(3, C);
  113. #endif
  114. #endif
  115. #ifdef TCCR4A
  116. PWM_CASE(4, A);
  117. PWM_CASE(4, B);
  118. PWM_CASE(4, C);
  119. #endif
  120. #if defined(TCCR5A) && defined(COM5A1)
  121. PWM_CASE(5, A);
  122. PWM_CASE(5, B);
  123. PWM_CASE(5, C);
  124. #endif
  125. case NOT_ON_TIMER:
  126. default:
  127. return false;
  128. }
  129. SERIAL_ECHO_SP(2);
  130. } // pwm_status
  131. const volatile uint8_t* const PWM_other[][3] PROGMEM = {
  132. { &TCCR0A, &TCCR0B, &TIMSK0 },
  133. { &TCCR1A, &TCCR1B, &TIMSK1 },
  134. #if defined(TCCR2A) && defined(COM2A1)
  135. { &TCCR2A, &TCCR2B, &TIMSK2 },
  136. #endif
  137. #if defined(TCCR3A) && defined(COM3A1)
  138. { &TCCR3A, &TCCR3B, &TIMSK3 },
  139. #endif
  140. #ifdef TCCR4A
  141. { &TCCR4A, &TCCR4B, &TIMSK4 },
  142. #endif
  143. #if defined(TCCR5A) && defined(COM5A1)
  144. { &TCCR5A, &TCCR5B, &TIMSK5 },
  145. #endif
  146. };
  147. const volatile uint8_t* const PWM_OCR[][3] PROGMEM = {
  148. #ifdef TIMER0A
  149. { &OCR0A, &OCR0B, 0 },
  150. #else
  151. { 0, &OCR0B, 0 },
  152. #endif
  153. #if defined(COM1C1) && defined(TIMER1C)
  154. { (const uint8_t*)&OCR1A, (const uint8_t*)&OCR1B, (const uint8_t*)&OCR1C },
  155. #else
  156. { (const uint8_t*)&OCR1A, (const uint8_t*)&OCR1B, 0 },
  157. #endif
  158. #if defined(TCCR2A) && defined(COM2A1)
  159. { &OCR2A, &OCR2B, 0 },
  160. #endif
  161. #if defined(TCCR3A) && defined(COM3A1)
  162. #ifdef COM3C1
  163. { (const uint8_t*)&OCR3A, (const uint8_t*)&OCR3B, (const uint8_t*)&OCR3C },
  164. #else
  165. { (const uint8_t*)&OCR3A, (const uint8_t*)&OCR3B, 0 },
  166. #endif
  167. #endif
  168. #ifdef TCCR4A
  169. { (const uint8_t*)&OCR4A, (const uint8_t*)&OCR4B, (const uint8_t*)&OCR4C },
  170. #endif
  171. #if defined(TCCR5A) && defined(COM5A1)
  172. { (const uint8_t*)&OCR5A, (const uint8_t*)&OCR5B, (const uint8_t*)&OCR5C },
  173. #endif
  174. };
  175. #define TCCR_A(T) pgm_read_word(&PWM_other[T][0])
  176. #define TCCR_B(T) pgm_read_word(&PWM_other[T][1])
  177. #define TIMSK(T) pgm_read_word(&PWM_other[T][2])
  178. #define CS_0 0
  179. #define CS_1 1
  180. #define CS_2 2
  181. #define WGM_0 0
  182. #define WGM_1 1
  183. #define WGM_2 3
  184. #define WGM_3 4
  185. #define TOIE 0
  186. #define OCR_VAL(T, L) pgm_read_word(&PWM_OCR[T][L])
  187. static void err_is_counter() { SERIAL_ECHOPGM(" non-standard PWM mode"); }
  188. static void err_is_interrupt() { SERIAL_ECHOPGM(" compare interrupt enabled"); }
  189. static void err_prob_interrupt() { SERIAL_ECHOPGM(" overflow interrupt enabled"); }
  190. static void print_is_also_tied() { SERIAL_ECHOPGM(" is also tied to this pin"); SERIAL_ECHO_SP(14); }
  191. inline void com_print(const uint8_t N, const uint8_t Z) {
  192. const uint8_t *TCCRA = (uint8_t*)TCCR_A(N);
  193. SERIAL_ECHOPGM(" COM");
  194. SERIAL_CHAR('0' + N, Z);
  195. SERIAL_ECHOPAIR(": ", int((*TCCRA >> (6 - Z * 2)) & 0x03));
  196. }
  197. void timer_prefix(uint8_t T, char L, uint8_t N) { // T - timer L - pwm N - WGM bit layout
  198. char buffer[20]; // for the sprintf statements
  199. const uint8_t *TCCRB = (uint8_t*)TCCR_B(T),
  200. *TCCRA = (uint8_t*)TCCR_A(T);
  201. uint8_t WGM = (((*TCCRB & _BV(WGM_2)) >> 1) | (*TCCRA & (_BV(WGM_0) | _BV(WGM_1))));
  202. if (N == 4) WGM |= ((*TCCRB & _BV(WGM_3)) >> 1);
  203. SERIAL_ECHOPGM(" TIMER");
  204. SERIAL_CHAR(T + '0', L);
  205. SERIAL_ECHO_SP(3);
  206. if (N == 3) {
  207. const uint8_t *OCRVAL8 = (uint8_t*)OCR_VAL(T, L - 'A');
  208. PWM_PRINT(*OCRVAL8);
  209. }
  210. else {
  211. const uint16_t *OCRVAL16 = (uint16_t*)OCR_VAL(T, L - 'A');
  212. PWM_PRINT(*OCRVAL16);
  213. }
  214. SERIAL_ECHOPAIR(" WGM: ", WGM);
  215. com_print(T,L);
  216. SERIAL_ECHOPAIR(" CS: ", (*TCCRB & (_BV(CS_0) | _BV(CS_1) | _BV(CS_2)) ));
  217. SERIAL_ECHOPGM(" TCCR");
  218. SERIAL_CHAR(T + '0');
  219. SERIAL_ECHOPAIR("A: ", *TCCRA);
  220. SERIAL_ECHOPGM(" TCCR");
  221. SERIAL_CHAR(T + '0');
  222. SERIAL_ECHOPAIR("B: ", *TCCRB);
  223. const uint8_t *TMSK = (uint8_t*)TIMSK(T);
  224. SERIAL_ECHOPGM(" TIMSK");
  225. SERIAL_CHAR(T + '0');
  226. SERIAL_ECHOPAIR(": ", *TMSK);
  227. const uint8_t OCIE = L - 'A' + 1;
  228. if (N == 3) { if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) err_is_counter(); }
  229. else { if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13) err_is_counter(); }
  230. if (TEST(*TMSK, OCIE)) err_is_interrupt();
  231. if (TEST(*TMSK, TOIE)) err_prob_interrupt();
  232. }
  233. static void pwm_details(uint8_t pin) {
  234. switch (digitalPinToTimer_DEBUG(pin)) {
  235. #if defined(TCCR0A) && defined(COM0A1)
  236. #ifdef TIMER0A
  237. #if !AVR_AT90USB1286_FAMILY // not available in Teensyduino type IDEs
  238. case TIMER0A: timer_prefix(0, 'A', 3); break;
  239. #endif
  240. #endif
  241. case TIMER0B: timer_prefix(0, 'B', 3); break;
  242. #endif
  243. #if defined(TCCR1A) && defined(COM1A1)
  244. case TIMER1A: timer_prefix(1, 'A', 4); break;
  245. case TIMER1B: timer_prefix(1, 'B', 4); break;
  246. #if defined(COM1C1) && defined(TIMER1C)
  247. case TIMER1C: timer_prefix(1, 'C', 4); break;
  248. #endif
  249. #endif
  250. #if defined(TCCR2A) && defined(COM2A1)
  251. case TIMER2A: timer_prefix(2, 'A', 3); break;
  252. case TIMER2B: timer_prefix(2, 'B', 3); break;
  253. #endif
  254. #if defined(TCCR3A) && defined(COM3A1)
  255. case TIMER3A: timer_prefix(3, 'A', 4); break;
  256. case TIMER3B: timer_prefix(3, 'B', 4); break;
  257. #ifdef COM3C1
  258. case TIMER3C: timer_prefix(3, 'C', 4); break;
  259. #endif
  260. #endif
  261. #ifdef TCCR4A
  262. case TIMER4A: timer_prefix(4, 'A', 4); break;
  263. case TIMER4B: timer_prefix(4, 'B', 4); break;
  264. case TIMER4C: timer_prefix(4, 'C', 4); break;
  265. #endif
  266. #if defined(TCCR5A) && defined(COM5A1)
  267. case TIMER5A: timer_prefix(5, 'A', 4); break;
  268. case TIMER5B: timer_prefix(5, 'B', 4); break;
  269. case TIMER5C: timer_prefix(5, 'C', 4); break;
  270. #endif
  271. case NOT_ON_TIMER: break;
  272. }
  273. SERIAL_ECHOPGM(" ");
  274. // on pins that have two PWMs, print info on second PWM
  275. #if AVR_ATmega2560_FAMILY || AVR_AT90USB1286_FAMILY
  276. // looking for port B7 - PWMs 0A and 1C
  277. if (digitalPinToPort_DEBUG(pin) == 'B' - 64 && 0x80 == digitalPinToBitMask_DEBUG(pin)) {
  278. #if !AVR_AT90USB1286_FAMILY
  279. SERIAL_ECHOPGM("\n .");
  280. SERIAL_ECHO_SP(18);
  281. SERIAL_ECHOPGM("TIMER1C");
  282. print_is_also_tied();
  283. timer_prefix(1, 'C', 4);
  284. #else
  285. SERIAL_ECHOPGM("\n .");
  286. SERIAL_ECHO_SP(18);
  287. SERIAL_ECHOPGM("TIMER0A");
  288. print_is_also_tied();
  289. timer_prefix(0, 'A', 3);
  290. #endif
  291. }
  292. #else
  293. UNUSED(print_is_also_tied);
  294. #endif
  295. } // pwm_details
  296. #ifndef digitalRead_mod // Use Teensyduino's version of digitalRead - it doesn't disable the PWMs
  297. int digitalRead_mod(const int8_t pin) { // same as digitalRead except the PWM stop section has been removed
  298. const uint8_t port = digitalPinToPort_DEBUG(pin);
  299. return (port != NOT_A_PIN) && (*portInputRegister(port) & digitalPinToBitMask_DEBUG(pin)) ? HIGH : LOW;
  300. }
  301. #endif
  302. #ifndef PRINT_PORT
  303. void print_port(int8_t pin) { // print port number
  304. #ifdef digitalPinToPort_DEBUG
  305. uint8_t x;
  306. SERIAL_ECHOPGM(" Port: ");
  307. #if AVR_AT90USB1286_FAMILY
  308. x = (pin == 46 || pin == 47) ? 'E' : digitalPinToPort_DEBUG(pin) + 64;
  309. #else
  310. x = digitalPinToPort_DEBUG(pin) + 64;
  311. #endif
  312. SERIAL_CHAR(x);
  313. #if AVR_AT90USB1286_FAMILY
  314. if (pin == 46)
  315. x = '2';
  316. else if (pin == 47)
  317. x = '3';
  318. else {
  319. uint8_t temp = digitalPinToBitMask_DEBUG(pin);
  320. for (x = '0'; x < '9' && temp != 1; x++) temp >>= 1;
  321. }
  322. #else
  323. uint8_t temp = digitalPinToBitMask_DEBUG(pin);
  324. for (x = '0'; x < '9' && temp != 1; x++) temp >>= 1;
  325. #endif
  326. SERIAL_CHAR(x);
  327. #else
  328. SERIAL_ECHO_SP(10);
  329. #endif
  330. }
  331. #define PRINT_PORT(p) print_port(p)
  332. #endif
  333. #define PRINT_PIN(p) do{ sprintf_P(buffer, PSTR("%3d "), p); SERIAL_ECHO(buffer); }while(0)