Class: Jtagulator::API::Client
- Inherits:
-
Object
- Object
- Jtagulator::API::Client
- Defined in:
- lib/jtagulator/client.rb
Constant Summary collapse
- PARITY_OPTIONS =
{ "none" => SerialPort::NONE, "even" => SerialPort::EVEN, "odd" => SerialPort::ODD, "mark" => SerialPort::MARK, "space" => SerialPort::SPACE }.freeze
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #collect_results_until_complete ⇒ Object
- #go_to_main_menu ⇒ Object
-
#initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false) ⇒ Client
constructor
A new instance of Client.
- #log(message, error: false) ⇒ Object
- #safe_read(wait_limit: 3) ⇒ Object
- #send_key(key) ⇒ Object
- #set_mode(mode) ⇒ Object
- #start_session ⇒ Object
Constructor Details
#initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 |
# File 'lib/jtagulator/client.rb', line 14 def initialize(port: "/dev/ttyUSB0", baud: 115_200, data_bits: 8, stop_bits: 1, parity: "none", debug: false) log("Initializing UART") parity_sym = PARITY_OPTIONS[parity] || SerialPort::NONE @port = SerialPort.new(port, baud, data_bits, stop_bits, parity_sym) @debug = debug end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/jtagulator/client.rb', line 4 def port @port end |
Instance Method Details
#collect_results_until_complete ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/jtagulator/client.rb', line 127 def collect_results_until_complete results = [] until (response = safe_read).include?("complete") result = response.strip results << result unless result == "-" || result.empty? log(response) end results end |
#go_to_main_menu ⇒ Object
85 86 87 88 89 90 |
# File 'lib/jtagulator/client.rb', line 85 def 3.times do send_key("m") sleep 0.25 end end |
#log(message, error: false) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/jtagulator/client.rb', line 103 def log(, error: false) = Time.now.strftime("[%H:%M:%S]") if @debug if error print puts "[ERROR] " + else if .include? "\r\r\n" prev_input = .split("\r\r\n")[0] print puts "[INPUT] " + prev_input msgs = .split("\r\r\n")[1].split("\r\n") msgs.each do |msg| print puts "[OUTPUT] " + msg.gsub("\r", "").gsub("\n", "") end else print puts "[LOG] " + end end end end |
#safe_read(wait_limit: 3) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jtagulator/client.rb', line 35 def safe_read(wait_limit: 3) reading = false wait_time = 0 begin @port.read_nonblock(1024) rescue IO::WaitReadable log("Waiting for response from board...") unless reading sleep 0.1 reading = true wait_time += 0.1 if wait_time > wait_limit log("No response after #{wait_limit} seconds", error: true) return "NO_RESPONSE_LIMIT_REACHED" end retry end end |
#send_key(key) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jtagulator/client.rb', line 92 def send_key(key) @port.write("#{key}\r") res = safe_read log(res) if res == ("?\r\r\n") || res.include?("Value out of range!") return false else return true end end |
#set_mode(mode) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jtagulator/client.rb', line 53 def set_mode(mode) log("Attempting to go to main menu") log("Setting device mode") map = { "j" => "JTAG", "u" => "UART", "g" => "GPIO", "s" => "SWD" } begin @port.write(mode + "\r") sleep 1 rescue Errno::EIO sleep 0.1 retry end begin res = @port.read_nonblock(1024) # attempt to read up to 1024 bytes non-blocking res = res.inspect.to_s.gsub('\r', "\r").gsub('\n', "\n") rescue IO::WaitReadable sleep 0.1 retry end unless res.include?(map[mode]) log(res, error: true) send_key("m") set_mode(mode) end end |
#start_session ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jtagulator/client.rb', line 21 def start_session log("Starting session") @port.flush_input @port.write(0x68) @port.read_timeout = 2000 # timeout in milliseconds res = safe_read if res == "NO_RESPONSE_LIMIT_REACHED" || res.inspect.to_s.length < 5 log("Failed to start session, attempting to restart", error: true) @port.write("\x04") # ctrl+d sleep 1 start_session end end |