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.

pinsDebug.h 12KB

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