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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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::PAGE_COUNT];
  46. template<typename Cfg>
  47. volatile bool SerialPageManager<Cfg>::page_states_dirty;
  48. template<typename Cfg>
  49. uint8_t SerialPageManager<Cfg>::pages[Cfg::PAGE_COUNT][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::PAGE_COUNT ; 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. }
  120. else if (write_byte_idx < write_page_size)
  121. return true;
  122. }
  123. else if (Cfg::DIRECTIONAL) {
  124. if (write_byte_idx != Cfg::PAGE_SIZE)
  125. return true;
  126. }
  127. else if (write_byte_idx < write_page_size)
  128. return true;
  129. state = State::CHECKSUM;
  130. return true;
  131. case State::CHECKSUM: {
  132. const PageState page_state = (checksum == c) ? PageState::OK : PageState::FAIL;
  133. set_page_state(write_page_idx, page_state);
  134. state = State::MONITOR;
  135. return true;
  136. }
  137. case State::UNFAIL:
  138. if (c == 0)
  139. set_page_state(write_page_idx, PageState::FREE);
  140. else
  141. fatal_error = true;
  142. state = State::MONITOR;
  143. return true;
  144. }
  145. }
  146. template <typename Cfg>
  147. void SerialPageManager<Cfg>::write_responses() {
  148. if (fatal_error) {
  149. kill(GET_TEXT_F(MSG_BAD_PAGE));
  150. return;
  151. }
  152. if (!page_states_dirty) return;
  153. page_states_dirty = false;
  154. SERIAL_CHAR(Cfg::CONTROL_CHAR);
  155. constexpr int state_bits = 2;
  156. constexpr int n_bytes = Cfg::PAGE_COUNT >> state_bits;
  157. volatile uint8_t bits_b[n_bytes] = { 0 };
  158. for (page_idx_t i = 0 ; i < Cfg::PAGE_COUNT ; i++) {
  159. bits_b[i >> state_bits] |= page_states[i] << ((i * state_bits) & 0x7);
  160. }
  161. uint8_t crc = 0;
  162. for (uint8_t i = 0 ; i < n_bytes ; i++) {
  163. crc ^= bits_b[i];
  164. SERIAL_CHAR(bits_b[i]);
  165. }
  166. SERIAL_CHAR(crc);
  167. SERIAL_EOL();
  168. }
  169. template <typename Cfg>
  170. FORCE_INLINE void SerialPageManager<Cfg>::set_page_state(const page_idx_t page_idx, const PageState page_state) {
  171. CHECK_PAGE(page_idx,);
  172. page_states[page_idx] = page_state;
  173. page_states_dirty = true;
  174. }
  175. template <>
  176. FORCE_INLINE uint8_t *PageManager::get_page(const page_idx_t page_idx) {
  177. CHECK_PAGE(page_idx, nullptr);
  178. return pages[page_idx];
  179. }
  180. template <>
  181. FORCE_INLINE void PageManager::free_page(const page_idx_t page_idx) {
  182. set_page_state(page_idx, PageState::FREE);
  183. }
  184. };
  185. DirectStepping::PageManager page_manager;
  186. const uint8_t segment_table[DirectStepping::Config::NUM_SEGMENTS][DirectStepping::Config::SEGMENT_STEPS] PROGMEM = {
  187. #if STEPPER_PAGE_FORMAT == SP_4x4D_128
  188. { 1, 1, 1, 1, 1, 1, 1 }, // 0 = -7
  189. { 1, 1, 1, 0, 1, 1, 1 }, // 1 = -6
  190. { 1, 1, 1, 0, 1, 0, 1 }, // 2 = -5
  191. { 1, 1, 0, 1, 0, 1, 0 }, // 3 = -4
  192. { 1, 1, 0, 0, 1, 0, 0 }, // 4 = -3
  193. { 0, 0, 1, 0, 0, 0, 1 }, // 5 = -2
  194. { 0, 0, 0, 1, 0, 0, 0 }, // 6 = -1
  195. { 0, 0, 0, 0, 0, 0, 0 }, // 7 = 0
  196. { 0, 0, 0, 1, 0, 0, 0 }, // 8 = 1
  197. { 0, 0, 1, 0, 0, 0, 1 }, // 9 = 2
  198. { 1, 1, 0, 0, 1, 0, 0 }, // 10 = 3
  199. { 1, 1, 0, 1, 0, 1, 0 }, // 11 = 4
  200. { 1, 1, 1, 0, 1, 0, 1 }, // 12 = 5
  201. { 1, 1, 1, 0, 1, 1, 1 }, // 13 = 6
  202. { 1, 1, 1, 1, 1, 1, 1 }, // 14 = 7
  203. { 0 }
  204. #elif STEPPER_PAGE_FORMAT == SP_4x2_256
  205. { 0, 0, 0 }, // 0
  206. { 0, 1, 0 }, // 1
  207. { 1, 0, 1 }, // 2
  208. { 1, 1, 1 }, // 3
  209. #elif STEPPER_PAGE_FORMAT == SP_4x1_512
  210. {0} // Uncompressed format, table not used
  211. #endif
  212. };
  213. #endif // DIRECT_STEPPING