jtagulator 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c34672f543ed66708e247abe5b3d8063c25c4c9b61b036cda1049832c2b4eb26
4
+ data.tar.gz: 1e54c6d0485918bbb0b38e5b6e3f4f5d8a506e71c7edc0fcbe537bd80a9cefdd
5
+ SHA512:
6
+ metadata.gz: 929a1a97190d0f9e261af7b59832cdb83baf4ed6c1cf2cb89a6a6d20ad3a94b29622f3366cbf465d1aaafbf7fd1938516dc440d45171c9b3bfd6f56bcc0ed2f4
7
+ data.tar.gz: c9a0281e8a02c2a194ce43077183fafc2e1d43b75573d71b60ce9550d11257310d30ce528f5d697e1dbb2ce07c4c9a51680094a18b96aab0c2337e86f6943d48
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at drewlongdesign@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jtagulator.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 0.80"
11
+
12
+ gem "serialport", "~> 1.3"
13
+
14
+ gem "json", "~> 2.6"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jtagulator (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ colorize (1.1.0)
11
+ json (2.6.3)
12
+ parallel (1.23.0)
13
+ parser (3.2.2.4)
14
+ ast (~> 2.4.1)
15
+ racc
16
+ racc (1.7.3)
17
+ rainbow (3.1.1)
18
+ rake (13.1.0)
19
+ regexp_parser (2.8.2)
20
+ rexml (3.2.6)
21
+ rubocop (0.93.1)
22
+ parallel (~> 1.10)
23
+ parser (>= 2.7.1.5)
24
+ rainbow (>= 2.2.2, < 4.0)
25
+ regexp_parser (>= 1.8)
26
+ rexml
27
+ rubocop-ast (>= 0.6.0)
28
+ ruby-progressbar (~> 1.7)
29
+ unicode-display_width (>= 1.4.0, < 2.0)
30
+ rubocop-ast (1.30.0)
31
+ parser (>= 3.2.1.0)
32
+ ruby-progressbar (1.13.0)
33
+ serialport (1.3.2)
34
+ unicode-display_width (1.8.0)
35
+
36
+ PLATFORMS
37
+ x86_64-linux
38
+
39
+ DEPENDENCIES
40
+ colorize (~> 1.1)
41
+ json (~> 2.6)
42
+ jtagulator!
43
+ rake (~> 13.0)
44
+ rubocop (~> 0.80)
45
+ serialport (~> 1.3)
46
+
47
+ BUNDLED WITH
48
+ 2.2.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 datagoboom
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Ruby JTAGulator
2
+
3
+ This is a simple ruby wrapper around the [JTAGulator](https://www.crowdsupply.com/grand-idea-studio/jtagulator) from [Grand Idea Studio](https://www.grandideastudio.com/). It is intended to be used as a library for automating hardware assessment using the JTAGulator. Currently, it only supports the following features:
4
+
5
+ - UART
6
+ - Identify UART Pinout
7
+ - UART Passthrough
8
+ - JTAG
9
+ - Identify JTAG Pinout (Default Mode)
10
+ - Identify JTAG pinout (IDCODE Scan)
11
+ - Identify JTAG pinout (BYPASS Scan)
12
+ - Identify RTCK (adaptive clocking)
13
+
14
+ More to come.
15
+
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'jtagulator'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle install
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install jtagulator
32
+
33
+ ## Usage
34
+
35
+ Check out the examples folder for basic usage.
36
+
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/datagoboom/jtagulator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jtagulator/blob/master/CODE_OF_CONDUCT.md).
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+
46
+ ## Code of Conduct
47
+
48
+ Everyone interacting in the Jtagulator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/jtagulator/blob/master/CODE_OF_CONDUCT.md).
49
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "jtagulator"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ require_relative "../lib/jtagulator"
2
+
3
+ def continuous_identify
4
+ jtag = Jtagulator::API::JTAG.new(port: "/dev/ttyUSB0", debug: true)
5
+ jtag.start_session
6
+ results = []
7
+ ["DEFAULT", "IDCODE", "BYPASS", "RTCK"].each do |mode|
8
+ puts jtag.identify(
9
+ id_mode: mode, # Optional, defaults to "DEFAULT" (j option)
10
+ wait_limit: 5, # Optional, defaults to 120
11
+ options: {
12
+ start_pin: 0,
13
+ end_pin: 9,
14
+ bring_low: true,
15
+ low_time: 100,
16
+ high_time: 100,
17
+
18
+ # You can optionally provide known pins for the BYPASS and RTCK modes
19
+ # For RTCK, only provide the tck pin (others will be ignored)
20
+
21
+ known_pins: {
22
+ # tdi: 0,
23
+ # tdo: 1,
24
+ tck: 2,
25
+ # tms: 3
26
+ }
27
+
28
+ }
29
+ )
30
+ end
31
+ end
32
+
33
+ continuous_identify
@@ -0,0 +1,32 @@
1
+ require "colorize"
2
+ require "jtagulator"
3
+
4
+ def continuous_identify
5
+ uart = Jtagulator::API::UART.new(port: "/dev/ttyUSB0", debug: true)
6
+ uart.start_session
7
+ results = []
8
+ while true
9
+ puts uart.identify(
10
+ wait_limit: 30,
11
+ options: {
12
+ bring_low: true,
13
+ low_time: 100,
14
+ high_time: 100,
15
+ start_pin: 0,
16
+ end_pin: 1,
17
+ output_str: "\\x0D"
18
+ }
19
+ ).to_s.green
20
+ end
21
+ end
22
+
23
+ def passthrough_test
24
+ uart = Jtagulator::API::UART.new(port: "/dev/ttyUSB0", debug: true)
25
+ uart.start_session
26
+ uart.start_passthrough
27
+ res = uart.send_command("1\n")
28
+ puts res
29
+ uart.stop_passthrough
30
+ end
31
+
32
+ continuous_identify
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jtagulator/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jtagulator"
7
+ spec.version = Jtagulator::VERSION
8
+ spec.authors = ["datagoboom"]
9
+ spec.email = ["andrew@datagoboom.com"]
10
+
11
+ spec.summary = "Simple library for interacting with the Jtagulator hardware"
12
+ spec.description = "Ruby library for interacting with the Jtagulator hardware, including functionality for JTAG, UART, GPIO, and SWD"
13
+ spec.homepage = "https://github.com/datagoboom/jtagulator"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,138 @@
1
+ module Jtagulator
2
+ module API
3
+ class Client
4
+ attr_reader :port
5
+
6
+ PARITY_OPTIONS = {
7
+ "none" => SerialPort::NONE,
8
+ "even" => SerialPort::EVEN,
9
+ "odd" => SerialPort::ODD,
10
+ "mark" => SerialPort::MARK,
11
+ "space" => SerialPort::SPACE
12
+ }.freeze
13
+
14
+ def initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false)
15
+ log("Initializing UART")
16
+ parity_sym = PARITY_OPTIONS[parity] || SerialPort::NONE
17
+ @port = SerialPort.new(port, baud, data_bits, stop_bits, parity_sym)
18
+ @debug = debug
19
+ end
20
+
21
+ def start_session
22
+ log("Starting session")
23
+ @port.flush_input
24
+ @port.write(0x68)
25
+ @port.read_timeout = 2000 # timeout in milliseconds
26
+ res = safe_read
27
+ if res == "NO_RESPONSE_LIMIT_REACHED" || res.inspect.to_s.length < 5
28
+ log("Failed to start session, attempting to restart", error: true)
29
+ @port.write("\x04") # ctrl+d
30
+ sleep 1
31
+ start_session
32
+ end
33
+ end
34
+
35
+ def safe_read(wait_limit: 3)
36
+ reading = false
37
+ wait_time = 0
38
+ begin
39
+ @port.read_nonblock(1024)
40
+ rescue IO::WaitReadable
41
+ log("Waiting for response from board...") unless reading
42
+ sleep 0.1
43
+ reading = true
44
+ wait_time += 0.1
45
+ if wait_time > wait_limit
46
+ log("No response after #{wait_limit} seconds", error: true)
47
+ return "NO_RESPONSE_LIMIT_REACHED"
48
+ end
49
+ retry
50
+ end
51
+ end
52
+
53
+ def set_mode(mode)
54
+ log("Attempting to go to main menu")
55
+ go_to_main_menu
56
+ log("Setting device mode")
57
+ map = {
58
+ "j" => "JTAG",
59
+ "u" => "UART",
60
+ "g" => "GPIO",
61
+ "s" => "SWD"
62
+ }
63
+ begin
64
+ @port.write(mode + "\r")
65
+ sleep 1
66
+ rescue Errno::EIO
67
+ sleep 0.1
68
+ retry
69
+ end
70
+ begin
71
+ res = @port.read_nonblock(1024) # attempt to read up to 1024 bytes non-blocking
72
+ res = res.inspect.to_s.gsub('\r', "\r").gsub('\n', "\n")
73
+ rescue IO::WaitReadable
74
+ sleep 0.1
75
+ retry
76
+ end
77
+
78
+ unless res.include?(map[mode])
79
+ log(res, error: true)
80
+ send_key("m")
81
+ set_mode(mode)
82
+ end
83
+ end
84
+
85
+ def go_to_main_menu
86
+ 3.times do
87
+ send_key("m")
88
+ sleep 0.25
89
+ end
90
+ end
91
+
92
+ def send_key(key)
93
+ @port.write("#{key}\r")
94
+ res = safe_read
95
+ log(res)
96
+ if res == ("?\r\r\n") || res.include?("Value out of range!")
97
+ return false
98
+ else
99
+ return true
100
+ end
101
+ end
102
+
103
+ def log(message, error: false)
104
+ timestamp = Time.now.strftime("[%H:%M:%S]")
105
+ if @debug
106
+ if error
107
+ print timestamp
108
+ puts "[ERROR] " + message
109
+ else
110
+ if message.include? "\r\r\n"
111
+ prev_input = message.split("\r\r\n")[0]
112
+ print timestamp
113
+ puts "[INPUT] " + prev_input
114
+ msgs = message.split("\r\r\n")[1].split("\r\n")
115
+ msgs.each do |msg|
116
+ print timestamp
117
+ puts "[OUTPUT] " + msg.gsub("\r", "").gsub("\n", "")
118
+ end
119
+ else
120
+ print timestamp
121
+ puts "[LOG] " + message
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ def collect_results_until_complete
128
+ results = []
129
+ until (response = safe_read).include?("complete")
130
+ result = response.strip
131
+ results << result unless result == "-" || result.empty?
132
+ log(response)
133
+ end
134
+ results
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jtagulator
4
+ module API
5
+ class JTAG
6
+ def initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false)
7
+ @client = Jtagulator::API::Client.new(
8
+ port: port,
9
+ baud: baud,
10
+ data_bits: data_bits,
11
+ stop_bits: stop_bits,
12
+ parity: parity,
13
+ debug: debug
14
+ )
15
+ @port = @client.port
16
+ end
17
+
18
+ def start_session
19
+ @client.start_session
20
+ end
21
+
22
+ def identify(id_mode: "DEFAULT", wait_limit: 120, options: {})
23
+ @client.set_mode("j")
24
+ @client.log("Identifying JTAG pins, mode: #{id_mode}")
25
+ configure_identify(id_mode, wait_limit, options)
26
+ response = @client.safe_read(wait_limit: wait_limit)
27
+
28
+ results = @client.collect_results_until_complete
29
+ end
30
+
31
+ def configure_identify(id_mode, wait_limit, options)
32
+ default_options = {
33
+ voltage: 1.5,
34
+ start_pin: 0,
35
+ end_pin: 6,
36
+ bring_low: false,
37
+ low_time: 100,
38
+ high_time: 100
39
+ }
40
+
41
+ options = default_options.merge(options)
42
+
43
+ modes = {
44
+ "DEFAULT" => "j",
45
+ "IDCODE" => "i",
46
+ "BYPASS" => "b",
47
+ "RTCK" => "r"
48
+ }
49
+
50
+ keys = []
51
+
52
+ base_keys = [
53
+ "v",
54
+ options[:voltage].to_s,
55
+ modes[id_mode],
56
+ options[:start_pin].to_s,
57
+ options[:end_pin].to_s
58
+ ]
59
+
60
+ timing_keys = [
61
+ options[:low_time].to_s,
62
+ options[:high_time].to_s
63
+ ]
64
+
65
+ if id_mode == "BYPASS"
66
+ known = false
67
+ known_pins = options[:known_pins]
68
+ if known_pins
69
+ known_pins.each do |k, v|
70
+ if v.to_i > 0 && v.to_i < 23
71
+ known = true
72
+ end
73
+ end
74
+ if known
75
+ # Say yes to "Are any pins already known?"
76
+ base_keys.push("y")
77
+
78
+ # Pins that are unknown will be set to x
79
+ pins = {
80
+ tdi: "x",
81
+ tdo: "x",
82
+ tck: "x",
83
+ tms: "x",
84
+ }
85
+
86
+ # Adding known pins to pin list
87
+ pins.merge!(options[:known_pins]) if options[:known_pins]
88
+
89
+ pins.each do |k, v|
90
+ # Add pin definitions as answers
91
+ base_keys.push(v)
92
+ end
93
+ end
94
+ else
95
+ # Say no to "Are any pins already known?"
96
+ base_keys.push("n")
97
+ end
98
+
99
+ elsif id_mode == "RTCK"
100
+ known = false
101
+ known_pins = options[:known_pins]
102
+ if known_pins
103
+ known = true if known_pins[:tck].to_i > 0 && known_pins[:tck].to_i < 23
104
+ if known
105
+ # Say yes to "Is TCK already known?"
106
+ base_keys.push("y")
107
+ # Add pin definition as answer
108
+ base_keys.push(known_pins[:tck].to_s)
109
+ end
110
+ end
111
+ end
112
+
113
+ if options[:bring_low]
114
+ base_keys.push("y")
115
+ keys = base_keys + timing_keys
116
+ else
117
+ base_keys.push("n")
118
+ keys = base_keys
119
+ end
120
+
121
+ keys.each do |key|
122
+ @client.send_key(key)
123
+ end
124
+ @port.write(" ")
125
+ end
126
+ end
127
+ end
128
+ end
129
+
@@ -0,0 +1,175 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jtagulator
4
+ module API
5
+ class UART
6
+ def initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false)
7
+ @client = Jtagulator::API::Client.new(
8
+ port: port,
9
+ baud: baud,
10
+ data_bits: data_bits,
11
+ stop_bits: stop_bits,
12
+ parity: parity,
13
+ debug: debug
14
+ )
15
+ @port = @client.port
16
+ end
17
+
18
+ def start_session
19
+ @client.start_session
20
+ end
21
+
22
+ def identify(wait_limit: 120, options: {})
23
+ @client.set_mode("u")
24
+ @client.log("Identifying UART pins")
25
+ configure_identify(options)
26
+
27
+ response = @client.safe_read(wait_limit: wait_limit)
28
+
29
+ results = @client.collect_results_until_complete
30
+ parsed_results = parse_results(results)
31
+
32
+ @client.log("UART pin identification complete")
33
+ @client.log("No results, is the target connected?", error: true) if results == []
34
+
35
+ parsed_results
36
+ end
37
+
38
+ def start_passthrough(options: {})
39
+ @client.set_mode("u")
40
+ @client.log("Starting UART passthrough")
41
+ configure_passthrough(options)
42
+ end
43
+
44
+ def send_command(command)
45
+ @client.send_key(command)
46
+ response = @client.safe_read
47
+ return "No response, timeout reached" if response == "NO_RESPONSE_LIMIT_REACHED"
48
+ end
49
+
50
+ def stop_passthrough
51
+ @client.log("Stopping UART passthrough")
52
+ @port.write("\x18") # ctrl+x
53
+ end
54
+
55
+ private
56
+
57
+ def configure_identify(options)
58
+ default_options = {
59
+ voltage: 1.5,
60
+ start_pin: 0,
61
+ end_pin: 1,
62
+ known_pins: false,
63
+ output_str: "\\x0D",
64
+ delay: 10,
65
+ ignore_non_printable: false,
66
+ bring_low: false,
67
+ low_time: 100,
68
+ high_time: 100
69
+ }
70
+
71
+ orig_opts = options
72
+ options = default_options.merge(options)
73
+
74
+ keys = [
75
+ "v",
76
+ options[:voltage].to_s,
77
+ "u",
78
+ options[:start_pin].to_s,
79
+ options[:end_pin].to_s, "n",
80
+ options[:output_str],
81
+ options[:delay].to_s,
82
+ options[:ignore_non_printable] ? "y" : "n",
83
+ options[:bring_low] ? "y" : "n"
84
+ ]
85
+
86
+ keys.push(options[:low_time].to_s, options[:high_time].to_s) if options[:bring_low]
87
+
88
+ recovered = false
89
+
90
+ keys.each do |key|
91
+ success = @client.send_key(key)
92
+ unless success
93
+ @client.log("Invalid output, restarting", error: true)
94
+ configure_identify(orig_opts)
95
+ recovered = true
96
+ break
97
+ end
98
+ end
99
+ @port.write(" ") unless recovered
100
+ end
101
+
102
+ def configure_passthrough(options)
103
+ default_options = {
104
+ voltage: 1.5,
105
+ baud: 115_200,
106
+ tx_pin: 0,
107
+ rx_pin: 1,
108
+ local_echo: false
109
+ }
110
+
111
+ options = default_options.merge(options)
112
+
113
+ keys = [
114
+ "v",
115
+ options[:voltage].to_s,
116
+ "p",
117
+ options[:tx_pin].to_s,
118
+ options[:rx_pin].to_s,
119
+ options[:baud].to_s,
120
+ options[:local_echo] ? "y" : "n"
121
+ ]
122
+
123
+ keys.each do |key|
124
+ @client.send_key(key)
125
+ end
126
+ end
127
+
128
+ def parse_results(results)
129
+ parsed_data = []
130
+ current_data = {}
131
+
132
+ results.each do |result|
133
+ result.split("\r\n").each do |line|
134
+ if txd_match = line.match(/TXD: (\d+)/)
135
+ current_data[:txd] = txd_match[1]
136
+ end
137
+
138
+ if rxd_match = line.match(/RXD: (\d+)/)
139
+ current_data[:rxd] = rxd_match[1]
140
+ end
141
+
142
+ if baud_match = line.match(/Baud: (\d+)/)
143
+ if current_data.key?(:baud)
144
+ parsed_data.push(current_data)
145
+ current_data = { txd: current_data[:txd], rxd: current_data[:rxd], baud: baud_match[1] }
146
+ else
147
+ current_data[:baud] = baud_match[1]
148
+ end
149
+ end
150
+
151
+ next unless data_match = line.match(/\[ ([A-F0-9\s]+) \]/)
152
+
153
+ data_obj = {
154
+ raw: data_match[1].split(" ").map { |x| x.to_i(16) },
155
+ ascii: data_match[1].split(" ").map { |x| x.to_i(16) }.map { |x| hex_to_ascii(x) }.join
156
+ }
157
+ current_data[:data] = data_obj
158
+ end
159
+ end
160
+
161
+ parsed_data.push(current_data) if current_data.key?(:baud)
162
+
163
+ parsed_data
164
+ end
165
+
166
+ def hex_to_ascii(hex)
167
+ if hex >= 32 && hex <= 126
168
+ hex.chr
169
+ else
170
+ "."
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jtagulator
4
+ VERSION = "0.1.0"
5
+ end
data/lib/jtagulator.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "serialport"
4
+ require "colorize"
5
+ require "json"
6
+ require "jtagulator/client"
7
+ require "jtagulator/jtag"
8
+ require "jtagulator/uart"
9
+ require "jtagulator/version"
10
+
11
+ module Jtagulator
12
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jtagulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - datagoboom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-11-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby library for interacting with the Jtagulator hardware, including
14
+ functionality for JTAG, UART, GPIO, and SWD
15
+ email:
16
+ - andrew@datagoboom.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - examples/jtag_examples.rb
31
+ - examples/uart_examples.rb
32
+ - jtagulator.gemspec
33
+ - lib/jtagulator.rb
34
+ - lib/jtagulator/client.rb
35
+ - lib/jtagulator/jtag.rb
36
+ - lib/jtagulator/uart.rb
37
+ - lib/jtagulator/version.rb
38
+ homepage: https://github.com/datagoboom/jtagulator
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://github.com/datagoboom/jtagulator
43
+ source_code_uri: https://github.com/datagoboom/jtagulator
44
+ changelog_uri: https://github.com/datagoboom/jtagulator/blob/main/CHANGELOG.md
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.2.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Simple library for interacting with the Jtagulator hardware
64
+ test_files: []