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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. #pragma once
  23. /**
  24. * PWM print routines for Atmel 8 bit AVR CPUs
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS
  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. for (uint8_t y = 0; y < MAX_NAME_LENGTH; y++) {
  63. char temp_char = pgm_read_byte(name_mem_pointer + y);
  64. if (temp_char != 0)
  65. SERIAL_CHAR(temp_char);
  66. else {
  67. for (uint8_t i = 0; i < MAX_NAME_LENGTH - y; i++) 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. void com_print(uint8_t N, uint8_t Z) {
  194. const uint8_t *TCCRA = (uint8_t*)TCCR_A(N);
  195. SERIAL_ECHOPGM(" COM");
  196. SERIAL_CHAR(N + '0');
  197. switch (Z) {
  198. case 'A':
  199. SERIAL_ECHOPAIR("A: ", ((*TCCRA & (_BV(7) | _BV(6))) >> 6));
  200. break;
  201. case 'B':
  202. SERIAL_ECHOPAIR("B: ", ((*TCCRA & (_BV(5) | _BV(4))) >> 4));
  203. break;
  204. case 'C':
  205. SERIAL_ECHOPAIR("C: ", ((*TCCRA & (_BV(3) | _BV(2))) >> 2));
  206. break;
  207. }
  208. }
  209. void timer_prefix(uint8_t T, char L, uint8_t N) { // T - timer L - pwm N - WGM bit layout
  210. char buffer[20]; // for the sprintf statements
  211. const uint8_t *TCCRB = (uint8_t*)TCCR_B(T),
  212. *TCCRA = (uint8_t*)TCCR_A(T);
  213. uint8_t WGM = (((*TCCRB & _BV(WGM_2)) >> 1) | (*TCCRA & (_BV(WGM_0) | _BV(WGM_1))));
  214. if (N == 4) WGM |= ((*TCCRB & _BV(WGM_3)) >> 1);
  215. SERIAL_ECHOPGM(" TIMER");
  216. SERIAL_CHAR(T + '0');
  217. SERIAL_CHAR(L);
  218. SERIAL_ECHO_SP(3);
  219. if (N == 3) {
  220. const uint8_t *OCRVAL8 = (uint8_t*)OCR_VAL(T, L - 'A');
  221. PWM_PRINT(*OCRVAL8);
  222. }
  223. else {
  224. const uint16_t *OCRVAL16 = (uint16_t*)OCR_VAL(T, L - 'A');
  225. PWM_PRINT(*OCRVAL16);
  226. }
  227. SERIAL_ECHOPAIR(" WGM: ", WGM);
  228. com_print(T,L);
  229. SERIAL_ECHOPAIR(" CS: ", (*TCCRB & (_BV(CS_0) | _BV(CS_1) | _BV(CS_2)) ));
  230. SERIAL_ECHOPGM(" TCCR");
  231. SERIAL_CHAR(T + '0');
  232. SERIAL_ECHOPAIR("A: ", *TCCRA);
  233. SERIAL_ECHOPGM(" TCCR");
  234. SERIAL_CHAR(T + '0');
  235. SERIAL_ECHOPAIR("B: ", *TCCRB);
  236. const uint8_t *TMSK = (uint8_t*)TIMSK(T);
  237. SERIAL_ECHOPGM(" TIMSK");
  238. SERIAL_CHAR(T + '0');
  239. SERIAL_ECHOPAIR(": ", *TMSK);
  240. const uint8_t OCIE = L - 'A' + 1;
  241. if (N == 3) { if (WGM == 0 || WGM == 2 || WGM == 4 || WGM == 6) err_is_counter(); }
  242. else { if (WGM == 0 || WGM == 4 || WGM == 12 || WGM == 13) err_is_counter(); }
  243. if (TEST(*TMSK, OCIE)) err_is_interrupt();
  244. if (TEST(*TMSK, TOIE)) err_prob_interrupt();
  245. }
  246. static void pwm_details(uint8_t pin) {
  247. switch (digitalPinToTimer_DEBUG(pin)) {
  248. #if defined(TCCR0A) && defined(COM0A1)
  249. #ifdef TIMER0A
  250. #if !AVR_AT90USB1286_FAMILY // not available in Teensyduino type IDEs
  251. case TIMER0A: timer_prefix(0, 'A', 3); break;
  252. #endif
  253. #endif
  254. case TIMER0B: timer_prefix(0, 'B', 3); break;
  255. #endif
  256. #if defined(TCCR1A) && defined(COM1A1)
  257. case TIMER1A: timer_prefix(1, 'A', 4); break;
  258. case TIMER1B: timer_prefix(1, 'B', 4); break;
  259. #if defined(COM1C1) && defined(TIMER1C)
  260. case TIMER1C: timer_prefix(1, 'C', 4); break;
  261. #endif
  262. #endif
  263. #if defined(TCCR2A) && defined(COM2A1)
  264. case TIMER2A: timer_prefix(2, 'A', 3); break;
  265. case TIMER2B: timer_prefix(2, 'B', 3); break;
  266. #endif
  267. #if defined(TCCR3A) && defined(COM3A1)
  268. case TIMER3A: timer_prefix(3, 'A', 4); break;
  269. case TIMER3B: timer_prefix(3, 'B', 4); break;
  270. #ifdef COM3C1
  271. case TIMER3C: timer_prefix(3, 'C', 4); break;
  272. #endif
  273. #endif
  274. #ifdef TCCR4A
  275. case TIMER4A: timer_prefix(4, 'A', 4); break;
  276. case TIMER4B: timer_prefix(4, 'B', 4); break;
  277. case TIMER4C: timer_prefix(4, 'C', 4); break;
  278. #endif
  279. #if defined(TCCR5A) && defined(COM5A1)
  280. case TIMER5A: timer_prefix(5, 'A', 4); break;
  281. case TIMER5B: timer_prefix(5, 'B', 4); break;
  282. case TIMER5C: timer_prefix(5, 'C', 4); break;
  283. #endif
  284. case NOT_ON_TIMER: break;
  285. }
  286. SERIAL_ECHOPGM(" ");
  287. // on pins that have two PWMs, print info on second PWM
  288. #if AVR_ATmega2560_FAMILY || AVR_AT90USB1286_FAMILY
  289. // looking for port B7 - PWMs 0A and 1C
  290. if (digitalPinToPort_DEBUG(pin) == 'B' - 64 && 0x80 == digitalPinToBitMask_DEBUG(pin)) {
  291. #if !AVR_AT90USB1286_FAMILY
  292. SERIAL_ECHOPGM("\n .");
  293. SERIAL_ECHO_SP(18);
  294. SERIAL_ECHOPGM("TIMER1C");
  295. print_is_also_tied();
  296. timer_prefix(1, 'C', 4);
  297. #else
  298. SERIAL_ECHOPGM("\n .");
  299. SERIAL_ECHO_SP(18);
  300. SERIAL_ECHOPGM("TIMER0A");
  301. print_is_also_tied();
  302. timer_prefix(0, 'A', 3);
  303. #endif
  304. }
  305. #endif
  306. } // pwm_details
  307. #ifndef digitalRead_mod // Use Teensyduino's version of digitalRead - it doesn't disable the PWMs
  308. int digitalRead_mod(const int8_t pin) { // same as digitalRead except the PWM stop section has been removed
  309. const uint8_t port = digitalPinToPort_DEBUG(pin);
  310. return (port != NOT_A_PIN) && (*portInputRegister(port) & digitalPinToBitMask_DEBUG(pin)) ? HIGH : LOW;
  311. }
  312. #endif
  313. #ifndef PRINT_PORT
  314. void print_port(int8_t pin) { // print port number
  315. #ifdef digitalPinToPort_DEBUG
  316. uint8_t x;
  317. SERIAL_ECHOPGM(" Port: ");
  318. #if AVR_AT90USB1286_FAMILY
  319. x = (pin == 46 || pin == 47) ? 'E' : digitalPinToPort_DEBUG(pin) + 64;
  320. #else
  321. x = digitalPinToPort_DEBUG(pin) + 64;
  322. #endif
  323. SERIAL_CHAR(x);
  324. #if AVR_AT90USB1286_FAMILY
  325. if (pin == 46)
  326. x = '2';
  327. else if (pin == 47)
  328. x = '3';
  329. else {
  330. uint8_t temp = digitalPinToBitMask_DEBUG(pin);
  331. for (x = '0'; x < '9' && temp != 1; x++) temp >>= 1;
  332. }
  333. #else
  334. uint8_t temp = digitalPinToBitMask_DEBUG(pin);
  335. for (x = '0'; x < '9' && temp != 1; x++) temp >>= 1;
  336. #endif
  337. SERIAL_CHAR(x);
  338. #else
  339. SERIAL_ECHO_SP(10);
  340. #endif
  341. }
  342. #define PRINT_PORT(p) print_port(p)
  343. #endif
  344. #define PRINT_PIN(p) do {sprintf_P(buffer, PSTR("%3d "), p); SERIAL_ECHO(buffer);} while (0)