Prusa MINI Firmware overview
trinamic.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 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 #pragma once
23 
24 /**
25  * stepper/trinamic.h
26  * Stepper driver indirection for Trinamic
27  */
28 
29 #include <TMCStepper.h>
30 #if TMCSTEPPER_VERSION < 0x000500
31  #error "Update TMCStepper library to 0.5.0 or newer."
32 #endif
33 
34 #include "../../inc/MarlinConfig.h"
35 #include "../../feature/tmc_util.h"
36 
37 #define ____TMC_CLASS(MODEL, A, I, E) TMCMarlin<TMC##MODEL##Stepper, A, I, E>
38 #define ___TMC_CLASS(MODEL, A, I, E) ____TMC_CLASS(MODEL, A, I, E)
39 #define __TMC_CLASS(MODEL, A, I, E) ___TMC_CLASS(_##MODEL, A, I, E)
40 #define _TMC_CLASS(MODEL, L, E) __TMC_CLASS(MODEL, L, E)
41 #define TMC_CLASS(ST, A) _TMC_CLASS(ST##_DRIVER_TYPE, TMC_##ST##_LABEL, A##_AXIS)
42 #if ENABLED(DISTINCT_E_FACTORS)
43  #define TMC_CLASS_E(I) TMC_CLASS(E##I, E##I)
44 #else
45  #define TMC_CLASS_E(I) TMC_CLASS(E##I, E)
46 #endif
47 
48 typedef struct {
50  int8_t hend;
53 
54 static constexpr chopper_timing_t chopper_timing = CHOPPER_TIMING;
55 
56 #if HAS_TMC220x
57  void tmc_serial_begin();
58 #endif
59 
62 
63 #define AXIS_HAS_SQUARE_WAVE(A) (AXIS_IS_TMC(A) && ENABLED(SQUARE_WAVE_STEPPING))
64 
65 // X Stepper
66 #if AXIS_IS_TMC(X)
67  extern TMC_CLASS(X, X) stepperX;
68  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
69  #define X_ENABLE_INIT NOOP
70  #define X_ENABLE_WRITE(STATE) stepperX.toff((STATE)==X_ENABLE_ON ? chopper_timing.toff : 0)
71  #define X_ENABLE_READ() stepperX.isEnabled()
72  #endif
73  #if AXIS_HAS_SQUARE_WAVE(X)
74  #define X_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(X_STEP_PIN); }while(0)
75  #endif
76 #endif
77 
78 // Y Stepper
79 #if AXIS_IS_TMC(Y)
80  extern TMC_CLASS(Y, Y) stepperY;
81  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
82  #define Y_ENABLE_INIT NOOP
83  #define Y_ENABLE_WRITE(STATE) stepperY.toff((STATE)==Y_ENABLE_ON ? chopper_timing.toff : 0)
84  #define Y_ENABLE_READ() stepperY.isEnabled()
85  #endif
86  #if AXIS_HAS_SQUARE_WAVE(Y)
87  #define Y_STEP_WRITE(STATE) do{ if (STATE) TOGGLE(Y_STEP_PIN); }while(0)
88  #endif
89 #endif
90 
91 // Z Stepper
92 #if AXIS_IS_TMC(Z)
93  extern TMC_CLASS(Z, Z) stepperZ;
94  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
95  #define Z_ENABLE_INIT NOOP
96  #define Z_ENABLE_WRITE(STATE) stepperZ.toff((STATE)==Z_ENABLE_ON ? chopper_timing.toff : 0)
97  #define Z_ENABLE_READ() stepperZ.isEnabled()
98  #endif
99  #if AXIS_HAS_SQUARE_WAVE(Z)
100  #define Z_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z_STEP_PIN); }while(0)
101  #endif
102 #endif
103 
104 // X2 Stepper
105 #if HAS_X2_ENABLE && AXIS_IS_TMC(X2)
106  extern TMC_CLASS(X2, X) stepperX2;
107  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
108  #define X2_ENABLE_INIT NOOP
109  #define X2_ENABLE_WRITE(STATE) stepperX2.toff((STATE)==X_ENABLE_ON ? chopper_timing.toff : 0)
110  #define X2_ENABLE_READ() stepperX2.isEnabled()
111  #endif
112  #if AXIS_HAS_SQUARE_WAVE(X2)
113  #define X2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(X2_STEP_PIN); }while(0)
114  #endif
115 #endif
116 
117 // Y2 Stepper
118 #if HAS_Y2_ENABLE && AXIS_IS_TMC(Y2)
119  extern TMC_CLASS(Y2, Y) stepperY2;
120  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
121  #define Y2_ENABLE_INIT NOOP
122  #define Y2_ENABLE_WRITE(STATE) stepperY2.toff((STATE)==Y_ENABLE_ON ? chopper_timing.toff : 0)
123  #define Y2_ENABLE_READ() stepperY2.isEnabled()
124  #endif
125  #if AXIS_HAS_SQUARE_WAVE(Y2)
126  #define Y2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Y2_STEP_PIN); }while(0)
127  #endif
128 #endif
129 
130 // Z2 Stepper
131 #if HAS_Z2_ENABLE && AXIS_IS_TMC(Z2)
132  extern TMC_CLASS(Z2, Z) stepperZ2;
133  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(Z2)
134  #define Z2_ENABLE_INIT NOOP
135  #define Z2_ENABLE_WRITE(STATE) stepperZ2.toff((STATE)==Z_ENABLE_ON ? chopper_timing.toff : 0)
136  #define Z2_ENABLE_READ() stepperZ2.isEnabled()
137  #endif
138  #if AXIS_HAS_SQUARE_WAVE(Z2)
139  #define Z2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z2_STEP_PIN); }while(0)
140  #endif
141 #endif
142 
143 // Z3 Stepper
144 #if HAS_Z3_ENABLE && AXIS_IS_TMC(Z3)
145  extern TMC_CLASS(Z3, Z) stepperZ3;
146  #if ENABLED(SOFTWARE_DRIVER_ENABLE)
147  #define Z3_ENABLE_INIT NOOP
148  #define Z3_ENABLE_WRITE(STATE) stepperZ3.toff((STATE)==Z_ENABLE_ON ? chopper_timing.toff : 0)
149  #define Z3_ENABLE_READ() stepperZ3.isEnabled()
150  #endif
151  #if AXIS_HAS_SQUARE_WAVE(Z3)
152  #define Z3_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(Z3_STEP_PIN); }while(0)
153  #endif
154 #endif
155 
156 // E0 Stepper
157 #if AXIS_IS_TMC(E0)
158  extern TMC_CLASS_E(0) stepperE0;
159  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E0)
160  #define E0_ENABLE_INIT NOOP
161  #define E0_ENABLE_WRITE(STATE) stepperE0.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
162  #define E0_ENABLE_READ() stepperE0.isEnabled()
163  #endif
164  #if AXIS_HAS_SQUARE_WAVE(E0)
165  #define E0_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E0_STEP_PIN); }while(0)
166  #endif
167 #endif
168 
169 // E1 Stepper
170 #if AXIS_IS_TMC(E1)
171  extern TMC_CLASS_E(1) stepperE1;
172  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E1)
173  #define E1_ENABLE_INIT NOOP
174  #define E1_ENABLE_WRITE(STATE) stepperE1.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
175  #define E1_ENABLE_READ() stepperE1.isEnabled()
176  #endif
177  #if AXIS_HAS_SQUARE_WAVE(E1)
178  #define E1_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E1_STEP_PIN); }while(0)
179  #endif
180 #endif
181 
182 // E2 Stepper
183 #if AXIS_IS_TMC(E2)
184  extern TMC_CLASS_E(2) stepperE2;
185  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E2)
186  #define E2_ENABLE_INIT NOOP
187  #define E2_ENABLE_WRITE(STATE) stepperE2.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
188  #define E2_ENABLE_READ() stepperE2.isEnabled()
189  #endif
190  #if AXIS_HAS_SQUARE_WAVE(E2)
191  #define E2_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E2_STEP_PIN); }while(0)
192  #endif
193 #endif
194 
195 // E3 Stepper
196 #if AXIS_IS_TMC(E3)
197  extern TMC_CLASS_E(3) stepperE3;
198  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E3)
199  #define E3_ENABLE_INIT NOOP
200  #define E3_ENABLE_WRITE(STATE) stepperE3.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
201  #define E3_ENABLE_READ() stepperE3.isEnabled()
202  #endif
203  #if AXIS_HAS_SQUARE_WAVE(E3)
204  #define E3_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E3_STEP_PIN); }while(0)
205  #endif
206 #endif
207 
208 // E4 Stepper
209 #if AXIS_IS_TMC(E4)
210  extern TMC_CLASS_E(4) stepperE4;
211  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E4)
212  #define E4_ENABLE_INIT NOOP
213  #define E4_ENABLE_WRITE(STATE) stepperE4.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
214  #define E4_ENABLE_READ() stepperE4.isEnabled()
215  #endif
216  #if AXIS_HAS_SQUARE_WAVE(E4)
217  #define E4_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E4_STEP_PIN); }while(0)
218  #endif
219 #endif
220 
221 // E5 Stepper
222 #if AXIS_IS_TMC(E5)
223  extern TMC_CLASS_E(5) stepperE5;
224  #if ENABLED(SOFTWARE_DRIVER_ENABLE) && AXIS_IS_TMC(E5)
225  #define E5_ENABLE_INIT NOOP
226  #define E5_ENABLE_WRITE(STATE) stepperE5.toff((STATE)==E_ENABLE_ON ? chopper_timing.toff : 0)
227  #define E5_ENABLE_READ() stepperE5.isEnabled()
228  #endif
229  #if AXIS_HAS_SQUARE_WAVE(E5)
230  #define E5_STEP_WRITE(STATE) do{ if(STATE) TOGGLE(E5_STEP_PIN); }while(0)
231  #endif
232 #endif
PIN_X_DIR
#define PIN_X_DIR
Definition: hwio_pindef.h:18
PIN_E_DIR
#define PIN_E_DIR
Definition: hwio_pindef.h:33
HardwareSerial.h
chopper_timing_t::toff
uint8_t toff
Definition: trinamic.h:49
PIN_Z_DIAG
#define PIN_Z_DIAG
Definition: hwio_pindef.h:31
E0_HARDWARE_SERIAL
#define E0_HARDWARE_SERIAL
Definition: pins_FYSETC_CHEETAH.h:83
E4
Definition: L6470_Marlin.h:30
E1
Definition: L6470_Marlin.h:30
tmc_set_move
void tmc_set_move(uint8_t tmc, uint32_t step, uint8_t dir, uint8_t speed)
stepper
Stepper stepper
Definition: stepper.cpp:82
E0
Definition: L6470_Marlin.h:30
chopper_timing_t
Definition: trinamic.h:48
chopper_timing_t::hend
int8_t hend
Definition: trinamic.h:50
Y
Definition: L6470_Marlin.h:30
init_tmc
void init_tmc(void)
tmc_set_mres
void tmc_set_mres()
Y_HARDWARE_SERIAL
#define Y_HARDWARE_SERIAL
Definition: pins_FYSETC_CHEETAH.h:81
dbg.h
Z3
Definition: L6470_Marlin.h:30
Z
Definition: L6470_Marlin.h:30
gpio_get
static int gpio_get(uint8_t pin8)
Definition: gpio.h:94
E3
Definition: L6470_Marlin.h:30
E2
Definition: L6470_Marlin.h:30
X2
Definition: L6470_Marlin.h:30
PIN_Y_DIAG
#define PIN_Y_DIAG
Definition: hwio_pindef.h:26
Stepper::set_directions
static void set_directions()
Definition: stepper.cpp:354
DBG
#define DBG
Definition: trinamic.cpp:9
PIN_Z_STEP
#define PIN_Z_STEP
Definition: hwio_pindef.h:29
chopper_timing_t::hstrt
uint8_t hstrt
Definition: trinamic.h:51
gpio.h
PIN_Y_STEP
#define PIN_Y_STEP
Definition: hwio_pindef.h:24
restore_trinamic_drivers
void restore_trinamic_drivers()
PIN_Y_DIR
#define PIN_Y_DIR
Definition: hwio_pindef.h:23
uint8_t
const uint8_t[]
Definition: 404_html.c:3
tmc_get_diag
uint8_t tmc_get_diag()
Z2
Definition: L6470_Marlin.h:30
PIN_E_STEP
#define PIN_E_STEP
Definition: hwio_pindef.h:34
delay
void delay(uint32_t ms)
Definition: wiring_time.c:42
PIN_X_STEP
#define PIN_X_STEP
Definition: hwio_pindef.h:19
TMC_CLASS_E
#define TMC_CLASS_E(I)
Definition: trinamic.h:45
X_HARDWARE_SERIAL
#define X_HARDWARE_SERIAL
Definition: pins_FYSETC_CHEETAH.h:80
Z_HARDWARE_SERIAL
#define Z_HARDWARE_SERIAL
Definition: pins_FYSETC_CHEETAH.h:82
PIN_E_DIAG
#define PIN_E_DIAG
Definition: hwio_pindef.h:36
gpio_set
static void gpio_set(uint8_t pin8, int state)
Definition: gpio.h:98
E5
Definition: L6470_Marlin.h:30
chopper_timing
static constexpr chopper_timing_t chopper_timing
Definition: trinamic.h:54
PIN_X_DIAG
#define PIN_X_DIAG
Definition: hwio_pindef.h:21
config.h
hwio_pindef.h
hwio_a3ides.h
PIN_Z_DIR
#define PIN_Z_DIR
Definition: hwio_pindef.h:28
X
Definition: L6470_Marlin.h:30
TMC_CLASS
#define TMC_CLASS(ST, A)
Definition: trinamic.h:41
tmc_set_sgthrs
void tmc_set_sgthrs(uint8_t SGT)
Y2
Definition: L6470_Marlin.h:30
UNUSED
#define UNUSED(X)
Definition: stm32f4xx_hal_def.h:74
reset_trinamic_drivers
void reset_trinamic_drivers()