Is there any way to test spiWriteRead()?

Thread Starter

bloguetronica

Joined Apr 27, 2007
1,541
Hi,

I'm currently improving a class, for Qt, to control the CP2130 from Silicon Labs. I'm implementing spiWriteRead() to send to the SPI bus while receiving synchronously, in blocks of 56 bytes. My question is, is there a way to test if the function works correctly by using a CP2130EK evaluation board?
Code:
// Writes to the SPI bus while reading back, returning a vector of the same size as the one given
// This is the prefered method of writing and reading, if both endpoint addresses are known
QVector<quint8> CP2130::spiWriteRead(const QVector<quint8> &data, quint8 endpointInAddr, quint8 endpointOutAddr, int &errcnt, QString &errstr)
{
    quint32 bytesToWriteRead = static_cast<quint32>(data.size());  // Conversion done for sanity purposes
    int bytesLeft = bytesToWriteRead;
    QVector<quint8> retdata;
    while (bytesLeft > 0) {
        int payload = bytesLeft > 56 ? 56 : bytesLeft;
        int bufsize = payload + 8;
        unsigned char *writeReadCommandBuffer = new unsigned char[bufsize] {
            0x00, 0x00,         // Reserved
            CP2130::WRITEREAD,  // WriteRead command
            0x00,               // Reserved
            static_cast<quint8>(payload),
            static_cast<quint8>(payload >> 8),
            static_cast<quint8>(payload >> 16),
            static_cast<quint8>(payload >> 24)
        };
        for (int i = 0; i < payload; ++i) {
            writeReadCommandBuffer[i + 8] = data[i];
        }
#if LIBUSB_API_VERSION >= 0x01000105
        bulkTransfer(endpointOutAddr, writeReadCommandBuffer, bufsize, nullptr, errcnt, errstr);
#else
        int bytesWritten;
        bulkTransfer(endpointOutAddr, writeReadCommandBuffer, bufsize, &bytesWritten, errcnt, errstr);
#endif
        delete[] writeReadCommandBuffer;
        unsigned char *writeReadInputBuffer = new unsigned char[payload];
        int bytesRead = 0;  // Important!
        bulkTransfer(endpointInAddr, writeReadInputBuffer, payload, &bytesRead, errcnt, errstr);
        for (int i = 0; i < bytesRead; ++i) {
            retdata.append(writeReadInputBuffer[i]);
        }
        delete[] writeReadInputBuffer;
        bytesLeft -= payload;
    }
    return retdata;
}
The source code is attached. Eventually, I'll produce a non-Qt version of this, as before. The remaining functions were tested and are proven. Only spiWriteRead() requires verification.

Kind regards, Samuel Lourenço
 

Attachments

Thread Starter

bloguetronica

Joined Apr 27, 2007
1,541
Thanks to the guys at the Silicon Labs forum, I was able to test the new function. The CP2130EK evaluation board has a Si8902 ADC, which is perfect for testing SPI writes and reads. I was able to write a small program and validate spiWriteRead().

Attached is the program that I've used for testing, as well as the finished classes for standard C++ and Qt.
 

Attachments

Top