1# Function definitions
2proc ::connect_server { {hostname localhost} {port 3121} } {
3    if { [string compare [current_hw_server -quiet] ""] != 0 } {
4        disconnect_server
5    }
6    connect_hw_server -url $hostname:$port
7}
8
9proc ::disconnect_server { } {
10    disconnect_hw_server [current_hw_server]
11}
12
13proc ::jtag_list {} {
14    # Iterate through all hardware targets
15    set hw_targets [get_hw_targets -of_objects [current_hw_server -quiet] -quiet]
16    set idx_t 0
17    foreach hw_target $hw_targets {
18        puts "== Target${idx_t}: $hw_target =="
19        open_hw_target $hw_target -quiet
20        # Iterate through all hardware devices
21        set hw_devices [get_hw_devices]
22        set idx_d 0
23        foreach hw_device $hw_devices {
24            puts "--- Device${idx_d}: $hw_device (Address = ${idx_t}:${idx_d})"
25            set idx_d [expr $idx_d + 1]
26        }
27        close_hw_target -quiet
28        set idx_t [expr $idx_t + 1]
29    }
30}
31
32proc ::jtag_program { filepath {serial "."} {address "0:0"} } {
33    set idx_t [lindex [split $address :] 0]
34    set idx_d [lindex [split $address :] 1]
35
36    set hw_targets [get_hw_targets -of_objects [current_hw_server]]
37    set hw_targets_regexp {}
38
39    foreach target $hw_targets {
40        if { [regexp $serial $target] } {
41            set hw_targets_regexp [concat $hw_targets_regexp $target]
42        }
43    }
44
45    set hw_target [lindex $hw_targets_regexp $idx_t]
46
47    if { [string compare $hw_target ""] == 0 } {
48        error "ERROR: Could not open hw_target $idx_t. Either the address $address is incorrect or the device is not connected."
49    } else {
50        open_hw_target $hw_target -quiet
51    }
52
53    set hw_device [lindex [get_hw_devices] $idx_d]
54    if { [string compare $hw_device ""] == 0 } {
55        close_hw_target -quiet
56        error "ERROR: Could not open hw_device $idx_d. Either the address $address is incorrect or the device is not connected."
57    } else {
58        puts "- Target: $hw_target"
59        puts "- Device: $hw_device"
60        puts "- Filename: $filepath"
61        puts "Programming..."
62        current_hw_device $hw_device
63        set_property PROBES.FILE {} [current_hw_device]
64        set_property PROGRAM.FILE $filepath [current_hw_device]
65        program_hw_devices [current_hw_device]
66        close_hw_target -quiet
67        puts "Programming DONE"
68    }
69}
70
71# Initialization sequence
72open_hw
73connect_server
74
75if [expr $argc > 0] {
76    #Execute a command and exit
77    set cmd [lindex $argv 0]
78    if { [string compare $cmd "list"] == 0 } {
79        jtag_list
80    } elseif { [string compare $cmd "program"] == 0 } {
81        set filepath [lindex $argv 1]
82        if [expr $argc == 3] {
83            set serial [lindex $argv 2]
84            jtag_program $filepath $serial
85        } elseif [expr $argc > 3] {
86            set serial [lindex $argv 2]
87            set devaddr [lindex $argv 3]
88            jtag_program $filepath $serial $devaddr
89        } else {
90            jtag_program $filepath
91        }
92    } else {
93        error "Invalid command: $cmd"
94    }
95    disconnect_server
96    exit
97}
98