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

Max7219_Debug_LEDs.cpp 11KB

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