From ced5bdfc75ddb6a9a5ce237ce3c1be13dcf6762d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:51:25 +0000 Subject: [PATCH 1/7] Add files via upload --- mcp2210.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mcp2210.cpp b/mcp2210.cpp index d844953..2e8cd2e 100644 --- a/mcp2210.cpp +++ b/mcp2210.cpp @@ -499,7 +499,7 @@ quint8 MCP2210::readEEPROMByte(quint8 address, int &errcnt, QString &errstr) } // Reads the EEPROM within the specified range, returning a vector -// In an error occurs, the size of the vector will be smaller than expected +// If an error occurs, the size of the vector will be smaller than expected QVector MCP2210::readEEPROMRange(quint8 begin, quint8 end, int &errcnt, QString &errstr) { QVector values; From 42e3ac76be117dc88e5785cadff56eb5047a6de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:56:54 +0000 Subject: [PATCH 2/7] Add files via upload --- mcp2210.cpp | 4 +- mcp2210.h | 114 ++++++++++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/mcp2210.cpp b/mcp2210.cpp index 2e8cd2e..07e9c0d 100644 --- a/mcp2210.cpp +++ b/mcp2210.cpp @@ -1,4 +1,4 @@ -/* MCP2210 class for Qt - Version 1.0.0 +/* MCP2210 class for Qt - Version 1.1.0 Copyright (c) 2022 Samuel Lourenço This library is free software: you can redistribute it and/or modify it @@ -435,7 +435,7 @@ QVector MCP2210::hidTransfer(const QVector &data, int &errcnt, Q unsigned char responseBuffer[COMMAND_SIZE]; int bytesRead = 0; // Important! interruptTransfer(EPIN, responseBuffer, static_cast(COMMAND_SIZE), &bytesRead, errcnt, errstr); - QVector retdata(COMMAND_SIZE); + QVector retdata(static_cast(COMMAND_SIZE)); for (int i = 0; i < bytesRead; ++i) { retdata[i] = responseBuffer[i]; } diff --git a/mcp2210.h b/mcp2210.h index d537e75..0910282 100644 --- a/mcp2210.h +++ b/mcp2210.h @@ -1,4 +1,4 @@ -/* MCP2210 class for Qt - Version 1.0.0 +/* MCP2210 class for Qt - Version 1.1.0 Copyright (c) 2022 Samuel Lourenço This library is free software: you can redistribute it and/or modify it @@ -98,7 +98,7 @@ class MCP2210 static const quint8 TRANSFER_STARTED = 0x20; // SPI transfer started (no data to receive) static const quint8 TRANSFER_NOT_FINISHED = 0x30; // SPI transfer not finished (received data available) - // The following values are applicable to ChipSettings/configureChipSettings()/getChipSettings() + // The following values are applicable to ChipSettings/configureChipSettings()/getChipSettings()/getNVChipSettings()/writeNVChipSettings() static const quint8 PCGPIO = 0x00; // Pin configured as GPIO static const quint8 PCCS = 0x01; // Pin configured as chip select static const quint8 PCFUNC = 0x02; // Pin configured as a dedicated function pin @@ -117,60 +117,60 @@ class MCP2210 static const bool PWNO = false; // Password not guessed static const bool PWOK = true; // Password guessed - // The following values are applicable to SPISettings/configureSPISettings()/getSPISettings() - static const quint32 BRT1K464 = 1464; // Value corresponding to a bit rate of 1.464Kbps - static const quint32 BRT1K5 = 1500; // Value corresponding to a bit rate of 1.5Kbps - static const quint32 BRT1K875 = 1875; // Value corresponding to a bit rate of 1.875Kbps - static const quint32 BRT2K5 = 2500; // Value corresponding to a bit rate of 2.5Kbps - static const quint32 BRT3K = 3000; // Value corresponding to a bit rate of 3Kbps - static const quint32 BRT3K125 = 3125; // Value corresponding to a bit rate of 3.125Kbps - static const quint32 BRT3K75 = 3750; // Value corresponding to a bit rate of 3.75Kbps - static const quint32 BRT5K = 5000; // Value corresponding to a bit rate of 5Kbps - static const quint32 BRT6K = 6000; // Value corresponding to a bit rate of 6Kbps - static const quint32 BRT6K25 = 6250; // Value corresponding to a bit rate of 6.25Kbps - static const quint32 BRT7K5 = 7500; // Value corresponding to a bit rate of 7.5Kbps - static const quint32 BRT9K375 = 9375; // Value corresponding to a bit rate of 9.375Kbps - static const quint32 BRT10K = 10000; // Value corresponding to a bit rate of 10Kbps - static const quint32 BRT12K = 12000; // Value corresponding to a bit rate of 12Kbps - static const quint32 BRT12K5 = 12500; // Value corresponding to a bit rate of 12.5Kbps - static const quint32 BRT15K = 15000; // Value corresponding to a bit rate of 15Kbps - static const quint32 BRT15K625 = 15625; // Value corresponding to a bit rate of 15.625Kbps - static const quint32 BRT18K75 = 18750; // Value corresponding to a bit rate of 18.750Kbps - static const quint32 BRT20K = 20000; // Value corresponding to a bit rate of 20Kbps - static const quint32 BRT24K = 24000; // Value corresponding to a bit rate of 24Kbps - static const quint32 BRT25K = 25000; // Value corresponding to a bit rate of 25Kbps - static const quint32 BRT30K = 30000; // Value corresponding to a bit rate of 30Kbps - static const quint32 BRT31K25 = 31250; // Value corresponding to a bit rate of 31.25Kbps - static const quint32 BRT37K5 = 37500; // Value corresponding to a bit rate of 37.5Kbps - static const quint32 BRT40K = 40000; // Value corresponding to a bit rate of 40Kbps - static const quint32 BRT46K875 = 46875; // Value corresponding to a bit rate of 46.875Kbps - static const quint32 BRT48K = 48000; // Value corresponding to a bit rate of 48Kbps - static const quint32 BRT50K = 50000; // Value corresponding to a bit rate of 50Kbps - static const quint32 BRT60K = 60000; // Value corresponding to a bit rate of 60Kbps - static const quint32 BRT62K5 = 62500; // Value corresponding to a bit rate of 62.5Kbps - static const quint32 BRT75K = 75000; // Value corresponding to a bit rate of 75Kbps - static const quint32 BRT80K = 80000; // Value corresponding to a bit rate of 80Kbps - static const quint32 BRT93K75 = 93750; // Value corresponding to a bit rate of 93.75Kbps - static const quint32 BRT100K = 100000; // Value corresponding to a bit rate of 100Kbps - static const quint32 BRT120K = 120000; // Value corresponding to a bit rate of 120Kbps - static const quint32 BRT125K = 125000; // Value corresponding to a bit rate of 125Kbps - static const quint32 BRT150K = 150000; // Value corresponding to a bit rate of 150Kbps - static const quint32 BRT187K5 = 187500; // Value corresponding to a bit rate of 187.5Kbps - static const quint32 BRT200K = 200000; // Value corresponding to a bit rate of 200Kbps - static const quint32 BRT240K = 240000; // Value corresponding to a bit rate of 240Kbps - static const quint32 BRT250K = 250000; // Value corresponding to a bit rate of 250Kbps - static const quint32 BRT300K = 300000; // Value corresponding to a bit rate of 300Kbps - static const quint32 BRT375K = 375000; // Value corresponding to a bit rate of 375Kbps - static const quint32 BRT400K = 400000; // Value corresponding to a bit rate of 400Kbps - static const quint32 BRT500K = 500000; // Value corresponding to a bit rate of 500Kbps - static const quint32 BRT600K = 600000; // Value corresponding to a bit rate of 600Kbps - static const quint32 BRT750K = 750000; // Value corresponding to a bit rate of 750Kbps - static const quint32 BRT1M = 1000000; // Value corresponding to a bit rate of 1Mbps - static const quint32 BRT1M2 = 1200000; // Value corresponding to a bit rate of 1.2Mbps - static const quint32 BRT1M5 = 1500000; // Value corresponding to a bit rate of 1.5Mbps - static const quint32 BRT2M = 2000000; // Value corresponding to a bit rate of 2Mbps - static const quint32 BRT3M = 3000000; // Value corresponding to a bit rate of 3Mbps - static const quint32 BRT12M = 12000000; // Value corresponding to a bit rate of 12Mbps + // The following values are applicable to SPISettings/configureSPISettings()/getSPISettings()/getNVSPISettings()/writeNVSPISettings() + static const quint32 BRT1K464 = 1464; // Value corresponding to a bit rate of 1.464 Kib/s + static const quint32 BRT1K5 = 1500; // Value corresponding to a bit rate of 1.5 Kib/s + static const quint32 BRT1K875 = 1875; // Value corresponding to a bit rate of 1.875 Kib/s + static const quint32 BRT2K5 = 2500; // Value corresponding to a bit rate of 2.5 Kib/s + static const quint32 BRT3K = 3000; // Value corresponding to a bit rate of 3 Kib/s + static const quint32 BRT3K125 = 3125; // Value corresponding to a bit rate of 3.125 Kib/s + static const quint32 BRT3K75 = 3750; // Value corresponding to a bit rate of 3.75 Kib/s + static const quint32 BRT5K = 5000; // Value corresponding to a bit rate of 5 Kib/s + static const quint32 BRT6K = 6000; // Value corresponding to a bit rate of 6 Kib/s + static const quint32 BRT6K25 = 6250; // Value corresponding to a bit rate of 6.25 Kib/s + static const quint32 BRT7K5 = 7500; // Value corresponding to a bit rate of 7.5 Kib/s + static const quint32 BRT9K375 = 9375; // Value corresponding to a bit rate of 9.375 Kib/s + static const quint32 BRT10K = 10000; // Value corresponding to a bit rate of 10 Kib/s + static const quint32 BRT12K = 12000; // Value corresponding to a bit rate of 12 Kib/s + static const quint32 BRT12K5 = 12500; // Value corresponding to a bit rate of 12.5 Kib/s + static const quint32 BRT15K = 15000; // Value corresponding to a bit rate of 15 Kib/s + static const quint32 BRT15K625 = 15625; // Value corresponding to a bit rate of 15.625 Kib/s + static const quint32 BRT18K75 = 18750; // Value corresponding to a bit rate of 18.750 Kib/s + static const quint32 BRT20K = 20000; // Value corresponding to a bit rate of 20 Kib/s + static const quint32 BRT24K = 24000; // Value corresponding to a bit rate of 24 Kib/s + static const quint32 BRT25K = 25000; // Value corresponding to a bit rate of 25 Kib/s + static const quint32 BRT30K = 30000; // Value corresponding to a bit rate of 30 Kib/s + static const quint32 BRT31K25 = 31250; // Value corresponding to a bit rate of 31.25 Kib/s + static const quint32 BRT37K5 = 37500; // Value corresponding to a bit rate of 37.5 Kib/s + static const quint32 BRT40K = 40000; // Value corresponding to a bit rate of 40 Kib/s + static const quint32 BRT46K875 = 46875; // Value corresponding to a bit rate of 46.875 Kib/s + static const quint32 BRT48K = 48000; // Value corresponding to a bit rate of 48 Kib/s + static const quint32 BRT50K = 50000; // Value corresponding to a bit rate of 50 Kib/s + static const quint32 BRT60K = 60000; // Value corresponding to a bit rate of 60 Kib/s + static const quint32 BRT62K5 = 62500; // Value corresponding to a bit rate of 62.5 Kib/s + static const quint32 BRT75K = 75000; // Value corresponding to a bit rate of 75 Kib/s + static const quint32 BRT80K = 80000; // Value corresponding to a bit rate of 80 Kib/s + static const quint32 BRT93K75 = 93750; // Value corresponding to a bit rate of 93.75 Kib/s + static const quint32 BRT100K = 100000; // Value corresponding to a bit rate of 100 Kib/s + static const quint32 BRT120K = 120000; // Value corresponding to a bit rate of 120 Kib/s + static const quint32 BRT125K = 125000; // Value corresponding to a bit rate of 125 Kib/s + static const quint32 BRT150K = 150000; // Value corresponding to a bit rate of 150 Kib/s + static const quint32 BRT187K5 = 187500; // Value corresponding to a bit rate of 187.5 Kib/s + static const quint32 BRT200K = 200000; // Value corresponding to a bit rate of 200 Kib/s + static const quint32 BRT240K = 240000; // Value corresponding to a bit rate of 240 Kib/s + static const quint32 BRT250K = 250000; // Value corresponding to a bit rate of 250 Kib/s + static const quint32 BRT300K = 300000; // Value corresponding to a bit rate of 300 Kib/s + static const quint32 BRT375K = 375000; // Value corresponding to a bit rate of 375 Kib/s + static const quint32 BRT400K = 400000; // Value corresponding to a bit rate of 400 Kib/s + static const quint32 BRT500K = 500000; // Value corresponding to a bit rate of 500 Kib/s + static const quint32 BRT600K = 600000; // Value corresponding to a bit rate of 600 Kib/s + static const quint32 BRT750K = 750000; // Value corresponding to a bit rate of 750 Kib/s + static const quint32 BRT1M = 1000000; // Value corresponding to a bit rate of 1 Mib/s + static const quint32 BRT1M2 = 1200000; // Value corresponding to a bit rate of 1.2 Mib/s + static const quint32 BRT1M5 = 1500000; // Value corresponding to a bit rate of 1.5 Mib/s + static const quint32 BRT2M = 2000000; // Value corresponding to a bit rate of 2 Mib/s + static const quint32 BRT3M = 3000000; // Value corresponding to a bit rate of 3 Mib/s + static const quint32 BRT12M = 12000000; // Value corresponding to a bit rate of 12 Mib/s static const quint8 SPIMODE0 = 0x00; // Value corresponding to SPI mode 0 static const quint8 SPIMODE1 = 0x01; // Value corresponding to SPI mode 1 static const quint8 SPIMODE2 = 0x02; // Value corresponding to SPI mode 2 @@ -246,7 +246,7 @@ class MCP2210 struct USBParameters { quint16 vid; // Vendor ID quint16 pid; // Product ID - quint8 maxpow; // Maximum consumption current (raw value un 2mA units) + 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 From f4f455eef54188719f7a16c78ce8dbdd262a0b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Tue, 22 Nov 2022 21:59:28 +0000 Subject: [PATCH 3/7] Add files via upload --- mcp2210.cpp | 33 ++++++++++++++++++++++++++------- mcp2210.h | 8 ++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/mcp2210.cpp b/mcp2210.cpp index 07e9c0d..c432102 100644 --- a/mcp2210.cpp +++ b/mcp2210.cpp @@ -225,6 +225,16 @@ quint8 MCP2210::configureSPISettings(const SPISettings &settings, int &errcnt, Q return response[1]; } +// Retrieves the access control mode from the MCP2210 NVRAM +quint8 MCP2210::getAccessControlMode(int &errcnt, QString &errstr) +{ + QVector command = { + GET_NVRAM_SETTINGS, NV_CHIP_SETTINGS // Header + }; + QVector response = hidTransfer(command, errcnt, errstr); + return response[18]; // Access control mode corresponds to byte 18 +} + // Returns applied chip settings MCP2210::ChipSettings MCP2210::getChipSettings(int &errcnt, QString &errstr) { @@ -280,7 +290,7 @@ quint16 MCP2210::getEventCount(int &errcnt, QString &errstr) bool MCP2210::getGPIO(int gpio, int &errcnt, QString &errstr) { bool value; - if (gpio < 0 || gpio > 8) { + if (gpio < GPIO0 || gpio > GPIO8) { ++errcnt; errstr += QObject::tr("In getGPIO(): GPIO pin number must be between 0 and 8.\n"); // Program logic error value = false; @@ -294,7 +304,7 @@ bool MCP2210::getGPIO(int gpio, int &errcnt, QString &errstr) bool MCP2210::getGPIODirection(int gpio, int &errcnt, QString &errstr) { bool direction; - if (gpio < 0 || gpio > 7) { + if (gpio < GPIO0 || gpio > GPIO7) { ++errcnt; errstr += QObject::tr("In getGPIODirection(): GPIO pin number must be between 0 and 7.\n"); // Program logic error direction = false; @@ -534,7 +544,7 @@ quint8 MCP2210::resetEventCounter(int &errcnt, QString &errstr) quint8 MCP2210::setGPIO(int gpio, bool value, int &errcnt, QString &errstr) { quint8 retval; - if (gpio < 0 || gpio > 7) { + if (gpio < GPIO0 || gpio > GPIO7) { ++errcnt; errstr += QObject::tr("In setGPIO(): GPIO pin number must be between 0 and 7.\n"); // Program logic error retval = OTHER_ERROR; @@ -560,7 +570,7 @@ quint8 MCP2210::setGPIO(int gpio, bool value, int &errcnt, QString &errstr) quint8 MCP2210::setGPIODirection(int gpio, bool direction, int &errcnt, QString &errstr) { quint8 retval; - if (gpio < 0 || gpio > 7) { + if (gpio < GPIO0 || gpio > GPIO7) { ++errcnt; errstr += QObject::tr("In setGPIODirection(): GPIO pin number must be between 0 and 7.\n"); // Program logic error retval = OTHER_ERROR; @@ -639,7 +649,7 @@ QVector MCP2210::spiTransfer(const QVector &data, quint8 &status quint8 MCP2210::toggleGPIO(int gpio, int &errcnt, QString &errstr) { quint8 retval; - if (gpio < 0 || gpio > 7) { + if (gpio < GPIO0 || gpio > GPIO7) { ++errcnt; errstr += QObject::tr("In toggleGPIO(): GPIO pin number must be between 0 and 7.\n"); // Program logic error retval = OTHER_ERROR; @@ -688,6 +698,7 @@ quint8 MCP2210::writeEEPROMRange(quint8 begin, quint8 end, const QVector errstr += QObject::tr("In writeEEPROMRange(): vector size does not match range size.\n"); // Program logic error retval = OTHER_ERROR; } else { + retval = COMPLETED; // Fix applied in version 1.1.0 for (int i = 0; i < vecSize; ++i) { int preverrcnt = errcnt; retval = writeEEPROMByte(static_cast(begin + i), values[i], errcnt, errstr); @@ -714,8 +725,9 @@ quint8 MCP2210::writeManufacturerDesc(const QString &manufacturer, int &errcnt, return retval; } -// Writes the given chip transfer settings to the MCP2210 OTP NVRAM -quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr) +// Writes the given chip transfer settings to the MCP2210 OTP NVRAM, while also setting the access control mode and password (expanded in version 1.1.0) +// Note that using an empty string for the password will have the effect of leaving it unchanged +quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, quint8 accessControlMode, const QString &password, int &errcnt, QString &errstr) { QVector command = { SET_NVRAM_SETTINGS, NV_CHIP_SETTINGS, 0x00, 0x00, // Header @@ -736,6 +748,13 @@ quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, int &errcnt, Q return response[1]; } +// Writes the given chip transfer settings to the MCP2210 OTP NVRAM (this overloaded function is functionally equivalent to the implementation of writeNVChipSettings() that is found in version 1.0.0) +// The use of this variant of writeNVChipSettings() sets the access control mode to "ACNONE" [0x00], but the password is kept unchanged +quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr) +{ + return writeNVChipSettings(settings, ACNONE, "", errcnt, errstr); +} + // Writes the given SPI transfer settings to the MCP2210 OTP NVRAM quint8 MCP2210::writeNVSPISettings(const SPISettings &settings, int &errcnt, QString &errstr) { diff --git a/mcp2210.h b/mcp2210.h index 0910282..d638977 100644 --- a/mcp2210.h +++ b/mcp2210.h @@ -48,6 +48,7 @@ class MCP2210 static const int ERROR_BUSY = 3; // Returned by open() if the device is already in use static const size_t COMMAND_SIZE = 64; // HID command size static const size_t SPIDATA_MAXSIZE = 60; // Maximum size of the data vector for a single SPI transfer (only applicable to basic SPI transfers) + static const size_t PASSWORD_MAXLEN = 8; // Maximum length for the password // Descriptor specific definitions static const size_t DESC_MAXLEN = 28; // Maximum length for any descriptor @@ -98,6 +99,11 @@ class MCP2210 static const quint8 TRANSFER_STARTED = 0x20; // SPI transfer started (no data to receive) static const quint8 TRANSFER_NOT_FINISHED = 0x30; // SPI transfer not finished (received data available) + // Access control modes, returned by getAccessControlMode() + static const quint8 ACNONE = 0x00; // Chip settings not protected (no access control) + static const quint8 ACPASSWORD = 0x40; // Chip settings protected by password access + static const quint8 ACLOCKED = 0x80; // Chip settings permanently locked + // The following values are applicable to ChipSettings/configureChipSettings()/getChipSettings()/getNVChipSettings()/writeNVChipSettings() static const quint8 PCGPIO = 0x00; // Pin configured as GPIO static const quint8 PCCS = 0x01; // Pin configured as chip select @@ -264,6 +270,7 @@ class MCP2210 void close(); quint8 configureChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr); quint8 configureSPISettings(const SPISettings &settings, int &errcnt, QString &errstr); + quint8 getAccessControlMode(int &errcnt, QString &errstr); ChipSettings getChipSettings(int &errcnt, QString &errstr); ChipStatus getChipStatus(int &errcnt, QString &errstr); quint16 getEventCount(int &errcnt, QString &errstr); @@ -291,6 +298,7 @@ class MCP2210 quint8 writeEEPROMByte(quint8 address, quint8 value, int &errcnt, QString &errstr); quint8 writeEEPROMRange(quint8 begin, quint8 end, const QVector &values, int &errcnt, QString &errstr); quint8 writeManufacturerDesc(const QString &manufacturer, int &errcnt, QString &errstr); + quint8 writeNVChipSettings(const ChipSettings &settings, quint8 accessControlMode, const QString &password, int &errcnt, QString &errstr); quint8 writeNVChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr); quint8 writeNVSPISettings(const SPISettings &settings, int &errcnt, QString &errstr); quint8 writeProductDesc(const QString &product, int &errcnt, QString &errstr); From ade31de54070cfc16beb8a3abe15006a42a3b2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Wed, 23 Nov 2022 15:49:37 +0000 Subject: [PATCH 4/7] Add files via upload --- mcp2210.cpp | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/mcp2210.cpp b/mcp2210.cpp index c432102..2e773cb 100644 --- a/mcp2210.cpp +++ b/mcp2210.cpp @@ -33,7 +33,7 @@ const unsigned int TR_TIMEOUT = 500; // Transfer timeout in milliseconds // Private generic function that is used to get any descriptor QString MCP2210::getDescGeneric(quint8 subcomid, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_NVRAM_SETTINGS, subcomid // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -163,7 +163,7 @@ bool MCP2210::isOpen() const // Cancels the ongoing SPI transfer quint8 MCP2210::cancelSPITransfer(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ CANCEL_SPI_TRANSFER // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -187,7 +187,7 @@ void MCP2210::close() // Configures volatile chip settings quint8 MCP2210::configureChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_CHIP_SETTINGS, 0x00, 0x00, 0x00, // Header settings.gp0, // GP0 pin configuration settings.gp1, // GP1 pin configuration @@ -209,7 +209,7 @@ quint8 MCP2210::configureChipSettings(const ChipSettings &settings, int &errcnt, // Configures volatile SPI transfer settings quint8 MCP2210::configureSPISettings(const SPISettings &settings, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_SPI_SETTINGS, 0x00, 0x00, 0x00, // Header static_cast(settings.bitrate), static_cast(settings.bitrate >> 8), // Bit rate static_cast(settings.bitrate >> 16), static_cast(settings.bitrate >> 24), @@ -228,7 +228,7 @@ quint8 MCP2210::configureSPISettings(const SPISettings &settings, int &errcnt, Q // Retrieves the access control mode from the MCP2210 NVRAM quint8 MCP2210::getAccessControlMode(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_NVRAM_SETTINGS, NV_CHIP_SETTINGS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -238,7 +238,7 @@ quint8 MCP2210::getAccessControlMode(int &errcnt, QString &errstr) // Returns applied chip settings MCP2210::ChipSettings MCP2210::getChipSettings(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_CHIP_SETTINGS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -263,7 +263,7 @@ MCP2210::ChipSettings MCP2210::getChipSettings(int &errcnt, QString &errstr) // Returns the current status MCP2210::ChipStatus MCP2210::getChipStatus(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_CHIP_STATUS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -278,7 +278,7 @@ MCP2210::ChipStatus MCP2210::getChipStatus(int &errcnt, QString &errstr) // Gets the number of events from the interrupt pin quint16 MCP2210::getEventCount(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_EVENT_COUNT, // Header 0x01 // Do not reset the event counter }; @@ -317,7 +317,7 @@ bool MCP2210::getGPIODirection(int gpio, int &errcnt, QString &errstr) // Returns the directions of all GPIO pins on the MCP2210 quint8 MCP2210::getGPIODirections(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_GPIO_DIRECTIONS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -327,7 +327,7 @@ quint8 MCP2210::getGPIODirections(int &errcnt, QString &errstr) // Returns the values of all GPIO pins on the MCP2210 quint16 MCP2210::getGPIOs(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_GPIO_VALUES // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -343,7 +343,7 @@ QString MCP2210::getManufacturerDesc(int &errcnt, QString &errstr) // Retrieves the power-up (non-volatile) chip settings from the MCP2210 NVRAM MCP2210::ChipSettings MCP2210::getNVChipSettings(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_NVRAM_SETTINGS, NV_CHIP_SETTINGS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -368,7 +368,7 @@ MCP2210::ChipSettings MCP2210::getNVChipSettings(int &errcnt, QString &errstr) // Retrieves the power-up (non-volatile) SPI transfer settings from the MCP2210 NVRAM MCP2210::SPISettings MCP2210::getNVSPISettings(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_NVRAM_SETTINGS, NV_SPI_SETTINGS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -393,7 +393,7 @@ QString MCP2210::getProductDesc(int &errcnt, QString &errstr) // Returns applied SPI transfer settings MCP2210::SPISettings MCP2210::getSPISettings(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_SPI_SETTINGS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -412,7 +412,7 @@ MCP2210::SPISettings MCP2210::getSPISettings(int &errcnt, QString &errstr) // Gets the USB parameters, namely VID, PID and power settings MCP2210::USBParameters MCP2210::getUSBParameters(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_NVRAM_SETTINGS, USB_PARAMETERS // Header }; QVector response = hidTransfer(command, errcnt, errstr); @@ -500,7 +500,7 @@ int MCP2210::open(quint16 vid, quint16 pid, const QString &serial) // Reads a byte from the given EEPROM address quint8 MCP2210::readEEPROMByte(quint8 address, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ READ_EEPROM, // Header address // Address to be read }; @@ -532,7 +532,7 @@ QVector MCP2210::readEEPROMRange(quint8 begin, quint8 end, int &errcnt, // Resets the interrupt event counter quint8 MCP2210::resetEventCounter(int &errcnt, QString &errstr) { - QVector command = { + QVector command{ GET_EVENT_COUNT, // Header 0x00 // Reset the event counter }; @@ -595,7 +595,7 @@ quint8 MCP2210::setGPIODirection(int gpio, bool direction, int &errcnt, QString // Sets the directions of all GPIO pins on the MCP2210 quint8 MCP2210::setGPIODirections(quint8 directions, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_GPIO_DIRECTIONS, 0x00, 0x00, 0x00, // Header directions, 0x01 // GPIO directions (GPIO7 to GPIO0) }; @@ -606,7 +606,7 @@ quint8 MCP2210::setGPIODirections(quint8 directions, int &errcnt, QString &errst // Sets the values of all GPIO pins on the MCP2210 quint8 MCP2210::setGPIOs(quint16 values, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_GPIO_VALUES, 0x00, 0x00, 0x00, // Header static_cast(values) // GPIO values (GPIO7 to GPPIO0 - GPIO8 is an input only pin) }; @@ -674,7 +674,7 @@ quint8 MCP2210::toggleGPIO(int gpio, int &errcnt, QString &errstr) // Writes a byte to a given EEPROM address quint8 MCP2210::writeEEPROMByte(quint8 address, quint8 value, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ WRITE_EEPROM, // Header address, // Address to be written value // Value @@ -729,7 +729,7 @@ quint8 MCP2210::writeManufacturerDesc(const QString &manufacturer, int &errcnt, // Note that using an empty string for the password will have the effect of leaving it unchanged quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, quint8 accessControlMode, const QString &password, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_NVRAM_SETTINGS, NV_CHIP_SETTINGS, 0x00, 0x00, // Header settings.gp0, // GP0 pin configuration settings.gp1, // GP1 pin configuration @@ -758,7 +758,7 @@ quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, int &errcnt, Q // Writes the given SPI transfer settings to the MCP2210 OTP NVRAM quint8 MCP2210::writeNVSPISettings(const SPISettings &settings, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_NVRAM_SETTINGS, NV_SPI_SETTINGS, 0x00, 0x00, // Header static_cast(settings.bitrate), static_cast(settings.bitrate >> 8), // Bit rate static_cast(settings.bitrate >> 16), static_cast(settings.bitrate >> 24), @@ -791,7 +791,7 @@ quint8 MCP2210::writeProductDesc(const QString &product, int &errcnt, QString &e // Writes the USB parameters to the MCP2210 OTP NVRAM quint8 MCP2210::writeUSBParameters(const USBParameters ¶meters, int &errcnt, QString &errstr) { - QVector command = { + QVector command{ SET_NVRAM_SETTINGS, USB_PARAMETERS, 0x00, 0x00, // Header static_cast(parameters.vid), static_cast(parameters.vid >> 8), // Vendor ID static_cast(parameters.pid), static_cast(parameters.pid >> 8), // Product ID From 6893b08f87b69f3ca74e3a515737b620f5d0cbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Thu, 24 Nov 2022 20:04:30 +0000 Subject: [PATCH 5/7] Add files via upload --- mcp2210.h | 1 - 1 file changed, 1 deletion(-) diff --git a/mcp2210.h b/mcp2210.h index d638977..067f87f 100644 --- a/mcp2210.h +++ b/mcp2210.h @@ -48,7 +48,6 @@ class MCP2210 static const int ERROR_BUSY = 3; // Returned by open() if the device is already in use static const size_t COMMAND_SIZE = 64; // HID command size static const size_t SPIDATA_MAXSIZE = 60; // Maximum size of the data vector for a single SPI transfer (only applicable to basic SPI transfers) - static const size_t PASSWORD_MAXLEN = 8; // Maximum length for the password // Descriptor specific definitions static const size_t DESC_MAXLEN = 28; // Maximum length for any descriptor From f23dcae01757a41d3db5950be72ad1e192563069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Thu, 24 Nov 2022 20:07:43 +0000 Subject: [PATCH 6/7] Add files via upload --- mcp2210.h | 1 + 1 file changed, 1 insertion(+) diff --git a/mcp2210.h b/mcp2210.h index 067f87f..d638977 100644 --- a/mcp2210.h +++ b/mcp2210.h @@ -48,6 +48,7 @@ class MCP2210 static const int ERROR_BUSY = 3; // Returned by open() if the device is already in use static const size_t COMMAND_SIZE = 64; // HID command size static const size_t SPIDATA_MAXSIZE = 60; // Maximum size of the data vector for a single SPI transfer (only applicable to basic SPI transfers) + static const size_t PASSWORD_MAXLEN = 8; // Maximum length for the password // Descriptor specific definitions static const size_t DESC_MAXLEN = 28; // Maximum length for any descriptor From 8dfe905192dc29a244334fd6f36392b2687c4130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Louren=C3=A7o?= <16304376+samuelfmlourenco@users.noreply.github.com> Date: Thu, 24 Nov 2022 22:43:01 +0000 Subject: [PATCH 7/7] Add files via upload --- mcp2210.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++----------- mcp2210.h | 2 ++ 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/mcp2210.cpp b/mcp2210.cpp index 2e773cb..e3c5166 100644 --- a/mcp2210.cpp +++ b/mcp2210.cpp @@ -19,6 +19,7 @@ // Includes +#include #include #include "mcp2210.h" extern "C" { @@ -671,6 +672,33 @@ quint8 MCP2210::toggleGPIO(int gpio, int &errcnt, QString &errstr) return retval; } +// Sends password over to the MCP2210 +// This function should be called before modifying a setting in the NVRAM, if a password is set +quint8 MCP2210::usePassword(const QString &password, int &errcnt, QString &errstr) +{ + quint8 retval; + QByteArray passwordLatin1 = password.toLatin1(); + int passwordLength = passwordLatin1.size(); + if (passwordLength > static_cast(PASSWORD_MAXLEN)) { + ++errcnt; + errstr += "In usePassword(): password cannot be longer than 8 characters.\n"; // Program logic error + retval = OTHER_ERROR; + } else if (password != passwordLatin1) { + ++errcnt; + errstr += "In usePassword(): password cannot have non-latin characters.\n"; // Program logic error + retval = OTHER_ERROR; + } else { + QVector command(passwordLength + 4); + command[0] = SEND_PASSWORD; // Header + for (int i = 0; i < passwordLength; ++i) { + command[i + 4] = static_cast(passwordLatin1[i]); + } + QVector response = hidTransfer(command, errcnt, errstr); + retval = response[1]; + } + return retval; +} + // Writes a byte to a given EEPROM address quint8 MCP2210::writeEEPROMByte(quint8 address, quint8 value, int &errcnt, QString &errstr) { @@ -729,23 +757,46 @@ quint8 MCP2210::writeManufacturerDesc(const QString &manufacturer, int &errcnt, // Note that using an empty string for the password will have the effect of leaving it unchanged quint8 MCP2210::writeNVChipSettings(const ChipSettings &settings, quint8 accessControlMode, const QString &password, int &errcnt, QString &errstr) { - QVector command{ - SET_NVRAM_SETTINGS, NV_CHIP_SETTINGS, 0x00, 0x00, // Header - settings.gp0, // GP0 pin configuration - settings.gp1, // GP1 pin configuration - settings.gp2, // GP2 pin configuration - settings.gp3, // GP3 pin configuration - settings.gp4, // GP4 pin configuration - settings.gp5, // GP5 pin configuration - settings.gp6, // GP6 pin configuration - settings.gp7, // GP7 pin configuration - settings.gp8, // GP8 pin configuration - settings.gpout, 0x00, // Default GPIO outputs (GPIO7 to GPIO0) - settings.gpdir, 0x01, // Default GPIO directions (GPIO7 to GPIO0) - static_cast(settings.rmwakeup << 4 | (0x07 & settings.intmode) << 1 | settings.nrelspi) // Other chip settings - }; - QVector response = hidTransfer(command, errcnt, errstr); - return response[1]; + quint8 retval; + QByteArray passwordLatin1 = password.toLatin1(); + int passwordLength = passwordLatin1.size(); + if (accessControlMode != ACNONE && accessControlMode != ACPASSWORD && accessControlMode != ACLOCKED) { + ++errcnt; + errstr += "In writeNVChipSettings(): the specified access control mode is not supported.\n"; // Program logic error + retval = OTHER_ERROR; + } else if (passwordLength > static_cast(PASSWORD_MAXLEN)) { + ++errcnt; + errstr += "In writeNVChipSettings(): password cannot be longer than 8 characters.\n"; // Program logic error + retval = OTHER_ERROR; + } else if (password != passwordLatin1) { + ++errcnt; + errstr += "In writeNVChipSettings(): password cannot have non-latin characters.\n"; // Program logic error + retval = OTHER_ERROR; + } else { + QVector command(passwordLength + 19); + command[0] = SET_NVRAM_SETTINGS; // Header + command[1] = NV_CHIP_SETTINGS; + command[4] = settings.gp0; // GP0 pin configuration + command[5] = settings.gp1; // GP1 pin configuration + command[6] = settings.gp2; // GP2 pin configuration + command[7] = settings.gp3; // GP3 pin configuration + command[8] = settings.gp4; // GP4 pin configuration + command[9] = settings.gp5; // GP5 pin configuration + command[10] = settings.gp6; // GP6 pin configuration + command[11] = settings.gp7; // GP7 pin configuration + command[12] = settings.gp8, // GP8 pin configuration + command[13] = settings.gpout; // Default GPIO outputs (GPIO7 to GPIO0) + command[15] = settings.gpdir; // Default GPIO directions (GPIO7 to GPIO0) + command[16] = 0x01; + command[17] = static_cast(settings.rmwakeup << 4 | (0x07 & settings.intmode) << 1 | settings.nrelspi); // Other chip settings + command[18] = accessControlMode; // Access control mode + for (int i = 0; i < passwordLength; ++i) { + command[i + 19] = static_cast(passwordLatin1[i]); + } + QVector response = hidTransfer(command, errcnt, errstr); + retval = response[1]; + } + return retval; } // Writes the given chip transfer settings to the MCP2210 OTP NVRAM (this overloaded function is functionally equivalent to the implementation of writeNVChipSettings() that is found in version 1.0.0) diff --git a/mcp2210.h b/mcp2210.h index d638977..04c2182 100644 --- a/mcp2210.h +++ b/mcp2210.h @@ -75,6 +75,7 @@ class MCP2210 static const quint8 WRITE_EEPROM = 0x51; // Write EEPROM static const quint8 SET_NVRAM_SETTINGS = 0x60; // Set NVRAM settings static const quint8 GET_NVRAM_SETTINGS = 0x61; // Get NVRAM settings + static const quint8 SEND_PASSWORD = 0x70; // Send password // NVRAM settings sub-command IDs static const quint8 NV_SPI_SETTINGS = 0x10; // Power-up (non-volatile) SPI transfer settings @@ -295,6 +296,7 @@ class MCP2210 quint8 setGPIOs(quint16 values, int &errcnt, QString &errstr); QVector spiTransfer(const QVector &data, quint8 &status, int &errcnt, QString &errstr); quint8 toggleGPIO(int gpio, int &errcnt, QString &errstr); + quint8 usePassword(const QString &password, int &errcnt, QString &errstr); quint8 writeEEPROMByte(quint8 address, quint8 value, int &errcnt, QString &errstr); quint8 writeEEPROMRange(quint8 begin, quint8 end, const QVector &values, int &errcnt, QString &errstr); quint8 writeManufacturerDesc(const QString &manufacturer, int &errcnt, QString &errstr);