My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Max7219_Debug_LEDs.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "MarlinConfig.h"
  53. #if ENABLED(MAX7219_DEBUG)
  54. #include "Max7219_Debug_LEDs.h"
  55. #include "planner.h"
  56. #include "stepper.h"
  57. #include "Marlin.h"
  58. static uint8_t LEDs[8] = { 0 };
  59. void Max7219_PutByte(uint8_t data) {
  60. CRITICAL_SECTION_START
  61. for (uint8_t i = 8; i--;) {
  62. #ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed
  63. delayMicroseconds(5); // to let the signal wires stabilize.
  64. WRITE(MAX7219_CLK_PIN, LOW); // tick
  65. delayMicroseconds(5);
  66. WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit
  67. delayMicroseconds(5);
  68. WRITE(MAX7219_CLK_PIN, HIGH); // tock
  69. delayMicroseconds(5);
  70. #else
  71. WRITE(MAX7219_CLK_PIN, LOW); // tick
  72. WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit
  73. WRITE(MAX7219_CLK_PIN, HIGH); // tock
  74. #endif
  75. data <<= 1;
  76. }
  77. CRITICAL_SECTION_END
  78. }
  79. void Max7219(const uint8_t reg, const uint8_t data) {
  80. #ifdef CPU_32_BIT
  81. delayMicroseconds(5);
  82. #endif
  83. CRITICAL_SECTION_START
  84. WRITE(MAX7219_LOAD_PIN, LOW); // begin
  85. #ifdef CPU_32_BIT // The 32-bit processors are so fast, a small delay in the code is needed
  86. delayMicroseconds(5); // to let the signal wires stabilize.
  87. #endif
  88. Max7219_PutByte(reg); // specify register
  89. #ifdef CPU_32_BIT
  90. delayMicroseconds(5);
  91. #endif
  92. Max7219_PutByte(data); // put data
  93. #ifdef CPU_32_BIT
  94. delayMicroseconds(5);
  95. #endif
  96. WRITE(MAX7219_LOAD_PIN, LOW); // and tell the chip to load the data
  97. #ifdef CPU_32_BIT
  98. delayMicroseconds(5);
  99. #endif
  100. WRITE(MAX7219_LOAD_PIN, HIGH);
  101. CRITICAL_SECTION_END
  102. #ifdef CPU_32_BIT
  103. delayMicroseconds(5);
  104. #endif
  105. }
  106. void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on) {
  107. if (row > 7 || col > 7) {
  108. int r,c;
  109. r = row;
  110. c = col;
  111. SERIAL_ECHOPAIR("??? Max7219_LED_Set(",r);
  112. SERIAL_ECHOPAIR(",",c);
  113. SERIAL_ECHO(")\n");
  114. return;
  115. }
  116. if (TEST(LEDs[row], col) == on) return; // if LED is already on/off, leave alone
  117. if (on) SBI(LEDs[row], col); else CBI(LEDs[row], col);
  118. Max7219(8 - row, LEDs[row]);
  119. }
  120. void Max7219_LED_On(const uint8_t col, const uint8_t row) {
  121. if (row > 7 || col > 7) {
  122. int r,c;
  123. r = row;
  124. c = col;
  125. SERIAL_ECHOPAIR("??? Max7219_LED_On(",c);
  126. SERIAL_ECHOPAIR(",",r);
  127. SERIAL_ECHO(")\n");
  128. return;
  129. }
  130. Max7219_LED_Set(col, row, true);
  131. }
  132. void Max7219_LED_Off(const uint8_t col, const uint8_t row) {
  133. if (row > 7 || col > 7) {
  134. int r,c;
  135. r = row;
  136. c = col;
  137. SERIAL_ECHOPAIR("??? Max7219_LED_Off(",r);
  138. SERIAL_ECHOPAIR(",",c);
  139. SERIAL_ECHO(")\n");
  140. return;
  141. }
  142. Max7219_LED_Set(col, row, false);
  143. }
  144. void Max7219_LED_Toggle(const uint8_t col, const uint8_t row) {
  145. if (row > 7 || col > 7) {
  146. int r,c;
  147. r = row;
  148. c = col;
  149. SERIAL_ECHOPAIR("??? Max7219_LED_Toggle(",r);
  150. SERIAL_ECHOPAIR(",",c);
  151. SERIAL_ECHO(")\n");
  152. return;
  153. }
  154. if (TEST(LEDs[row], col))
  155. Max7219_LED_Off(col, row);
  156. else
  157. Max7219_LED_On(col, row);
  158. }
  159. void Max7219_Clear_Column(const uint8_t col) {
  160. if (col > 7) {
  161. int c;
  162. c = col;
  163. SERIAL_ECHOPAIR("??? Max7219_Clear_Column(",c);
  164. SERIAL_ECHO(")\n");
  165. return;
  166. }
  167. LEDs[col] = 0;
  168. Max7219(8 - col, LEDs[col]);
  169. }
  170. void Max7219_Clear_Row(const uint8_t row) {
  171. if (row > 7) {
  172. int r;
  173. r = row;
  174. SERIAL_ECHOPAIR("??? Max7219_Clear_Row(",r);
  175. SERIAL_ECHO(")\n");
  176. return;
  177. }
  178. for (uint8_t c = 0; c <= 7; c++)
  179. Max7219_LED_Off(c, row);
  180. }
  181. void Max7219_Set_Row(const uint8_t row, const uint8_t val) {
  182. if (row > 7 || val>255) {
  183. int r, v;
  184. r = row;
  185. v = val;
  186. SERIAL_ECHOPAIR("??? Max7219_Set_Row(",r);
  187. SERIAL_ECHOPAIR(",",v);
  188. SERIAL_ECHO(")\n");
  189. return;
  190. }
  191. for (uint8_t b = 0; b <= 7; b++)
  192. if (TEST(val, b))
  193. Max7219_LED_On(7 - b, row);
  194. else
  195. Max7219_LED_Off(7 - b, row);
  196. }
  197. void Max7219_Set_2_Rows(const uint8_t row, const uint16_t val) {
  198. if (row > 6 || val>65535) {
  199. int r, v;
  200. r = row;
  201. v = val;
  202. SERIAL_ECHOPAIR("??? Max7219_Set_2_Rows(",r);
  203. SERIAL_ECHOPAIR(",",v);
  204. SERIAL_ECHO(")\n");
  205. return;
  206. }
  207. Max7219_Set_Row(row+1, (val & 0xff00) >> 8 );
  208. Max7219_Set_Row(row+0, (val & 0xff));
  209. }
  210. void Max7219_Set_4_Rows(const uint8_t row, const uint32_t val) {
  211. if (row > 4 ) {
  212. int r;
  213. long v;
  214. r = row;
  215. v = val;
  216. SERIAL_ECHOPAIR("??? Max7219_Set_4_Rows(",r);
  217. SERIAL_ECHOPAIR(",",v);
  218. SERIAL_ECHO(")\n");
  219. return;
  220. }
  221. Max7219_Set_Row(row+3, (val & 0xff000000) >> 24);
  222. Max7219_Set_Row(row+2, (val & 0xff0000) >> 16);
  223. Max7219_Set_Row(row+1, (val & 0xff00) >> 8);
  224. Max7219_Set_Row(row+0, (val & 0xff));
  225. }
  226. void Max7219_Set_Column(const uint8_t col, const uint8_t val) {
  227. if (val > 255 || col > 7) {
  228. int v,c;
  229. v = val;
  230. c = col;
  231. SERIAL_ECHOPAIR("??? Max7219_Column(",c);
  232. SERIAL_ECHOPAIR(",",v);
  233. SERIAL_ECHO(")\n");
  234. return;
  235. }
  236. LEDs[col] = val;
  237. Max7219(8 - col, LEDs[col]);
  238. }
  239. void Max7219_init() {
  240. uint8_t i, x, y;
  241. SET_OUTPUT(MAX7219_DIN_PIN);
  242. SET_OUTPUT(MAX7219_CLK_PIN);
  243. OUT_WRITE(MAX7219_LOAD_PIN, HIGH);
  244. delay(1);
  245. //initiation of the max 7219
  246. Max7219(max7219_reg_scanLimit, 0x07);
  247. Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  248. Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode
  249. Max7219(max7219_reg_displayTest, 0x00); // no display test
  250. Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set
  251. // range: 0x00 to 0x0F
  252. for (i = 0; i <= 7; i++) { // empty registers, turn all LEDs off
  253. LEDs[i] = 0x00;
  254. Max7219(i + 1, 0);
  255. }
  256. for (x = 0; x <= 7; x++) // Do an aesthetically pleasing pattern to fully test
  257. for (y = 0; y <= 7; y++) { // the Max7219 module and LEDs. First, turn them
  258. Max7219_LED_On(x, y); // all on.
  259. delay(3);
  260. }
  261. for (x = 0; x <= 7; x++) // Now, turn them all off.
  262. for (y = 0; y <= 7; y++) {
  263. Max7219_LED_Off(x, y);
  264. delay(3); // delay() is OK here. Max7219_init() is only called from
  265. } // setup() and nothing is running yet.
  266. delay(150);
  267. for (x = 8; x--;) // Now, do the same thing from the opposite direction
  268. for (y = 0; y <= 7; y++) {
  269. Max7219_LED_On(x, y);
  270. delay(2);
  271. }
  272. for (x = 8; x--;)
  273. for (y = 0; y <= 7; y++) {
  274. Max7219_LED_Off(x, y);
  275. delay(2);
  276. }
  277. }
  278. /**
  279. * These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes.
  280. * There is very little CPU burden added to the system by displaying information within the idle()
  281. * task.
  282. *
  283. * But with that said, if your debugging can be facilitated by making calls into the library from
  284. * other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED
  285. * or clear a row is not very significant.
  286. */
  287. void Max7219_idle_tasks() {
  288. #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
  289. CRITICAL_SECTION_START
  290. #if MAX7219_DEBUG_STEPPER_HEAD || MAX7219_DEBUG_STEPPER_QUEUE
  291. uint8_t head;
  292. head = planner.block_buffer_head;
  293. #endif
  294. #if MAX7219_DEBUG_STEPPER_TAIL || MAX7219_DEBUG_STEPPER_QUEUE
  295. uint8_t tail;
  296. tail = planner.block_buffer_tail;
  297. #endif
  298. CRITICAL_SECTION_END
  299. #endif
  300. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  301. static millis_t next_blink = 0;
  302. if (ELAPSED(millis(), next_blink)) {
  303. Max7219_LED_Toggle(7, 7);
  304. next_blink = millis() + 750;
  305. }
  306. #endif
  307. #ifdef MAX7219_DEBUG_STEPPER_HEAD
  308. static int16_t last_head_cnt=0;
  309. if (last_head_cnt != head) {
  310. if ( last_head_cnt < 8)
  311. Max7219_LED_Off( last_head_cnt, MAX7219_DEBUG_STEPPER_HEAD);
  312. else
  313. Max7219_LED_Off( last_head_cnt-8, MAX7219_DEBUG_STEPPER_HEAD+1);
  314. last_head_cnt = head;
  315. if ( head < 8)
  316. Max7219_LED_On(head, MAX7219_DEBUG_STEPPER_HEAD);
  317. else
  318. Max7219_LED_On(head-8, MAX7219_DEBUG_STEPPER_HEAD+1);
  319. }
  320. #endif
  321. #ifdef MAX7219_DEBUG_STEPPER_TAIL
  322. static int16_t last_tail_cnt=0;
  323. if (last_tail_cnt != tail) {
  324. if ( last_tail_cnt < 8)
  325. Max7219_LED_Off( last_tail_cnt, MAX7219_DEBUG_STEPPER_TAIL);
  326. else
  327. Max7219_LED_Off( last_tail_cnt-8, MAX7219_DEBUG_STEPPER_TAIL+1);
  328. last_tail_cnt = tail;
  329. if ( tail < 8)
  330. Max7219_LED_On(tail, MAX7219_DEBUG_STEPPER_TAIL);
  331. else
  332. Max7219_LED_On(tail-8, MAX7219_DEBUG_STEPPER_TAIL+1);
  333. }
  334. #endif
  335. #ifdef MAX7219_DEBUG_STEPPER_QUEUE
  336. static int16_t last_depth = 0;
  337. int16_t current_depth = head - tail;
  338. if (current_depth != last_depth) { // usually, no update will be needed.
  339. if (current_depth < 0) current_depth += BLOCK_BUFFER_SIZE;
  340. NOMORE(current_depth, BLOCK_BUFFER_SIZE);
  341. NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines
  342. // of LEDs is enough to see if the buffer is draining
  343. const uint8_t st = min(current_depth, last_depth),
  344. en = max(current_depth, last_depth);
  345. if (current_depth < last_depth)
  346. for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs
  347. Max7219_LED_Off(i/2, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1));
  348. else
  349. for (uint8_t i = st; i <= en; i++) // set the LEDs to current depth
  350. Max7219_LED_On(i/2, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1));
  351. last_depth = current_depth;
  352. }
  353. #endif
  354. }
  355. #endif // MAX7219_DEBUG