fiji_download.pl 4.12 KB
Newer Older
1
## @file
Stefan Tauner's avatar
Stefan Tauner committed
2
3
4

use strict;
use warnings;
5

Stefan Tauner's avatar
Stefan Tauner committed
6
7
use Log::Log4perl qw(get_logger);

8
9
use FIJI::Connection;
use FIJI::Settings;
Stefan Tauner's avatar
Stefan Tauner committed
10
11

sub test_fi_uart {
12
  my ($port, $payload_ref, $arm_duration, $inject_duration, $trigger, $reset, $fiji_consts) = @_;
Stefan Tauner's avatar
Stefan Tauner committed
13
  # 1.: reset FIJI HW FSM to a sane state (hopefully)
14
  $port->reset_comm($fiji_consts);
Stefan Tauner's avatar
Stefan Tauner committed
15
16
17
18
  # my @payload = map hex($_), $cfg_str =~ /(..)/g; # TODO: how to do this with unpack?
  my %config = (
    payload => $payload_ref,
    arm_duration => $arm_duration,
19
    inject_duration => $inject_duration,
Stefan Tauner's avatar
Stefan Tauner committed
20
21
22
23
    trigger => $trigger,
    reset => $reset,
    consts => $fiji_consts,
  );
24
  $port->send_config(\%config, 1000, 0, 1);
Stefan Tauner's avatar
Stefan Tauner committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
}


sub main {
  my $logger = get_logger();
  my $name = $0;
  $name =~ s/\.p[lm]//;
  my $cfgname = $name . ".cfg";
  $logger->debug("=== Starting new execution of $name ===");
  $logger->debug(sprintf("%d argument(s)%s", scalar(@_), scalar(@_) > 0 ? ": @_" : ""));

  my %cfg;
  if (!Config::Simple->import_from($cfgname, \%cfg)) {
    $logger->fatal("Could not read config file \"$cfgname\": " . Config::Simple->error());
    return 1;
  }

42
  my $fiji_consts = FIJI::Settings::read_configfile($cfg{"cli.fiji_cfg"});
Stefan Tauner's avatar
Stefan Tauner committed
43
44
45
46
47
48
49
  return 1 if !defined($fiji_consts);

  # use Data::Dumper;
  # print Dumper(\%{cfg});
  # print Dumper($fiji_consts);

  my $portname = shift || $cfg{"cli.uart"} || "/dev/ttyUSB1";
50
  my $port = FIJI::Connection->init($portname, $fiji_consts->{'BAUDRATE'}) or $logger->fatal("Could not init UART.") and return 1;
Stefan Tauner's avatar
Stefan Tauner committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  my $cfg_mask = 2 ** $fiji_consts->{'FIU_CFG_BITS'} - 1;
  while (1) {
    my $default_cfg = $cfg_mask;
    my @payload;
    for (my $i = 0; $i < $fiji_consts->{'FIU_NUM'}; $i++) {
      printf("Enter configuration for FIU #%d (default: 0x%x): ", $i, $default_cfg);
    
      my $cfg_str = <STDIN>;
      last unless defined $cfg_str;
      $cfg_str =~ s/\R//g; # remove line breaks globally
      # $cfg_str =~ s/^0x//i; # remove optional 0x prefix
      # if ($cfg_str !~ m/^[0-9A-F]+$|^$/i) {
        # printf("This is not hexadecimal.\n");
        # next;
      # }
      my $cur_cfg = oct((length($cfg_str) == 0) ? $default_cfg : $cfg_str);
      $logger->debug(sprintf("Configuration of FIU #%d is 0x%x.", $i, $cur_cfg));
      push(@payload, $cur_cfg);
    }
    
    # default arm duration to maximum/2
    my $default_arm_dur = oct("0x" . ("FF" x ($fiji_consts->{'ARM_DURATION_WIDTH'}/8))) / 2;
74
    printf("Enter the duration of the ARM timer (default: 0x%x): ", $default_arm_dur);
Stefan Tauner's avatar
Stefan Tauner committed
75
76
77
78
79
80
81
    my $arm_duration = <STDIN>;
    last unless defined $arm_duration;
    $arm_duration =~ s/\R//g; # remove line breaks globally
    $arm_duration = int((length($arm_duration) == 0) ? $default_arm_dur : oct($arm_duration));
    $logger->debug("arm duration is $arm_duration.");

    # default arm duration to maximum/2
82
83
84
85
86
87
88
    my $default_inj_dur = oct("0x" . ("FF" x ($fiji_consts->{'INJECT_DURATION_WIDTH'}/8))) / 2;
    printf("Enter the duration of the INJECT timer (default: 0x%x): ", $default_inj_dur);
    my $inject_duration = <STDIN>;
    last unless defined $inject_duration;
    $inject_duration =~ s/\R//g; # remove line breaks globally
    $inject_duration = int((length($inject_duration) == 0) ? $default_inj_dur : oct($inject_duration));
    $logger->debug("injection duration is $inject_duration.");
Stefan Tauner's avatar
Stefan Tauner committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    printf("Decide the external trigger enable (default: 0): ");
    my $trigger = <STDIN>;
    last unless defined $trigger;
    $trigger =~ s/\R//g; # remove line breaks globally
    $trigger = ($trigger =~ /1|yes|y/i) ? 1 : 0;
    $logger->debug(sprintf("trigger is %sabled.", $trigger == 0 ? "dis" : "en"));

    printf("Decide the reset enable (default: 0): ");
    my $reset = <STDIN>;
    last unless defined $reset;
    $reset =~ s/\R//g; # remove line breaks globally
    $reset = ($reset =~ /1|yes|y/i) ? 1 : 0;
    $logger->debug(sprintf("reset is %sabled.", $reset == 0 ? "dis" : "en"));

104
    if (test_fi_uart($port, \@payload, $arm_duration, $inject_duration, $trigger, $reset, $fiji_consts) != 0) {
Stefan Tauner's avatar
Stefan Tauner committed
105
106
107
108
109
110
111
112
113
114
115
      $logger->error("UART transaction failed.");
    }
  }

  printf "\n";
  $logger->trace("=== Stopping execution ===");
  return 0;
}

Log::Log4perl::init_and_watch('logger.conf', 'HUP');
exit main(@ARGV);