My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Max7219_Debug_LEDs.cpp 11KB

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