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

📄 flashverify.m

📁 matlab给DSP编程的代码
💻 M
字号:
function flashVerifyStatus=flashVerify(ccLink,device,flashApiLib,...
    flashApiHeadersListStruct,coffFile,listHexAddLen,startAddressLoadRAMSection,loadRAMSize)
% FLASHVERIFY - Program c28xx Flash memory
%
% parameters:
%
% ccLink : handle to CCSLINK object
% device : device name ex: 'F2812'
% flashApiLib : TI flash API library
% flashApiHeadersListStruct : TI Flash API headers
% coffFile : COFF file to FLash in memory
% listHexAddLen
% startAddressLoadRAMSection : Start address of the RAM load section
%
%
%   Copyright 2006 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $
%   $Date: 2006/11/29 22:35:08 $

%flashAdressesLength contains two columns and the addresses are listed
%row-wise
%listOfHexfiles is a column cell array

% Use the coffFile provided to create hex files
% The COFF File argument has to be a file with extension .out

% starting address of RAM location where the hex file has to be loaded
% before transferring to flash
% varargin refers to the flashApi header files

% Global variable declarations
% Can change if we incorporate 280x in the future


ccsProjectName = 'flashVerifyTest';
outFileName = strcat(ccsProjectName,'.out');

switch device
    case {'F2812' 'F2811' 'F2810'}
        flashdeviceHeader = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','inc','DSP281x_Device.h');
        flashApiExamplesHeader = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','inc','DSP281x_Examples.h');
        dspGlobalVariableDefsSrcFile = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','src','DSP281x_GlobalVariableDefs.c');
        dspSysCtrlSrcFile = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','src','DSP281x_SysCtrl.c');
        flashVerifyCmdFile = fullfile(matlabroot,'toolbox','rtw','targets','tic2000',...
            'tic2000demos','flashSourceCode','flash281xprog.cmd');
    case {'F2801' 'F2802' 'F2806' 'F2808' 'F2809' 'F28015' 'F28016' 'F28044'}
        flashdeviceHeader = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','inc','DSP280x_Device.h');
        flashApiExamplesHeader = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','inc','DSP280x_Examples.h');
        dspGlobalVariableDefsSrcFile = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','src','DSP280x_GlobalVariableDefs.c');
        dspSysCtrlSrcFile = fullfile(matlabroot,'toolbox','rtw','targets',...
            'ccslink','ccslink','src','DSP280x_SysCtrl.c');
        flashVerifyCmdFile = fullfile(matlabroot,'toolbox','rtw','targets','tic2000',...
            'tic2000demos','flashSourceCode','flash280xprog.cmd');
    otherwise
        error('Invalid device specified');
end

rtsLibFile = fullfile(ccLink.ccsappexe,'..','..','C2000','cgtools','lib','rts2800_ml.lib');

flashVerifySrcFile = fullfile(matlabroot,'toolbox','rtw','targets','tic2000',...
    'tic2000demos','flashSourceCode','testVerifyFlash.c');

mwflashApiHeadersFile = 'tmwFlashApiHeaders.h'; % flash API headers

memoryWidthInBits = 16; % Memory widths could change for other DSPs

% TI's hex converter

hexConverterName = fullfile(ccLink.ccsappexe,'..','..','C2000','cgtools','bin','hex2000');
hexConvTempDir = [ccsProjectName '_HexConv'];
hexConversionFlashCmdFileName = fullfile(matlabroot,'toolbox','rtw','targets','tic2000','tic2000demos',...
    'flashSourceCode',[device,'HexConv.cmd']);

% Generic coff Filename used by the hex conveter

flashCoffFileName = 'flashCOFF.out';

% Hex conversion global variables

structArrInd = 1; % counter for number of buffers of size loadRAMSize to be written to buffer

% Save the current directory
currDir = cd;

% hex conversion utility
% The COFF file name has to be used to generated a hex2000 parsable cmd
% file depending on the device with fill option parameter 0xFFFF

% If the COFF file is specified with a bunch of flash start addresses and
% length , generating a CMD file based on provided flash addresses
% upper layer If the start address is non-valid error out
% At this point assuming everything is valid

disp('Creating hex files from COFF file');

if ~isempty(coffFile)

    % Making a temporary directory for the hex conversion process
    mkdir(hexConvTempDir);

    copyfile(coffFile,strcat(hexConvTempDir,'\',flashCoffFileName),'f');
    cd(hexConvTempDir);

    dos([hexConverterName ' -q ' hexConversionFlashCmdFileName]);

    hexConvFileStruct = dir('*.hex');
    delete('*.hex');

    % Run the hex converter with the -image -zero option
    % Generate all the hex Files corresponding to all the sections defined
    % in the Hex CMD file

    dos([hexConverterName ' -q -image -zero ' hexConversionFlashCmdFileName]);

    for i=1:length(hexConvFileStruct)
        copyfile(hexConvFileStruct(i).name,currDir,'f');
    end
    delete('*.*');
    cd(currDir);
    for i=1:length(hexConvFileStruct)
        listHexAddLen(i).filename=hexConvFileStruct(i).name;
        [PATHSTR,flashSectName,EXT,VERSN] = fileparts(listHexAddLen(i).filename);
        [listHexAddLen(i).startaddress,listHexAddLen(i).length]=flashDeviceMemMap(device,flashSectName);
    end

    rmdir(hexConvTempDir,'s');

end

disp('Parsing hex files into manageable units ...');
flashHexProgram(listHexAddLen,startAddressLoadRAMSection);

cd(ccLink,pwd);
warning off;
mkdir(ccsProjectName);
warning on;
cd(ccLink,ccsProjectName);

fpHeaders = fopen(fullfile(pwd,ccsProjectName,mwflashApiHeadersFile),'w');

%flashApiHeadersListStruct
fprintf(fpHeaders,'#include "%s"\n',flashApiHeadersListStruct.flashapiconfig);
fprintf(fpHeaders,'#include "%s"\n',flashApiHeadersListStruct.flashapilib);
fprintf(fpHeaders,'#include "%s"\n',flashdeviceHeader);
fprintf(fpHeaders,'#include "%s"\n',flashApiExamplesHeader);

fclose(fpHeaders);

try
    warning off;
    save(ccLink,ccsProjectName,'project');
    close(ccLink,ccsProjectName,'project');
    warning on;
catch
    warning on;
end
new(ccLink,ccsProjectName,'project');

currentCcsWorkingDirectory = ccLink.cd;

add(ccLink,flashVerifySrcFile);
add(ccLink,flashVerifyCmdFile);

add(ccLink,dspGlobalVariableDefsSrcFile);
add(ccLink,dspSysCtrlSrcFile);

% To set the build options to satisfy all dependencies

buopts = ccLink.getbuildopt;

compilerOptionString = strcat(buopts(4).optstring,'-i "."',' -i"',currentCcsWorkingDirectory,'"',...
    ' -i"',matlabroot,'\toolbox\rtw\targets\ccslink\ccslink\inc"');

setbuildopt(ccLink,'Compiler',compilerOptionString);

linkerOptionString = strcat('-c -m"',ccsProjectName,'.map" -o"',ccsProjectName,...
    '.out" -w -x',' -stack400',' -i"',matlabroot,...
    '\toolbox\rtw\targets\ccslink\rtlib"',' -l"',rtsLibFile,'"',...
    ' -l"',flashApiLib,'"');

setbuildopt(ccLink,'Linker',linkerOptionString);

% Saving the project in CCS
save(ccLink,ccsProjectName,'project');
build(ccLink,'all');
reset(ccLink);
load(ccLink,outFileName);

ffs = list(ccLink, 'function', 'tmwbreakpt');
FuncAdd = ffs.tmwbreakpt.address;
insert(ccLink,FuncAdd.start(1));

ffs = list(ccLink, 'function', 'main');
FuncAdd = ffs.main.address;
insert(ccLink,FuncAdd.end(1));

restart(ccLink);

run(ccLink,'main');

startAddloadRAMObj = createobj(ccLink,'StartAddLoadRAM');
startAddloadRAMObj.write(uint32(hex2dec(startAddressLoadRAMSection)));

% Programming all the hex cut buffers into flash
% Structure arrays with one index are filled in row major format.

%Number of buffers to be programmed passed from MATLAB
numHexCutBufObj = createobj(ccLink,'NumHexBuf');
numHexCutBufObj.write(size(structArrhexCutBuf,2));

% Run to stop at the defined breakpoint
run(ccLink,'runtohalt',100);

for i=1:size(structArrhexCutBuf,2)

    % At the break point and fill the variables using link for CCS
    % commands

    % Embedded objects for interacting with symbols in the CCS project

    % Passing data to CCS from MATLAB

    % Buffer of size load RAM size containing hexadecimal data written
    % directly to the load RAM section
    
    if iswritable(ccLink,startAddressLoadRAMSection,'uint16',loadRAMSize);
        write(ccLink,startAddressLoadRAMSection,structArrhexCutBuf(i).buffer,100);
    else
        error('Cannot write to loadRAM section\n unconfigured memory or out of memory');
    end

    startAddFlashObj = createobj(ccLink,'StartAddFlash');
    startAddFlashObj.write(structArrhexCutBuf(i).startaddress);

    % Length in number of memory locations to be verified

    lengthVerifyObj = createobj(ccLink,'Length');
    lengthVerifyObj.write(structArrhexCutBuf(i).length);

    disp(['Verifying Flash section ' num2str(i) ' of ' num2str(size(structArrhexCutBuf,2)) ' .......']);
    run(ccLink,'runtohalt',100); % It will stop at the break point.

    verifyStatusObj = createobj(ccLink,'VerifyStatus');
    flashVerifyStatus = verifyStatusObj.read;

    verifyStatusStrucObj = createobj(ccLink,'VerifyStatusStruct');
    flashVerifyStatusStruc = verifyStatusStrucObj.read;

    if flashVerifyStatus
        cleanup;
        error(['Flash Verify API returned error code:' num2str(flashProgramStatus)]);
    else
        disp(['Verifying Flash section ' num2str(i) ' successful!'])
    end
end
cleanup;

    function flashHexProgram(listHexAddLen,startAddressLoadRAMSection)
        
        for numHexCount = 1:size(listHexAddLen,2)

            disp(['Parsing hex file ' num2str(numHexCount) ' of ' ...
                num2str(size(listHexAddLen,2)) ' ...']);

            fpHexOuttoFlash = fopen(listHexAddLen(numHexCount).filename,'r');
            fileReadBuf = fscanf(fpHexOuttoFlash ,'%s');

            if isempty(fileReadBuf)
                error('The hex file %s is empty, please provide a hex file with hex characters',...
                    listHexAddLen(numHexCount).filename);
            end

            hexCharBufIndex=regexp(fileReadBuf,'[A-F]|[0-9]|[a-f]'); %Strip the above so that it contains only hex
            hexFileBuf = fileReadBuf(hexCharBufIndex); % hexFile is the hex string from one hex File.

            flashMemSuffCheck;

            remDivBy4 = mod(length(hexFileBuf),memoryWidthInBits/4);

            if remDivBy4 ~= 0
                for numFchar = 1:remDivBy4
                    hexFCharPad(numFchar)='F';
                end
            else
                hexFCharPad='';
            end

            hexFileBuf = strcat(hexFileBuf,hexFCharPad);
            numBufHexFile = floor(length(hexFileBuf)/(loadRAMSize*memoryWidthInBits/4));
            remHexCharCut=mod(length(hexFileBuf),(loadRAMSize*memoryWidthInBits/4));

            tStartAdd=uint32(hex2dec(listHexAddLen(numHexCount).startaddress)); % should be uint32
            for i = 1:numBufHexFile
                for j=1:loadRAMSize

                    %tcount and terrmsg are dumy variables
                    [tempstr,tcount,terrmsg,tnextindex] = sscanf(hexFileBuf, '%c', memoryWidthInBits/4);
                    tempBuf(j)=uint16(hex2dec(tempstr));
                    hexFileBuf=hexFileBuf(tnextindex:end);
                end

                structArrhexCutBuf(structArrInd).buffer = tempBuf;
                structArrhexCutBuf(structArrInd).startaddress=tStartAdd;
                structArrhexCutBuf(structArrInd).length=uint16(loadRAMSize);
                structArrInd=structArrInd+1;
                tStartAdd = tStartAdd + uint32(loadRAMSize);
            end
            % The remainder of hex characters can be empty
            if remHexCharCut > 0
                for i=1:loadRAMSize
                    if i <= (remHexCharCut/4)
                        [tempstr,tcount,terrmsg,tnextindex] = sscanf(hexFileBuf, '%c', memoryWidthInBits/4);
                        tempBuf(i)=uint16(hex2dec(tempstr));
                        hexFileBuf=hexFileBuf(tnextindex:end);
                    else
                        tempBuf(i)=uint16(hex2dec('FFFF'));
                    end
                end
                structArrhexCutBuf(structArrInd).buffer = tempBuf;
                structArrhexCutBuf(structArrInd).startaddress=tStartAdd;
                structArrhexCutBuf(structArrInd).length=uint16(remHexCharCut/4);
                structArrInd=structArrInd+1;
            end
        end % End of Main For loop -loops through the different hex files
        function flashMemSuffCheck
            % Checks memory sufficiency for flash programming the
            % particular Mex file
            if hex2dec(listHexAddLen(numHexCount).length) < (length(hexFileBuf)/4)
                error('%s is insufficient for flashing the %s',listHexAddLen(numHexCount).length,...
                    listHexAddLen(numHexCount).filename);
            end

        end % End of flashMemSuffCheck function
    end % end of the flashHexProgram function

    function cleanup

        try
            warnstate = warning;  % Save the current warning state
            warning off;
            delete(ccLink,'all');
            save(ccLink,ccsProjectName,'project');
            close(ccLink,ccsProjectName,'project');
            cd(ccsProjectName)
            delete('*.*');  % to delete all the files
            rmdir('*.*','s'); %  to delete all the directories
            cd(currDir);
            rmdir(ccsProjectName,'s');
            warning(warnstate);
        catch
        end
    end %cleanup function
end % end of the Main flashProgram function


⌨️ 快捷键说明

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