TMC2209 UART - HELP

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Ragnar
Posts: 1
Joined: Mon Jun 14, 2021 8:54 pm

TMC2209 UART - HELP

Post by Ragnar » Mon Jun 14, 2021 9:13 pm

Hi guys,

Very new to the ESP32 and Micropython (basically new to everything) so bear with me if I'm missing something obvious.

I'm working with a ESP32 breakout board along with a TMC2209 breakout board. I'm attempting to communicate to the TM2209 over its UART interface. I've been successfully using the STEP/DIR to control a stepper motor but I need to change some of the configurations of the TMC2209 over UART.

For people unfamiliar to the TMC2209 here is a link to the datasheet: https://www.trinamic.com/fileadmin/asse ... t_V103.pdf
(If external links not allowed let me know)

Between the TX and RX pins I have a 1K resistor as described here:
https://www.instructables.com/UART-This ... s-With-th/

So far this is my test code:

Code: Select all

from machine import UART
import time

uart = UART(2, tx=17, rx=16)
uart.init(500000 , bits=8, parity=None, stop=1)


GCONF          =    0x00
IFCNT          =    0x02


stepper_id = 0
reg = 0

def compute_crc8_atm(datagram, initial_value=0):
    crc = initial_value
    # Iterate bytes in data
    for byte in datagram:
        # Iterate bits in byte
        for _ in range(0, 8):
            if (crc >> 7) ^ (byte & 0x01):
                crc = ((crc << 1) ^ 0x07) & 0xFF
            else:
                crc = (crc << 1) & 0xFF
            # Shift to next bit
            byte = byte >> 1
    return crc

def read_reg(mtr_id,reg):
    x = [0x55, 0, 0, 0]
    x[1] = mtr_id
    x[2] = reg
    x[3] = compute_crc8_atm(x[:-1])
    
    y = uart.write(bytes(x))
    if y != len(x):
        print("Err in read")
        return False
    if uart.any():
#         y = uart.read(8)
        print("bytes received")
        y = uart.read(8)
    else:
        y = 0
    time.sleep(.000005)
    
    return(y)

def write_reg(mtr_id,reg, val):
    x = [0x55, 0, 0, 0, 0, 0, 0, 0]
    x[1] = mtr_id
    x[2] = reg | 0x80
    x[3] = 0xFF & (val>>24)
    x[4] = 0xFF & (val>>16)
    x[5] = 0xFF & (val>>8)
    x[6] = 0xFF & val
    
    x[7] = compute_crc8_atm(x[:-1])
    
    y = uart.write(bytes(x))
    if y != len(x):
        print("Err in write")
        return False 
    time.sleep(.000002)
    
# def read_int(mtr_id, reg):
#     y = 1

z = 0
while True:
    z = z + 1
    time.sleep(2)
    print("##############################")
    a = read_reg(0, IFCNT)
    write_reg(0, GCONF, 0x0000000C)
    b = read_reg(0,IFCNT)
    #print(type(a))
    print("reading reg attempt", z)
    print("REG: ",a)
    print("REG: ",b)
    print("##############################")
    USRInput = input("read again?")
I'm not sure if there is something wrong with the CRC calculation or if I've completely missed a step. When I run the code and loop a couple times this is the output:

"##############################
bytes received
reading reg attempt 1
REG: 0
REG: b'U\x00\x02\x08\x05\xff\x02\x00'
##############################
read again?
##############################
bytes received
bytes received
reading reg attempt 2
REG: b'\x00\x00\x00LU\x00\x80\x00'
REG: b'\x00\x00\x0c\x8aU\x00\x02\x08'
##############################
read again?
##############################
bytes received
bytes received
reading reg attempt 3
REG: b'\x05\xff\x02\x00\x00\x00\x01\xc5'
REG: b'U\x00\x02\x08\x05\xff\x02\x00'
##############################
read again?"


I'm a little lost as to the next steps for debugging the problem. I've never done something like this before and would appreciate any feedback.

Thanks!
Ragnar

kjk25
Posts: 2
Joined: Sun Sep 12, 2021 6:52 pm

Re: TMC2209 UART - HELP

Post by kjk25 » Sun Sep 12, 2021 7:34 pm

Hi, thank you for the code ,
works now on my Lolin32

import machine
from machine import Pin
from machine import UART
from time import sleep

#DIAG_PIN = Pin(13,Pin.IN) #, Pin.PULL_UP)input STALL motor
EN_PIN = Pin(26,Pin.OUT) #output Enable
DIR_PIN = Pin(14,Pin.OUT) #output Direction
STEP_PIN = Pin(27,Pin.OUT)#output Step
DRIVER_ADDRESS = b'0' #TMC2209 Driver address according to MS1 and MS2
STALL_VALUE = 2 #[0..255]
uart2 = UART(2, baudrate=115200, tx=16, rx=17)

###############################
# TMC2209 registermap
GCONF = 0x00
GSTAT = 0x01
IFCNT = 0x02
SLAVECONF = 0x03
OTP_PROG = 0x04
OTP_READ = 0x05
IOIN = 0x06
FACTORY_CONF = 0x07
IHOLD_IRUN = 0x10
TPOWER_DOWN = 0x11
TSTEP = 0x12
TPWMTHRS = 0x13
TCOOLTHRS = 0x14
VACTUAL = 0x22
SGTHRS = 0x40
SG_RESULT = 0x41
COOLCONF = 0x42
MSCNT = 0x6A
MSCURACT = 0x6B
CHOPCONF = 0x6C
DRV_STATUS = 0x6F
PWMCONF = 0x70
PWM_SCALE = 0x71
PWM_AUTO = 0x72

###############################
def compute_crc8_atm(datagram, initial_value=0):
crc = initial_value
# Iterate bytes in data
for byte in datagram:
# Iterate bits in byte
for _ in range(0, 8):
if (crc >> 7) ^ (byte & 0x01):
crc = ((crc << 1) ^ 0x07) & 0xFF
else:
crc = (crc << 1) & 0xFF
# Shift to next bit
byte = byte >> 1
return crc

def read_reg(mtr_id,reg):
global uart2
x = [0x55, 0, 0, 0]
x[1] = mtr_id
x[2] = reg
x[3] = compute_crc8_atm(x[:-1])
y = uart2.write(bytes(x))
if y != len(x):
print("Err in read")
return False
sleep(.01)
if uart2.any():
y = uart2.read(4)#read what it self send and trash it (RX and TX one line)
y = uart2.read()
else:
y = 0
sleep(.000005)
return(y)

def write_reg(mtr_id,reg, val):
global uart2
x = [0x55, 0, 0, 0, 0, 0, 0, 0]
x[1] = mtr_id
x[2] = reg | 0x80
x[3] = 0xFF & (val>>24)
x[4] = 0xFF & (val>>16)
x[5] = 0xFF & (val>>8)
x[6] = 0xFF & val
x[7] = compute_crc8_atm(x[:-1])
y = uart2.write(bytes(x))
if y != len(x):
print("Err in write")
return False
sleep(.01)
if uart2.any():
y = uart2.read()#read what it self send and trash it (RX and TX one line)
sleep(.000002)

def init():
print("init")
global uart2
uart2.init(115200 , bits=8, parity=None, stop=1)

EN_PIN.value(0)
DIR_PIN.value(0)

z = 0
while True:
z = z + 1
a = read_reg(0, IOIN)
print(a[3],a[4],a[5],a[6])
print("##############################")
a = read_reg(0, IFCNT)
write_reg(0, GCONF, 0x000000020)
b = read_reg(0, IFCNT)
print("reading reg attempt", z)
for i in bytearray(a):
print("REG: ", hex(i))
print("-")
for i in bytearray(b):
print("REG: ", hex(i))
print("##############################")
USRInput = input("read again?")
init()

Post Reply