Using FT4222 with Matlab

19 visualizzazioni (ultimi 30 giorni)
sebastian_z
sebastian_z il 9 Nov 2018
Commentato: Soeren Sofke il 13 Apr 2019
Hi all,
I have to communicate via Matlab with an FTDI FT4222 (USB to SPI/I2C converter). To do this I need two libraries provided by FTDI: - std2xx-lib - FT4222-lib
The first library is needed to connect to the chip. This is working without errors, the correct chip is found. But the second library is not working correctly. I think it is because some functions expect pointers to pointers (void**) and other just expects pointers (void*). In C it is totaly clear but not in Matlab. For programming I used a small example, written in C++ and provided by FTDI, which is working without any problems.
Doing this in Matlab there is an error calling the following function, the function can be called but the return value is: FTDI4222_DEVICE_NOT_SUPPORTED:
% initialization of the device specific part
ftStatus = calllib(ftdi_ft4222_dll, 'FT4222_I2CMaster_Init', ftHandle, 400);
if (ftStatus ~= FT_OK)
disp('Init FT4222 as I2C master device failed!');
return
end
Can anybody help? I would be very glad and if you do have any questions, don't hesitate to ask!
Kind regards Sebastian
  2 Commenti
sebastian_z
sebastian_z il 9 Nov 2018
The appropriate C-Code is as follows:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
ftStatus = FT_OpenEx((PVOID)g_FT4222DevList[0].LocId, FT_OPEN_BY_LOCATION, &ftHandle);
if (FT_OK != ftStatus)
{
printf("Open a FT4222 device failed!\n");
return 0;
}
printf("\n\n");
printf("Init FT4222 as I2C master\n");
ftStatus = FT4222_I2CMaster_Init(ftHandle, 400);
if (FT_OK != ftStatus)
{
printf("Init FT4222 as I2C master device failed!\n");
return 0;
}
So the problem is related to some pointer arithmetic. I hope anyone can help.
Kind regards Sebastian
Soeren Sofke
Soeren Sofke il 13 Apr 2019
Hey Sebastian,
I am operating FT4222 as well instrumenting SPI with a similar flow.
It seems you have to first load d2xx (since libFT4222 sits on top of the d2xx). Then open the channel and afterwards, load the FT4222 and initialize SPI or I2C.
I got this output:
0
FT4222_OK
FT4222_OK
FT4222_OK
0
First and last is from d2xx, second to second last is from FT4222. The following files are in my working directory:
ft4222.png
And here is the code - good luck!
Sören
%% load d2xx library and open channel #0
[~,~] = loadlibrary('ftd2xx.dll', 'ftd2xx.h', 'alias', 'd2xx');
deviceId = 0;
ftHandle = libpointer('voidPtr', 0);
status = calllib('d2xx', 'FT_Open', deviceId, ftHandle);
disp(status);
%% load ft4222 library and initialize SPI master
[~,~] = loadlibrary('LibFT4222-64.dll', 'LibFT4222.h', 'alias', 'ft4222');
SPI_IO_SINGLE = 1;
CLK_DIV_4 = 2;
CLK_IDLE_LOW = 0;
CLK_LEADING = 0;
status = calllib('ft4222', 'FT4222_SPIMaster_Init', ftHandle, SPI_IO_SINGLE, CLK_DIV_4, CLK_IDLE_LOW, CLK_LEADING, 1);
disp(status);
%% write data to SPI slave
dataTx = 1:10;
bufferPtr = libpointer('uint8Ptr', uint8(dataTx));
bytesToWrite = uint16(numel(dataTx));
sizeTransferredPtr = libpointer('uint16Ptr', 0);
isEndTransaction = true;
status = calllib('ft4222', 'FT4222_SPIMaster_SingleWrite', ftHandle, bufferPtr, bytesToWrite, sizeTransferredPtr, isEndTransaction);
disp(status);
%% tear down
status = calllib('ft4222', 'FT4222_UnInitialize', ftHandle);
disp(status);
status = calllib('d2xx', 'FT_Close', ftHandle);
disp(status);
%% unload libraries
unloadlibrary('ft4222')
unloadlibrary('d2xx')

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su External Language Interfaces in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by