bloguetronica / mcp2210-qt

A C++ class that uses libusb to interface with MCP2210 devices. It can be used to control the several aspects, such as NVRAM or volatile configurations, GPIO directions and states, or to perform SPI transfers. Note that this is a class variant made specifically for Qt. If you wish to use the original, non-Qt variant, please refer to https://github.com/bloguetronica/mcp2210.
0 stars 0 forks source link

Function getUSBParameters() returns negated power mode #8

Closed samuelfmlourenco closed 1 year ago

samuelfmlourenco commented 1 year ago

In getUSBParameters(), the power mode variable (parameters.powmode) is based on bit 7 of byte 29 of the response given by the device. This is evident by looking at the function:

// Gets the USB parameters, namely VID, PID and power settings
MCP2210::USBParameters MCP2210::getUSBParameters(int &errcnt, QString &errstr)
{
    QVector<quint8> command{
        GET_NVRAM_SETTINGS, USB_PARAMETERS  // Header
    };
    QVector<quint8> response = hidTransfer(command, errcnt, errstr);
    USBParameters parameters;
    parameters.vid = static_cast<quint16>(response[13] << 8 | response[12]);  // Vendor ID corresponds to bytes 12 and 13 (little-endian conversion)
    parameters.pid = static_cast<quint32>(response[15] << 8 | response[14]);  // Product ID corresponds to bytes 14 and 15 (little-endian conversion)
    parameters.maxpow = response[30];                                         // Maximum consumption current corresponds to byte 30
    parameters.powmode = (0x80 & response[29]) != 0x00;                       // Power mode corresponds to bit 7 of byte 29 (bit 6 is redundant)
    parameters.rmwakeup = (0x20 & response[29]) != 0x00;                      // Remote wakeup corresponds to bit 5 of byte 29
    return parameters;
}

However, it is bit 6 that translates to the self-powered status, and that bit is true when the device is self-powered, unlike bit 7. So, in order for the function to work as intended, the 12th line, which corresponds to line 425 (located inside mcp2210.cpp), must be changed to:

parameters.powmode = (0x40 & response[29]) != 0x00; // Power mode corresponds to bit 6 of byte 29 (bit 7 is redundant)

This aligns with the values set in the header file (mcp2210.h):

    // The following values are applicable to USBParameters/getUSBParameters()/writeUSBParameters()
    static const bool PMBUS = false;  // Value corresponding to USB bus-powered mode
    static const bool PMSELF = true;  // Value corresponding to USB self-powered mode
    struct USBParameters {
        quint16 vid;    // Vendor ID
        quint16 pid;    // Product ID
        quint8 maxpow;  // Maximum consumption current (raw value in 2 mA units)
        bool powmode;   // Power mode (false for bus-powered, true for self-powered)
        bool rmwakeup;  // Remote wakeup

        bool operator ==(const USBParameters &other) const;
        bool operator !=(const USBParameters &other) const;
    };
samuelfmlourenco commented 1 year ago

Fixed in version 1.2.1.