📄 mmregwrite.m
字号:
function resp = mmregwrite(cc,mmr,data,represent,timeout)
% Private.
% Writes DATA into the TMS320C54x memory-mapped register MMR
% Copyright 2002 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2002/05/07 14:44:03 $
if ischar(data)
data = hex2dec(data);
end
if ~isempty(strmatch(upper(mmr),{'A','B'},'exact'))
error(['Cannot write to accumulator ''' upper(mmr) ''', ' ...
'write to each component instead (' upper(mmr) 'L,' upper(mmr) 'H,' upper(mmr) 'G) ']);
end
% Do not map register-represention to memory-representation on
% special registers (non 16-bit regs) right away
if isempty(strmatch(upper(mmr),{'A','B','AG','BG'},'exact')) & ...
isempty(findstr(':',mmr)) & ~strcmpi('PC',mmr),
data = GetDataFromRepresent(mmr,data,represent);
end
switch upper(mmr)
case 'AR0'
% % addr = {'10' '1'};
addr = [16 1];
write(cc,addr,data,timeout);
case 'AR1'
% % cc,addr = {'11' '1'};
addr = [17 1];
write(cc,addr,data,timeout);
case 'AR2'
% addr = {'12' '1'};
addr = [18 1];
write(cc,addr,data,timeout);
case 'AR3'
% addr = {'13' '1'};
addr = [19 1];
write(cc,addr,data,timeout);
case 'AR4'
% addr = {'14' '1'};
addr = [20 1];
write(cc,addr,data,timeout);
case 'AR5'
% addr = {'15' '1'};
addr = [21 1];
write(cc,addr,data,timeout);
case 'AR6'
% addr = {'16' '1'};
addr = [22 1];
write(cc,addr,data,timeout);
case 'AR7'
% addr = {'17' '1'};
addr = [23 1];
write(cc,addr,data,timeout);
case 'A',
WriteAccumulator(cc,data,'A',[8 9 10],represent,timeout);
case 'AL'
% addr = {'8' '1'};
addr = [8 1];
write(cc,addr,data,timeout);
case 'AH'
% addr = {'9' '1'};
addr = [9 1];
write(cc,addr,data,timeout);
case 'AG'
% addr = {'A' '1'};
addr = [10 1];
Write8BitGuardReg(cc,data,'AG',addr,represent,timeout);
case 'B'
WriteAccumulator(cc,data,'A',[11 12 13],represent,timeout);
case 'BL'
% addr = {'B' '1'};
addr = [11 1];
write(cc,addr,data,timeout);
case 'BH'
% addr = {'C' '1'};
addr = [12 1];
write(cc,addr,data,timeout);
case 'BG'
% addr = {'D' '1'};
addr = [13 1];
Write8BitGuardReg(cc,data,'BG',addr,represent,timeout);
case 'T'
% addr = {'E' '1'};
addr = [14 1];
write(cc,addr,data,timeout);
case 'TRN'
% addr = {'F' '1'};
addr = [15 1];
write(cc,addr,data,timeout);
case 'SP'
% addr = {'18' '1'};
addr = [24 1];
write(cc,addr,data,timeout);
case 'XPC'
% addr = {'1E' '1'};
addr = [30 1];
write(cc,addr,data,timeout);
case 'PC'
% no mapping in memory
absmaxaddress = 2^23 - 1;
if data>absmaxaddress
error('ADDRESS exceeds available memory space.');
end
ccsmexswitchyard([54 cc.boardnum cc.procnum timeout cc.eventwaitms],['PC=' num2str(data)]);
otherwise
if findstr(':',mmr)
WriteToRegisterPair(cc,data,upper(mmr),represent,timeout);
else
error(['REGISTER ''' mmr ''' is not a ''C54x memory-mapped register.']);
end
end
% ------------------------------------
function dummy = WriteToRegisterPair(cc,data,mmr,represent,timeout)
colon = findstr(':',upper(mmr));
reglo = mmr(1:colon-1); % right reg
reghi = mmr(colon+1:end); % left reg
if strmatch(lower(represent),{'binary','2scomp'},'exact')
if data<0 & strcmp(represent,'binary')
data = 0;
data_str = '00000000';
elseif data<0 & strcmp(represent,'2scomp') % negative
pos = data - (-2^31);
data_str = dec2hex(bin2dec(horzcat('1',dec2bin(pos,31))),round(32/4)); % convert to bin with 31 characters & prepend neg bit after
else % positive
data_str = dec2hex(data,round(32/4));
end
elseif strcmp(represent,'ieee')
data_str = sprintf('%tX',data);
end
dataH = uint16( hex2dec( data_str(1:4) ) ); % big-endian
dataL = uint16( hex2dec( data_str(5:end) ) );
mmregwrite(cc,reglo,double(dataL),'binary',timeout);
mmregwrite(cc,reghi,double(dataH),'binary',timeout);
% ---------------------------------
function dummy = Write8BitGuardReg(cc,data,acc,addr,represent,timeout)
if strcmp(represent,'binary')
write(cc,addr,uint8(data),timeout);
elseif strcmp(represent,'2scomp')
write(cc,addr,int8(data),timeout);
elseif strcmp(represent,'ieee')
error(['Input data cannot be written in IEEE for on Register ' acc ' which contains 8 bits']);
end
%---------------------------------------------
function dummy = WriteAccumulator(cc,data,acc,address,represent,timeout)
% Write to 40-bit accummulator
addrLo = address(1); % XL
addrHi = address(2); % XH
addrGu = address(3); % XG
if strmatch(lower(represent),{'binary','2scomp'},'exact')
if data<0 & strcmp(represent,'binary')
data = 0;
data_str = '0000000000';
elseif data<0 & strcmp(represent,'2scomp')
pos = data - (-2^39);
data_str = dec2hex(bin2dec(horzcat('1',dec2bin(pos,39))),round(40/4)); % convert to bin with 39 characters & prepend neg bit after
else % positive
data_str = dec2hex(data,round(40/4));
end
dataG = uint8( hex2dec( data_str(1:2) ) );
dataL = uint16( hex2dec( data_str(3:6) ) ); % big-endian
dataH = uint16( hex2dec( data_str(7:end) ) );
write(cc,[addrGu 1],dataG,timeout);
elseif strcmp(represent,'ieee')
warning(['For IEEE format, data is written to registers ' acc 'L and ' acc 'H (single precision) only ']);
hex = sprintf('%tX',data);
dataL = uint16( hex2dec( hex(1:4) ) );
dataH = uint16( hex2dec( hex(5:end) ) );
end
write(cc,[addrLo 1],uint16(dataL),timeout);
write(cc,[addrHi 1],uint16(dataH),timeout);
% ----------------------------
function data = GetDataFromRepresent(mmr,data,represent)
if strcmpi(represent,'binary')
data = uint16(data);
elseif strcmpi(represent,'2scomp')
data = int16(data);
elseif strcmpi(represent,'ieee')
error(['Input data cannot be written in IEEE for on Register ''' upper(mmr) ''' which contains less than 32 bits']);
end
% [EOF] mmregwrite.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -