Follow us on Facebook
Follow us on Twitter
Signalogic on LinkedIn

MATLAB® DSP/Analog Hardware Interface Example #2

Source Code Home

Below is an example of interfacing off-the-shelf DSP/analog hardware to MATLAB using DirectDSP® software. Note the use of the ShowHwSelector call to bring up a list of supported hardware.


% test / example MATLAB program for DirectDSP® software
% shows hardware selector, opens Hypersignal program as DSP engine, initializes 
% selected board, creates figure window, and displays analog input buffers in the window
% jhb, 29 FEB 96

load windows.con -mat  % misc. constants used by DirectDSP test and example programs
load enmgr.con -mat    % Engine Manager DLL constants
load hwlib.con -mat    % Hardware Library DLL constants

% initialize flags

fCanceled = FALSE;          % result of hardware selector dialog
hEngine = NULL;             % engine handle
hBoard = NULL;              % board handle
fBoardInitialized = FALSE;  % board status

ShowStat;  % turn on debug status window in Engine Manager

% show hardware selector dialog box ("Hardware Manager"); returns board designator
% string selected by user
% note:  if hardware choice is fixed, then skip this call and use correct board designator
% string in AssignBoard call below

[fSelect, BoardStr] = ShowHwSelector(NULL);

if (fSelect == IDCANCEL)  % user canceled?
    return;
end


% open dsp engine

hEngine = EngineOpen(DS_EO_HSM, NULL, NULL);  % first try to open Hypersignal-Macro 
(or Macro EX) as engine

if (hEngine == NULL)

    hEngine = EngineOpen(DS_EO_HSA, NULL, NULL);  % if that doesn't work, try 
Hypersignal-Acoustic

    if (hEngine == NULL)

        errstr = ['EngineOpen failed; error code = ' num2str(GetHwLibErrStat)];
        disp(errstr);

        return;  % abort
    end
end


% assign a board handle:  engine handle, board designator string, instruction set, 
IO base addr, Mem base addr

hBoard = AssignBoard(hEngine, BoardStr, NULL, NULL, NULL);  % NULL == use defaults where 
possible


% initialize the board; make sure it's installed and responding

fBoardInitialized = InitBoard(hBoard);

if (fBoardInitialized == FALSE)

    errstr = ['InitBoard failed; error code = ' num2str(GetEngErrStat(hEngine))];
    disp(errstr);

    if (hBoard ~= NULL)
        FreeBoard(hBoard);
    end

    if (hEngine ~= NULL)
        EngineClose(hEngine);
    end

    return;  % abort
end


% load executable DSP code file (usually a COFF file produced by DSP manufacturer's 
linker)

if (LoadProcessor(hBoard, NULL, 1) == NULL)  % load default file for the board type 
(processor 0 only)

    errstr = ['LoadProcessor failed; error code = ' num2str(GetEngErrStat(hEngine))];
    disp(errstr);

    if (hBoard ~= NULL)
        FreeBoard(hBoard);
    end

    if (hEngine ~= NULL)
        EngineClose(hEngine);
    end

    return;  % abort
end


% prepare hardware and data transfer values, allocate memory to receive buffers

BufNum = 0;  % start with buffer 0
BufSize = 1024;  % default buffer size
NumChan = 1;

BoardClass = GetClass(hBoard);  % board classification

MemArch = GetMemArch(hBoard);  % board memory architecture

TimBaseAddr = GetDSPPropert(hBoard, DSP_TIMDATAADDR);  % base address of board mem.

% calculate actual samping frequency, because most hardware allows only certain
% possibilities; return values include a "mode" suitable for programming the hardware
% and the actual value allowed, in Hz

[FsMode, FsActual] = CalcSampFreq(hBoard, 22050, NumChan, NULL);  % default is 1 channel,
 Fs = 22.05 kHz

buf = zeros(1, BufSize);  % allocate buffer memory


% write values to DSP code on board

ResetBoard(hBoard);  % reset board (should already be in reset state)

PutDSPProperty(hBoard, DSP_BOARDCLASS, rem(BoardClass, 256));   % main type in low byte
PutDSPProperty(hBoard, DSP_BOARDSUBCLASS, fix(BoardClass/256));  % subtype in high byte

PutDSPProperty(hBoard, DSP_OPMODE, 2);         % use Digital Scope (frame) mode (mode 
value == 2)

PutDSPProperty(hBoard, DSP_FILTTYPE1, 0);      % disable trace 1 real-time filter
PutDSPProperty(hBoard, DSP_FILTTYPE2, 0);      % disable trace 2 real-time filter

PutDSPProperty(hBoard, DSP_TRIGLEVEL, 0);      % free-run triggering
PutDSPProperty(hBoard, DSP_TRIGCHANLIST, 0);

PutDSPProperty(hBoard, DSP_BUFLEN, BufSize);   % buffer size

PutDSPProperty(hBoard, DSP_HOSTBUFNUM, 0);
PutDSPProperty(hBoard, DSP_BUFNUM, 0);

PutDSPProperty(hBoard, DSP_CHANLIST, 0);       % channel list
PutDSPProperty(hBoard, DSP_NUMCHAN, NumChan);  % number of channels

PutDSPProperty(hBoard, DSP_GAINLIST, 0);       % gain list

PutDSPProperty(hBoard, DSP_FSMODE, FsMode);    % sampling rate control register (mode 
value)

PutDSPProperty(hBoard, DSP_FSVALUE, FsActual); % actual sampling rate (in Hz)


% create figure window, axis

hFig = figure;

axis([0, BufSize-1, -32767, 32767]);


% Start board and wait for analog input buffer.  Note SYNC flag, which causes 
% program to wait until buffer is complete; async. processing (the preferred 
% method), requires a callback window handle to process buffers as they become 
% ready (see RegMsgWnd function).

RunProcessor(hBoard, 1);  % run; processor #1 only

WaitForBuffer(hBoard, BufNum, NULL, DS_WFB_POLLED | DS_WFB_SYNC);

if (GetMemArch(hBoard) == DS_GMA_VECTOR)  % board has vector memory architecture

    if (BufNum == 0)
        Status = GetMem(hBoard, DS_GM_VECTOR_DATA_X, TimBaseAddr, DS_GM_SIZE16_CVT, 
buf, BufSize);
    else
        Status = GetMem(hBoard, DS_GM_VECTOR_DATA_Y, TimBaseAddr, DS_GM_SIZE16_CVT, 
buf, BufSize);
    end
    
else  % board has linear or Harvard memory architecture

    Status = GetMem(hBoard, DS_GM_LINEAR_DATA_RT, TimBaseAddr+BufNum*BufSize, 
DS_GM_SIZE16_CVT, buf, BufSize);
end

if (Status == NULL)
    errstr = ['problem with GetMem; error code = ' num2str(GetEngErrStat(hEngine))];
    disp(errstr);
end


% draw buffer data

plot(buf);


% done; leave board in disabled state, clean up handles, engines

if (fBoardInitialized == TRUE)
    DisableBoard(hBoard);
end

if (hBoard ~= NULL)
    FreeBoard(hBoard);
end

if (hEngine ~= NULL)
    EngineClose(hEngine);
end