My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Max7219_Debug_LEDs.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. * This module is off by default, but can be enabled to facilitate the display of
  24. * extra debug information during code development. It assumes the existence of a
  25. * Max7219 LED Matrix. A suitable device can be obtained on eBay similar to this:
  26. * http://www.ebay.com/itm/191781645249 for under $2.00 including shipping.
  27. *
  28. * Just connect up +5v and GND to give it power, then connect up the pins assigned
  29. * in Configuration_adv.h. For example, on the Re-ARM you could use:
  30. *
  31. * #define MAX7219_CLK_PIN 77
  32. * #define MAX7219_DIN_PIN 78
  33. * #define MAX7219_LOAD_PIN 79
  34. *
  35. * Max7219_init() is called automatically at startup, and then there are a number of
  36. * support functions available to control the LEDs in the 8x8 grid.
  37. *
  38. * void Max7219_init();
  39. * void Max7219_PutByte(uint8_t data);
  40. * void Max7219(uint8_t reg, uint8_t data);
  41. * void Max7219_LED_On(uint8_t col, uint8_t row);
  42. * void Max7219_LED_Off(uint8_t col, uint8_t row);
  43. * void Max7219_LED_Toggle(uint8_t col, uint8_t row);
  44. * void Max7219_Clear_Row(uint8_t row);
  45. * void Max7219_Clear_Column(uint8_t col);
  46. * void Max7219_Set_Row(uint8_t row, uint8_t val);
  47. * void Max7219_Set_2_Rows(uint8_t row, uint16_t val);
  48. * void Max7219_Set_4_Rows(uint8_t row, uint32_t val);
  49. * void Max7219_Set_Column(uint8_t col, uint8_t val);
  50. * void Max7219_idle_tasks();
  51. */
  52. #include "../inc/MarlinConfig.h"
  53. #if ENABLED(MAX7219_DEBUG)
  54. #include "Max7219_Debug_LEDs.h"
  55. #include "../module/planner.h"
  56. #include "../module/stepper.h"
  57. #include "../Marlin.h"
  58. #include "../HAL/Delay.h"
  59. static uint8_t LEDs[8] = { 0 };
  60. #ifdef CPU_32_BIT
  61. // Approximate a 1µs delay on 32-bit ARM
  62. #define SIG_DELAY() DELAY_US(1)
  63. #else
  64. // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
  65. #define SIG_DELAY() DELAY_NS(188)
  66. #endif
  67. void Max7219_PutByte(uint8_t data) {
  68. for (uint8_t i = 8; i--;) {
  69. SIG_DELAY();
  70. WRITE(MAX7219_CLK_PIN, LOW); // tick
  71. SIG_DELAY();
  72. WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit
  73. SIG_DELAY();
  74. WRITE(MAX7219_CLK_PIN, HIGH); // tock
  75. SIG_DELAY();
  76. data <<= 1;
  77. }
  78. }
  79. void Max7219(const uint8_t reg, const uint8_t data) {
  80. SIG_DELAY();
  81. WRITE(MAX7219_LOAD_PIN, LOW); // begin
  82. SIG_DELAY();
  83. Max7219_PutByte(reg); // specify register
  84. SIG_DELAY();
  85. Max7219_PutByte(data); // put data
  86. SIG_DELAY();
  87. WRITE(MAX7219_LOAD_PIN, LOW); // and tell the chip to load the data
  88. SIG_DELAY();
  89. WRITE(MAX7219_LOAD_PIN, HIGH);
  90. SIG_DELAY();
  91. }
  92. void Max7219_LED_Set(const uint8_t col, const uint8_t row, const bool on) {
  93. if (row > 7 || col > 7) {
  94. SERIAL_ECHOPAIR("??? Max7219_LED_Set(", (int)row);
  95. SERIAL_ECHOPAIR(",", (int)col);
  96. SERIAL_ECHOLNPGM(")");
  97. return;
  98. }
  99. if (TEST(LEDs[col], row) == on) return; // if LED is already on/off, leave alone
  100. if (on) SBI(LEDs[col], row); else CBI(LEDs[col], row);
  101. Max7219(8 - col, LEDs[col]);
  102. }
  103. void Max7219_LED_On(const uint8_t col, const uint8_t row) {
  104. if (row > 7 || col > 7) {
  105. SERIAL_ECHOPAIR("??? Max7219_LED_On(", (int)col);
  106. SERIAL_ECHOPAIR(",", (int)row);
  107. SERIAL_ECHOLNPGM(")");
  108. return;
  109. }
  110. Max7219_LED_Set(col, row, true);
  111. }
  112. void Max7219_LED_Off(const uint8_t col, const uint8_t row) {
  113. if (row > 7 || col > 7) {
  114. SERIAL_ECHOPAIR("??? Max7219_LED_Off(", (int)row);
  115. SERIAL_ECHOPAIR(",", (int)col);
  116. SERIAL_ECHOLNPGM(")");
  117. return;
  118. }
  119. Max7219_LED_Set(col, row, false);
  120. }
  121. void Max7219_LED_Toggle(const uint8_t col, const uint8_t row) {
  122. if (row > 7 || col > 7) {
  123. SERIAL_ECHOPAIR("??? Max7219_LED_Toggle(", (int)row);
  124. SERIAL_ECHOPAIR(",", (int)col);
  125. SERIAL_ECHOLNPGM(")");
  126. return;
  127. }
  128. if (TEST(LEDs[row], col))
  129. Max7219_LED_Off(col, row);
  130. else
  131. Max7219_LED_On(col, row);
  132. }
  133. void Max7219_Clear_Column(const uint8_t col) {
  134. if (col > 7) {
  135. SERIAL_ECHOPAIR("??? Max7219_Clear_Column(", (int)col);
  136. SERIAL_ECHOLNPGM(")");
  137. return;
  138. }
  139. LEDs[col] = 0;
  140. Max7219(8 - col, LEDs[col]);
  141. }
  142. void Max7219_Clear_Row(const uint8_t row) {
  143. if (row > 7) {
  144. SERIAL_ECHOPAIR("??? Max7219_Clear_Row(", (int)row);
  145. SERIAL_ECHOLNPGM(")");
  146. return;
  147. }
  148. for (uint8_t c = 0; c <= 7; c++)
  149. Max7219_LED_Off(c, row);
  150. }
  151. void Max7219_Set_Row(const uint8_t row, const uint8_t val) {
  152. if (row > 7) {
  153. SERIAL_ECHOPAIR("??? Max7219_Set_Row(", (int)row);
  154. SERIAL_ECHOPAIR(",", (int)val);
  155. SERIAL_ECHOLNPGM(")");
  156. return;
  157. }
  158. for (uint8_t b = 0; b <= 7; b++)
  159. if (TEST(val, b))
  160. Max7219_LED_On(7 - b, row);
  161. else
  162. Max7219_LED_Off(7 - b, row);
  163. }
  164. void Max7219_Set_2_Rows(const uint8_t row, const uint16_t val) {
  165. if (row > 6) {
  166. SERIAL_ECHOPAIR("??? Max7219_Set_2_Rows(", (int)row);
  167. SERIAL_ECHOPAIR(",", (int)val);
  168. SERIAL_ECHOLNPGM(")");
  169. return;
  170. }
  171. Max7219_Set_Row(row + 1, (val >> 8) & 0xFF);
  172. Max7219_Set_Row(row + 0, (val ) & 0xFF);
  173. }
  174. void Max7219_Set_4_Rows(const uint8_t row, const uint32_t val) {
  175. if (row > 4) {
  176. SERIAL_ECHOPAIR("??? Max7219_Set_4_Rows(", (int)row);
  177. SERIAL_ECHOPAIR(",", (long)val);
  178. SERIAL_ECHOLNPGM(")");
  179. return;
  180. }
  181. Max7219_Set_Row(row + 3, (val >> 24) & 0xFF);
  182. Max7219_Set_Row(row + 2, (val >> 16) & 0xFF);
  183. Max7219_Set_Row(row + 1, (val >> 8) & 0xFF);
  184. Max7219_Set_Row(row + 0, (val ) & 0xFF);
  185. }
  186. void Max7219_Set_Column(const uint8_t col, const uint8_t val) {
  187. if (col > 7) {
  188. SERIAL_ECHOPAIR("??? Max7219_Column(", (int)col);
  189. SERIAL_ECHOPAIR(",", (int)val);
  190. SERIAL_ECHOLNPGM(")");
  191. return;
  192. }
  193. LEDs[col] = val;
  194. Max7219(8 - col, LEDs[col]);
  195. }
  196. void Max7219_register_setup() {
  197. //initiation of the max 7219
  198. Max7219(max7219_reg_scanLimit, 0x07);
  199. Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  200. Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode
  201. Max7219(max7219_reg_displayTest, 0x00); // no display test
  202. Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set
  203. // range: 0x00 to 0x0F
  204. }
  205. void Max7219_init() {
  206. uint8_t i, x, y;
  207. SET_OUTPUT(MAX7219_DIN_PIN);
  208. SET_OUTPUT(MAX7219_CLK_PIN);
  209. OUT_WRITE(MAX7219_LOAD_PIN, HIGH);
  210. delay(1);
  211. Max7219_register_setup();
  212. for (i = 0; i <= 7; i++) { // empty registers, turn all LEDs off
  213. LEDs[i] = 0x00;
  214. Max7219(i + 1, 0);
  215. }
  216. for (x = 0; x <= 7; x++) // Do an aesthetically pleasing pattern to fully test
  217. for (y = 0; y <= 7; y++) { // the Max7219 module and LEDs. First, turn them
  218. Max7219_LED_On(y, x); // all on.
  219. delay(3);
  220. }
  221. for (x = 0; x <= 7; x++) // Now, turn them all off.
  222. for (y = 0; y <= 7; y++) {
  223. Max7219_LED_Off(y, x);
  224. delay(3); // delay() is OK here. Max7219_init() is only called from
  225. } // setup() and nothing is running yet.
  226. delay(150);
  227. for (x = 8; x--;) // Now, do the same thing from the opposite direction
  228. for (y = 0; y <= 7; y++) {
  229. Max7219_LED_On(y, x);
  230. delay(2);
  231. }
  232. for (x = 8; x--;)
  233. for (y = 0; y <= 7; y++) {
  234. Max7219_LED_Off(y, x);
  235. delay(2);
  236. }
  237. }
  238. /**
  239. * These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes.
  240. * There is very little CPU burden added to the system by displaying information within the idle()
  241. * task.
  242. *
  243. * But with that said, if your debugging can be facilitated by making calls into the library from
  244. * other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED
  245. * or clear a row is not very significant.
  246. */
  247. void Max7219_idle_tasks() {
  248. #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
  249. CRITICAL_SECTION_START;
  250. #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
  251. const uint8_t head = planner.block_buffer_head;
  252. #endif
  253. #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
  254. const uint8_t tail = planner.block_buffer_tail;
  255. #endif
  256. CRITICAL_SECTION_END;
  257. #endif
  258. static uint16_t refresh_cnt = 0; // The Max7219 circuit boards available for several dollars on eBay
  259. if (refresh_cnt++ > 50000) { // are vulnerable to electrical noise, especially with long wires
  260. Max7219_register_setup(); // next to high current wires. If the display becomes corrupted due
  261. Max7219_LED_Toggle(7, 0); // to electrical noise, this will fix it within a couple of seconds.
  262. refresh_cnt = 0;
  263. }
  264. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  265. static millis_t next_blink = 0;
  266. if (ELAPSED(millis(), next_blink)) {
  267. Max7219_LED_Toggle(7, 7);
  268. next_blink = millis() + 750;
  269. }
  270. #endif
  271. #ifdef MAX7219_DEBUG_STEPPER_HEAD
  272. static int16_t last_head_cnt = 0;
  273. if (last_head_cnt != head) {
  274. if (last_head_cnt < 8)
  275. Max7219_LED_Off(MAX7219_DEBUG_STEPPER_HEAD, last_head_cnt);
  276. else
  277. Max7219_LED_Off(MAX7219_DEBUG_STEPPER_HEAD + 1, last_head_cnt - 8);
  278. last_head_cnt = head;
  279. if (head < 8)
  280. Max7219_LED_On(MAX7219_DEBUG_STEPPER_HEAD, head);
  281. else
  282. Max7219_LED_On(MAX7219_DEBUG_STEPPER_HEAD + 1, head - 8);
  283. }
  284. #endif
  285. #ifdef MAX7219_DEBUG_STEPPER_TAIL
  286. static int16_t last_tail_cnt = 0;
  287. if (last_tail_cnt != tail) {
  288. if (last_tail_cnt < 8)
  289. Max7219_LED_Off(MAX7219_DEBUG_STEPPER_TAIL, last_tail_cnt);
  290. else
  291. Max7219_LED_Off(MAX7219_DEBUG_STEPPER_TAIL + 1, last_tail_cnt - 8);
  292. last_tail_cnt = tail;
  293. if (tail < 8)
  294. Max7219_LED_On(MAX7219_DEBUG_STEPPER_TAIL, tail);
  295. else
  296. Max7219_LED_On(MAX7219_DEBUG_STEPPER_TAIL + 1, tail - 8);
  297. }
  298. #endif
  299. #ifdef MAX7219_DEBUG_STEPPER_QUEUE
  300. static int16_t last_depth = 0;
  301. int16_t current_depth = head - tail;
  302. if (current_depth != last_depth) { // usually, no update will be needed.
  303. if (current_depth < 0) current_depth += BLOCK_BUFFER_SIZE;
  304. NOMORE(current_depth, BLOCK_BUFFER_SIZE);
  305. NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines
  306. // of LEDs is enough to see if the buffer is draining
  307. const uint8_t st = MIN(current_depth, last_depth),
  308. en = MAX(current_depth, last_depth);
  309. if (current_depth < last_depth)
  310. for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs
  311. Max7219_LED_Off(MAX7219_DEBUG_STEPPER_QUEUE + (i & 1), i / 2);
  312. else
  313. for (uint8_t i = st; i <= en; i++) // set the LEDs to current depth
  314. Max7219_LED_On(MAX7219_DEBUG_STEPPER_QUEUE + (i & 1), i / 2);
  315. last_depth = current_depth;
  316. }
  317. #endif
  318. }
  319. #endif // MAX7219_DEBUG