Program ESP8266 Via raspberry pi serial

papatrexas
Posts: 2
Joined: Wed Nov 30, 2022 12:07 pm

Program ESP8266 Via raspberry pi serial

Postby papatrexas » Tue Dec 06, 2022 9:33 am

Hello,

I've been developing recently on the gen4-IoD-32T LCD from 4d Systems that is using the Espressif ESP8266 SoC.
The "default" way to develop on that lcd is via Arduino IDE and through a gen4-IoD Programming Adaptor board that comes with the LCD. Well actually you start with their (4d systems) IDE, to generate the graphic files, but in the end everything is passed and handled by arduino.
This LCD is then connected to a raspberry pi (Rx,Tx,+5V,GND,Reset). So, I was able to use the flash_download_tool_3.9.3 on my computer, which sorts out the issue I would have in the future with mass deployment. But that leaves me with my second issue that I will have and that will be updating. At the moment, the only option I see is for each of our customers to have a computer with this tool installed, and then having to take each and every one of those LCDs to that computer to program them.
So, the only real option would be to program the LCDs via the raspberry pi. I had a look at what arduino does when it programs the LCD. Long story short I was able to get the files from AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2/tools/, throw them to a folder in my pc, make a batch file that calls python3 that calls update.py and it worked. I then tried putting those files in the raspberry pi and execute the upload.py with the same --switches arduino uses. My problem is that I don't have a way of telling esptool (upload.py calls this file in the end) on how to reset the ESP8266 so the poor raspberry is trying to connect and fails in the end:
"Connecting........_____....._____....._____"
Is there a way to achieve what I am trying to do, which is programming the LCDs without an adaptor board via RxTx and a reset pin?
Thank you for reading me.

papatrexas
Posts: 2
Joined: Wed Nov 30, 2022 12:07 pm

Re: Program ESP8266 Via raspberry pi serial

Postby papatrexas » Mon Dec 12, 2022 12:56 pm

So, an update. I was able to reset the lcd by editing esptool.py

Code: Select all

def _setRTS(self, state):
        GPIO.output(4, not state)#EDIT HERE.GPIO4 is where the reset pin goes on my rpi
        self._port.setRTS(state)
        # Work-around for adapters on Windows using the usbser.sys driver:
        # generate a dummy change to DTR so that the set-control-line-state
        # request is sent with the updated RTS state and the same DTR state
        self._port.setDTR(self._port.dtr)
  
I am however getting an error after the max retries:
esptool.FatalError: Failed to connect to ESP8266: Invalid head of packet (0x43)

Code: Select all

def slip_reader(port, trace_function):
    """Generator to read SLIP packets from a serial port.
    Yields one full SLIP packet at a time, raises exception on timeout or invalid data.

    Designed to avoid too many calls to serial.read(1), which can bog
    down on slow systems.
    """
    partial_packet = None
    in_escape = False
    while True:
        waiting = port.inWaiting()
        read_bytes = port.read(1 if waiting == 0 else waiting)
        if read_bytes == b'':
            waiting_for = "header" if partial_packet is None else "content"
            trace_function("Timed out waiting for packet %s", waiting_for)
            raise FatalError("Timed out waiting for packet %s" % waiting_for)
        trace_function("Read %d bytes: %s", len(read_bytes), HexFormatter(read_bytes))
        for b in read_bytes:
            if type(b) is int:
                b = bytes([b])  # python 2/3 compat
                print ("Byte in:", b)#EDIT HERE.

            if partial_packet is None:  # waiting for packet header
                if b == b'\x0c':
                    partial_packet = b""
                else:
                    trace_function("Read invalid data: %s", HexFormatter(read_bytes))
                    trace_function("Remaining data in serial buffer: %s", HexFormatter(port.read(port.inWaiting())))
                    raise FatalError('Invalid head of packet (0x%s)' % hexify(b))
            elif in_escape:  # part-way through escape sequence
                in_escape = False
                if b == b'\xdc':
                    partial_packet += b'\xc0'
                elif b == b'\xdd':
                    partial_packet += b'\xdb'
                else:
                    trace_function("Read invalid data: %s", HexFormatter(read_bytes))
                    trace_function("Remaining data in serial buffer: %s", HexFormatter(port.read(port.inWaiting())))
                    raise FatalError('Invalid SLIP escape (0xdb, 0x%s)' % (hexify(b)))
            elif b == b'\xdb':  # start of escape sequence
                in_escape = True
            elif b == b'\xc0':  # end of packet
                trace_function("Received full packet: %s", HexFormatter(partial_packet))
                yield partial_packet
                partial_packet = None
            else:  # normal byte in packet
                partial_packet += b

adding print ("Byte in:", b) in the slip_reader outputs this:
root@rpi78e84ccc:/home/pi# python3 -I ProgramLCD/esp8266/hardware/esp8266/3.0.2/tools/upload.py --chip esp8266 --port /dev/ttyAMA0 --baud 115200 --before default_reset --after hard_reset write_flash 0x0 ProgramLCD/mainProg.ino.bin
esptool.py v3.0
Serial port /dev/ttyAMA0
Connecting...Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_Byte in: b's'
.....____Byte in: b'C'
_
Traceback (most recent call last):
File "ProgramLCD/esp8266/hardware/esp8266/3.0.2/tools/upload.py", line 68, in <module>
esptool.main(cmdline)
File "/home/pi/ProgramLCD/esp8266/hardware/esp8266/3.0.2/tools/esptool/esptool.py", line 3560, in main
esp.connect(args.before, args.connect_attempts)
File "/home/pi/ProgramLCD/esp8266/hardware/esp8266/3.0.2/tools/esptool/esptool.py", line 536, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Invalid head of packet (0x43)
These are the byte arrays I get when I restart the LCD:
restart1
0x73,0x88,0x03,0x00,0x6C,0x9C,0xC8,0x73,0x4F,0x80,0x46,0x6C,0xE0,0xF1,0x6F,0x40,0x03,0x46,0xF8,0x06,0x84,0xC6,0x0C,0x8E,0x83,0xEC,0x8E,0x70,0x84,0xBE,0x8F,0x83,0x4F,0x80,0x76,0x9C,0x81,0x93,0x4F,0x7C,0x13,0x63,0x6C,0x8E,0x70,0x0C,0x88,0x0C,0xFB,0x6F,0x6F,0xF6,0x03,0x46,0xEF,0xF9,0x67,0xC6,0x93,0x4E,0xF3,0x06,0x63,0x1C,0x70,0xEC,0x87,0xB1,0x6C,0x60,0x09,0x93,0x60,0x09,0xFF,0x13,0x83,0x4F,0x7C,0xFF,0x49,0x6F,0xE0,0x89,0x67,0xCF,0xE3,0x63,0x70,0x00,0xFC
restart2
0x73,0x88,0x03,0x00,0x6C,0x9C,0xC8,0x73,0x4F,0x80,0x46,0x6C,0xE0,0xF1,0x6F,0x40,0x03,0x46,0xF0,0x06,0x84,0xC6,0x0C,0x8E,0x83,0xEC,0x8E,0x70,0x8C,0xBE,0x8F,0x83,0x4F,0x80,0x76,0x9C,0x81,0x93,0x4F,0x7C,0x1B,0x63,0x64,0x8E,0x70,0x04,0x88,0x0C,0xF3,0x6F,0x6F,0xF6,0x03,0x46,0xEF,0xF9,0x6F,0xC6,0x93,0x4E,0xFB,0x06,0x63,0x1C,0x70,0x6C,0x8F,0xB1,0x6C,0x60,0x09,0x9B,0x60,0x09,0xFF,0x13,0x83,0x4F,0x7C,0xFF,0x49,0x67,0xE0,0x89,0x67,0xCF,0xE3,0x63,0x70,0x00,0xFC,
0x00,0xFC,0xE0,0xBE,0x30,0xF8
restart3
0x73,0x88,0x03,0x00,0x6C,0x9C,0xC8,0x73,0x4F,0x80,0x46,0x64,0xE0,0xF1,0x6F,0x40,0x03,0x46,0xF0,0x06,0x84,0xC6,0x0C,0x8E,0x83,0xEC,0x8E,0x70,0x8C,0xBE,0x8F,0x83,0x4F,0x80,0x76,0x9C,0x81,0x9B,0x4F,0x7C,0x1B,0x63,0x6C,0x8E,0x78,0x04,0x88,0x0C,0xF3,0x6F,0x6F,0xF6,0x03,0x46,0xEF,0xF9,0x6F,0xC6,0x93,0x4E,0xFB,0x06,0x63,0x1C,0x70,0xEC,0x8F,0xB1,0x6C,0x60,0x09,0x9B,0x60,0x09,0xFF,0x13,0x83,0x4F,0x7C,0xFF,0x49,0x6F,0xE0,0x89,0x67,0xCF,0xE3,0x63,0x70,0x00,0xFC
There are some minor differences between the byte arrays. Don't know if they are enumerations, timestamps or what.

Any help is really apreciated.

Who is online

Users browsing this forum: Bing [Bot] and 67 guests