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_AVR_8_bit.h 12KB

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