Serial communication control with python and SPI communication (using USBGPIO8 device)

About this article

Control serial communication with python and perform I2C communication (using USBGPIO8 device), so please read the I2C edition first.

About SPI communication

Although there are basic specifications, I think it is better to refer to the data sheet for each IC. The following is an easy-to-understand explanation of general SPI communication. http://www.picfun.com/f1/f05.html https://lab.fujiele.co.jp/articles/8191/

Caution

EEPROM (AT93C46) used in this article has CS (chip select) opposite to normal. ** HIGH to start communication and LOW to end communication. ** ** Generally, LOW starts communication and HIGH ends communication.

I should have experimented with a general IC, but I chose AT93C46 because AT93C46 was the cheapest at 30 yen among ICs that use SPI, and as a result, the article lacked versatility.

What to do

Control USBGPIO8 using serial communication with python, control IC (EEPROM) from USBGPIO8, write 2 bytes, then read 2 bytes Confirm that the written value can be read.

circuit diagram

Let CS be port 0 of USBGPIO8. Let SK be port 1 of USBGPIO8 (clock) Let the 2nd port of USBGPIO8 be DI Let port 3 of USBGPIO8 be DO

GPIO8_SPI_回路図.png

How to use EEPROM (AT93C46)

Hard setting

16-bit mode with Vcc connection for No. 6 (ORG) 8-bit mode with No. 6 (ORG) connected to GND This time, we will operate in 16bit mode. In 16-bit mode, 64 addresses x 2 bytes, a total of 128 bytes of data can be saved.

AT93C46 instruction set

・ EWEN write permission status ・ WRITE write ・ Read READ This time we will use these 3 instructions. In order to write, it is necessary to set to EWEN mode at startup, once it is set to EWEN mode, the mode is maintained until the power is turned off.

The transmitted data is SB + OP + [Data etc ...] It takes the configuration.

SB = [00000001] Fixed OP = [????????] Add the pattern + address, etc. specified by the mode. If there is data etc., add it.

Show a concrete example


EWEN mode

SB[00000001]
OP[00110000]
>0000000100110000
And send continuously

WRITE mode

SB[00000001]
OP[010?????]The lower 5 bits are the write address
Higher byte[????????]
Low byte[????????]
>00000001010?????????????????????
And send continuously

READ mode

SB[00000001]
OP[100?????]The lower 5 bits are the read address
Dummy for high-order byte[00000000]Dummy data to receive from DO
Dummy for low byte[00000000]Dummy data to receive from DO
>00000001100?????0000000000000000
And send continuously

Program structure

It is the same as Serial communication control with python and I2C communication (using USBGPIO8 device), so refer to that.

Source code


# usbgpio8_spi_read_write_sample.py

import serial
import sys
import time

SerialInstance = None

def SerialInit(comString):
    global SerialInstance
    #SerialInstance = serial.Serial(comString, 115200, timeout=0.01)
    SerialInstance = serial.Serial(comString, 19200, timeout=0.1)

def SerialEnd():
    SerialInstance.close()

def SerialTalk(cmd, response=False):
    readLen = len(cmd) + 1 # gpio read 0\n\r #From the beginning\Because it has r+Not 2+1 do
    if response == True:
        readLen += 3 # N\n\r
    readLen += 1 # >
    cnt = SerialInstance.write(cmd.encode())
    res = SerialInstance.read(readLen)
    res = res.decode("utf-8").strip()
    return res

def gpioHigh(n):
    SerialTalk("gpio set {}\r".format(n))

def gpioLow(n):
    SerialTalk("gpio clear {}\r".format(n))

def gpioRead(n):
    res = SerialTalk("gpio read {}\r".format(n), response=True)
    return res

def ByteToLH(b):
    lh = []
    for i in range(8):
        if (b << i & 128) == 0:
            lh.append(0)
        else:
            lh.append(1)
    return lh

def CS_LOW():
    gpioLow(0)

def CS_HIGH():
    gpioHigh(0)

def SCK_LOW():
    gpioLow(1)

def SCK_HIGH():
    gpioHigh(1)

def DI_LOW():
    gpioLow(2)

def DI_HIGH():
    gpioHigh(2)

def READ_DATA():
    return gpioRead(3)

def parseData(all):
    res = []
    for l in all:
        a = l.split("\n\r")
        res.append(a[1])
    return res

def SPI_CommandExec(cmd):
    # start condition
    size = len(cmd)
    data = []
    SCK_LOW()
    for i in range(size):
        d = cmd[i]
        if d == 0:
            DI_LOW()
        elif d == 1:
            DI_HIGH()
        SCK_HIGH()
        if d == 2:
            b = READ_DATA()
            data.append(b)
        SCK_LOW()
    return parseData(data)

def WriteBytes(addr, buffer1, buffer2):
    # EWEN command
    SB = ByteToLH(0b00000001)
    OP = ByteToLH(0b00110000)
    cmdEWEN = SB + OP
    
    # exec
    CS_HIGH()
    resEWEN = SPI_CommandExec(cmdEWEN)
    CS_LOW()
    
    # write command
    SB = ByteToLH(0b00000001)
    OP = ByteToLH(0b01000000 | (addr & 0x3f))
    buffer1 = ByteToLH(buffer1)
    buffer2 = ByteToLH(buffer2)
    cmdWrite = SB + OP + buffer1 + buffer2
    
    # exec
    CS_HIGH()
    resWrite = SPI_CommandExec(cmdWrite)
    CS_LOW()
    #Waiting for writing, 5ms by specification, but wait for a long time
    time.sleep(0.01)
    
    response = resEWEN + resWrite
    return response

def ReadBytes(addr):
    # create command
    SB = ByteToLH(0b00000001)
    OP = ByteToLH(0b10000000 | (addr & 0x3f))
    buffer1 = [2] * 8
    buffer2 = [2] * 8
    cmd = SB + OP + buffer1 + buffer2
    CS_HIGH()
    response = SPI_CommandExec(cmd)
    CS_LOW()
    return response


def Test_WriteBytes(comString):
    SerialInit(comString)
    
    response = WriteBytes(7, 0x34, 0x56)
    print(response)
    
    SerialEnd()

def Test_ReadBytes(comString):
    SerialInit(comString)
    
    response = ReadBytes(7)
    print(response)
    
    SerialEnd()

def run(comString):
    Test_WriteBytes(comString)
    Test_ReadBytes(comString)
    
if __name__ == "__main__":
    run(sys.argv[1])
# python usbgpio8_spi_read_write_sample.py COM4

How to use

For Linux

python usbgpio8_spi_read_write_sample.py /dev/ttyUSB0

For Windows

python usbgpio8_spi_read_write_sample.py COM4

How to read the return value

[] #Nothing in particular when writing
['0', '0', '1', '1', '0', '1', '0', '0', '0', '1', '0', '1', '0', '1', '1', '0'] #The first 8 bits are the read upper byte, and the following 8 bits are the read lower byte.

that's all

Recommended Posts

Serial communication control with python and SPI communication (using USBGPIO8 device)
Serial communication control with python and I2C communication (using USBGPIO8 device)
Serial communication with Python
Serial communication with python
Using Python and MeCab with Azure Databricks
Socket communication using socketserver with python now
[Python] [Windows] Serial communication in Python using DLL
I'm using tox and Python 3.3 with Travis-CI
Send and receive binary data via serial communication with python3 (on mac)
Let's control EV3 motors and sensors with Python
Socket communication with Python
WiringPi-SPI communication using Python
HTTP communication with Python
Control other programs from Python (communication between Python and exe)
Get and automate ASP Datepicker control using Python and Selenium
Version control of Node, Ruby and Python with anyenv
Non-linear simultaneous equations can be easily solved in Python.
Python iterators and generators
WiringPi-SPI communication using Python
Serial communication control with python and I2C communication (using USBGPIO8 device)
Scripts that can be used when using bottle in Python
Serial communication control with python and SPI communication (using USBGPIO8 device)
Programming with Python and Tkinter
Python and hardware-Using RS232C with Python-
[S3] CRUD with S3 using Python [Python]
Using Quaternion with Python ~ numpy-quaternion ~
[Python] Using OpenCV with Python (Basic)
IP spoof using tor on macOS and check with python
Using Python with SPSS Modeler extension nodes ① Setup and visualization
Introduction to serial communication [Python]
Instrument control using Python [pyvisa]
View Python communication with Fiddler
python with pyenv and venv
Using OpenCV with Python @Mac
This and that for using Step Functions with CDK + Python
Works with Python and R
Send using Python with Gmail
Control the motor with a motor driver using python on Raspberry Pi 3!
Communicate with FX-5204PS with Python and PyUSB
Complement python with emacs using company-jedi
Shining life with Python and OpenCV
Harmonic mean with Python Harmonic mean (using SciPy)
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
Scraping with Node, Ruby and Python
Using Rstan from Python with PypeR
Authentication using tweepy-User authentication and application authentication (Python)
[Python] Using OpenCV with Python (Image transformation)
Scraping with Python, Selenium and Chromedriver
Serial communication with Raspberry Pi + PySerial
[Python] Using OpenCV with Python (Edge Detection)
Scraping with Python and Beautiful Soup
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Just check serial communication with tk
Clustering and visualization using Python and CytoScape
Python3 socket module and socket communication flow
I played with PyQt5 and Python3
Notes on using rstrip with python.
Reading and writing CSV with Python
Try frequency control simulation with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)