60031 2 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04
60031 3 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
+ 70031 0 0x02 0x02 0x02 0x02 0x02 0x02 0x02 0x02
+ 70031 1 0x03 0x03 0x03 0x03 0x03 0x03 0x03 0x03
+ 70031 2 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04
+ 70031 3 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
+ 60161 0 0x02 0x02 0x02 0x02 0x02 0x02 0x02 0x02
+ 60161 1 0x03 0x03 0x03 0x03 0x03 0x03 0x03 0x03
+# 60161 2 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04
+ 60161 3 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
+
+# 70161 0 0x02 0x02 0x02 0x02 0x02 0x02 0x02 0x02
+# 70161 1 0x03 0x03 0x03 0x03 0x03 0x03 0x03 0x03
+# 70161 2 0x04 0x04 0x04 0x04 0x04 0x04 0x04 0x04
+# 70161 3 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01
-##Scripts
+#Scripts#
-* `XYZ` generates trbcmd files from the common baseline database
-* `ABC` generates and loads settings from the 'settings' directory to the FPGAs flashes
-* `DEF` loads all settings from the 'settings' directory to the FPGA directly
+* `generate_pasttrec_settings.pl` prepares data from common baseline database
+ * loads settings to FPGA directly
+ * writes trbcmd files for later writing to Flash ROM
+ * settings are written for boards currently in the system (otherwise serial number / uid can't be matched to addresses)
+ * default PASTTREC registers are currently hardcoded (!)
+
+* `merge_and_flash_settings.pl` generates and loads settings from the 'settings' directory to the FPGAs flashes
+
+* `generate_address_settings.pl` makes the files needed to automatically set the board addresses from Flash ROM
+
+
+##Notes##
+All scripts assume that the daqtool repository is available next to this repository.
--- /dev/null
+#!/usr/bin/perl -w
+use warnings;
+no warnings "portable";
+use FileHandle;
+use Getopt::Long;
+use Data::Dumper;
+use HADES::TrbNet;
+
+my $DEBUG = 0; #print debug messages
+trb_init_ports() or die trb_strerror();
+
+###############################################################################
+#Find available boards & write address setting files for each of them
+###############################################################################
+system("mkdir -p ../settings_oep/auto");
+system("rm ../settings_oep/auto/????_02_network_address.trbcmd");
+
+my $boards = trb_read_uid(0xfe90);
+
+foreach my $uid (keys %$boards) {
+ foreach my $k (keys %{$boards->{$uid}}) {
+ my $addr = $boards->{$uid}{$k};
+
+ my $str = sprintf("0x%04x 0x7001 0x%08x\n", $addr, 0x80000000+$addr);
+
+ my $filename = sprintf("../settings_oep/auto/%04x_02_network_address.trbcmd",$addr);
+ open(FILE, '>', $filename) or die $!;
+ print FILE $str;
+ close FILE;
+ }
+ }
+
+system("mkdir -p ../settings_tdc/auto");
+system("rm ../settings_tdc/auto/????_02_network_address.trbcmd");
+
+$boards = trb_read_uid(0xfe91);
+
+foreach my $uid (keys %$boards) {
+ foreach my $k (keys %{$boards->{$uid}}) {
+ my $addr = $boards->{$uid}{$k};
+
+ my $str = sprintf("0x%04x 0x7001 0x%08x\n", $addr, 0x80000000+$addr);
+
+ my $filename = sprintf("../settings_tdc/auto/%04x_02_network_address.trbcmd",$addr);
+ open(FILE, '>', $filename) or die $!;
+ print FILE $str;
+ close FILE;
+ }
+ }
--- /dev/null
+#!/usr/bin/perl -w
+use warnings;
+no warnings "portable";
+use FileHandle;
+use Getopt::Long;
+use Data::Dumper;
+use HADES::TrbNet;
+
+my $DEBUG = 0; #print debug messages
+my $LOAD_DIRECT = 1; #load settings and initialize
+my $WRITE_FILES = 1; #write configration files
+
+my @default_settings = (0x50018,
+ 0x5011e,
+ 0x50215,
+ 0x5030a,
+ 0x50400,0x50500,0x50600,0x50700,0x50800,0x50900,0x50a00,0x50b00,
+ 0,0,0,0);
+
+
+my $baselines; #serial->pt->@baselines (in hex!)
+my $serials; #uid -> serial
+my $addresses; #addr -> serial
+my $settings; #addr -> @registers(0xa040-0xa07F)
+
+###############################################################################
+#Read baseline file and store values
+###############################################################################
+open FILE, "../pasttrec/pasttrec_baseline.db" or die $!."\npasttrec_baseline.db not found.";
+while (my $a = <FILE>) {
+ if(my @values = $a =~ /^\s*(\d+)\s+(\d)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s+0x(\w\w)\s*$/) {
+ my $s = shift @values;
+ my $pt = shift @values;
+ $baselines->{$s}{$pt}=\@values;
+ }
+ }
+close FILE;
+
+###############################################################################
+#Read serials file and store values
+###############################################################################
+open FILE, "../serials/serials_mdcmbo.db" or die $!."\nserials_mdcmbo.db not found.";
+while (my $a = <FILE>) {
+ if(my @values = $a =~ /^\s*(\d+)\s+0x([\w]{16})\s*$/) {
+ my $s = shift @values;
+ my $uid = shift @values;
+ $serials->{hex($uid)} = $s;
+ }
+ }
+close FILE;
+
+###############################################################################
+#Find available boards
+###############################################################################
+trb_init_ports() or die trb_strerror();
+my $boards = trb_read_uid(0xfe91);
+foreach my $uid (keys %$boards) {
+ foreach my $k (keys %{$boards->{$uid}}) {
+ $addresses->{$boards->{$uid}{$k}} = $serials->{$uid};
+ }
+ }
+
+###############################################################################
+#Loop over available boards, generate settings for each
+###############################################################################
+foreach my $a (keys %$addresses) {
+ if(!defined $baselines->{$addresses->{$a}}) {
+ printf("Baselines for FPGA %04x not found.\n",$a);
+ }
+ else {
+ printf("board %04x found.\n",$a) if $DEBUG;
+ foreach my $pt (0..3) { #for each PT
+ foreach my $i (0..15) { #copy default settings
+ $settings->{$a}[$pt*16+$i] = $default_settings[$i];
+ }
+ if(!defined $baselines->{$addresses->{$a}}{$pt}) {
+ printf("Baseline for FPGA %04x, PT %i not found.\n",$a,$pt);
+ }
+ else{
+ foreach my $i (0..7) { #replace baselines
+ my $t = $settings->{$a}[$pt*16+4+$i];
+ $t &= 0xFFF00;
+ $t += hex($baselines->{$addresses->{$a}}{$pt}[$i]);
+ $settings->{$a}[$pt*16+4+$i] = $t;
+ }
+ }
+ }
+ }
+ }
+
+###############################################################################
+#Load settings via trbcmd
+###############################################################################
+if($LOAD_DIRECT) {
+ foreach my $a (keys %$settings) {
+ trb_register_write_mem($a,0xa002,0,[0x4c504c40,0x4c704c60],2);#set register sets
+ trb_register_write_mem($a,0xa040,0,$settings->{$a},64); #write settings
+ trb_register_write($a,0xaa00,1); #init Pasttrec
+ }
+ }
+
+###############################################################################
+#Write configuration files
+###############################################################################
+if($WRITE_FILES) {
+ system("mkdir -p ../settings_tdc/auto");
+ system("rm ../settings_tdc/auto/????_10_pasttrec_settings.trbcmd");
+ foreach my $a (keys %$settings) {
+ my $str ="";
+ foreach my $i (0..63) {
+ $str .= sprintf("0x%04x 0x%04x 0x%08x\n", $a, 0xa040+$i, $settings->{$a}[$i]);
+ }
+ my $filename = sprintf("../settings_tdc/auto/%04x_10_pasttrec_settings.trbcmd",$a);
+ open(FILE, '>', $filename) or die $!;
+ print FILE $str;
+ close FILE;
+ }
+ }
+
+
+++ /dev/null
-##Temporary Files to be loaded to Flash
-
-* All settings that should be stored in Flash need to be present in this directory.
-* All files should be valid trbcmd files that can be loaded directly with `trbcmd -f`
-
-* Filenames need to start with the four digits (lower case) of the board address they should be loaded to.
-* Extension of all files should be .trbcmd.
-* Multiple files for the same target can exist with arbitrary additional characters in the file name.
-* Automatically generated files should have a filename ending in 'baseline' or 'auto' to be ignored by git.
-
-* Files that start with "common" are loaded to all boards.
-* Common files are loaded first, so that settings can be overwritten with addressed files
-
-* Manually created files should be added to the git repository. Automatically generated files (e.g. baselines) should not be added.
-
*baseline.trbcmd
*auto.trbcmd
+auto
--- /dev/null
+##Temporary Files to be loaded to Flash##
+
+* All settings that should be stored in Flash need to be present in this directory.
+* All files should be valid trbcmd files that can be loaded directly with `trbcmd -f`
+
+* Filenames need to start with the four digits (lower case) of the board address they should be loaded to.
+* Filenames starting with "common" or "final" are loaded to all boards.
+* Extension of all files should be .trbcmd.
+* Multiple files for the same target can exist with arbitrary additional characters in the file name.
+ * files in the same group are loaded in alphabetic order. Add a two digit number in front of the descriptive text for obvious sorting order.
+
+* Automatically generated files will be in the "auto" directory.
+* Manually created files should be added to the git repository. Automatically generated files (e.g. baselines) should not be added.
+
+###Loading Order###
+ * "common" files in the auto directory
+ * "common" files in the main directory (so that settings can be overwritten with addressed files)
+ * Addressed files in the auto directory
+ * Addressed files in the main directory
+ * "final" files in the auto directory
+ * "final" files in the main directory
+
+Within each group, files are sorted alphabetically, e.g. 2123_10_xyz.trbcmd will be loaded before 2123_99_abc.trbcmd
+
--- /dev/null
+*baseline.trbcmd
+*auto.trbcmd
+auto
--- /dev/null
+##Temporary Files to be loaded to Flash##
+
+* All settings that should be stored in Flash need to be present in this directory.
+* All files should be valid trbcmd files that can be loaded directly with `trbcmd -f`
+
+* Filenames need to start with the four digits (lower case) of the board address they should be loaded to.
+* Filenames starting with "common" or "final" are loaded to all boards.
+* Extension of all files should be .trbcmd.
+* Multiple files for the same target can exist with arbitrary additional characters in the file name.
+ * files in the same group are loaded in alphabetic order. Add a two digit number in front of the descriptive text for obvious sorting order.
+
+* Automatically generated files will be in the "auto" directory.
+* Manually created files should be added to the git repository. Automatically generated files (e.g. baselines) should not be added.
+
+###Loading Order###
+ * "common" files in the auto directory
+ * "common" files in the main directory (so that settings can be overwritten with addressed files)
+ * Addressed files in the auto directory
+ * Addressed files in the main directory
+ * "final" files in the auto directory
+ * "final" files in the main directory
+
+Within each group, files are sorted alphabetically, e.g. 2123_10_xyz.trbcmd will be loaded before 2123_99_abc.trbcmd
+
--- /dev/null
+#Select memory location 40,50,60,70 for the four Pasttrec chips, activate autoload of 12 values
+0xfe91 0xa002 0x4c504c40
+0xfe91 0xa003 0x4c704c60
--- /dev/null
+0xfe91 0xaa00 0