MATLAB Answers

FTDI libMPSSE 0.6 SPI_ReadWrite weird behaviour, loadlibrary, calllib

53 views (last 30 days)
Holzi
Holzi on 15 Apr 2020
Answered: Holzi on 19 Nov 2020
Hello,
currenty I am trying to work with the libMPSSE C-Library provided from FTDI using the loadlibrary() and calllib() matlab-to-c interface (Hardware: FT2232H Mini-Module). The header-file in use is the one prvided by the file-excange 'FTDI SPI library access' example (here). The *.dll file was taken precompiled from the official x64 zip-file from FTDI.
I created the c-type pointers as shown in the minimal excample below. I think the definitions should be correct. The example shows the following steps:
  • load the library using loadlibrary()
  • initialize libMPSSE (Init_libMPSSE)
  • Open a FTDI channel
  • Init the Channel
  • Write 2 bytes of data (working!)
  • ReadWrtie 2 bytes of data, first excecution (not working!)
  • ReadWrtie 2 bytes of data, second execution (not working!)
Using the SPI_Read and SPI_Write function of the library is working flawlessly. I observed the data lines on the oscillloscope. This is why I think that my pointer definitions are correct.
The problems start if I try to use the SPI_ReadWrite function. The pointer definitions are made in a similar way as in the SPI_Write use case. However the behaviour is strange. The SCK Clock is not clocking the expected 16 Bits in or out. Instead it is clocking only three times in the first execution and then several hundert times for a few millisecons with every follwing exection. The read data is then - of course corrupted. I tryed multiple modifications of the pointer definitions and also reviewed the c-header-file. Nothing changed the behaviour of SPI_ReadWrite. The Function deklaration looks acutally quite simple:
Out of frustration I tried different *.dll files from github. I found a repository (here, master/lib/spi/windows/x64) with a apparently different binary file. With this binary everything works perfekt. I can not figure out what the difference is or why the original FTDI dll does not work. I want to use the original dll from FTDI because I don´t feel confident using a dll file from github. Somewhere must be an error in the path from matlab to the original FTDI libMPSSE.dll.
Any suggestions how to narrow this down any further? Thanks for your help!
liba = 'libMPSSE';
libn = 'libMPSSE.dll';
%libn = 'libMPSSE_git.dll';
%load the library
loadlibrary(libn, 'libMPSSE_spi_matlabFriendly.h', 'alias', liba);
%Init the library
calllib(liba, 'Init_libMPSSE');
%Open Channel 0
pChannelHandle = libpointer('voidPtr',255);
calllib(liba,'SPI_OpenChannel',0, pChannelHandle);
% Define the channel configuration struct, and initialize the channel.
ChConfig.ClockRate = uint32(250e3); % Clock speed, Hz
ChConfig.LatencyTimer = uint8(2); % Users guide section 3.4, suggested value is 2-255 for all devices
ChConfig.configOptions = uint32(0); % Bit 1 is CPOL, bit 0 is CPHA. Higher order bits configure the chip select.
st = calllib(liba,'SPI_InitChannel',pChannelHandle,ChConfig); pause(0.1);
disp(st);
%Data to write
transfer_opt = 0; %no CS used
w_buf = [1 , 2];
size_to_tr = 2;
p_w_buf = libpointer('uint8Ptr', uint8(w_buf));
p_size_trnsfred = libpointer('uint32Ptr', 1);
%SPI_Write is working with no problems
st = calllib(liba, 'SPI_Write', pChannelHandle, p_w_buf, uint32(size_to_tr), p_size_trnsfred, uint32(transfer_opt));
disp(st);
%SPI_ReadWrite shows weird behaviour, SCK is first cloking only thre time in the first execution
w_buf = [1 , 2];
size_to_tr = 2;
p_w_buf = libpointer('uint8Ptr', uint8(w_buf));
p_r_buf = libpointer('uint8Ptr', zeros(1, size_to_tr));
p_size_trnsfred = libpointer('uint32Ptr', 1);
st = calllib(liba, 'SPI_ReadWrite', pChannelHandle, p_r_buf, p_w_buf, uint32(size_to_tr), p_size_trnsfred, uint32(transfer_opt));
disp(st);
% and then clocking hundrets of times for several milliseconds for the sencond
w_buf = [1 , 2];
size_to_tr = 2;
p_w_buf = libpointer('uint8Ptr', uint8(w_buf));
p_r_buf = libpointer('uint8Ptr', zeros(1, size_to_tr));
p_size_trnsfred = libpointer('uint32Ptr', 1);
st = calllib(liba, 'SPI_ReadWrite', pChannelHandle, p_r_buf, p_w_buf, uint32(size_to_tr), p_size_trnsfred, uint32(transfer_opt));
disp(st);
%Return value
r_buf = p_r_buf.Value;
disp(r_buf);

Accepted Answer

Holzi
Holzi on 19 Nov 2020
Hello,
Yes! The problem was actually on the FTDI c-code side . There is an error in the libMPSSE 0.6 library. FTDI support promised to fix the issue in the next version of the library.
The suggested fix from FTDI support (which worked perfect for me):
>Edit Line 752 of ftdi_spi.c in the source files and set the third Argument to 3 instead of sizeof(cmdBuffer)
Of course you have to recompile the library.
I hope this helps.

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!