tribble-control 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8f86442fab5a950e990f85b2cf2b24576a1c2b51232a1e75adb939fca4ab68c
4
- data.tar.gz: 57badcadcf3e5402054e3a5f4aae9677eb292cc12afcb0b2bd2076ae630680d2
3
+ metadata.gz: ba1987843323e07de479049fef78dce81d358c984361179302d75b4aefdce33f
4
+ data.tar.gz: b0c5233517bf26e2a3290203e87812d9c34ecd7de023af23d3287653e5552a3e
5
5
  SHA512:
6
- metadata.gz: 16d0575d4b5abaf92640e8a2a06cf2d38706961f686a18fe02e2af8147819666ca4e506f611fbd10bcddc5d80eabe78b7d7231e8ca5ca05d9dcb14d8fa138dba
7
- data.tar.gz: fac31a478cb52fa7cda51b1ea2be40c19e74a4022276f6c5a959380efe2b4406db3034cb3d4420aea4b0949132441bf85c91f42406a91537df7f70a82b6c4ff6
6
+ metadata.gz: '058b6e7796cbcf074721e72727c8a26d566543eff3c36111526530b82a952b4ce2bc62e9ad4d9f90107f79fb29687c4438013f382601828dabba3f2bf7902eae'
7
+ data.tar.gz: a905e62fefbffcbc0835f2d4f06548359a0ae5ab515e7f228731ad1edfab2a78dd81c961f4ef3f94a3a3d927bfb29167e494c786cbc45e134a6d1423604088e9
data/DESIGN.md CHANGED
@@ -57,6 +57,7 @@ lib/tribble-control/
57
57
  hub/exsys.rb the ExSYS hub, over the exsys gem
58
58
  hub/usb.rb any hub that switches its own ports, via usbconfig
59
59
  tally.rb the seam where firmware knowledge goes
60
+ firmware.rb what a firmware file is: .hex or .elf, checked
60
61
  cli.rb options, configuration, each_device, openocd
61
62
  cli/*.rb one file per command (usb, flash, ...)
62
63
  man/man1/tribble-control.1 the manual (mdoc), rendered by --man
@@ -471,10 +472,10 @@ Two things about this method are load-bearing:
471
472
 
472
473
  ```sh
473
474
  openocd \
474
- -c 'set WORKAREASIZE 0x<work_area>' # unless work_area = none
475
+ -c 'set WORKAREASIZE 0x<work_area>; list' \ # unless work_area = none
475
476
  -c 'source [find interface/<interface>.cfg]' \
476
- -c 'transport select <transport>' \ # unless transport = none
477
- -c 'source [find target/<target>.cfg]' \
477
+ -c 'transport select <transport>; list' \ # unless transport = none
478
+ -c 'source [find target/<target>.cfg]' \ # nRF: see below
478
479
  -c 'adapter usb location <usb>' \ # --method usb only
479
480
  -c 'adapter serial <serial>' \ # whenever the configuration has one
480
481
  -c '<each command the caller passed>' \
@@ -483,7 +484,9 @@ openocd \
483
484
 
484
485
  `adapter usb location` is documentation of intent, not a selector:
485
486
  openocd 0.12 reaches the same adapter whichever path it is given,
486
- including one that does not exist. `adapter serial` is what actually
487
+ including one that does not exist. openocd prints what each command
488
+ returns; `; list` returns nothing, so the work area and the transport
489
+ are not echoed into the output a failure report shows. `adapter serial` is what actually
487
490
  selects, which is why `--method usb` passes it too whenever the configuration
488
491
  has one. A board with no `serial =`, on a bench with more than one
489
492
  adapter powered, is a board chosen at random.
@@ -493,6 +496,49 @@ switched. A command that always needs it says so with `OPENOCD =
493
496
  true`; `connect` does not, and checks when it reaches for it under
494
497
  `--reset`.
495
498
 
499
+ A run that fails is reported by `CLI#openocd` itself, under the
500
+ board's name (`name:`, the serial when none is given): the last
501
+ `OPENOCD_TAIL` (15) non-empty lines of its output at the error level,
502
+ leaving out what openocd prints on every run (its banner, the
503
+ acknowledgement of `shutdown`: `OPENOCD_NOISE`, matched by content
504
+ wherever it falls, not by dropping the first lines), and every line
505
+ at debug. openocd stops on the first error, so the cause, and the
506
+ target state it read just before, is in the tail; the caller then
507
+ says only which step failed. A run that exits 0 is reported
508
+ too when its output has an `Error:` line, the same tail at the
509
+ warning level: openocd writes an image it misread as raw data, faults
510
+ the core, and still exits 0. `flash` refuses such a file before any
511
+ port is switched (`Firmware.type`: Intel HEX named `.hex` with every
512
+ record checked, or ELF named `.elf`) and passes openocd the type; the
513
+ warning is for whatever gets past that. Every run's output, failed or not, is
514
+ at debug. A core found in lockup or a hard
515
+ fault (`LOCKUP`: `pc 0xfffffffe`, or openocd's own "lockup" or
516
+ "HardFault", since a garbage vector table locks the core at a pc of
517
+ its own) is named in a warning: a fact about the board, not a
518
+ verdict, since a flash can succeed from it: the `reset init` at the
519
+ head of the flash sequence takes the core out of lockup before
520
+ anything is written, so lockup alone never decides a flash.
521
+
522
+ `flash` tries a failed board once more, whatever the failure, with a
523
+ warning: the sequence erases and writes from scratch, so a second run
524
+ is safe, and a transient fault clears on it -- positioning-bench saw
525
+ the SWD link drop mid-erase on one flash in nine, and the next run
526
+ from the same state succeed. A failure that persists costs one more
527
+ run. Nothing else is retried, `reset` included, and nothing is
528
+ power-cycled on that account.
529
+
530
+ `reset` and `connect --reset` share one sequence, `OPENOCD_RESET`
531
+ (`init`, `targets`, `reset run`): `targets` changes nothing and prints
532
+ each target's state, which a failure's tail then shows. `flash` runs
533
+ `init`, `targets`, `reset init`, `flash write_image erase FIRMWARE 0 TYPE`
534
+ and `reset run`. The operator sees `Flashed` or `Flash failed`, `Reset`
535
+ or `Reset failed`, per board.
536
+
537
+ `--man` reads only the page beside the code (`CLI::MANUAL`), never a
538
+ system-installed one, because an installed page may describe another
539
+ version. A copy holding `lib/` and `exe/` alone fails with a message
540
+ saying to install the gem or deploy the whole checkout, `man/` included.
541
+
496
542
 
497
543
  ## Extension points
498
544
 
@@ -501,7 +547,12 @@ true`; `connect` does not, and checks when it reaches for it under
501
547
  Add the keys to the configuration, or to a `types` block. `interface` and
502
548
  `target` are independent: the first is the openocd interface script
503
549
  (which probe), the second its target script (which chip). Either is
504
- any name openocd can find, without the `.cfg`.
550
+ any name openocd can find, without the `.cfg`. A bare nRF name
551
+ (`TARGET_DIRS`) is looked for under `target/nordic/` first, by a Tcl
552
+ `catch {find ...}` on the command line: openocd after 0.12 keeps the
553
+ nRF scripts there and warns on the flat name, 0.12 has only the flat
554
+ one, and one configuration serves both. A name with a `/` is taken
555
+ as written.
505
556
 
506
557
  | Key | Default | Meaning |
507
558
  | :---------- | :---------- | :-------------------------------------------- |
@@ -693,21 +744,18 @@ is refused rather than ignored.
693
744
 
694
745
  The instance gets `@cli`, and delegates the whole bench vocabulary to it:
695
746
 
696
- | Call | Gives back |
697
- | :------------------------ | :---------------------------------------------- |
698
- | `hub` | the hub object: `Hub::ExSYS` or `Hub::USB` |
699
- | `tty` | the logger (`TTY::Logger`), or `nil` |
700
- | `openocd(*cmds, **hopts)` | `true` on success; a block gets `(ok, output)` |
701
- | `each_device(ids, &b)` | as above; with no block, an Enumerator |
702
- | `port_list(ids)` | names or numbers → port Integers |
703
- | `devices` | every declared board that is on the bench |
704
- | `switchable` | the ports that may be powered down |
705
- | `offable(ports, force:)` | the vetted list, or raises |
706
- | `offable?(port, force:)` | `true` or `false` |
707
- | `tally(id)` | this board's tally name, from the configuration |
708
-
709
- `conf` is delegated too and is vestigial: `@conf` is never assigned, so
710
- it always answers `nil`. Do not build on it.
747
+ | Call | Gives back |
748
+ | :------------------------------- | :--------------------------------------------------------------- |
749
+ | `hub` | the hub object: `Hub::ExSYS` or `Hub::USB` |
750
+ | `tty` | the logger (`TTY::Logger`), or `nil` |
751
+ | `openocd(*cmds, name:, **hopts)` | `true` on success, a failure logged; a block gets `(ok, output)` |
752
+ | `each_device(ids, &b)` | as above; with no block, an Enumerator |
753
+ | `port_list(ids)` | names or numbers → port Integers |
754
+ | `devices` | every declared board that is on the bench |
755
+ | `switchable` | the ports that may be powered down |
756
+ | `offable(ports, force:)` | the vetted list, or raises |
757
+ | `offable?(port, force:)` | `true` or `false` |
758
+ | `tally(id)` | this board's tally name, from the configuration |
711
759
 
712
760
  A command that reports per-device success returns `false` if any device
713
761
  failed, which `CLI.run` turns into exit status 1. `connect` does the
data/README.md CHANGED
@@ -113,7 +113,8 @@ bundle binstubs tribble-control
113
113
  ```
114
114
 
115
115
  The binstub sets that bundle up before loading anything, so it is what
116
- such a host should call.
116
+ such a host should call. Deploy the whole checkout, `man/` included:
117
+ `--man` reads the page beside `lib/` and nowhere else.
117
118
 
118
119
 
119
120
  ## The configuration
@@ -389,6 +390,17 @@ device but those with `port = none`). Note that `serial`
389
390
  prints a probe's identifying number, not console text — reading a
390
391
  board's console is `connect`.
391
392
 
393
+ `flash` takes Intel HEX named `.hex` or ELF named `.elf` and refuses
394
+ anything else (`.bin` included) before any port is switched; openocd is
395
+ told the type, so it never guesses. Each board ends as `Flashed` or
396
+ `Flash failed`, and `reset` says `Reset` or `Reset failed`. A failed
397
+ flash is tried once more, with a warning.
398
+
399
+ A failed openocd run is reported under the board's name with the last 15
400
+ lines of its output; a run that succeeded but printed `Error:` lines is
401
+ reported the same way, as a warning. `--debug` shows openocd's whole
402
+ output for every run.
403
+
392
404
  Naming a board is not the same as telling openocd which one to talk
393
405
  to, and `-m`/`--method` is how that is decided. Each command accepts
394
406
  only the methods that make sense for it and uses the first as its
@@ -180,7 +180,7 @@ class Connect < CLI::Command
180
180
  sleep(1) # let the reader threads settle
181
181
  connected.each do |name, hopts|
182
182
  tty&.info "Resetting #{name}"
183
- unless openocd('init', 'reset run', **hopts)
183
+ unless openocd(*OPENOCD_RESET, name:, **hopts)
184
184
  tty&.error "Device #{name}: Reset failed"
185
185
  end
186
186
  end
@@ -33,10 +33,24 @@ class Flash < CLI::Command
33
33
  'says (power_cycle = after-flash)'
34
34
  end
35
35
 
36
- def flash(firmware, **hopts)
37
- openocd('init', 'targets', 'reset init',
38
- "flash write_image erase #{firmware}",
39
- 'reset run', **hopts)
36
+ # A failed flash is tried once more, whatever the failure: the
37
+ # sequence erases and writes from scratch, so a second run is safe,
38
+ # and a transient fault -- the SWD link dropping mid-erase -- clears
39
+ # on it. A failure that persists (no such probe) costs one more run,
40
+ # and the warning keeps a flaky link from hiding behind the retry.
41
+ # A core in lockup needs nothing of its own: the reset init at the
42
+ # head of the sequence takes it out before anything is written.
43
+ #
44
+ # +type+ is openocd's name for the image type (Firmware.type): given,
45
+ # openocd is told it rather than left to guess from the content.
46
+ def flash(firmware, type: nil, name: nil, **hopts)
47
+ write = "flash write_image erase #{firmware}"
48
+ write += " 0 #{type}" if type
49
+ commands = [ 'init', 'targets', 'reset init', write, 'reset run' ]
50
+ return true if openocd(*commands, name:, **hopts)
51
+
52
+ tty&.warn "Device #{name}: flash failed (above), trying once more"
53
+ openocd(*commands, name:, **hopts)
40
54
  end
41
55
 
42
56
  def run(argv, **opts)
@@ -49,13 +63,18 @@ class Flash < CLI::Command
49
63
  unless File.file?(firmware) && File.readable?(firmware)
50
64
  raise Error, "flash: no readable firmware file '#{firmware}'"
51
65
  end
66
+ begin
67
+ type = Firmware.type(firmware)
68
+ rescue Firmware::Error => e
69
+ raise Error, "flash: #{e.message}"
70
+ end
52
71
  tty&.info "Firmware: #{firmware}"
53
72
 
54
73
  each_device(argv).map do |name, hopts={}|
55
- flash(firmware, **hopts).tap do |ok|
74
+ flash(firmware, type:, name:, **hopts).tap do |ok|
56
75
  if ok
57
76
  then tty&.success "Device #{name}: Flashed"
58
- else tty&.error "Device #{name}: Flashed failed"
77
+ else tty&.error "Device #{name}: Flash failed"
59
78
  end
60
79
  # A DWM1001-DEV comes out of the openocd flash sequence
61
80
  # (reset init, write, reset run) in a state where its DW1000
@@ -19,15 +19,15 @@ class Reset < CLI::Command
19
19
  opts.separator ''
20
20
  end
21
21
 
22
- def reset(**hopts)
23
- openocd('init', 'targets', 'reset run', **hopts)
22
+ def reset(name: nil, **hopts)
23
+ openocd(*OPENOCD_RESET, name:, **hopts)
24
24
  end
25
25
 
26
26
  def run(argv, **_opts)
27
27
  each_device(argv).map do |name, hopts={}|
28
- reset(**hopts).tap do |ok|
28
+ reset(name:, **hopts).tap do |ok|
29
29
  if ok
30
- then tty&.success "Device #{name}: Reseted"
30
+ then tty&.success "Device #{name}: Reset"
31
31
  else tty&.error "Device #{name}: Reset failed"
32
32
  end
33
33
  end
@@ -48,7 +48,7 @@ class CLI
48
48
  class Command
49
49
  extend Forwardable
50
50
 
51
- def_delegators :@cli, :hub, :tty, :conf, :openocd, :each_device,
51
+ def_delegators :@cli, :hub, :tty, :openocd, :each_device,
52
52
  :port_list, :switchable, :offable, :offable?,
53
53
  :devices, :tally, :warn_link_only
54
54
 
@@ -300,9 +300,15 @@ class CLI
300
300
  # Display the manual, paging it when the output is a terminal. A
301
301
  # missing file or a host with no renderer says so, rather than dying
302
302
  # on Errno::ENOENT from somewhere inside the pager.
303
+ #
304
+ # The page is looked for beside the code only: an installed copy
305
+ # elsewhere may describe another version. A copy of lib/ and exe/
306
+ # alone has none, and the error says what to deploy instead.
303
307
  def self.show_manual
304
308
  unless File.readable?(MANUAL)
305
- raise CLI::Error, "no manual found (#{MANUAL})"
309
+ raise CLI::Error, "no manual found (#{MANUAL}): this copy" \
310
+ " lacks man/; install the gem, or deploy the" \
311
+ " whole checkout, man/ included"
306
312
  end
307
313
  unless (text = self.render_manual)
308
314
  raise CLI::Error, 'no manual page renderer found (tried' \
@@ -359,7 +365,6 @@ class CLI
359
365
  # command switches ports through. See Hub.
360
366
  attr_reader :hub
361
367
  attr_reader :tty
362
- attr_reader :conf
363
368
 
364
369
  # The hub's control line, once parse has settled which it is: what
365
370
  # -d named, or what the configuration's 'device' line named, or the one
@@ -844,18 +849,49 @@ class CLI
844
849
  end
845
850
  end
846
851
 
847
- def openocd(*commands, usb: nil, serial: nil,
852
+ # A board reset, wherever one is asked for. 'targets' changes
853
+ # nothing: it prints each target and its state (running, halted),
854
+ # which is what a failed run's tail needs to say what the core was
855
+ # doing.
856
+ OPENOCD_RESET = [ 'init', 'targets', 'reset run' ].freeze
857
+
858
+ # How much of a failed openocd run is shown. The cause is in its
859
+ # last lines -- the Error: that stopped it, and the target state it
860
+ # read just before -- and the whole output is there under --debug.
861
+ OPENOCD_TAIL = 15
862
+
863
+ # What openocd prints on every run, whatever happened: its banner,
864
+ # and the acknowledgement of our own shutdown. Left out of the tail,
865
+ # where it would take the room of the lines that say something;
866
+ # --debug keeps it.
867
+ OPENOCD_NOISE = [
868
+ /\AOpen On-Chip Debugger /,
869
+ /\ALicensed under GNU GPL/,
870
+ /\AFor bug reports, read\z/,
871
+ %r{\A\s*https?://openocd\.org/doc/doxygen/bugs\.html\z},
872
+ /\Ashutdown command invoked\z/
873
+ ].freeze
874
+
875
+ # Run openocd with +commands+ on one board. On failure its output
876
+ # is reported under +name+ (the serial when no name is given):
877
+ # without it, "failed" is all an operator gets, and a by-hand rerun
878
+ # that succeeds cannot say what the failed one hit. A run that
879
+ # exits 0 is not proof either: openocd writes an image it misread
880
+ # and still succeeds, so its Error: lines are reported all the same.
881
+ def openocd(*commands, name: nil, usb: nil, serial: nil,
848
882
  interface: 'cmsis-dap', target: 'nrf52',
849
883
  transport: 'swd', work_area: 0x4000, &block)
850
884
  cmd = [ self.openocd_path ]
885
+ # openocd prints what a command returns: '; list' returns nothing,
886
+ # so the work area and the transport are not echoed into the output.
851
887
  if work_area
852
- cmd += [ '-c', format('set WORKAREASIZE 0x%x', work_area) ]
888
+ cmd += [ '-c', format('set WORKAREASIZE 0x%x; list', work_area) ]
853
889
  end
854
890
  cmd += [ '-c', "source [find interface/#{interface}.cfg]" ]
855
891
  unless transport.nil? || PORT_NONE.include?(transport)
856
- cmd += [ '-c', "transport select #{transport}" ]
892
+ cmd += [ '-c', "transport select #{transport}; list" ]
857
893
  end
858
- cmd += [ '-c', "source [find target/#{target}.cfg]" ]
894
+ cmd += [ '-c', target_source(target) ]
859
895
  cmd += [ '-c', "adapter usb location #{usb}" ] if usb
860
896
  cmd += [ '-c', "adapter serial #{serial}" ] if serial
861
897
  cmd += commands.flat_map {|c| [ '-c', c ] }
@@ -866,10 +902,73 @@ class CLI
866
902
  output, pstatus = Open3.capture2e(*cmd)
867
903
  ok = pstatus.exitstatus.zero?
868
904
 
905
+ openocd_report(name || serial || 'openocd', output, ok)
869
906
  block.call(ok, output) if block
870
907
  ok
871
908
  end
872
909
 
910
+ # openocd after 0.12 keeps the nRF scripts under target/nordic/, and
911
+ # warns that the flat name is deprecated; 0.12 has only the flat one.
912
+ # For a bare name in such a family, the command line asks openocd
913
+ # which it has, so one configuration serves both. A name with a
914
+ # directory in it is taken as written.
915
+ TARGET_DIRS = { /\Anrf/ => 'nordic' }.freeze
916
+
917
+ def target_source(target)
918
+ flat = "target/#{target}.cfg"
919
+ dir = TARGET_DIRS.find {|re, _| target.match?(re) }&.last
920
+ return "source [find #{flat}]" if dir.nil? || target.include?('/')
921
+
922
+ found = "find target/#{dir}/#{target}.cfg"
923
+ "if {[catch {#{found}} f]} {source [find #{flat}]} else {source $f}"
924
+ end
925
+
926
+ # One log line per openocd line, each carrying the board: boards run
927
+ # in parallel threads, and their lines interleave. The whole output
928
+ # goes to --debug, whatever the outcome; the tail goes to the error
929
+ # level on failure, to the warning level on a success that said
930
+ # Error:, and nowhere on a clean success.
931
+ def openocd_report(label, output, ok)
932
+ lines = output.to_s.lines.map(&:chomp).reject {|l| l.strip.empty? }
933
+ lines.each {|l| @tty&.debug "Device #{label}: openocd: #{l}" }
934
+ lines = lines.reject {|l| OPENOCD_NOISE.any? {|re| l.match?(re) } }
935
+
936
+ if ok
937
+ return unless lines.any? {|l| l.start_with?('Error:') }
938
+
939
+ openocd_tail(label, lines, :warn, 'openocd succeeded, but reported errors')
940
+ elsif lines.empty?
941
+ @tty&.error "Device #{label}: openocd failed without saying why"
942
+ return
943
+ else
944
+ openocd_tail(label, lines, :error, 'openocd said')
945
+ end
946
+
947
+ if lines.any? {|l| l.match?(LOCKUP) }
948
+ @tty&.warn "Device #{label}: the core was found in lockup" \
949
+ ' or a hard fault: the firmware on it faults'
950
+ end
951
+ end
952
+
953
+ # The last lines of +lines+ at +level+, under a header saying how many.
954
+ def openocd_tail(label, lines, level, said)
955
+ shown = [ lines.size, OPENOCD_TAIL ].min
956
+ count = shown == 1 ? 'line' : "#{shown} lines"
957
+ @tty&.public_send(level, "Device #{label}: #{said} (last #{count}):")
958
+ lines.last(OPENOCD_TAIL).each do |l|
959
+ @tty&.public_send(level, "Device #{label}: #{l}")
960
+ end
961
+ end
962
+
963
+ # A core in lockup or a hard fault, as openocd shows it: the pc a
964
+ # Cortex-M reports in lockup ("pc: 0xfffffffe"), or openocd's own
965
+ # words for it ("clearing lockup after double fault", "Handler
966
+ # HardFault"). The words matter: a vector table of garbage locks
967
+ # the core at whatever pc the garbage names. Said by name because
968
+ # it is easy to read past in the tail. It is a fact about the
969
+ # board, not a verdict on the failure: a flash can succeed from it.
970
+ LOCKUP = /\bpc:?\s*0xfffffffe\b|\blockup\b|\bHardFault\b/i
971
+
873
972
  # Parse +argv+: global options, -r files, the command and its
874
973
  # options, the configuration, then the hub. Returns self.
875
974
  def parse(argv)
@@ -0,0 +1,88 @@
1
+ #
2
+ # What a firmware file is, checked before any port is switched.
3
+ #
4
+ # openocd takes the image type from the file's content, and whatever it
5
+ # cannot parse it writes as raw bytes from address 0: a text file named
6
+ # .hex replaces the vector table, faults the core, and openocd still
7
+ # exits 0. So the type is the extension's, the content must match it,
8
+ # and openocd is then told the type rather than left to guess.
9
+ #
10
+
11
+ module TribbleControl
12
+
13
+ module Firmware
14
+ # A firmware the extension, or the content, rules out.
15
+ class Error < StandardError; end
16
+
17
+ # Extension => openocd's name for the type (flash write_image).
18
+ TYPES = { '.hex' => 'ihex', '.elf' => 'elf' }.freeze
19
+
20
+ ELF_MAGIC = "\x7FELF".b.freeze
21
+
22
+ # An Intel HEX record: a colon, then the count, address, type, data
23
+ # and checksum bytes, in hex.
24
+ IHEX_RECORD = /\A:((?:\h\h){5,})\z/
25
+
26
+ # Record types 00-05: data, end of file, and the four address ones.
27
+ IHEX_TYPES = 0..5
28
+ IHEX_EOF = 1
29
+
30
+ # openocd's type for the firmware at +path+, or Error saying why it
31
+ # is not one.
32
+ def self.type(path)
33
+ type = TYPES[File.extname(path).downcase]
34
+ unless type
35
+ raise Error, "'#{path}': not a .hex or .elf file (openocd" \
36
+ ' would write anything else as raw bytes)'
37
+ end
38
+ case type
39
+ when 'ihex' then check_ihex(path)
40
+ when 'elf' then check_elf(path)
41
+ end
42
+ type
43
+ end
44
+
45
+ def self.check_elf(path)
46
+ return if File.binread(path, 4) == ELF_MAGIC
47
+
48
+ raise Error, "'#{path}': not an ELF file"
49
+ end
50
+
51
+ # Every record well formed and summing to zero, and the last one the
52
+ # end-of-file record: a file cut short is refused like a wrong one.
53
+ def self.check_ihex(path)
54
+ last = nil
55
+ File.foreach(path, mode: 'rb').with_index(1) do |line, n|
56
+ line = line.chomp
57
+ next if line.empty?
58
+
59
+ where = "'#{path}', line #{n}"
60
+ raise Error, "#{where}: after the end-of-file record" \
61
+ if last == IHEX_EOF
62
+ bytes = ihex_bytes(line, where)
63
+ last = bytes[3]
64
+ end
65
+ return if last == IHEX_EOF
66
+
67
+ raise Error, "'#{path}': not Intel HEX, or cut short (no" \
68
+ ' end-of-file record)'
69
+ end
70
+
71
+ def self.ihex_bytes(line, where)
72
+ m = IHEX_RECORD.match(line)
73
+ raise Error, "#{where}: not an Intel HEX record" unless m
74
+
75
+ bytes = [ m[1] ].pack('H*').bytes
76
+ raise Error, "#{where}: wrong length" \
77
+ unless bytes.size == bytes[0] + 5
78
+ raise Error, "#{where}: bad checksum" \
79
+ unless (bytes.sum % 256).zero?
80
+ raise Error, "#{where}: unknown record type" \
81
+ unless IHEX_TYPES.cover?(bytes[3])
82
+
83
+ bytes
84
+ end
85
+ private_class_method :check_elf, :check_ihex, :ihex_bytes
86
+ end
87
+
88
+ end
@@ -3,6 +3,6 @@ module TribbleControl
3
3
  # Bumped by hand. The version lives here alone: the gemspec reads it
4
4
  # from this file, and `tribble-control --version` prints the same
5
5
  # constant, so a release cannot have two numbers.
6
- VERSION = '0.4.4'.freeze
6
+ VERSION = '0.5.0'.freeze
7
7
 
8
8
  end
@@ -17,6 +17,7 @@
17
17
  #
18
18
  require_relative 'tribble-control/version'
19
19
  require_relative 'tribble-control/platform'
20
+ require_relative 'tribble-control/firmware'
20
21
  require_relative 'tribble-control/hub'
21
22
  require_relative 'tribble-control/hub/exsys'
22
23
  require_relative 'tribble-control/cli'
@@ -395,6 +395,14 @@ it.
395
395
  .It Fl -debug Ns Oo = Ns Ar FILE Oc
396
396
  Show debug output \(em chiefly the openocd command line issued for each
397
397
  board, which is the thing worth having when a flash behaves oddly.
398
+ A failed openocd run is always reported with the last 15 lines of its
399
+ output, under the board's name; a run that succeeds but printed Error:
400
+ lines is reported the same way, as a warning.
401
+ When that output shows the core in lockup or a hard fault (pc 0xfffffffe,
402
+ or openocd saying lockup or HardFault), a further warning says so: the
403
+ firmware on the board faults.
404
+ .Fl -debug
405
+ shows all the output of every run.
398
406
  With
399
407
  .Ar FILE ,
400
408
  the whole log is copied there as well as shown, appending, so a long
@@ -406,6 +414,12 @@ Run verbosely.
406
414
  Print the tribble\-control and ExSYS library versions and exit.
407
415
  .It Fl h , Fl -help
408
416
  Usage, including the command list.
417
+ .It Fl -man
418
+ Render this manual with the first of mandoc, groff or nroff that is
419
+ installed, paged when the output is a terminal.
420
+ The page is the one beside the code, never one installed elsewhere, which
421
+ may describe another version: a copy of lib/ and exe/ alone has none and
422
+ says so, so install the gem or deploy the whole checkout, man/ included.
409
423
  .El
410
424
  .Pp
411
425
  Per\-command: 'usb' takes \-\-default=BOOL, 'connect' takes \-\-off.
@@ -578,6 +592,9 @@ The openocd target script, without the .cfg:
578
592
  .Cm nrf52
579
593
  (the default, which is what the whole bench runs) or whatever openocd
580
594
  calls the chip on a board of another family.
595
+ An nRF name with no directory in it is looked for under target/nordic/
596
+ first, where openocd after 0.12 keeps it, then under target/, where
597
+ 0.12 does; a name such as nordic/nrf52 is taken as written.
581
598
  Used wherever interface= is.
582
599
  The two are independent: the probe says how to reach the chip, the
583
600
  target says which chip it is.
@@ -990,14 +1007,24 @@ the OS also means no board has to be the only one powered, which
990
1007
  asking openocd would have required, since it cannot choose between
991
1008
  adapters by itself.
992
1009
  .It Ic flash Ar FIRMWARE Oo Ar PORT | DEVNAME Oc ...
993
- openocd 'flash write_image erase FIRMWARE' then 'reset run', against
1010
+ openocd 'init', 'targets', 'reset init',
1011
+ \&'flash write_image erase FIRMWARE 0 TYPE' (TYPE ihex or elf, below),
1012
+ then 'reset run', against
994
1013
  the board's target= script (nrf52 by default) over its interface=
995
1014
  script (cmsis\-dap by default), on its transport= (swd by default).
996
1015
  FIRMWARE is resolved by
997
1016
  openocd relative to the current directory, and is checked to be a
998
1017
  readable file before any port is switched.
1018
+ It must be Intel HEX named .hex, every record well formed and the last
1019
+ one the end of file, or ELF named .elf; anything else is refused there
1020
+ too, and openocd is told the type.
1021
+ openocd would otherwise write what it cannot parse as raw bytes from
1022
+ address 0, and report success.
999
1023
  Every selected board is attempted even if an earlier one failed; the
1000
1024
  exit status reflects the whole run.
1025
+ A board whose flash fails is flashed once more, whatever the failure,
1026
+ with a warning; it fails only if the second attempt fails too.
1027
+ Each board ends as 'Flashed' or 'Flash failed'.
1001
1028
  A board whose configuration entry lists after\-flash under power_cycle (see
1002
1029
  CONFIGURATION: the DWM1001\-DEV) has its port powered off and on again after
1003
1030
  a successful flash; \-\-power\-cycle, before FIRMWARE, does the same for
@@ -1007,7 +1034,9 @@ Selection methods: serial (default), power.
1007
1034
  Exit status: 1 if any
1008
1035
  board failed.
1009
1036
  .It Ic reset Oo Ar PORT | DEVNAME Oc ...
1010
- openocd 'reset run'.
1037
+ openocd 'init', 'targets', 'reset run' \(em the sequence
1038
+ .Ic connect --reset
1039
+ also uses; each board ends as 'Reset' or 'Reset failed'.
1011
1040
  .Pp
1012
1041
  Selection method: serial only, so it cannot address a board that has
1013
1042
  no serial= in the configuration.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tribble-control
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephane D'Alu
@@ -157,6 +157,7 @@ files:
157
157
  - lib/tribble-control/cli/reset.rb
158
158
  - lib/tribble-control/cli/serial.rb
159
159
  - lib/tribble-control/cli/usb.rb
160
+ - lib/tribble-control/firmware.rb
160
161
  - lib/tribble-control/hub.rb
161
162
  - lib/tribble-control/hub/exsys.rb
162
163
  - lib/tribble-control/hub/usb.rb
@@ -186,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  - !ruby/object:Gem::Version
187
188
  version: '0'
188
189
  requirements: []
189
- rubygems_version: 4.0.16
190
+ rubygems_version: 4.0.21
190
191
  specification_version: 4
191
192
  summary: Power, flash and monitor the boards plugged into a switchable USB hub
192
193
  test_files: []