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?")
"##############################
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