Page 1 of 1
SPI transfer config in Matlab
Posted: Tue Jul 06, 2010 12:26 pm
by dsp28x
Hi, I'm new to SUB-20. I want to use the SUB-20 SPI interface to read from another SPI modul, Using SUB-20 tool works OK, even though its only read a byte. The problem is in MATLAB, how to configure so that the SUB-20 can read data via MATLAB setup.
Here is my configuration
/* Configure SPI */
sub_spi_config( hndl, SPI_ENABLE|SPI_CPOL_RISE|SPI_SMPL_SETUP|SPI_MSB_FIRST|SPI_CLK_4MHZ, 0 );
Matlab Configure SPI
if ~ sub20.SPI_SetConfig(sub20.SpiEnable + sub20.SpiCpolFall + ...
sub20.SpiSmplSetup + sub20.SpiMsbFirst + sub20.SpiClk_8MHz)
break;
end
Synopsis
int sub_spi_transfer( sub_handle hndl, char* out_buf, char* in_buf, int sz, int ss_config )
rc = sub_spi_transfer( hndl, out, 0, 10, SS_CONF(0,SS_LO) );
if ~ sub20.SPI_SetConfig(sub20.SpiEnable + sub20.SpiCpolFall + ...
sub20.SpiSmplSetup + sub20.SpiMsbFirst + sub20.SpiClk_8MHz)
break;
end
% Create .NET data array
Data = NET.createArray('System.Byte', TransferSize);
% Fill up the array with data bytes to be sent
Data.Set(6, hex2dec('a'));
Data.Set(7, hex2dec('b'));
Data.Set(8, hex2dec('c'));
Data.Set(9, hex2dec('d'));
if ~ sub20.SPI_Transfer_r(Data, 0, sub20.SpiSS_HHL)
break;
I hope to hear from you.
dsp
Re: SPI transfer config in Matlab
Posted: Wed Jul 07, 2010 5:26 am
by serg
Hi,
Have you initialized the TransferSize variable before executing this code? Please note that the TransferSize must be at least 10 in your case. All the rest looks good. We have a similar SPI sample code.
%---------------------------------
disp 'SUB-20 SPI sample. Configures SPI and exchanges data with SPI slave(SS=2)'
TransferSize = 3;
NET.addAssembly('C:\Program Files\SUB-20\bin\sub20dnc.dll');
sub20 = Xdimax.Sub20();
% simulate do { ... } while(0) loop
for k=0:0
% Open first available SUB-20 device
if ~ sub20.Open(0)
break;
end
% Set SPI configuration
if ~ sub20.SPI_SetConfig(sub20.SpiEnable + sub20.SpiCpolRise + ...
sub20.SpiSmplSetup + sub20.SpiMsbFirst + sub20.SpiClk_4MHz )
break;
end
% Create .NET data array
Data = NET.createArray('System.Byte', TransferSize);
% Fill up the array with data bytes to be sent
Data.Set(0, hex2dec('2a'));
Data.Set(1, hex2dec('2b'));
Data.Set(2, hex2dec('2c'));
% Exchange data with SPI slave(SS=2). SS signal stays high
if ~ sub20.SPI_Transfer(Data, 2, sub20.SpiSS_H)
break;
end
% Copy read data to the Matlab array
ReadData=zeros(1, TransferSize);
for i=1:TransferSize
ReadData(i)= Data.Get(i-1) ;
end
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', sub20.GetLastError() )
error('Error!')
end
%---------------------------------------------
Regards
Re: SPI transfer config in Matlab
Posted: Wed Jul 07, 2010 9:17 am
by dsp28x
Hi serg,
I've intialized the TransferSiZe to 10. but the question is if SPI modul is slave, is there any way to intialize my code in MATLAB, so that I can automatically retrieve my data.
The example code in matlab code the values are constants.
% Create .NET data array
Data = NET.createArray('System.Byte', TransferSize);
% Fill up the array with data bytes to be sent
Data.Set(0, hex2dec('2a'));
Data.Set(1, hex2dec('2b'));
Data.Set(2, hex2dec('2c'));
% Exchange data with SPI slave(SS=0). SS signal stays high
if ~ sub20.SPI_Transfer(Data, 0, sub20.SpiSS_HHL)
break;
end
I've also tried to setup spi_transfer like this:
sub20.SPI_config(0,LO)
sub20.SPI_Transfer( hndl, out, 0, 10, SS_CONF(0,SS_LO) );
But it doesn't work. If there is proper way to setup the Matlab code, I'll glad to hear you.
Regards
Re: SPI transfer config in Matlab
Posted: Wed Jul 07, 2010 10:28 am
by xol
Hi,
I'm not working with MATLAB but if you use SUB-20 as SPI slave you have to use another functions.
Take a look here:
http://www.xdimax.com/sub20/doc/HTML/spi_slave.htm
Re: SPI transfer config in Matlab
Posted: Wed Jul 07, 2010 12:19 pm
by serg
Hi,
Xol is right. If you want to use the SUB-20 as a SPI slave device the config sequence is different .
1) Add the sub20.SpiSlave flag to the sub20.SPI_SetConfig call
2) Call the sub20.FIFO_SetConfig( sub20.SelectSpi )
3) Call either FIFO_Read or FIFO_Write methods
Regards
Re: SPI transfer config in Matlab
Posted: Thu Jul 08, 2010 1:54 pm
by dsp28x
Hi xol
I've tried to read data via SPI slave mode. It reads reads the last byte (Eg. 1234 will return as 3412). By the way are spi_read() and fifo_Read() does not support in Matlab? Error:
No method 'SPI_Read' with matching signature found for class 'Xdimax.Sub20'.
Here is my code (in MATLAB) if I've done something wrong :
SpiDataToRead = 10;
NET.addAssembly('C:\Programmer\SUB-20\bin\sub20dnc.dll')
sub20 = Xdimax.Sub20();
% simulate do { ... } while(0) loop
for k=0:0
% Open first available SUB-20 device
if ~ sub20.Open(0)
break;
end
% Set SPI configuration as slave
if ~ sub20.SPI_SetConfig(sub20.SpiEnable + sub20.SpiSlave + sub20.SpiCpolRise + ...
sub20.SpiSmplSetup + sub20.SpiMsbFirst + sub20.SpiClk_8MHz)
break;
end
% Spi fifo configurations
if ~ sub20.FIFO_SetConfig(sub20.FifoSelectSpi);
break;
end
% Create .NET data array
Data = NET.createArray('System.Byte', SpiDataToRead);
Arr = NET.createArray('System.Byte', SpiDataToRead);
% Setup array to SPI MOSI
for i=0:(SpiDataToRead-1)
Arr.Set(i,1);
end
% SPI read
if ~ sub20.SPI_Read(Data, Arr)
break;
end
% Exchange data with SPI slave(SS=0). SS signal stays high
if ~ sub20.SPI_Transfer(Data, 0, sub20.SpiSS_H)
break;
end
% Copy read data to the Matlab array
ReadData=zeros(1, SpiDataToRead);
for i=1:SpiDataToRead
ReadData(i)= Data.Get(i-1);
end
%
end
err = sub20.GetLastError();
% Close SUB-20 device
sub20.Close()
if err > 0
fprintf('Error %d\n', sub20.GetLastError() )
error('Error!')
end
%--------------------------------------------------------------------------
Best reagrads
Re: SPI transfer config in Matlab
Posted: Thu Jul 08, 2010 2:03 pm
by xol
I've tried to read data via SPI slave mode. It reads reads the last byte (Eg. 1234 will return as 3412).
This is well known big/little endiens. SPI works on byte level and does not care about it. Check how your SPI master sends it.