# This file contains common pin mappings for the BIGTREETECH SKR mini
# E3 v1.2. To use this config, the firmware should be compiled for the
# STM32F103 with a "28KiB bootloader" and USB communication. Also,
# select "Enable extra low-level configuration options" and configure
# "GPIO pins to set at micro-controller startup" to "!PC13".
# The "make flash" command does not work on the SKR mini E3. Instead,
# after running "make", copy the generated "out/klipper.bin" file to a
# file named "firmware.bin" on an SD card and then restart the SKR
# mini E3 with that SD card.
# See docs/Config_Reference.md for a description of parameters.
# Note: This board has a design flaw in its thermistor circuits that
# cause inaccurate temperatures (most noticeable at low temperatures).
[include KAMP/KAMP_Settings.cfg]
[include KAMP/Adaptive_Meshing.cfg]
[include KAMP/Voron_Purge.cfg]
[exclude_object]
[stepper_x]
step_pin: PB13
dir_pin: PB12
enable_pin: !PB14
rotation_distance: 40
full_steps_per_rotation: 200
microsteps: 32
endstop_pin: ^PC0
position_endstop: 215
position_min: 0
position_max: 215
homing_speed: 70
homing_positive_dir: true
[tmc2209 stepper_x]
uart_pin: PB15
run_current: 0.5
stealthchop_threshold: 999999
[stepper_y]
step_pin: PB10
dir_pin: PB2
enable_pin: !PB11
rotation_distance: 40
full_steps_per_rotation: 200
microsteps: 32
endstop_pin: ^PC1
position_endstop: 0
position_min: 0
position_max: 225
homing_speed: 70
homing_positive_dir: false
[tmc2209 stepper_y]
uart_pin: PC6
run_current: 0.7
stealthchop_threshold: 999999
[stepper_z]
step_pin: PB0
dir_pin: PC5
enable_pin: !PB1
rotation_distance: 40
full_steps_per_rotation: 200
microsteps: 32
endstop_pin: ^PC2
position_endstop: 0.7
position_max: 220
homing_speed: 40
[tmc2209 stepper_z]
uart_pin: PC10
run_current: 0.5
stealthchop_threshold: 999999
[extruder]
step_pin: PB3
dir_pin: !PB4
enable_pin: !PD2
gear_ratio: 50:10               #BMG Gear Ratio CW2
microsteps: 32
full_steps_per_rotation: 200    #200 for 1.8 degree, 400 for 0.9 degree
rotation_distance: 22.6789511
nozzle_diameter: 0.400
filament_diameter: 1.750
heater_pin: PC8
sensor_type: PT1000
sensor_pin: PA0
#control: pid
min_temp:10
max_temp: 350
max_power: 1.0
min_extrude_temp: 10
max_extrude_only_distance: 100
max_extrude_cross_section: 5
##  Try to keep pressure_advance below 1.0
pressure_advance: 0.045
##  Default is 0.040, leave stock
pressure_advance_smooth_time: 0.040
[tmc2209 extruder]
uart_pin: PC11
run_current: 0.30
stealthchop_threshold: 999999
[heater_bed]
heater_pin: PC9
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC3
#control: pid
#pid_kp = 18.538
#pid_ki = 0.714
#pid_kd = 120.266
min_temp: 0
max_temp: 130
[fan]
pin: PA8
[mcu]
serial: /dev/serial/by-id/usb-Klipper_stm32f103xe_34FFD7055055323909761643-if00
[temperature_sensor mcu_temp]
sensor_type: temperature_mcu
min_temp: 0
max_temp: 100
#[mcu rpi]
#serial: /tmp/klipper_host_mcu
#[temperature_sensor Pi]
#sensor_type: temperature_host
#min_temp: 10
#max_temp: 100
#[adxl345]
#cs_pin: rpi:None
#[resonance_tester]
#accel_chip: adxl345
#probe_points:
#    100, 100, 20  # an example
[input_shaper]
shaper_freq_x: 54.6
shaper_type_x: mzv
shaper_freq_y: 29
shaper_type_y: ei
[printer]
kinematics: corexz
max_velocity: 200
max_accel: 1500
max_z_velocity: 50
max_z_accel: 1000
square_corner_velocity: 4.0
[homing_override]
axes: z
set_position_z: 0
gcode:
    G90
    G0 Z5 F500
    G28 X0 Y0
    G0 X115 Y100 F9000
    G28 Z0
    G0 Z10 F500
[skew_correction]
[static_digital_output usb_pullup_enable]
pins: !PC13
[display]
lcd_type: st7920
cs_pin: EXP1_7
sclk_pin: EXP1_6
sid_pin: EXP1_8
encoder_pins: ^EXP1_5, ^EXP1_3
click_pin: ^!EXP1_2
[output_pin beeper]
pin: EXP1_1
[virtual_sdcard]
path: /home/biqu/printer_data/gcodes
[pause_resume]
[board_pins]
aliases:
    # EXP1 header
    EXP1_1=PB5, EXP1_3=PA9,   EXP1_5=PA10, EXP1_7=PB8, EXP1_9=<GND>,
    EXP1_2=PB6, EXP1_4=<RST>, EXP1_6=PB9,  EXP1_8=PB7, EXP1_10=<5V>
[bed_screws]
screw1: 35, 35
screw2: 35, 205
screw3: 205, 35
screw4: 205, 205
[bltouch]
sensor_pin: PC14
control_pin: PA1
#Z_offset: 0.5
X_offset:-44.464
Y_offset:-1.184
samples: 3
samples_result: median
sample_retract_dist: 5.0
samples_tolerance: 0.05
[bed_mesh]
speed: 50
horizontal_move_z: 8
mesh_min: 0, 0
mesh_max: 170, 200
probe_count: 7, 7
mesh_pps: 2, 2
algorithm: bicubic
bicubic_tension: 0.2
[firmware_retraction]
retract_length: 0.8
retract_speed: 40
unretract_extra_length: 0
unretract_speed: 40
## Macros
##Pause Macro
##
##
[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: PAUSE_BASE
# change this if you need more or less extrusion
variable_extrude: 1.0
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  ##### set park positon for x and y #####
  # default is your max posion from your printer.cfg
  {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
  {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
  ##### calculate save lift position #####
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% if act_z < (max_z - 2.0) %}
      {% set z_safe = 2.0 %}
  {% else %}
      {% set z_safe = max_z - act_z %}
  {% endif %}
  ##### end of definitions #####
  PAUSE_BASE
  G91
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G1 E-{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    G1 Z{z_safe} F900
    G90
    G1 X{x_park} Y{y_park} F6000
  {% else %}
    {action_respond_info("Printer not homed")}
  {% endif %} 
 ##Resume Macro
 ##
 ##
[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: RESUME_BASE
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  #### get VELOCITY parameter if specified ####
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  ##### end of definitions #####
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G91
    G1 E{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}  
  RESUME_BASE {get_params}
##Cancel Print
##
##
[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: CANCEL_PRINT_BASE
gcode:
  TURN_OFF_HEATERS
  CANCEL_PRINT_BASE
##Start Print
##
##
#[gcode_macro START_PRINT]
#gcode:
    #{% set BED_TEMP = params.BED_TEMP|default(60)|float %}
    #{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
    ## Start bed heating
    #M140 S{BED_TEMP}
    ## Use absolute coordinates
    #G90
    ## Reset the G-Code Z offset (adjust Z offset if needed)
    #SET_GCODE_OFFSET Z=0.0
    ## Home the printer
    #G28
    ## Move the nozzle away from the bed
    #G1 Z20 F3000
    #    # Wait for bed to reach temperature
    #M190 S{BED_TEMP}
    ## Set and wait for nozzle to reach temperature
    #M109 S{EXTRUDER_TEMP}
    ## Home for Mesh Level
    #G28
    # Start Bed Mesh Level
    #BED_MESH_CALIBRATE
    # Home for Prime Lines
    #G28
    # Prime Lines
    #PRIME_NOZZLE
[gcode_macro START_PRINT]
#   Use START_PRINT for the slicer starting script - please customise for your slicer of choice
gcode:
    SETUP_KAMP_MESHING DISPLAY_PARAMETERS=1 LED_ENABLE=1 FUZZ_ENABLE=1
    SETUP_VORON_PURGE DISPLAY_PARAMETERS=1 ADAPTIVE_ENABLE=1
    #{% set BED_TEMP = params.BED_TEMP|default(60)|float %}
    #{% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
    # Wait for bed to reach temperature
    #M190 S{BED_TEMP}
    # Set and wait for nozzle to reach temperature
    #M109 S{EXTRUDER_TEMP}
    G28                            ; home all axes
    BED_MESH_CLEAR
    BED_MESH_CALIBRATE
    VORON_PURGE
    G90                            ; absolute positioning
    G1 Z20 F3000                   ; move nozzle away from bed    
    SKEW_PROFILE LOAD=my_skew_profile
  
  
##End Print
##
##
[gcode_macro END_PRINT]
#   Use PRINT_END for the slicer ending script
gcode:
    #   Get Boundaries
    {% set max_x = printer.configfile.config["stepper_x"]["position_max"]|float %}
    {% set max_y = printer.configfile.config["stepper_y"]["position_max"]|float %}
    {% set max_z = printer.configfile.config["stepper_z"]["position_max"]|float %}
    
    #   Check end position to determine safe directions to move
    {% if printer.toolhead.position.x < (max_x - 20) %}
        {% set x_safe = 20.0 %}
    {% else %}
        {% set x_safe = -20.0 %}
    {% endif %}
    {% if printer.toolhead.position.y < (max_y - 20) %}
        {% set y_safe = 20.0 %}
    {% else %}
        {% set y_safe = -20.0 %}
    {% endif %}
    {% if printer.toolhead.position.z < (max_z - 2) %}
        {% set z_safe = 2.0 %}
    {% else %}
        {% set z_safe = max_z - printer.toolhead.position.z %}
    {% endif %}
    
    #  Commence PRINT_END
    M400                             ; wait for buffer to clear
    G92 E0                           ; zero the extruder
    G1 E-4.0 F3600                   ; retract
    G91                              ; relative positioning
    G0 Z{z_safe} F3600               ; move nozzle up
    G0 X{x_safe} Y{y_safe} F20000    ; move nozzle to remove stringing
    
    M104 S0                          ; turn off hotend
    M140 S0                          ; turn off bed
    M106 S0                          ; turn off fan
    G90                              ; absolute positioning
    G0 X{max_x / 2} Y215 F3600   ; park nozzle at rear
    M117 Finished!
## M204 Error beheben Macro
##
##
[gcode_macro M204]
rename_existing: M204.1
gcode:
  {% set f = params.F|default(0.5)|float %}
  {% if 'S' in params %}
    {% set s = params.S|float %}
    SET_VELOCITY_LIMIT ACCEL={s} ACCEL_TO_DECEL={ s * f }
  {% else %}
    {% if 'P' in params %}
      {% set p = params.P|float %}
      {% if 'T' in params %}
        {% set t = params.T|float %}
        {% if p < t %}
          SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
        {% else %}
          SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
        {% endif %}
      {% else %}
        SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
      {% endif %}
    {% elif 'T' in params %}
      {% set t = params.T|float %}
      SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
    {% endif %}
  {% endif %}
[gcode_macro PRIME_NOZZLE]
gcode:
    SAVE_GCODE_STATE NAME=PRIME_NOZZLE_STATE
    G90                 ; Absolute coordinates.
    M83                 ; Relative extruder mode.
    G92 E0
    ; Move to start of line.
    G1 Z10 F900
    G1 Y3 X3 F18000
    G1 Z0.2 F900
    ; Print the line.
    G91                ; Relative coordinates.
    G1 Y100 E20 F1000  ; Extrude filament 20mm (how much it retracted in PRINT_END).
    G1 X-2 F1000
    G1 Y-60 E9 F1000    ; Print second part of the line.
    G1 E-0.5 F3000      ; Retract to avoid stringing.
    G1 Y0.5 E0 F1000    ; Wipe back to break string.
    G1 Y-5.5 E0 F1000   ; Wipe forward to break string.
    RESTORE_GCODE_STATE NAME=PRIME_NOZZLE_STATE
#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [bed_mesh default]
#*# version = 1
#*# points =
#*#     0.921875, 0.818750, 0.834375, 0.756250, 0.696875, 0.696875, 0.668750
#*#     0.712500, 0.675000, 0.668750, 0.603125, 0.534375, 0.553125, 0.537500
#*#     0.593750, 0.543750, 0.565625, 0.537500, 0.453125, 0.481250, 0.475000
#*#     0.543750, 0.500000, 0.515625, 0.475000, 0.415625, 0.418750, 0.450000
#*#     0.515625, 0.462500, 0.481250, 0.465625, 0.421875, 0.431250, 0.450000
#*#     0.593750, 0.525000, 0.525000, 0.512500, 0.440625, 0.478125, 0.475000
#*#     0.703125, 0.650000, 0.656250, 0.587500, 0.546875, 0.537500, 0.543750
#*# tension = 0.2
#*# min_x = -0.004
#*# algo = bicubic
#*# y_count = 7
#*# mesh_y_pps = 2
#*# min_y = -0.004
#*# x_count = 7
#*# max_y = 199.976
#*# mesh_x_pps = 2
#*# max_x = 169.976
#*#
#*# [extruder]
#*# pid_kp = 18.426
#*# pid_ki = 0.698
#*# pid_kd = 121.609
#*# control = pid
#*#
#*# [skew_correction my_skew_profile]
#*# xy_skew = -0.00100110146555
#*# xz_skew = 0.0
#*# yz_skew = 0.0
#*#
#*# [heater_bed]
#*# control = pid
#*# pid_kp = 64.129
#*# pid_ki = 0.723
#*# pid_kd = 1421.255
#*#
#*# [bltouch]
#*# z_offset = 2.150