⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ezkit.m

📁 matlab串口m文件
💻 M
📖 第 1 页 / 共 2 页
字号:
if (rsp == InterfaceSpeedRsp)			% check to see if response is valid
	%fprintf('115200 Baud\n')
else
	fprintf('Baud change failed!\n')
	rsp
end


%---------------------------------------------------------------------------------------
function ExitCode = EZEndCommun(s)
%
% End communications with the board
%

InterfaceSpeedCmd9600 = [ 0   0   0   0   ,...
	                       4   0   6   2   ,...
                          120 0   0   0   ,...
     	                    0   0   0   0   ];
InterfaceSpeedRsp     = [ 0   0   0   0   ,...
      		              2   0   6   130 ];

WaitForResyncCmd      = [ 0   0   0   0   ,...
                          2   0   8   2   ];
WaitForResyncRsp      = [ 0   0   0   0   ,...
                          2   0   8   130 ];

% Send the "set serial port speed" packet ordering a speed of 9600 baud
fwrite(s,InterfaceSpeedCmd9600)

fclose(s)
s.BaudRate = 9600;						% Set the local serial communications to 9600 baud
fopen(s)

if EZWaitForResponse(s,2);				% wait for response from board
	error('No response, please manually reset the board');
end

rsp = fread(s,s.BytesAvailable)';	% read response
if (rsp == InterfaceSpeedRsp)			% check to see if response is valid
	%fprintf('9600 Baud\n')
else
	fprintf('Baud change failed!\n')
	rsp
end

fwrite(s,WaitForResyncCmd)				% send the "wait for re-sync" packet

if EZWaitForResponse(s,2);				% wait for response from board
	error('No response, please manually reset the board');
end

rsp = fread(s,s.BytesAvailable)';	% read response
if (rsp == WaitForResyncRsp)			% check to see if response is valid
	%fprintf('Exiting...\n')
else
	fprintf('WTF!?\n')
	rsp
end

fclose(s)
freeserial

ExitCode = 1;


%---------------------------------------------------------------------------------------
function data = EZReadDM(s,addr,len)
%
% Read 'len' number of 32-bit Data Memory locations starting at address
% 'addr'.
%
% 'len' is an integer number of locations to read from
% 'addr' is a string containing the starting address of the data in hex
%

MAXLOCA = 251;		% maximum number of locations to read

if (len > MAXLOCA)
	error(['Maxium number of loacations to read is ' num2str(MAXLOCA)])
end

% ----------------------------------------------------------------------
% Convert the hex address, 'addr', into a 4-element vector of integers,
% 'addr_array', containing values between 0 and 255. The first number in
% the vector corresponds to the LSB of hex address, while the last
% number corresponds to the MSB; this is the order of transmission
% expected by the SHARC EZ-Kit Lite.
%
% Examples:
%     '23001' should convert to [1 48 2 0]      (LSB = '01', MSB = '00')
%  '100f60a4' should convert to [164 96 15 16]  (LSB = 'a4', MSB = '10')

addr_len = length(addr);

if addr_len > 8
	error('Address length too long');
elseif mod(addr_len,2)
	addr = ['0' addr];
	addr_len = addr_len + 1;
end

addr_array = [0 0 0 0];

for k = 1:addr_len/2
	sa = addr_len-(k*2-1);
	ea = addr_len-2*(k-1);
	addr_array(k) = hex2dec(addr(sa:ea));
end

% ----------------------------------------------------------------------
% Transform 'len', the number of memory locations to read, into a 4-
% element vector of integer bytes, 'len_array', valued 0 to 255.
%
% Examples:
%  If you wanted to get the contents of only 1 memory location, then
%            len = 1, and len_array = [1 0 0 0]
%  if you wanted to get the contents of 256 locations, then
%          len = 256, and len_array = [0 1 0 0]

% initialize the vector containing the num of locations
len_array = [0 0 0 0];

len_new = len;
for k = 1:ceil(nextpow2(MAXLOCA+1)/8)
	len_array(k) = mod(len_new,256^k);
	len_new = len_new - len_array(k)*256^(k-1);
	len_new = len_new/256;
end

% ----------------------------------------------------------------------
% Set up the command and response packets used for communicating with
% the SHARC EZ-Kit Lite

ReadDM32Cmd  = [ 0   0   0   0   ,...		% read DM32 packet
                 4   0   5   1   ,...
                 addr_array      ,...
                 len_array       ];

ReadDM32Rsp  = [ 0     0   0   0   ,...	% read DM32 response
                 len+4 0   5   129 ,...
                 addr_array        ,...
                 len_array         ];

% ----------------------------------------------------------------------
% Send the packets, wait for and read response, get data.

% Send the "read DM32" packet
fwrite(s,ReadDM32Cmd)

% wait for response from board
if EZWaitForResponse(s,2);
	error('No response, please manually reset the board');
end

rsp = fread(s,16,'uint8')';		% read response
if (rsp == ReadDM32Rsp)				% check to see if response is valid
	%fprintf('Recieved data\n')
else
	fprintf('Wrong response while reading data\n')
	rsp
end

% Read the actual 32-bit floating-point data from the serial port
data = fread(s,len,'float32')';


%---------------------------------------------------------------------------------------
function success = EZWriteDM(s,addr,len,data)
%
% Write 'len' number of 32-bit Data Memory locations starting at
% address 'addr'.
%
% 'len' is an integer number of locations to write.
% 'addr' is a string containing the starting address in hex
% 'data' should be a row vector of numbers that you want to write to the
%   DSP board

MAXLOCA = 251;		% maximum number of locations to write

if (len > MAXLOCA)
	error(['Maxium number of loacations to read is ' num2str(MAXLOCA)])
end

% ----------------------------------------------------------------------
% Convert the hex address, 'addr', into a 4-element vector of integers,
% 'addr_array', containing values between 0 and 255. The first number in
% the vector corresponds to the LSB of hex address, while the last
% number corresponds to the MSB; this is the order of transmission
% expected by the SHARC EZ-Kit Lite.
%
% Examples:
%     '23001' should convert to [1 48 2 0]      (LSB = '01', MSB = '00')
%  '100f60a4' should convert to [164 96 15 16]  (LSB = 'a4', MSB = '10')

addr_len = length(addr);

if addr_len > 8
	error('Address length too long');
elseif mod(addr_len,2)
	addr = ['0' addr];
	addr_len = addr_len + 1;
end

addr_array = [0 0 0 0];

for k = 1:addr_len/2
	sa = addr_len-(k*2-1);
	ea = addr_len-2*(k-1);
	addr_array(k) = hex2dec(addr(sa:ea));
end

% ----------------------------------------------------------------------
% Transform 'len', the number of memory locations to read, into a 4-
% element vector of integer bytes, 'len_array', valued 0 to 255.
%
% Examples:
%  If you wanted to get the contents of only 1 memory location, then
%            len = 1, and len_array = [1 0 0 0]
%  if you wanted to get the contents of 256 locations, then
%          len = 256, and len_array = [0 1 0 0]

% initialize the vector containing the num of locations
len_array = [0 0 0 0];

len_new = len;
for k = 1:ceil(nextpow2(MAXLOCA+1)/8)
	len_array(k) = mod(len_new,256^k);
	len_new = len_new - len_array(k)*256^(k-1);
	len_new = len_new/256;
end

% ----------------------------------------------------------------------
% Set up the command and response packets used for communicating with
% the SHARC EZ-Kit Lite

WriteDM32Cmd  = [ 0     0   0   0   ,...		% write DM32 packet
                  len+4 0   5   0   ,...
                  addr_array        ,...
                  len_array         ,...
						data              ];

WriteDM32Rsp  = [ 0   0   0   0   ,...			% write DM32 response
                  2   0   5   128 ];

% ----------------------------------------------------------------------
% Send the "Write DM32" packet, wait for and analyze response.

% Send the "read DM32" packet
fwrite(s,WriteDM32Cmd(1:16),'uint8')
fwrite(s,WriteDM32Cmd(17:length(WriteDM32Cmd)),'float32')

% wait for response from board
if EZWaitForResponse(s,2);
	error('No response, please manually reset the board');
end

rsp = fread(s,8,'uint8')';			% read response
if (rsp == WriteDM32Rsp)			% check to see if response is valid
	%fprintf('Data Written\n')
else
	fprintf('Wrong response while writing data\n')
	rsp
end

if s.BytesAvailable > 0
	rsp = fread(s,s.BytesAvailable,'uint8')'		% read response
	Error('Extra data!\n')
end

success = 1;


%---------------------------------------------------------------------------------------
function err = EZWaitForResponse(s,time)

err = 0;

tic
while (s.BytesAvailable == 0)		% Wait for response from board
	if toc > time						% if wait time too large, error has occured
		err = 1;
		break
	end
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -