rshell mostly works, the one exception I've found in a brief trial being copying files to the WiPy. Support over telnet would be brilliantOnce my WiPy arrives, I was planning on testing it out with that to add support over telnet.
I2C/SPI gateway for a PC
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: I2C/SPI gateway for a PC
@dhylands
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 70
- Joined: Wed Oct 14, 2015 5:07 pm
Re: I2C/SPI gateway for a PC
OK here is the code I use on the PC to pretend to be a bbio i2c device.
This way I can run and debug the code on the pc and not on the BBB (BeagleBoneBlack)
The problem is seeing is that very occasionally (and this is bashing hell out of the raw repl) is that it rejects a command with syntax error, which implies one of the bytes got corrupted somehow, this happens with normal repl and raw repl.
So I suspect that there might be a small problem with the repl input handler. In the code above I just retry but it shouldn't happen
This way I can run and debug the code on the pc and not on the BBB (BeagleBoneBlack)
Code: Select all
# need to reference the win32 serial stuff
import serial
import time
import binascii
# mocks the bbio interface
# and talks to the attached pyb board using its repl
class I2CDev:
pass
def __init__(self, bus):
# open the serial port
self.serial = serial.Serial("COM3", 115200, timeout=1)
pass
def enter_repl(self):
self.serial.write(b'\r\x02\x04') # soft reset
time.sleep(0.25) # small delay
self.serial.flushInput()
#data = self.serial.read
# send ^A to go into raw repl
# leaving the '>' in the input buffer
self.serial.write(b'\x01')
data = self.serial.readline();
data = self.serial.readline();
pass
def read_packet(self): # , until):
packet = self.serial.read(1)
while not packet.endswith(b'\x04'):
packet += self.serial.read(1)
return packet[:-1]
def send_command(self, cmd):
print(cmd)
#check weve got a prompt
data = self.serial.read(1)
if data != b'>':
#pass
raise IOError
# send out the data
self.serial.write(bytes(cmd, encoding='utf8'))
# send eof
self.serial.write(b'\x04')
# check OK
data = self.serial.read(2)
if data != b'OK':
raise IOError
# pick up answers
reply = self.read_packet()
error = self.read_packet()
print(reply)
return reply, error
def eval_command(self, cmd):
return self.send_command("print({})".format(cmd))
def open(self):
self.enter_repl()
# and establish an i2c object to chat with
self.send_command("from pyb import I2C")
self.send_command("import ubinascii")
self.send_command("i2c=I2C(1,I2C.MASTER)")
def write(self, address, data):
# formay python statement to run on the pyb
s = "i2c.send(bytes({0}),{1})".format(data, address).replace(' ','')
self.send_command( s ) #"i2c.send(bytes({0}),{1})".format(data, address))
def readTransaction(self, address, reg, count):
try:
data, error = self.eval_command("ubinascii.hexlify(i2c.mem_read({0},{1},{2}))".format(count, address, reg))
# b'00[nnnnnnnnnnn]'\r\n
# nb occasionally we get back empty in repl and raw repl modes
r = data[2:-3]
# ================================================
if len(r) != 2 * count:
print("WTF!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
# did we get an error
self.open()
data, error = self.eval_command("ubinascii.hexlify(i2c.mem_read({0},{1},{2}))".format(count, address, reg))
r = data[2:-3]
# ================================================
wtf = binascii.unhexlify(r)
return list(wtf) ##[0]
except:
# reallocate or nothing works??
self.open()
#raise IOError
So I suspect that there might be a small problem with the repl input handler. In the code above I just retry but it shouldn't happen