I2C/SPI gateway for a PC

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C/SPI gateway for a PC

Post by pythoncoder » Sat Oct 17, 2015 7:46 am

@dhylands
Once my WiPy arrives, I was planning on testing it out with that to add support over telnet.
rshell mostly works, the one exception I've found in a brief trial being copying files to the WiPy. Support over telnet would be brilliant :)
Peter Hinch
Index to my micropython libraries.

Peter.Kenyon
Posts: 70
Joined: Wed Oct 14, 2015 5:07 pm

Re: I2C/SPI gateway for a PC

Post by Peter.Kenyon » Sun Oct 18, 2015 11:39 am

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)

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
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

Post Reply