FTDI Community

General Category => Discussion - Software => Topic started by: dania on March 07, 2022, 01:10:01 PM

Title: LibMPSSE - unexpected behavior of SPI_ReadWrite function
Post by: dania on March 07, 2022, 01:10:01 PM
Hello,

Code: [Select]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* OS specific libraries */
#ifdef _WIN32
#include<windows.h>
#endif

/* Include D2XX header*/
#include "ftd2xx.h"

/* Include libMPSSE header */
#include "libMPSSE_spi.h"

uint8 wr_buffer[128];
uint8 rd_buffer[128];
uint32 preneseno = 0;

int main()
{
FT_HANDLE handle = NULL;
FT_STATUS status = FT_OK;
FT_DEVICE_LIST_INFO_NODE devList = {0};
ChannelConfig channelConf = {0};
uint32 channels = 0;
uint8 i = 0;

channelConf.ClockRate = 800000;
channelConf.LatencyTimer = 1;
channelConf.configOptions = 0x0020;
channelConf.Pin = 0xFEFBFEFB;

/* init library */
#ifdef _MSC_VER
Init_libMPSSE();
#endif
status = SPI_GetNumChannels(&channels);
if (status != FT_OK) {
printf("status != FT_OK");
return 0;
}
printf("Number of available SPI channels = %d\n",(int)channels);

if (channels > 0)
{
for (i = 0;i < channels;i++)
{
status = SPI_GetChannelInfo(i, &devList);
if (status != FT_OK) {
printf("status != FT_OK");
return 0;
}
if (strcmp("Mila 5000 B", devList.Description) == 0) {
//device is connected
status = SPI_OpenChannel(i, &handle);
if(status != FT_OK) {
printf("status != FT_OK");
return 0;
}
break;
}
}
}
if (handle == NULL) {
printf("not opened");
return 0;
}
status = SPI_InitChannel(handle, &channelConf);
if (status != FT_OK) {
printf("status != FT_OK");
return 0;
}

while (1)
{
//write nasledovane read
wr_buffer[0] = 0x90;
status = SPI_Write(handle, wr_buffer, 1, &preneseno, 0x02);
if (status != FT_OK) {
printf("status != FT_OK");
return 0;
}
status = SPI_Read(handle, rd_buffer, 1, &preneseno, 0x06);

//readwrite
wr_buffer[0] = 0x90;
wr_buffer[1] = 0xFF;
status = SPI_ReadWrite(handle, rd_buffer, wr_buffer, 2, &preneseno, 0x06);
}

return 0;
}


SPI_ReadWrite sends unexpected 0xCC, 0xCC and reads data from SPI slave correctly. Then, even on the second call, SPI_Write sends 0xCC. I think there is a problem in LibMPSSE. How fix it?

Title: Re: LibMPSSE - unexpected behavior of SPI_ReadWrite function
Post by: FTDI Community on March 08, 2022, 05:02:39 PM
Hi,
What version of LibMPSSE are you using?  The latest version of libMPSSE is v0.6:
https://ftdichip.com/software-examples/mpsse-projects/libmpsse-spi-examples/ (https://ftdichip.com/software-examples/mpsse-projects/libmpsse-spi-examples/)

You could add debug commands to try and figure out where the delays are, Another option is not to use libMPSSE and use the d2xx drivers direct:

To use I2C/SPI as a master, see AN_135 - MPSSE Basics and our existing MPSSE Example projects:
https://ftdichip.com/wp-content/uploads/2020/08/AN_135_MPSSE_Basics.pdf (https://ftdichip.com/wp-content/uploads/2020/08/AN_135_MPSSE_Basics.pdf)
https://ftdichip.com/software-examples/mpsse-projects/  (https://ftdichip.com/software-examples/mpsse-projects/)

There are two options to use I2C/SPI modes with the MPSSE engine:
 
a.   Use LibMPSSE libraries. Example code is provided with the download.
b.   Use D2XX drivers direct.  AN_108 provides the necessary information
https://ftdichip.com/wp-content/uploads/2020/08/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf (https://ftdichip.com/wp-content/uploads/2020/08/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf)

Regards
FTDI Community 

Title: Re: LibMPSSE - unexpected behavior of SPI_ReadWrite function
Post by: dania on March 10, 2022, 08:42:42 AM
Hi,
 I have LibMPSSE ver. 0.6. I created the simplest code possible to verify that the problem was in the LibMPSSE.dll library. This is a logical explanation for the fact that other functions work normally. In another forum one user alerted me to https://in.mathworks.com/matlabcentral/answers/518039-ftdi-libmpsse-0-6-spi_readwrite-weird-behaviour-loadlibrary-calllib (https://in.mathworks.com/matlabcentral/answers/518039-ftdi-libmpsse-0-6-spi_readwrite-weird-behaviour-loadlibrary-calllib).

The easiest way is to avoid using the SPI_ReadWrite function.

I suggest placing this information in the FAQ so that users don't have to grope like me.

Regards
Dania