% Small script to read back the Flash memory and write it to a file 
% (cannot be done with the EngPanel)
% This allows to retrieve e.g. the BCULogic and the BCUNios (Program)
%
% @2014-05-10 : GOX

ARGOS_BCU_IP   = '192.168.31.141';
sys_data.bcu_board      = 255;

filename='C:\Documents and Settings\lorenzo\Documenti\MATLAB\readFlash\User_Program.bin'
% As read from the LBT_EngPanel source code v3_7 in Include/LBT_EngPanel.h:
% 
% User_Program_Saddr : 0x1c0000
% User_Logic_Saddr   : 0x200000
% N.B.: Len is taken here to 0x80000, the file should be cropped afterwards, e.g.
% using Linux
% The Nios (Program) total memory size is : 262144 (0x40000)
% The Logic total memory size is : 1572864 (0x180000)
% Example in linux terminal:
%   >> head -c 262144 User_Program.bin >> User_Program.bin.cropped

len =hex2dec('80000')
addr = hex2dec('1c0000')

AOConnect(ARGOS_BCU_IP);

data=mgp_op_rdseq_flash(sys_data.bcu_board,sys_data.bcu_board,len,addr/4,1);

% writing to binary file
fid=fopen(filename,'wb');
count=fwrite(fid,data,'uint32');
fclose(fid);