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