improve speed

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
cyberlab
Posts: 56
Joined: Sat Apr 21, 2018 5:02 am

improve speed

Post by cyberlab » Sat Oct 06, 2018 3:06 am

Hello everyone, at this moment I have an application in a module esp8266 that is connected to a machine through a parallel port where the parallel port in the esp8266 is by means of a PCF8574P.
I send to the industrial machine a file via wifi through a program that I elaborated through Labview.
all the information required by the operator of the machine appears on a screen oled, when the operator wants it, it loads in the machine the last file sent to esp8266.
everything works perfectly, but I would like to improve the speed of sending the file to the machine, currently sending 40 bytes per second.
I have the cpu speed at 160 mhz.
the freq. of port I2C in 400,000 attached part of the code I want to improve, I think it would be a good idea to remove the PCF8574P and write the output byte in 8 pins of esp8266, I have pins available, but I have not found any module or instruction to write simultaneously in 8 pins, any ideas? Thanks in advance.

Code: Select all

                with open("Design.DST", "rb") as f: # open design file in binary.
                    f.seek(511) # point to start of design data. 
                    byte = f.read(1) # buffer one byte only.
                    while byte != b"": # read until end of design file.
                        if D_set.value()== False: #Request data from machine.
                            time_out=0
                            byte = f.read(1) # read design file byte by byte.
                            byte_out = ~byte[0]&0xff #invert the byte
                            i2c.writeto(0x20,bytearray([byte_out])) # write to output port.  
                            time.sleep_us(1)# delay for stable data on output port
                            Strobe.value(1) # strobe pulse for machine.
                            time.sleep_ms(7) # min. width of strobe pulse.
                            Strobe.value(0) 

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: improve speed

Post by pythoncoder » Sat Oct 06, 2018 5:16 am

You might consider using SPI instead of I2C. The interface is essentially that of a shift register, so a shift register chip with a latch could be used (using a third pin to clock the latch). While not as fast as 8-bit parallel it should be very much faster than I2C.

Another alternative would be to consider inline assembler. I'm not sure where this is documented but it's discussed in this thread.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: improve speed

Post by Roberthh » Sat Oct 06, 2018 6:25 am

Looking at you code sample, the strobe pulse of 7 ms alone limits the speed to 140 events/second. Maybe that can be improved. Besides that, if you want to write directly to the GPIO registers, you can do so with direct memory writes. below is a short piece of code which writes directly to the GPIO Pin. The only problem I see is that you cannot use that directly in parallel, because the available Pins (0, 2, 4, 5, 12, 13, 14, 15 on my board) do not have consecutive numbers. So it needs a little bit of bit fiddling. But all of the is waste of time if you need the 7ms strobe.

Code: Select all

def loop():
    import machine
    machine.Pin(4, machine.Pin.OUT, value=0)
    a=1
    do_loop()
    a=2
    print("Done")

@micropython.viper
def do_loop():

    GPIO_BASE = ptr32(0x60000300) # GPIO Output base register
    for i in range(10):
        GPIO_BASE[1] = 0x10 # set bit 4
        GPIO_BASE[2] = 0x10 # clear bit 4

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: improve speed

Post by OutoftheBOTS_ » Sat Oct 06, 2018 9:30 am

Maybe replacing the ESP8266 with ESP32 and using the hardware SPI of the ESP32 is a lot faster than that, off memory it can do 26Mhz mutex pins and 40Mhz on native pins.

cyberlab
Posts: 56
Joined: Sat Apr 21, 2018 5:02 am

Re: improve speed

Post by cyberlab » Sun Oct 07, 2018 6:38 pm

Hello everyone thank you for answering.
Roberthh, less than 7 ms. begins to enter a part of the code to resynchronize with the machine and not lose any byte of the file.
Phytoncoder and OutoftheBOTS_, my first design uses an SPI chip instead of the I2C chip, but the file lost some bytes in the transfer, I believe by the distance between the module and the cable connected to the machine, I think the I2C protocol is more secure in this case.
OutoftheBOTS_ also use an ESP32 and it is actually faster 240MHZ.
is practically the same code for the 2, with a few small changes for the ESP32,
first reason why you do not use ESP32: there is a part of the code, that at the given moment a file is being sent to the machine is disconnected from the router, to avoid corrupting the file that is being sent to the machine with a possible file that is I sent from the PC to the module, since I use the same name for all the files, that is, it is overwritten.
once the sending to the machine is completed, it is reconnected to the router, in the case of the ESP266 it is connected and disconnected, but in the ESP32 it is never disconnected, i Dont know why.
Second reason: I had already ordered 100 pieces of the ESP8266.

I will try to form an 8-bit port, to write directly to it, using the code mentioned by Roberthh and thread from pythoncoder

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: improve speed

Post by OutoftheBOTS_ » Sun Oct 07, 2018 9:47 pm

OutoftheBOTS_ also use an ESP32 and it is actually faster 240MHZ.
That's the speed of the CPU not the max speed of the SPI.

By your last post it seems the distance is being your limiting factor for serial communication due to there being corruption of data over the distance. I have been lead to believe that as cables get longer then you get better speed without corruption as you use a higher voltage. I am lead to believe many people use old school RS232 that uses 10v - 12v data lines. I am not sure if 1 of these at either end of your cable might allow faster serial https://www.aliexpress.com/item/MAX3232 ... autifyAB=0

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: improve speed

Post by pythoncoder » Mon Oct 08, 2018 6:16 am

Neither I2C nor SPI are suitable for transmission over any distance. They are designed for communication between chips on a PCB. However SPI can readily be made suitable using differential line driver and receiver chips. That would provide a fast, reliable interface.

I2C is harder to extend because it uses open drain hardware to achieve bidirectional communication on a single wire. The limitations of this approach are why it is normally restricted to 400Kbps. Its open drain topology actually makes it less suitable than SPI for use over distance. Perhaps there are special purpose line driver chips for it, but SPI is unidirectional. This means that extending it will be simple with industry-standard line driver chips and the outcome will be much faster.

I strongly recommend you look into this. Even with I2C I think you will run into reliability issues in an industrial environment.
Peter Hinch
Index to my micropython libraries.

User avatar
emtee
Posts: 15
Joined: Thu Jun 14, 2018 4:55 pm
Location: West Kootenay, BC, Canada

Re: improve speed

Post by emtee » Mon Oct 08, 2018 4:38 pm

I agree with pythoncoder about using a more robust interface in an industrial environment. Even better would be the use of a RS-485 serial connection or for better compatibility with a PC a RS-232 serial connection.

RS-485 is better in an electrically noisy environment as it uses differential voltages to transmit data. It also has the ability to multi-drop (that is, have multiple devices on a single communications line) and work at longer distances (up to 1220 meters).

I did a quick look on the 'Net and there appears a number of I2C to RS485 chips and boards available. The B&B Electronics website has some excellent free articles on RS-485.

P.S. I earn my living as an Industrial Automation programmer/specialist.
--
Mark

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: improve speed

Post by pythoncoder » Mon Oct 08, 2018 5:17 pm

@emtree While I agree with everything you say, bear in mind the need for speed.

I'd say the simplest solution for a unidirectional single drop interface is SPI buffered by RS422 line drivers such as uA9638C. Twisted pair shielded cable with an appropriate terminating resistor and line receiver should offer transmission rates of upto 10Mbps (although I haven't actually done the sums). So we're talking 25 times faster than standard I2C, whatever subsequent buffering is used.

I think this is the fastest, simple solution with the merit of single 5V supply operation.

I too designed industrial and military electronics, but it was rather a while ago. ;)
Peter Hinch
Index to my micropython libraries.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: improve speed

Post by jickster » Mon Oct 08, 2018 5:30 pm

The title of this post is way too vague.


Sent from my iPhone using Tapatalk Pro

Post Reply