vpaeder / pymcp2221

A python driver for the Microchip MCP2221/MCP2221A USB 2.0 to I2C/UART protocol converters
https://pypi.org/project/pymcp2221
MIT License
5 stars 2 forks source link

_read_flash: I think the data length is 2 bytes shorter than currently reported #5

Closed ianccole closed 1 year ago

ianccole commented 1 year ago

Sorry to report another but...

https://github.com/vpaeder/pymcp2221/blob/e4c66973052516822bdf6d0de362c372a51dc207/mcp2221/__init__.py#L319

datasheet http://ww1.microchip.com/downloads/en/devicedoc/20005292c.pdf table 3-9 indicates that the length at byte index 2 is the number of bytes +2

So I see spurious characters at the end of the strings - but these are fixed if I subtract 2 from the length:

    data = self._write(0xb0, code)
    len = data[2] - 2
    return data[4:(4+len)]
vpaeder commented 1 year ago

Thanks for mentioning this. Turns out data length for flash reads is defined in Table 3-4. The issue is specific to USB descriptors. Tables 3-7 to 3-9 specify that the 2 last bytes can be dropped only if the utf-16-encoded string is shorter than 60 bytes. I fixed the code to handle that and the fringe case correctly.

ianccole commented 1 year ago

Appreciate it - thanks