mirror of https://github.com/Desuuuu/klipper.git
tmc2208: Initial support for configuring TMC2208 drivers
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
afd5d55c00
commit
ca7a80a946
|
@ -584,6 +584,59 @@
|
|||
# to an appropriate sensitivity value.) The default is to not enable
|
||||
# sensorless homing.
|
||||
|
||||
# Configure a TMC2208 (or TMC2224) stepper motor driver via single
|
||||
# wire UART. To use this feature, define a config section with a
|
||||
# "tmc2208" prefix followed by the name of the corresponding stepper
|
||||
# config section (for example, "[tmc2208 stepper_x]").
|
||||
#[tmc2208 stepper_x]
|
||||
#uart_pin:
|
||||
# The pin connected to the TMC2208 PDN_UART line. This parameter
|
||||
# must be provided.
|
||||
#tx_pin:
|
||||
# If using separate receive and transmit lines to communicate with
|
||||
# the driver then set uart_pin to the receive pin and tx_pin to the
|
||||
# transmit pin. The default is to use uart_pin for both reading and
|
||||
# writing.
|
||||
#microsteps:
|
||||
# The number of microsteps to configure the driver to use. Valid
|
||||
# values are 1, 2, 4, 8, 16, 32, 64, 128, 256. This parameter must
|
||||
# be provided.
|
||||
#interpolate: True
|
||||
# If true, enable step interpolation (the driver will interally step
|
||||
# at a rate of 256 micro-steps). The default is True.
|
||||
#run_current:
|
||||
# The amount of current (in amps) to configure the driver to use
|
||||
# during stepper movement. This parameter must be provided.
|
||||
#hold_current:
|
||||
# The amount of current (in amps) to configure the driver to use
|
||||
# when the stepper is not moving. The default is to use the same
|
||||
# value as run_current.
|
||||
#sense_resistor: 0.110
|
||||
# The resistance (in ohms) of the motor sense resistor. The default
|
||||
# is 0.110 ohms.
|
||||
#stealthchop_threshold: 0
|
||||
# The velocity (in mm/s) to set the "stealthChop" threshold to. When
|
||||
# set, "stealthChop" mode will be enabled if the stepper motor
|
||||
# velocity is below this value. The default is 0, which disables
|
||||
# "stealthChop" mode.
|
||||
#driver_IHOLDDELAY: 8
|
||||
#driver_TPOWERDOWN: 20
|
||||
#driver_BLANK_TIME_SELECT: 2
|
||||
#driver_TOFF: 3
|
||||
#driver_HEND: 0
|
||||
#driver_HSTRT: 5
|
||||
#driver_PWM_AUTOGRAD: True
|
||||
#driver_PWM_AUTOSCALE: True
|
||||
#driver_PWM_LIM: 12
|
||||
#driver_PWM_REG: 8
|
||||
#driver_PWM_FREQ: 1
|
||||
#driver_PWM_GRAD: 14
|
||||
#driver_PWM_OFS: 36
|
||||
# Set the given register during the configuration of the TMC2208
|
||||
# chip. This may be used to set custom motor parameters. The
|
||||
# defaults for each parameter are next to the parameter name in the
|
||||
# above list.
|
||||
|
||||
|
||||
# Homing override. One may use this mechanism to run a series of
|
||||
# g-code commands in place of a G28 found in the normal g-code input.
|
||||
|
|
|
@ -0,0 +1,234 @@
|
|||
# TMC2208 UART communication and configuration
|
||||
#
|
||||
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
||||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import math, logging, collections
|
||||
|
||||
TMC_FREQUENCY=12000000.
|
||||
GCONF_PDN_DISABLE = 1<<6
|
||||
GCONF_MSTEP_REG_SELECT = 1<<7
|
||||
GCONF_MULTISTEP_FILT = 1<<8
|
||||
|
||||
Registers = {
|
||||
"GCONF": 0x00, "GSTAT": 0x01, "IFCNT": 0x02, "SLAVECONF": 0x03,
|
||||
"OTP_PROG": 0x04, "OTP_READ": 0x05, "IOIN": 0x06, "FACTORY_CONF": 0x07,
|
||||
"IHOLD_IRUN": 0x10, "TPOWERDOWN": 0x11, "TSTEP": 0x12, "TPWMTHRS": 0x13,
|
||||
"VACTUAL": 0x22, "MSCNT": 0x6a, "MSCURACT": 0x6b, "CHOPCONF": 0x6c,
|
||||
"DRV_STATUS": 0x6f, "PWMCONF": 0x70, "PWM_SCALE": 0x71, "PWM_AUTO": 0x72
|
||||
}
|
||||
|
||||
ReadRegisters = [
|
||||
"GCONF", "GSTAT", "IFCNT", "OTP_READ", "IOIN", "FACTORY_CONF", "TSTEP",
|
||||
"MSCNT", "MSCURACT", "CHOPCONF", "DRV_STATUS",
|
||||
"PWMCONF", "PWM_SCALE", "PWM_AUTO"
|
||||
]
|
||||
|
||||
|
||||
######################################################################
|
||||
# TMC2208 communication
|
||||
######################################################################
|
||||
|
||||
# Generate a CRC8-ATM value for a bytearray
|
||||
def calc_crc8(data):
|
||||
crc = 0
|
||||
for b in data:
|
||||
for i in range(8):
|
||||
if (crc >> 7) ^ (b & 0x01):
|
||||
crc = (crc << 1) ^ 0x07
|
||||
else:
|
||||
crc = (crc << 1)
|
||||
crc &= 0xff
|
||||
b >>= 1
|
||||
return crc
|
||||
|
||||
# Add serial start and stop bits to a message in a bytearray
|
||||
def add_serial_bits(data):
|
||||
out = 0
|
||||
pos = 0
|
||||
for d in data:
|
||||
b = (d << 1) | 0x200
|
||||
out |= (b << pos)
|
||||
pos += 10
|
||||
res = bytearray()
|
||||
for i in range((pos+7)//8):
|
||||
res.append((out >> (i*8)) & 0xff)
|
||||
return res
|
||||
|
||||
# Generate a tmc2208 read register message
|
||||
def encode_tmc2208_read(sync, addr, reg):
|
||||
msg = bytearray([sync, addr, reg])
|
||||
msg.append(calc_crc8(msg))
|
||||
return add_serial_bits(msg)
|
||||
|
||||
# Generate a tmc2208 write register message
|
||||
def encode_tmc2208_write(sync, addr, reg, val):
|
||||
msg = bytearray([sync, addr, reg, (val >> 24) & 0xff, (val >> 16) & 0xff,
|
||||
(val >> 8) & 0xff, val & 0xff])
|
||||
msg.append(calc_crc8(msg))
|
||||
return add_serial_bits(msg)
|
||||
|
||||
# Extract a tmc2208 read response message
|
||||
def decode_tmc2208_read(reg, data):
|
||||
# Convert data into a long integer for easy manipulation
|
||||
if len(data) != 10:
|
||||
return None
|
||||
mval = pos = 0
|
||||
for d in bytearray(data):
|
||||
mval |= d << pos
|
||||
pos += 8
|
||||
# Extract register value
|
||||
val = ((((mval >> 31) & 0xff) << 24) | (((mval >> 41) & 0xff) << 16)
|
||||
| (((mval >> 51) & 0xff) << 8) | ((mval >> 61) & 0xff))
|
||||
# Verify start/stop bits and crc
|
||||
encoded_data = encode_tmc2208_write(0x05, 0xff, reg, val)
|
||||
if data != encoded_data:
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
######################################################################
|
||||
# TMC2208 printer object
|
||||
######################################################################
|
||||
|
||||
class TMC2208:
|
||||
def __init__(self, config):
|
||||
self.printer = config.get_printer()
|
||||
self.name = config.get_name().split()[1]
|
||||
# pin setup
|
||||
ppins = self.printer.lookup_object("pins")
|
||||
rx_pin_params = ppins.lookup_pin(
|
||||
config.get('uart_pin'), can_pullup=True)
|
||||
tx_pin_desc = config.get('tx_pin', None)
|
||||
if tx_pin_desc is None:
|
||||
tx_pin_params = rx_pin_params
|
||||
else:
|
||||
tx_pin_params = ppins.lookup_pin(tx_pin_desc)
|
||||
if rx_pin_params['chip'] is not tx_pin_params['chip']:
|
||||
raise pins.error("TMC2208 rx and tx pins must be on the same mcu")
|
||||
self.mcu = rx_pin_params['chip']
|
||||
self.pullup = rx_pin_params['pullup']
|
||||
self.rx_pin = rx_pin_params['pin']
|
||||
self.tx_pin = tx_pin_params['pin']
|
||||
self.oid = self.mcu.create_oid()
|
||||
self.tmcuart_send_cmd = None
|
||||
self.mcu.add_config_object(self)
|
||||
# Add DUMP_TMC command
|
||||
gcode = self.printer.lookup_object("gcode")
|
||||
gcode.register_mux_command(
|
||||
"DUMP_TMC", "STEPPER", self.name,
|
||||
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
||||
# Get config for initial driver settings
|
||||
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
||||
hold_current = config.getfloat('hold_current', run_current,
|
||||
above=0., maxval=2.)
|
||||
sense_resistor = config.getfloat('sense_resistor', 0.110, above=0.)
|
||||
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
|
||||
'8': 5, '4': 6, '2': 7, '1': 8}
|
||||
self.mres = config.getchoice('microsteps', steps)
|
||||
interpolate = config.getboolean('interpolate', True)
|
||||
sc_velocity = config.getfloat('stealthchop_threshold', 0., minval=0.)
|
||||
sc_threshold = self.velocity_to_clock(config, sc_velocity)
|
||||
iholddelay = config.getint('driver_IHOLDDELAY', 8, minval=0, maxval=15)
|
||||
tpowerdown = config.getint('driver_TPOWERDOWN', 20, minval=0, maxval=255)
|
||||
blank_time_select = config.getint('driver_BLANK_TIME_SELECT', 2,
|
||||
minval=0, maxval=3)
|
||||
toff = config.getint('driver_TOFF', 3, minval=1, maxval=15)
|
||||
hend = config.getint('driver_HEND', 0, minval=0, maxval=15)
|
||||
hstrt = config.getint('driver_HSTRT', 5, minval=0, maxval=7)
|
||||
pwm_autograd = config.getboolean('driver_PWM_AUTOGRAD', True)
|
||||
pwm_autoscale = config.getboolean('driver_PWM_AUTOSCALE', True)
|
||||
pwm_lim = config.getint('driver_PWM_LIM', 12, minval=0, maxval=15)
|
||||
pwm_reg = config.getint('driver_PWM_REG', 8, minval=0, maxval=15)
|
||||
pwm_freq = config.getint('driver_PWM_FREQ', 1, minval=0, maxval=3)
|
||||
pwm_grad = config.getint('driver_PWM_GRAD', 14, minval=0, maxval=255)
|
||||
pwm_ofs = config.getint('driver_PWM_OFS', 36, minval=0, maxval=255)
|
||||
# calculate current
|
||||
vsense = False
|
||||
irun = self.current_bits(run_current, sense_resistor, vsense)
|
||||
ihold = self.current_bits(hold_current, sense_resistor, vsense)
|
||||
if irun < 16 and ihold < 16:
|
||||
vsense = True
|
||||
irun = self.current_bits(run_current, sense_resistor, vsense)
|
||||
ihold = self.current_bits(hold_current, sense_resistor, vsense)
|
||||
# Configure registers
|
||||
self.ifcnt = None
|
||||
self.init_regs = collections.OrderedDict()
|
||||
self.init_regs['GCONF'] = (
|
||||
((sc_velocity == 0.) << 2) | GCONF_PDN_DISABLE
|
||||
| GCONF_MSTEP_REG_SELECT | GCONF_MULTISTEP_FILT)
|
||||
self.init_regs['CHOPCONF'] = (
|
||||
toff | (hstrt << 4) | (hend << 7) | (blank_time_select << 15)
|
||||
| (vsense << 17) | (self.mres << 24) | (interpolate << 28))
|
||||
self.init_regs['IHOLD_IRUN'] = ihold | (irun << 8) | (iholddelay << 16)
|
||||
self.init_regs['TPOWERDOWN'] = tpowerdown
|
||||
self.init_regs['TPWMTHRS'] = max(0, min(0xfffff, sc_threshold))
|
||||
self.init_regs['PWMCONF'] = (
|
||||
pwm_ofs | (pwm_grad << 8) | (pwm_freq << 16)
|
||||
| (pwm_autoscale << 18) | (pwm_autograd << 19)
|
||||
| (pwm_reg << 24) | (pwm_lim << 28))
|
||||
def current_bits(self, current, sense_resistor, vsense_on):
|
||||
sense_resistor += 0.020
|
||||
vsense = 0.32
|
||||
if vsense_on:
|
||||
vsense = 0.18
|
||||
cs = int(32. * current * sense_resistor * math.sqrt(2.) / vsense
|
||||
- 1. + .5)
|
||||
return max(0, min(31, cs))
|
||||
def velocity_to_clock(self, config, velocity):
|
||||
if not velocity:
|
||||
return 0
|
||||
stepper_name = config.get_name().split()[1]
|
||||
stepper_config = config.getsection(stepper_name)
|
||||
step_dist = stepper_config.getfloat('step_distance')
|
||||
step_dist_256 = step_dist / (1 << self.mres)
|
||||
return int(TMC_FREQUENCY * step_dist_256 / velocity + .5)
|
||||
def build_config(self):
|
||||
bit_ticks = int(self.mcu.get_adjusted_freq() / 9000.)
|
||||
self.mcu.add_config_cmd(
|
||||
"config_tmcuart oid=%d rx_pin=%s pull_up=%d tx_pin=%s bit_time=%d"
|
||||
% (self.oid, self.rx_pin, self.pullup, self.tx_pin, bit_ticks))
|
||||
cmd_queue = self.mcu.alloc_command_queue()
|
||||
self.tmcuart_send_cmd = self.mcu.lookup_command(
|
||||
"tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue)
|
||||
def printer_state(self, state):
|
||||
if state == 'ready':
|
||||
for reg_name, val in self.init_regs.items():
|
||||
self.set_register(reg_name, val)
|
||||
def get_register(self, reg_name):
|
||||
reg = Registers[reg_name]
|
||||
msg = encode_tmc2208_read(0xf5, 0x00, reg)
|
||||
for retry in range(5):
|
||||
params = self.tmcuart_send_cmd.send_with_response(
|
||||
[self.oid, msg, 10], 'tmcuart_response', self.oid)
|
||||
val = decode_tmc2208_read(reg, params['read'])
|
||||
if val is not None:
|
||||
return val
|
||||
raise self.printer.config_error(
|
||||
"Unable to read tmc2208 '%s' register %s" % (self.name, reg_name))
|
||||
def set_register(self, reg_name, val):
|
||||
msg = encode_tmc2208_write(0xf5, 0x00, Registers[reg_name] | 0x80, val)
|
||||
for retry in range(5):
|
||||
ifcnt = self.ifcnt
|
||||
if ifcnt is None:
|
||||
self.ifcnt = ifcnt = self.get_register("IFCNT")
|
||||
params = self.tmcuart_send_cmd.send_with_response(
|
||||
[self.oid, msg, 0], 'tmcuart_response', self.oid)
|
||||
self.ifcnt = self.get_register("IFCNT")
|
||||
if self.ifcnt == (ifcnt + 1) & 0xff:
|
||||
return
|
||||
raise self.printer.config_error(
|
||||
"Unable to write tmc2208 '%s' register %s" % (self.name, reg_name))
|
||||
cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
|
||||
def cmd_DUMP_TMC(self, params):
|
||||
self.printer.lookup_object('toolhead').get_last_move_time()
|
||||
gcode = self.printer.lookup_object('gcode')
|
||||
logging.info("DUMP_TMC %s", self.name)
|
||||
for reg_name in ReadRegisters:
|
||||
val = self.get_register(reg_name)
|
||||
msg = "%-15s %08x" % (reg_name + ":", val)
|
||||
logging.info(msg)
|
||||
gcode.respond_info(msg)
|
||||
|
||||
def load_config_prefix(config):
|
||||
return TMC2208(config)
|
|
@ -5,4 +5,5 @@ src-$(CONFIG_HAVE_GPIO) += gpiocmds.c stepper.c endstop.c
|
|||
src-$(CONFIG_HAVE_GPIO_ADC) += adccmds.c
|
||||
src-$(CONFIG_HAVE_GPIO_SPI) += spicmds.c thermocouple.c
|
||||
src-$(CONFIG_HAVE_GPIO_HARD_PWM) += pwmcmds.c
|
||||
src-$(CONFIG_HAVE_GPIO_BITBANGING) += lcd_st7920.c lcd_hd44780.c buttons.c
|
||||
src-$(CONFIG_HAVE_GPIO_BITBANGING) += lcd_st7920.c lcd_hd44780.c buttons.c \
|
||||
tmcuart.c
|
||||
|
|
|
@ -0,0 +1,249 @@
|
|||
// Commands for sending messages to a TMC2208 via its single wire UART
|
||||
//
|
||||
// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "board/gpio.h" // gpio_out_write
|
||||
#include "board/irq.h" // irq_disable
|
||||
#include "board/misc.h" // timer_read_time
|
||||
#include "basecmd.h" // oid_alloc
|
||||
#include "command.h" // DECL_COMMAND
|
||||
#include "sched.h" // DECL_SHUTDOWN
|
||||
|
||||
struct tmcuart_s {
|
||||
struct timer timer;
|
||||
struct gpio_out tx_pin;
|
||||
struct gpio_in rx_pin;
|
||||
uint8_t flags;
|
||||
uint8_t pos, read_count, write_count;
|
||||
uint32_t cfg_bit_time, bit_time;
|
||||
uint8_t data[10];
|
||||
};
|
||||
|
||||
enum {
|
||||
TU_LINE_HIGH = 1<<0, TU_ACTIVE = 1<<1, TU_READ_SYNC = 1<<2,
|
||||
TU_REPORT = 1<<3, TU_PULLUP = 1<<4, TU_SINGLE_WIRE = 1<<5
|
||||
};
|
||||
|
||||
static struct task_wake tmcuart_wake;
|
||||
|
||||
// Restore uart line to normal "idle" mode
|
||||
static void
|
||||
tmcuart_reset_line(struct tmcuart_s *t)
|
||||
{
|
||||
if (t->flags & TU_SINGLE_WIRE)
|
||||
gpio_out_reset(t->tx_pin, 1);
|
||||
else
|
||||
gpio_out_write(t->tx_pin, 1);
|
||||
t->flags = (t->flags & (TU_PULLUP | TU_SINGLE_WIRE)) | TU_LINE_HIGH;
|
||||
}
|
||||
|
||||
// Helper function to end a transmission and schedule a response
|
||||
static uint_fast8_t
|
||||
tmcaurt_finalize(struct tmcuart_s *t)
|
||||
{
|
||||
tmcuart_reset_line(t);
|
||||
t->flags |= TU_REPORT;
|
||||
sched_wake_task(&tmcuart_wake);
|
||||
return SF_DONE;
|
||||
}
|
||||
|
||||
// Event handler for reading uart bits
|
||||
static uint_fast8_t
|
||||
tmcuart_read_event(struct timer *timer)
|
||||
{
|
||||
struct tmcuart_s *t = container_of(timer, struct tmcuart_s, timer);
|
||||
uint8_t v = gpio_in_read(t->rx_pin);
|
||||
// Read and store bit
|
||||
uint8_t pos = t->pos, mask = 1 << (pos & 0x07), data = t->data[pos >> 3];
|
||||
if (v)
|
||||
data |= mask;
|
||||
else
|
||||
data &= ~mask;
|
||||
t->data[pos >> 3] = data;
|
||||
pos++;
|
||||
if (pos >= t->read_count)
|
||||
return tmcaurt_finalize(t);
|
||||
t->pos = pos;
|
||||
t->timer.waketime += t->bit_time;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
|
||||
// Event handler for detecting start of data reception
|
||||
static uint_fast8_t
|
||||
tmcuart_read_sync_event(struct timer *timer)
|
||||
{
|
||||
struct tmcuart_s *t = container_of(timer, struct tmcuart_s, timer);
|
||||
uint8_t v = gpio_in_read(t->rx_pin);
|
||||
if (v) {
|
||||
t->flags |= TU_READ_SYNC;
|
||||
} else if (t->flags & TU_READ_SYNC) {
|
||||
// Now synchronized - begin reading
|
||||
t->pos = 0;
|
||||
t->timer.func = tmcuart_read_event;
|
||||
return tmcuart_read_event(timer);
|
||||
}
|
||||
if (t->pos++ >= 64) {
|
||||
// Timeout
|
||||
t->read_count = 0;
|
||||
return tmcaurt_finalize(t);
|
||||
}
|
||||
t->timer.waketime += t->bit_time;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
|
||||
// Event handler called at end of uart writing
|
||||
static uint_fast8_t
|
||||
tmcuart_send_finish_event(struct timer *timer)
|
||||
{
|
||||
struct tmcuart_s *t = container_of(timer, struct tmcuart_s, timer);
|
||||
if (!t->read_count)
|
||||
// This is a tx only operation - success
|
||||
return tmcaurt_finalize(t);
|
||||
// Prepare for message rx
|
||||
if (t->flags & TU_SINGLE_WIRE)
|
||||
gpio_in_reset(t->rx_pin, t->flags & TU_PULLUP);
|
||||
t->pos = 0;
|
||||
t->timer.func = tmcuart_read_sync_event;
|
||||
t->timer.waketime += t->bit_time * 4;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
|
||||
// Event handler for sending uart bits
|
||||
static uint_fast8_t
|
||||
tmcuart_send_event(struct timer *timer)
|
||||
{
|
||||
struct tmcuart_s *t = container_of(timer, struct tmcuart_s, timer);
|
||||
// Toggle uart output
|
||||
gpio_out_toggle_noirq(t->tx_pin);
|
||||
t->flags ^= TU_LINE_HIGH;
|
||||
// Determine next toggle time
|
||||
uint8_t line_state = t->flags & TU_LINE_HIGH;
|
||||
uint32_t bit_time = t->bit_time, next = bit_time;
|
||||
uint8_t pos = t->pos;
|
||||
for (;;) {
|
||||
pos++;
|
||||
if (pos >= t->write_count) {
|
||||
// No more toggles necessary - schedule finish event
|
||||
t->timer.func = tmcuart_send_finish_event;
|
||||
t->timer.waketime += next;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
uint8_t data = t->data[pos >> 3], bit = (data >> (pos & 0x07)) & 0x01;
|
||||
if (bit != line_state)
|
||||
break;
|
||||
next += bit_time;
|
||||
}
|
||||
t->pos = pos;
|
||||
t->timer.waketime += next;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
|
||||
// Event handler for sending sync nibble with enhanced baud detection
|
||||
static uint_fast8_t
|
||||
tmcuart_send_sync_event(struct timer *timer)
|
||||
{
|
||||
struct tmcuart_s *t = container_of(timer, struct tmcuart_s, timer);
|
||||
// Toggle uart output and note toggle time
|
||||
gpio_out_toggle_noirq(t->tx_pin);
|
||||
uint32_t cur = timer_read_time();
|
||||
t->flags ^= TU_LINE_HIGH;
|
||||
// Determine next wakeup time
|
||||
t->pos++;
|
||||
if (t->pos == 1) {
|
||||
// First bit just sent - record scheduling offset for later use
|
||||
uint32_t offset = cur - t->timer.waketime;
|
||||
t->bit_time = offset;
|
||||
} else if (t->pos >= 5) {
|
||||
// Last bit of sync nibble just sent - calculate actual baud rate used
|
||||
uint32_t offset = cur - t->timer.waketime, start_offset = t->bit_time;
|
||||
int32_t diff = offset - start_offset;
|
||||
t->bit_time = t->cfg_bit_time + DIV_ROUND_CLOSEST(diff, 4);
|
||||
t->timer.func = tmcuart_send_event;
|
||||
t->timer.waketime += diff + t->bit_time;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
t->timer.waketime += t->cfg_bit_time;
|
||||
return SF_RESCHEDULE;
|
||||
}
|
||||
|
||||
void
|
||||
command_config_tmcuart(uint32_t *args)
|
||||
{
|
||||
struct tmcuart_s *t = oid_alloc(args[0], command_config_tmcuart
|
||||
, sizeof(*t));
|
||||
uint8_t pull_up = args[2];
|
||||
uint32_t rx_pin = args[1], tx_pin = args[3];
|
||||
t->rx_pin = gpio_in_setup(rx_pin, pull_up);
|
||||
t->tx_pin = gpio_out_setup(tx_pin, 1);
|
||||
t->cfg_bit_time = args[4];
|
||||
t->flags = (TU_LINE_HIGH | (pull_up ? TU_PULLUP : 0)
|
||||
| (rx_pin == tx_pin ? TU_SINGLE_WIRE : 0));
|
||||
}
|
||||
DECL_COMMAND(command_config_tmcuart,
|
||||
"config_tmcuart oid=%c rx_pin=%u pull_up=%c"
|
||||
" tx_pin=%u bit_time=%u");
|
||||
|
||||
// Parse and schedule a TMC UART transmission request
|
||||
void
|
||||
command_tmcuart_send(uint32_t *args)
|
||||
{
|
||||
struct tmcuart_s *t = oid_lookup(args[0], command_config_tmcuart);
|
||||
if (t->flags & TU_ACTIVE)
|
||||
// Uart is busy - silently drop this request (host should retransmit)
|
||||
return;
|
||||
uint8_t write_len = args[1];
|
||||
uint8_t *write = (void*)(size_t)args[2];
|
||||
uint8_t read_len = args[3];
|
||||
if (write_len > sizeof(t->data) || read_len > sizeof(t->data))
|
||||
shutdown("tmcuart data too large");
|
||||
memcpy(t->data, write, write_len);
|
||||
t->pos = 0;
|
||||
t->flags = (t->flags & (TU_LINE_HIGH|TU_PULLUP|TU_SINGLE_WIRE)) | TU_ACTIVE;
|
||||
t->write_count = write_len * 8;
|
||||
t->read_count = read_len * 8;
|
||||
if (write_len >= 1 && (t->data[0] & 0x3f) == 0x2a) {
|
||||
t->timer.func = tmcuart_send_sync_event;
|
||||
} else {
|
||||
t->bit_time = t->cfg_bit_time;
|
||||
t->timer.func = tmcuart_send_event;
|
||||
}
|
||||
irq_disable();
|
||||
t->timer.waketime = timer_read_time() + timer_from_us(200);
|
||||
sched_add_timer(&t->timer);
|
||||
irq_enable();
|
||||
}
|
||||
DECL_COMMAND(command_tmcuart_send, "tmcuart_send oid=%c write=%*s read=%c");
|
||||
|
||||
// Report completed response message back to host
|
||||
void
|
||||
tmcuart_task(void)
|
||||
{
|
||||
if (!sched_check_wake(&tmcuart_wake))
|
||||
return;
|
||||
uint8_t oid;
|
||||
struct tmcuart_s *t;
|
||||
foreach_oid(oid, t, command_config_tmcuart) {
|
||||
if (!(t->flags & TU_REPORT))
|
||||
continue;
|
||||
irq_disable();
|
||||
t->flags &= ~TU_REPORT;
|
||||
irq_enable();
|
||||
sendf("tmcuart_response oid=%c read=%*s"
|
||||
, oid, t->read_count / 8, t->data);
|
||||
}
|
||||
}
|
||||
DECL_TASK(tmcuart_task);
|
||||
|
||||
void
|
||||
tmcuart_shutdown(void)
|
||||
{
|
||||
uint8_t i;
|
||||
struct tmcuart_s *t;
|
||||
foreach_oid(i, t, command_config_tmcuart) {
|
||||
tmcuart_reset_line(t);
|
||||
}
|
||||
}
|
||||
DECL_SHUTDOWN(tmcuart_shutdown);
|
Loading…
Reference in New Issue