aboutsummaryrefslogtreecommitdiff
path: root/Marlin/src/feature/direct_stepping.cpp
blob: 9766d1464053143962eb757709e54fb0b1bd660b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * Marlin 3D Printer Firmware
 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 *
 * Based on Sprinter and grbl.
 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */
#include "../inc/MarlinConfigPre.h"

#if ENABLED(DIRECT_STEPPING)

#include "direct_stepping.h"

#include "../MarlinCore.h"

#define CHECK_PAGE(I, R) do{                                \
  if (I >= sizeof(page_states) / sizeof(page_states[0])) {  \
    fatal_error = true;                                     \
    return R;                                               \
  }                                                         \
}while(0)

#define CHECK_PAGE_STATE(I, R, S) do { \
  CHECK_PAGE(I, R);                    \
  if (page_states[I] != S) {           \
    fatal_error = true;                \
    return R;                          \
  }                                    \
}while(0)

namespace DirectStepping {

  template<typename Cfg>
  State SerialPageManager<Cfg>::state;

  template<typename Cfg>
  volatile bool SerialPageManager<Cfg>::fatal_error;

  template<typename Cfg>
  volatile PageState SerialPageManager<Cfg>::page_states[Cfg::NUM_PAGES];

  template<typename Cfg>
  volatile bool SerialPageManager<Cfg>::page_states_dirty;

  template<typename Cfg>
  uint8_t SerialPageManager<Cfg>::pages[Cfg::NUM_PAGES][Cfg::PAGE_SIZE];

  template<typename Cfg>
  uint8_t SerialPageManager<Cfg>::checksum;

  template<typename Cfg>
  typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_byte_idx;

  template<typename Cfg>
  typename Cfg::page_idx_t SerialPageManager<Cfg>::write_page_idx;

  template<typename Cfg>
  typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_page_size;

  template <typename Cfg>
  void SerialPageManager<Cfg>::init() {
    for (int i = 0 ; i < Cfg::NUM_PAGES ; i++)
      page_states[i] = PageState::FREE;

    fatal_error = false;
    state = State::NEWLINE;

    page_states_dirty = false;

    SERIAL_ECHOLNPGM("pages_ready");
  }

  template<typename Cfg>
  FORCE_INLINE bool SerialPageManager<Cfg>::maybe_store_rxd_char(uint8_t c) {
    switch (state) {
      default:
      case State::MONITOR:
        switch (c) {
          case '\n':
          case '\r':
            state = State::NEWLINE;
          default:
            return false;
        }
      case State::NEWLINE:
        switch (c) {
          case Cfg::CONTROL_CHAR:
            state = State::ADDRESS;
            return true;
          case '\n':
          case '\r':
            state = State::NEWLINE;
            return false;
          default:
            state = State::MONITOR;
            return false;
        }
      case State::ADDRESS:
        //TODO: 16 bit address, State::ADDRESS2
        write_page_idx = c;
        write_byte_idx = 0;
        checksum = 0;

        CHECK_PAGE(write_page_idx, true);

        if (page_states[write_page_idx] == PageState::FAIL) {
          // Special case for fail
          state = State::UNFAIL;
          return true;
        }

        set_page_state(write_page_idx, PageState::WRITING);

        state = Cfg::DIRECTIONAL ? State::COLLECT : State::SIZE;

        return true;
      case State::SIZE:
        // Zero means full page size
        write_page_size = c;
        state = State::COLLECT;
        return true;
      case State::COLLECT:
        pages[write_page_idx][write_byte_idx++] = c;
        checksum ^= c;

        // check if still collecting
        if (Cfg::PAGE_SIZE == 256) {
          // special case for 8-bit, check if rolled back to 0
          if (Cfg::DIRECTIONAL || !write_page_size) { // full 256 bytes
            if (write_byte_idx) return true;
          } else {
            if (write_byte_idx < write_page_size) return true;
          }
        } else if (Cfg::DIRECTIONAL) {
          if (write_byte_idx != Cfg::PAGE_SIZE) return true;
        } else {
          if (write_byte_idx < write_page_size) return true;
        }

        state = State::CHECKSUM;
        return true;
      case State::CHECKSUM: {
        const PageState page_state = (checksum == c) ? PageState::OK : PageState::FAIL;
        set_page_state(write_page_idx, page_state);
        state = State::MONITOR;
        return true;
      }
      case State::UNFAIL:
        if (c == 0) {
          set_page_state(write_page_idx, PageState::FREE);
        } else {
          fatal_error = true;
        }
        state = State::MONITOR;
        return true;
    }
  }

  template <typename Cfg>
  void SerialPageManager<Cfg>::write_responses() {
    if (fatal_error) {
      kill(GET_TEXT(MSG_BAD_PAGE));
      return;
    }

    if (!page_states_dirty) return;
    page_states_dirty = false;

    SERIAL_ECHO(Cfg::CONTROL_CHAR);
    constexpr int state_bits = 2;
    constexpr int n_bytes = Cfg::NUM_PAGES >> state_bits;
    volatile uint8_t bits_b[n_bytes] = { 0 };

    for (page_idx_t i = 0 ; i < Cfg::NUM_PAGES ; i++) {
      bits_b[i >> state_bits] |= page_states[i] << ((i * state_bits) & 0x7);
    }

    uint8_t crc = 0;
    for (uint8_t i = 0 ; i < n_bytes ; i++) {
      crc ^= bits_b[i];
      SERIAL_ECHO(bits_b[i]);
    }

    SERIAL_ECHO(crc);
    SERIAL_EOL();
  }

  template <typename Cfg>
  FORCE_INLINE void SerialPageManager<Cfg>::set_page_state(const page_idx_t page_idx, const PageState page_state) {
    CHECK_PAGE(page_idx,);

    page_states[page_idx] = page_state;
    page_states_dirty = true;
  }

  template <>
  FORCE_INLINE uint8_t *PageManager::get_page(const page_idx_t page_idx) {
    CHECK_PAGE(page_idx, nullptr);

    return pages[page_idx];
  }

  template <>
  FORCE_INLINE void PageManager::free_page(const page_idx_t page_idx) {
    set_page_state(page_idx, PageState::FREE);
  }

};

DirectStepping::PageManager page_manager;

const uint8_t segment_table[DirectStepping::Config::NUM_SEGMENTS][DirectStepping::Config::SEGMENT_STEPS] PROGMEM = {

  #if STEPPER_PAGE_FORMAT == SP_4x4D_128

    { 1, 1, 1, 1, 1, 1, 1 }, //  0 = -7
    { 1, 1, 1, 0, 1, 1, 1 }, //  1 = -6
    { 1, 1, 1, 0, 1, 0, 1 }, //  2 = -5
    { 1, 1, 0, 1, 0, 1, 0 }, //  3 = -4
    { 1, 1, 0, 0, 1, 0, 0 }, //  4 = -3
    { 0, 0, 1, 0, 0, 0, 1 }, //  5 = -2
    { 0, 0, 0, 1, 0, 0, 0 }, //  6 = -1
    { 0, 0, 0, 0, 0, 0, 0 }, //  7 =  0
    { 0, 0, 0, 1, 0, 0, 0 }, //  8 =  1
    { 0, 0, 1, 0, 0, 0, 1 }, //  9 =  2
    { 1, 1, 0, 0, 1, 0, 0 }, // 10 =  3
    { 1, 1, 0, 1, 0, 1, 0 }, // 11 =  4
    { 1, 1, 1, 0, 1, 0, 1 }, // 12 =  5
    { 1, 1, 1, 0, 1, 1, 1 }, // 13 =  6
    { 1, 1, 1, 1, 1, 1, 1 }, // 14 =  7
    { 0 }

  #elif STEPPER_PAGE_FORMAT == SP_4x2_256

    { 0, 0, 0 }, // 0
    { 0, 1, 0 }, // 1
    { 1, 0, 1 }, // 2
    { 1, 1, 1 }, // 3

  #elif STEPPER_PAGE_FORMAT == SP_4x1_512

    {0} // Uncompressed format, table not used

  #endif

};

#endif // DIRECT_STEPPING