florisla / stm32loader

Flash firmware to STM32 microcontrollers using Python.
GNU General Public License v3.0
109 stars 53 forks source link

Fails to download if MCU serial port was transmitting before reset #48

Closed blueskull closed 2 years ago

blueskull commented 4 years ago

As a known behavior of CP2102N, particularly at 115.2kbps, its driver will NOT flush input buffer (device to host) when the port is being opened, thus if the device was transmitting, the data may be still in buffer, causing stm32loader fail to detect ACK.

Testing condition: Ubuntu 20.04, CP2102N, GD32F350G8U6. GigaDevice's official Windows ISP GUI works fine, so it must be the new code, which I just added a UART heartbeat, to be the problem.

Proposed solution: add serial_connection.reset_input_buffer() after reset, or in case CP2102N being very janky, adding the reset command after every write() call.

def write(self, *args, **kwargs):
    """Write the given data to the serial connection."""
    ret=self.serial_connection.write(*args, **kwargs)
    self.serial_connection.reset_input_buffer()
    return ret

Solution validation: works reliably even if when device sends out data rabidly.

Best Regards

florisla commented 4 years ago

I'm not sure it's a good idea to reset the buffer so often.

Does the ACK end up in the buffer at all when it's not empty to begin with?

How does this behave if you only reset the buffer at connection time?

blueskull commented 4 years ago

How does this behave if you only reset the buffer at connection time?

It works too. I guess it is a better fix.

florisla commented 2 years ago

Hi @blueskull can you share the code for the fix you are using?

I'd like to incorporate it into the next release.

florisla commented 2 years ago

Update: I'll incorporate the fix from #46.

florisla commented 2 years ago

Implemented in c018d70e3498ce4196cd73e592e2a894c00abdb2 .