Browse Source

♻️ Minimize endstop bits

Scott Lahteine 4 years ago
parent
commit
02f904dbf9
3 changed files with 68 additions and 44 deletions
  1. 8
    9
      Marlin/src/module/endstops.cpp
  2. 56
    31
      Marlin/src/module/endstops.h
  3. 4
    4
      Marlin/src/module/stepper.cpp

+ 8
- 9
Marlin/src/module/endstops.cpp View File

@@ -56,12 +56,12 @@ Endstops endstops;
56 56
 // private:
57 57
 
58 58
 bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
59
-volatile uint8_t Endstops::hit_state;
60 59
 
61
-Endstops::esbits_t Endstops::live_state = 0;
60
+volatile Endstops::endstop_mask_t Endstops::hit_state;
61
+Endstops::endstop_mask_t Endstops::live_state = 0;
62 62
 
63 63
 #if ENDSTOP_NOISE_THRESHOLD
64
-  Endstops::esbits_t Endstops::validated_live_state;
64
+  Endstops::endstop_mask_t Endstops::validated_live_state;
65 65
   uint8_t Endstops::endstop_poll_count;
66 66
 #endif
67 67
 
@@ -356,7 +356,7 @@ void Endstops::resync() {
356 356
 #endif
357 357
 
358 358
 void Endstops::event_handler() {
359
-  static uint8_t prev_hit_state; // = 0
359
+  static endstop_mask_t prev_hit_state; // = 0
360 360
   if (hit_state == prev_hit_state) return;
361 361
   prev_hit_state = hit_state;
362 362
   if (hit_state) {
@@ -364,15 +364,14 @@ void Endstops::event_handler() {
364 364
       char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
365 365
       #define _SET_STOP_CHAR(A,C) (chr## A = C)
366 366
     #else
367
-      #define _SET_STOP_CHAR(A,C) ;
367
+      #define _SET_STOP_CHAR(A,C) NOOP
368 368
     #endif
369 369
 
370 370
     #define _ENDSTOP_HIT_ECHO(A,C) do{ \
371
-      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
372
-      _SET_STOP_CHAR(A,C); }while(0)
371
+      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); _SET_STOP_CHAR(A,C); }while(0)
373 372
 
374 373
     #define _ENDSTOP_HIT_TEST(A,C) \
375
-      if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
374
+      if (TERN0(HAS_##A##_MIN, TEST(hit_state, A##_MIN)) || TERN0(HAS_##A##_MAX, TEST(hit_state, A##_MAX))) \
376 375
         _ENDSTOP_HIT_ECHO(A,C)
377 376
 
378 377
     #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
@@ -659,7 +658,7 @@ void Endstops::update() {
659 658
      * still exist. The only way to reduce them further is to increase the number of samples.
660 659
      * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
661 660
      */
662
-    static esbits_t old_live_state;
661
+    static endstop_mask_t old_live_state;
663 662
     if (old_live_state != live_state) {
664 663
       endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
665 664
       old_live_state = live_state;

+ 56
- 31
Marlin/src/module/endstops.h View File

@@ -28,50 +28,75 @@
28 28
 #include "../inc/MarlinConfig.h"
29 29
 #include <stdint.h>
30 30
 
31
+#define __ES_ITEM(N) N,
32
+#define _ES_ITEM(K,N) TERN_(K,DEFER4(__ES_ITEM)(N))
33
+
31 34
 enum EndstopEnum : char {
32
-  X_MIN,  Y_MIN,  Z_MIN,  Z_MIN_PROBE,
33
-  X_MAX,  Y_MAX,  Z_MAX,
34
-  X2_MIN, X2_MAX,
35
-  Y2_MIN, Y2_MAX,
36
-  Z2_MIN, Z2_MAX,
37
-  Z3_MIN, Z3_MAX,
38
-  Z4_MIN, Z4_MAX
35
+  _ES_ITEM(HAS_X_MIN, X_MIN)
36
+  _ES_ITEM(HAS_X_MAX, X_MAX)
37
+  _ES_ITEM(HAS_Y_MIN, Y_MIN)
38
+  _ES_ITEM(HAS_Y_MAX, Y_MAX)
39
+  _ES_ITEM(HAS_Z_MIN, Z_MIN)
40
+  _ES_ITEM(HAS_Z_MAX, Z_MAX)
41
+  #if ENABLED(X_DUAL_ENDSTOPS)
42
+    _ES_ITEM(HAS_X_MIN, X2_MIN)
43
+    _ES_ITEM(HAS_X_MAX, X2_MAX)
44
+  #endif
45
+  #if ENABLED(Y_DUAL_ENDSTOPS)
46
+    _ES_ITEM(HAS_Y_MIN, Y2_MIN)
47
+    _ES_ITEM(HAS_Y_MAX, Y2_MAX)
48
+  #endif
49
+  #if ENABLED(Z_MULTI_ENDSTOPS)
50
+    _ES_ITEM(HAS_Z_MIN, Z2_MIN)
51
+    _ES_ITEM(HAS_Z_MAX, Z2_MAX)
52
+    #if NUM_Z_STEPPER_DRIVERS >= 3
53
+      _ES_ITEM(HAS_Z_MIN, Z3_MIN)
54
+      _ES_ITEM(HAS_Z_MAX, Z3_MAX)
55
+    #endif
56
+    #if NUM_Z_STEPPER_DRIVERS >= 4
57
+      _ES_ITEM(HAS_Z_MIN, Z4_MIN)
58
+      _ES_ITEM(HAS_Z_MAX, Z4_MAX)
59
+    #endif
60
+  #endif
61
+  _ES_ITEM(HAS_Z_MIN_PROBE_PIN, Z_MIN_PROBE)
62
+  NUM_ENDSTOP_STATES
39 63
 };
40 64
 
41 65
 #define X_ENDSTOP (x_home_dir(active_extruder) < 0 ? X_MIN : X_MAX)
42 66
 #define Y_ENDSTOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
43 67
 #define Z_ENDSTOP (Z_HOME_DIR < 0 ? TERN(HOMING_Z_WITH_PROBE, Z_MIN, Z_MIN_PROBE) : Z_MAX)
44 68
 
69
+#undef __ES_ITEM
70
+#undef _ES_ITEM
71
+
45 72
 class Endstops {
46 73
   public:
47
-    #if HAS_EXTRA_ENDSTOPS
48
-      typedef uint16_t esbits_t;
49
-      #if ENABLED(X_DUAL_ENDSTOPS)
50
-        static float x2_endstop_adj;
51
-      #endif
52
-      #if ENABLED(Y_DUAL_ENDSTOPS)
53
-        static float y2_endstop_adj;
54
-      #endif
55
-      #if ENABLED(Z_MULTI_ENDSTOPS)
56
-        static float z2_endstop_adj;
57
-      #endif
58
-      #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
59
-        static float z3_endstop_adj;
60
-      #endif
61
-      #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
62
-        static float z4_endstop_adj;
63
-      #endif
64
-    #else
65
-      typedef uint8_t esbits_t;
74
+
75
+    typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
76
+
77
+    #if ENABLED(X_DUAL_ENDSTOPS)
78
+      static float x2_endstop_adj;
79
+    #endif
80
+    #if ENABLED(Y_DUAL_ENDSTOPS)
81
+      static float y2_endstop_adj;
82
+    #endif
83
+    #if ENABLED(Z_MULTI_ENDSTOPS)
84
+      static float z2_endstop_adj;
85
+    #endif
86
+    #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
87
+      static float z3_endstop_adj;
88
+    #endif
89
+    #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
90
+      static float z4_endstop_adj;
66 91
     #endif
67 92
 
68 93
   private:
69 94
     static bool enabled, enabled_globally;
70
-    static esbits_t live_state;
71
-    static volatile uint8_t hit_state;      // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
95
+    static endstop_mask_t live_state;
96
+    static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
72 97
 
73 98
     #if ENDSTOP_NOISE_THRESHOLD
74
-      static esbits_t validated_live_state;
99
+      static endstop_mask_t validated_live_state;
75 100
       static uint8_t endstop_poll_count;    // Countdown from threshold for polling
76 101
     #endif
77 102
 
@@ -107,12 +132,12 @@ class Endstops {
107 132
     /**
108 133
      * Get Endstop hit state.
109 134
      */
110
-    FORCE_INLINE static uint8_t trigger_state() { return hit_state; }
135
+    FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
111 136
 
112 137
     /**
113 138
      * Get current endstops state
114 139
      */
115
-    FORCE_INLINE static esbits_t state() {
140
+    FORCE_INLINE static endstop_mask_t state() {
116 141
       return
117 142
         #if ENDSTOP_NOISE_THRESHOLD
118 143
           validated_live_state

+ 4
- 4
Marlin/src/module/stepper.cpp View File

@@ -260,12 +260,12 @@ xyze_int8_t Stepper::count_direction{0};
260 260
 #define DUAL_ENDSTOP_APPLY_STEP(A,V)                                                                                        \
261 261
   if (separate_multi_axis) {                                                                                                \
262 262
     if (A##_HOME_DIR < 0) {                                                                                                 \
263
-      if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V);    \
264
-      if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
263
+      if (TERN0(HAS_##A##_MIN, !(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor)) A##_STEP_WRITE(V);     \
264
+      if (TERN0(HAS_##A##2_MIN, !(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor)) A##2_STEP_WRITE(V); \
265 265
     }                                                                                                                       \
266 266
     else {                                                                                                                  \
267
-      if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V);    \
268
-      if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
267
+      if (TERN0(HAS_##A##_MAX, !(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor)) A##_STEP_WRITE(V);     \
268
+      if (TERN0(HAS_##A##2_MAX, !(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor)) A##2_STEP_WRITE(V); \
269 269
     }                                                                                                                       \
270 270
   }                                                                                                                         \
271 271
   else {                                                                                                                    \

Loading…
Cancel
Save