# Is there any way to test spiWriteRead()?

#### bloguetronica

Joined Apr 27, 2007
1,521
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
{
quint32 bytesToWriteRead = static_cast<quint32>(data.size());  // Conversion done for sanity purposes
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
0x00,               // Reserved
};
for (int i = 0; i < payload; ++i) {
}
#if LIBUSB_API_VERSION >= 0x01000105
#else
int bytesWritten;
#endif
int bytesRead = 0;  // Important!
for (int i = 0; i < bytesRead; ++i) {
}
}
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

• 17.2 KB Views: 0

#### bloguetronica

Joined Apr 27, 2007
1,521
For anyone that wishes to test a pure C++ version, here it is. I haven't tested it, though.

#### Attachments

• 236.4 KB Views: 0

#### bloguetronica

Joined Apr 27, 2007
1,521
I've fixed the code for both classes (Qt and non-Qt). The spiRead() function also required some improvements.

#### Attachments

• 17.5 KB Views: 2
• 17.4 KB Views: 2

#### bloguetronica

Joined Apr 27, 2007
1,521
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

• 53.5 KB Views: 2
• 20.1 KB Views: 0
• 20.4 KB Views: 1