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