Commit c0f0b9e3 authored by Maciej Lipinski's avatar Maciej Lipinski

WIP-tmp-backup

parent fc8a9d85
......@@ -13,7 +13,7 @@
#define SERVPORT 12345
#define RING_BUFFER_ENTRIES 1048576
#define ENTRIES_PER_PACKET 128
#define ENTRIES_PER_PACKET 16
#define DBG_Y 0
......@@ -100,6 +100,7 @@ struct pll_stat {
int mark_event;
int event;
int record;
int locked;
};
void usage(char *name)
......@@ -157,7 +158,7 @@ void print_pll(struct pll_stat *s,FILE *f)
if((s->flags >> DBG_EVENT) & 0x1) fprintf(f,"%10d",s->mark_event);else fprintf(f,"%10d",0);
if((s->flags >> DBG_AVG_L) & 0x1) fprintf(f,"%10d",s->avg_l); else fprintf(f,"%10d",0);
if((s->flags >> DBG_AVG_S) & 0x1) fprintf(f,"%10d",s->avg_s); else fprintf(f,"%10d",0);
if((s->flags >> DBG_Y) & 0x1) fprintf(f,"%10d",s->locked); else fprintf(f,"%10d",0);
// if((s->flags >> DBG_PERIOD) & 0x1) fprintf(f,"%10d",s->period); else fprintf(f,"%10d",0);
fprintf(f,"\n");
......@@ -179,7 +180,8 @@ int process(struct pll_stat *s, FILE *f, uint16_t seq_id, uint32_t value, int wh
return -1;
int event = 0;
s->mark_event = 0;
if(what == DBG_Y) s->y = value;
if(what == DBG_Y) s->y = value & 0x0FFFF;
if(what == DBG_Y) s->locked =(value & 0x10000) ? 1 : 0 ;
if(what == DBG_ERR) s->err = convertNumber(value);
if(what == DBG_TAG) s->tag = value;
if(what == DBG_REF) s->setpoint = convertNumber(value);
......@@ -209,6 +211,8 @@ int main(int argc, char *argv[])
int i;
int totalcnt = 0;
int finish_after_marker = 0;
int matlab_read = 0;
int debug_to_hex = 0;
FILE *mPLL, *bPLL, *hPLL, *xPLL[4];
struct pll_stat mpll_stat, bpll_stat[4], hpll_stat;
mpll_stat=clear_stat;
......@@ -232,7 +236,7 @@ int main(int argc, char *argv[])
for (name = s = argv[0]; s[0]; s++)
if (s[0] == '/' && s[1])
name = &s[1];
optstring = "?d:b:p";
optstring = "?xmrd:b:p";
printf("Startup options\n");
while ((op = getopt(argc, argv, optstring)) != -1)
{
......@@ -252,6 +256,19 @@ int main(int argc, char *argv[])
print = 1;
printf("print all data to stdout\n");
break;
case 'r':
printf("record all\n");
mpll_stat.record = 1;
hpll_stat.record = 1;
break;
case 'm':
printf("matlab read\n");
matlab_read = 1;
break;
case 'x':
printf("debug to hex\n");
debug_to_hex = 1;
break;
case '?':
default:
usage(name);
......@@ -317,7 +334,7 @@ int main(int argc, char *argv[])
else
printf("Problem to created files\n");
if(finish_after_marker == 0)
if(finish_after_marker == 0 && matlab_read == 0)
{
fprintf(mPLL, "%s \n", tab_content);
// fprintf(bPLL, "%s \n", tab_content);
......@@ -332,6 +349,7 @@ int main(int argc, char *argv[])
int got_marker=0;
int after_marker_cnt = 0;
int cnt_three=0;
while(1)
{
struct fifo_entry tx_buf[ENTRIES_PER_PACKET];
......@@ -352,10 +370,30 @@ int main(int argc, char *argv[])
else if(rc> 0)
{
if(print) printf("Read %d byes of data\n", rc);
for(i=0;i<ENTRIES_PER_PACKET;i++)
// cnt_three = 0;
// fprintf(hPLL,"\n");
if((rc>>3) != ENTRIES_PER_PACKET)
printf("rc != ENTRIES_PER_PACKET: %d \n", rc);
for(i=0;i<(rc>>3);i++)
{
int value = (0xffffff & tx_buf[i].value);
if(debug_to_hex)
{
fprintf(hPLL,"%10x",value);
cnt_three++;
if(cnt_three == 3 )
{
fprintf(hPLL,"\n");
cnt_three = 0;
}
continue;
}
if(print || (0xF & (tx_buf[i].value>>24)) == DBG_EVENT)
{
printf("[ID:%8d] VAL:0x%8x | ", tx_buf[i].seq_id, tx_buf[i].value);
......
......@@ -16,10 +16,9 @@ figure
subplot(4,1,1)
hold on
% plot(Xaxis,mpll(start:finish,3)*to_ps,'b');
plot(Xaxis,smooth(mpll(start:finish,5),10)*to_ps,'k');
% plot(Xaxis,mpll(start:finish,7)*to_ps,'g');
% plot(Xaxis,mpll(start:finish,8)*to_ps,'m');
plot(Xaxis,mpll(start:finish,6)*mean(mpll(start:finish,5)),'r' );
% plot(Xaxis,smooth(mpll(start:finish,5),10)*to_ps,'b');
plot(Xaxis,mpll(start:finish,5)*to_ps,'b');
plot(Xaxis,mpll(start:finish,6)*max(mpll(start:finish,5)),'r' );
legend('err (input to PI)''switchover','Location','northwest');
......@@ -29,8 +28,8 @@ ylabel('phase [ps]');
axis tight
subplot(4,1,2)
hold on
% plot(Xaxis,smooth(bpll(start:finish,5),20)*to_ps,'b');
plot(Xaxis,bpll(start:finish,5)*to_ps,'b');
% plot(Xaxis,bpll(start:finish,7)*to_ps,'g');
plot(Xaxis,bpll(start:finish,6)*max(bpll(start:finish,5)),'r' );
legend('err','switchover','Location','northwest');
title('bPLL');
......@@ -47,9 +46,9 @@ axis tight
subplot(4,1,4)
hold on
% plot(Xaxis,mpll(start:finish,2),'b',switchover,max(mpll(start:finish,2)),'*r' );
plot(Xaxis,mpll(start:finish,2));
plot(Xaxis,mpll(start:finish,4),'g');
plot((switchover-start+1)*unitScale,max(mpll(start:finish,2)),'*r' );
plot(Xaxis,smooth(mpll(start:finish,2),10));
plot(Xaxis,smooth(mpll(start:finish,4),10),'g');
plot((switchover-start+1)*unitScale,mean(mpll(start:finish,2)),'*r' );
legend('Y','long average','switchover','Location','northwest');
title('Y');
......
% Maciej Lipinski / CERN / 2014-10-22
%
% scripts to analyzer debugging messages from the SoftPLL of the switch
%
% options:
%
%
function drawTune(mpll, hpll, start, finish, option)
unitScale = (1/((62.5-(62.5*((2^14)/(1+2^14))))*10^6)); % [s]
% Xaxis = (start:finish)*unitScale;
Xaxis = (1:length(mpll))*unitScale;
to_ps = 16000/(2^14);
figure
subplot(4,1,1)
hold on
if(option~=1)
plot(Xaxis,mpll(:,5)*to_ps,'k'); % mpll error
legend('err','Location','northwest');
title('mPLL');
xlabel('time [s]');
ylabel('phase [ps]');
axis tight
end
clr=['r','g','b','c']
y_max = max(hpll(:,2));
y_avg = mean(hpll(:,2));
err_max=max(hpll(:,3));
err_avg=mean(hpll(:,3));
y_event = (~hpll(:,6))*y_avg + (y_avg+10)*hpll(:,9);
err_event = (~hpll(:,6))*err_avg + err_max*hpll(:,9);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,2)
hold on
% plot(Xaxis,hpll(:,3)*to_ps,'k'); %hpll error
% plot(Xaxis,err_event*to_ps,'r'); %hpll holdonver
plot(Xaxis(start:finish),hpll(start:finish,5),'k');
legend('lock_cnt','Location','northwest');
title('lock cont');
xlabel('time [s]');
ylabel('phase [ps]');
axis tight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,3)
hold on
if(option~=1)
plot(Xaxis(start:finish),mpll(start:finish,5)*to_ps,'k');
legend('err','Location','northwest');
title('mPLL');
xlabel('time [s]');
ylabel('phase [ps]');
axis tight
end
plot(Xaxis(start:finish),hpll(start:finish,2)*to_ps,'k');
title('hPLL');
xlabel('time [s]');
ylabel('Y [ps]');
axis tight
clr=['r','g','b','c']
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,4)
hold on
plot(Xaxis(start:finish),hpll(start:finish,3)*to_ps,'k');
plot(Xaxis(start:finish),err_event(start:finish)*to_ps,'r');
title('hPLLs');
legend('err','delock','Location','northwest');
xlabel('time [s]');
ylabel('phase [ps]');
axis tight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
subplot(4,1,1)
hold on
if(option~=1)
plot(Xaxis,mpll(:,2)*to_ps,'k');
legend('Y','Location','northwest');
title('mPLL');
xlabel('time [s]');
ylabel('DACout');
axis tight
end
clr=['r','g','b','c']
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,2)
hold on
plot(Xaxis,hpll(:,2)*to_ps,'k');
% plot(Xaxis,y_event*to_ps,'r');
legend('Y','delock','Location','northwest');
title('hPLLs');
xlabel('time [s]');
ylabel('DACout');
axis tight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,3)
hold on
if(option~=1)
plot(Xaxis(start:finish),mpll(start:finish,2)*to_ps,'k');
legend('Y','Location','northwest');
title('mPLL');
xlabel('time [s]');
ylabel('DACout');
axis tight
end
clr=['r','g','b','c']
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(4,1,4)
hold on
plot(Xaxis(start:finish),hpll(start:finish,2)*to_ps,'k');
plot(Xaxis(start:finish), y_event(start:finish)*to_ps,'r');
legend('Y','delock','Location','northwest');
title('b=hPLLs');
xlabel('time [s]');
ylabel('DACout');
axis tight
return
\ No newline at end of file
% Maciej Lipinski / CERN / 2014-10-22
%
% c+p from
% http://stackoverflow.com/questions/20151038/matlab-best-technique-to-remove-outliers-in-data
%
function output = outliers2(input, threshold_vec, name)
size_t = size(input);
tmp = zeros(size_t);
average = mean(input)
tlength = length(threshold_vec);
threshold=zeros(tlength,1);
for j=1:size_t(2)
if threshold_vec(j) > 0
threshold(j) = average(j)*threshold_vec(j);
else
threshold(j) = 0;
end
end
for j=1:size_t(2)
if (threshold_vec(j) == 0)
continue
end
all_idx = 1:length(input(:,j));
outlier_idx = abs(input(:,j) - median(input(:,j))) > 3*std(input(:,j)); % Find outlier idx
input(outlier_idx,j) = interp1(all_idx(~outlier_idx), input(~outlier_idx,j), all_idx(outlier_idx)); % Do the same thing for y
end
output =input;
return
\ No newline at end of file
function [mpll, hpll] = plotHPLLtuning(path_name, start_offset_s, stop_offset_s, option)
% options
% 1 - skip data from mPLL, replace with hPLL - used for analyzing data only from hPLL
close all;
freq = 3184; % Hz
T =1/freq;% s
unitScale = (1/((62.5-(62.5*((2^14)/(1+2^14))))*10^6)); % [s]
T = unitScale;
if(start_offset_s < 0)
disp('start_offset max possible');
start_offset = start_offset_s;
else
start_offset = ceil(start_offset_s/(T)); % samples
end
if(stop_offset_s < 0)
disp('stop_offset max possible');
stop_offset = stop_offset_s;
else
stop_offset = ceil(stop_offset_s/(T)); % samples
end
mpll_tmp =load('-ascii', sprintf('%s/mPLL.txt',path_name), 'data');
hpll_tmp =load('-ascii', sprintf('%s/hPLL.txt',path_name), 'data');
disp('size mpll_tmp');
size(mpll_tmp)
hack_offset_0 = 1;
threshold_vec = zeros(size(hpll_tmp,2));
threshold_vec(2)=0.5;
% threshold_vec(3)=0.5;
% threshold_vec(5)=0.5;
hpll_cleared = hpll_tmp;
if(option ==1)
mpll_cleared = hpll_tmp;
else
mpll_cleared = mpll_tmp;
end
% mpll_cleared = outliers(mpll_cleared,threshold_vec, 'mpll');
hpll_cleared = outliers(hpll_cleared,threshold_vec, 'hpll');
%
% smart_threshold_vec = zeros(size(hpll_tmp,2));
% smart_threshold_vec(2)=3;
% smart_threshold_vec(3)=3;
% smart_threshold_vec(5)=3;
%
% mpll_cleared = smartOutliers(mpll_cleared,smart_threshold_vec, 'mpll');
% hpll_cleared = smartOutliers(hpll_cleared,smart_threshold_vec, 'hpll');
%
unitScale = (1/((62.5-(62.5*((2^14)/(1+2^14))))*10^6))*10^3; % [ms]
sz= min([length(mpll_cleared),length(hpll_cleared)]);
mpll = mpll_cleared(1:sz,:);
disp('size mpll');
size(mpll)
hpll = hpll_cleared(1:sz,:);
% drawTune(mpll, hpll, 1, sz , option)
if(start_offset < 0)
start = 1;
else
start = start_offset;
end
if(stop_offset < 0 || (start_offset + stop_offset) > sz)
finish = sz;
else
finish = start_offset + stop_offset;
end
drawTune(mpll, hpll, start, finish, option)
hpll_tmp(start: finish, [1,2,3, 6])
return
......@@ -84,19 +84,22 @@ end
% outliers by fraction of avarage
threshold_vec = zeros(size(mpll_tmp,2));
threshold_vec(1)=0.5;
threshold_vec(2)=0.5;
threshold_vec(2)=0.5; % Y
threshold_vec(4)=0.5;
threshold_vec(5)=0.5;
% outliers by number of standard deviations
smart_threshold_vec = zeros(size(mpll_tmp,2));
smart_threshold_vec(1)=3;
smart_threshold_vec(2)=9; % Y
smart_threshold_vec(1)=3;
smart_threshold_vec(2)=3; % Y
smart_threshold_vec(4)=3; % long avg %
smart_threshold_vec(5)=3;
mpll_cleared = outliers(mpll_tmp(hack_offset_0:end,:),threshold_vec, 'mpll ');
%%%%
mpll_cleared = outliers2(mpll_tmp(hack_offset_0:end,:),threshold_vec, 'mpll ');
%%%%
mpll_cleared = smartOutliers(mpll_cleared,smart_threshold_vec, 'mpll smart ');
......@@ -105,10 +108,10 @@ size(mpll_cleared);
threshold_vec(7)=0.5;
smart_threshold_vec(7)=3;
hpll_cleared = outliers(hpll_tmp(hack_offset_0:end,:),threshold_vec, 'hpll ');
hpll_cleared = outliers2(hpll_tmp(hack_offset_0:end,:),threshold_vec, 'hpll ');
hpll_cleared = smartOutliers(hpll_cleared,smart_threshold_vec, 'hpll smart ');
bpll_cleared_0 = outliers(bpll_tmp_0(hack_offset_0:end,:),threshold_vec, 'bpll 0');
bpll_cleared_0 = outliers2(bpll_tmp_0(hack_offset_0:end,:),threshold_vec, 'bpll 0');
bpll_cleared_0 = smartOutliers(bpll_cleared_0,smart_threshold_vec, 'bpll 0 smart ');
bpll_switchover_0 = detectSwitchover(bpll_cleared_0,6)
if(backup_n > 1)
......
......@@ -32,7 +32,7 @@
/* Size of the software FIFO ring buffer */
#define RING_BUFFER_ENTRIES 1048576
#define ENTRIES_PER_PACKET 128
#define ENTRIES_PER_PACKET 16
__attribute__((packed)) struct fifo_entry {
uint32_t value;
......@@ -50,6 +50,7 @@ struct ring_buffer {
int wr_ptr, rd_ptr, count;
};
int sample_ids[10] = {0,0,0,0,0,0,0,0,0,0};
int rbuf_init(struct ring_buffer *rbuf, int num_entries, int entry_size)
{
......@@ -105,6 +106,25 @@ static struct SPLL_WB *_spll_regs = (struct SPLL_WB*) SPLL_BASE;
#define REG(x) ((uint32_t)(&_spll_regs->x))
#define DBG_MAIN 0x00 /* ... : Main PLL */
#define DBG_HELPER 0x10 /* Sample source: Helper PLL */
#define DBG_EXT 0x20 /* Sample source: External Reference PLL */
#define DBG_BACKUP 0x40
#define DBG_BACKUP_0 0x40
#define DBG_BACKUP_1 0x50
#define DBG_BACKUP_2 0x60
#define DBG_BACKUP_3 0x70
#define DBG_Y 0
#define DBG_ERR 1
#define DBG_TAG 2
#define DBG_REF 5
#define DBG_AVG_L 3
#define DBG_EVENT 4
#define DBG_SAMPLE_ID 6
#define DBG_AVG_S 7
void poll_spll_fifo(int purge)
{
......@@ -136,6 +156,20 @@ void poll_spll_fifo(int purge)
ent.value = _fpga_readl(REG(DFR_HOST_R0));
ent.seq_id = _fpga_readl(REG(DFR_HOST_R1)) & 0xffff;
int what = 0xFF & (ent.value >> 24);
// if(what & (DBG_SAMPLE_ID | DBG_HELPER))
// {
// fprintf(stderr, "%d\n",ent.value & 0xffffff);
//
// if((ent.value & 0xffffff) > sample_ids[0])
// sample_ids[0] = (ent.value & 0xffffff);
// else
// {
// fprintf(stderr, "sample_n order fucked, skipping!\n");
// return;
// }
// }
// fprintf(stderr, "v: %x\n", ent.value);
......@@ -199,7 +233,7 @@ void proxy_stuff(int fd)
}
}
// fprintf(stderr,"Count :%d\n", spll_trace.count);
usleep(10000);
// usleep(10000);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment