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.

direct_stepping.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #if ENABLED(DIRECT_STEPPING)
  24. #include "direct_stepping.h"
  25. #include "../MarlinCore.h"
  26. #define CHECK_PAGE(I, R) do{ \
  27. if (I >= sizeof(page_states) / sizeof(page_states[0])) { \
  28. fatal_error = true; \
  29. return R; \
  30. } \
  31. }while(0)
  32. #define CHECK_PAGE_STATE(I, R, S) do { \
  33. CHECK_PAGE(I, R); \
  34. if (page_states[I] != S) { \
  35. fatal_error = true; \
  36. return R; \
  37. } \
  38. }while(0)
  39. namespace DirectStepping {
  40. template<typename Cfg>
  41. State SerialPageManager<Cfg>::state;
  42. template<typename Cfg>
  43. volatile bool SerialPageManager<Cfg>::fatal_error;
  44. template<typename Cfg>
  45. volatile PageState SerialPageManager<Cfg>::page_states[Cfg::NUM_PAGES];
  46. template<typename Cfg>
  47. volatile bool SerialPageManager<Cfg>::page_states_dirty;
  48. template<typename Cfg>
  49. uint8_t SerialPageManager<Cfg>::pages[Cfg::NUM_PAGES][Cfg::PAGE_SIZE];
  50. template<typename Cfg>
  51. uint8_t SerialPageManager<Cfg>::checksum;
  52. template<typename Cfg>
  53. typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_byte_idx;
  54. template<typename Cfg>
  55. typename Cfg::page_idx_t SerialPageManager<Cfg>::write_page_idx;
  56. template<typename Cfg>
  57. typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_page_size;
  58. template <typename Cfg>
  59. void SerialPageManager<Cfg>::init() {
  60. for (int i = 0 ; i < Cfg::NUM_PAGES ; i++)
  61. page_states[i] = PageState::FREE;
  62. fatal_error = false;
  63. state = State::NEWLINE;
  64. page_states_dirty = false;
  65. SERIAL_ECHOLNPGM("pages_ready");
  66. }
  67. template<typename Cfg>
  68. FORCE_INLINE bool SerialPageManager<Cfg>::maybe_store_rxd_char(uint8_t c) {
  69. switch (state) {
  70. default:
  71. case State::MONITOR:
  72. switch (c) {
  73. case '\n':
  74. case '\r':
  75. state = State::NEWLINE;
  76. default:
  77. return false;
  78. }
  79. case State::NEWLINE:
  80. switch (c) {
  81. case Cfg::CONTROL_CHAR:
  82. state = State::ADDRESS;
  83. return true;
  84. case '\n':
  85. case '\r':
  86. state = State::NEWLINE;
  87. return false;
  88. default:
  89. state = State::MONITOR;
  90. return false;
  91. }
  92. case State::ADDRESS:
  93. //TODO: 16 bit address, State::ADDRESS2
  94. write_page_idx = c;
  95. write_byte_idx = 0;
  96. checksum = 0;
  97. CHECK_PAGE(write_page_idx, true);
  98. if (page_states[write_page_idx] == PageState::FAIL) {
  99. // Special case for fail
  100. state = State::UNFAIL;
  101. return true;
  102. }
  103. set_page_state(write_page_idx, PageState::WRITING);
  104. state = Cfg::DIRECTIONAL ? State::COLLECT : State::SIZE;
  105. return true;
  106. case State::SIZE:
  107. // Zero means full page size
  108. write_page_size = c;
  109. state = State::COLLECT;
  110. return true;
  111. case State::COLLECT:
  112. pages[write_page_idx][write_byte_idx++] = c;
  113. checksum ^= c;
  114. // check if still collecting
  115. if (Cfg::PAGE_SIZE == 256) {
  116. // special case for 8-bit, check if rolled back to 0
  117. if (Cfg::DIRECTIONAL || !write_page_size) { // full 256 bytes
  118. if (write_byte_idx) return true;
  119. } else {
  120. if (write_byte_idx < write_page_size) return true;
  121. }
  122. } else if (Cfg::DIRECTIONAL) {
  123. if (write_byte_idx != Cfg::PAGE_SIZE) return true;
  124. } else {
  125. if (write_byte_idx < write_page_size) return true;
  126. }
  127. state = State::CHECKSUM;
  128. return true;
  129. case State::CHECKSUM: {
  130. const PageState page_state = (checksum == c) ? PageState::OK : PageState::FAIL;
  131. set_page_state(write_page_idx, page_state);
  132. state = State::MONITOR;
  133. return true;
  134. }
  135. case State::UNFAIL:
  136. if (c == 0) {
  137. set_page_state(write_page_idx, PageState::FREE);
  138. } else {
  139. fatal_error = true;
  140. }
  141. state = State::MONITOR;
  142. return true;
  143. }
  144. }
  145. template <typename Cfg>
  146. void SerialPageManager<Cfg>::write_responses() {
  147. if (fatal_error) {
  148. kill(GET_TEXT(MSG_BAD_PAGE));
  149. return;
  150. }
  151. if (!page_states_dirty) return;
  152. page_states_dirty = false;
  153. SERIAL_ECHO(Cfg::CONTROL_CHAR);
  154. constexpr int state_bits = 2;
  155. constexpr int n_bytes = Cfg::NUM_PAGES >> state_bits;
  156. volatile uint8_t bits_b[n_bytes] = { 0 };
  157. for (page_idx_t i = 0 ; i < Cfg::NUM_PAGES ; i++) {
  158. bits_b[i >> state_bits] |= page_states[i] << ((i * state_bits) & 0x7);
  159. }
  160. uint8_t crc = 0;
  161. for (uint8_t i = 0 ; i < n_bytes ; i++) {
  162. crc ^= bits_b[i];
  163. SERIAL_ECHO(bits_b[i]);
  164. }
  165. SERIAL_ECHO(crc);
  166. SERIAL_EOL();
  167. }
  168. template <typename Cfg>
  169. FORCE_INLINE void SerialPageManager<Cfg>::set_page_state(const page_idx_t page_idx, const PageState page_state) {
  170. CHECK_PAGE(page_idx,);
  171. page_states[page_idx] = page_state;
  172. page_states_dirty = true;
  173. }
  174. template <>
  175. FORCE_INLINE uint8_t *PageManager::get_page(const page_idx_t page_idx) {
  176. CHECK_PAGE(page_idx, nullptr);
  177. return pages[page_idx];
  178. }
  179. template <>
  180. FORCE_INLINE void PageManager::free_page(const page_idx_t page_idx) {
  181. set_page_state(page_idx, PageState::FREE);
  182. }
  183. };
  184. DirectStepping::PageManager page_manager;
  185. const uint8_t segment_table[DirectStepping::Config::NUM_SEGMENTS][DirectStepping::Config::SEGMENT_STEPS] PROGMEM = {
  186. #if STEPPER_PAGE_FORMAT == SP_4x4D_128
  187. { 1, 1, 1, 1, 1, 1, 1 }, // 0 = -7
  188. { 1, 1, 1, 0, 1, 1, 1 }, // 1 = -6
  189. { 1, 1, 1, 0, 1, 0, 1 }, // 2 = -5
  190. { 1, 1, 0, 1, 0, 1, 0 }, // 3 = -4
  191. { 1, 1, 0, 0, 1, 0, 0 }, // 4 = -3
  192. { 0, 0, 1, 0, 0, 0, 1 }, // 5 = -2
  193. { 0, 0, 0, 1, 0, 0, 0 }, // 6 = -1
  194. { 0, 0, 0, 0, 0, 0, 0 }, // 7 = 0
  195. { 0, 0, 0, 1, 0, 0, 0 }, // 8 = 1
  196. { 0, 0, 1, 0, 0, 0, 1 }, // 9 = 2
  197. { 1, 1, 0, 0, 1, 0, 0 }, // 10 = 3
  198. { 1, 1, 0, 1, 0, 1, 0 }, // 11 = 4
  199. { 1, 1, 1, 0, 1, 0, 1 }, // 12 = 5
  200. { 1, 1, 1, 0, 1, 1, 1 }, // 13 = 6
  201. { 1, 1, 1, 1, 1, 1, 1 }, // 14 = 7
  202. { 0 }
  203. #elif STEPPER_PAGE_FORMAT == SP_4x2_256
  204. { 0, 0, 0 }, // 0
  205. { 0, 1, 0 }, // 1
  206. { 1, 0, 1 }, // 2
  207. { 1, 1, 1 }, // 3
  208. #elif STEPPER_PAGE_FORMAT == SP_4x1_512
  209. {0} // Uncompressed format, table not used
  210. #endif
  211. };
  212. #endif // DIRECT_STEPPING