intel_hex 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/intel_hex_reader +13 -15
- data/lib/intel_hex.rb +3 -1
- data/lib/intel_hex/file_reader.rb +6 -4
- data/lib/intel_hex/record.rb +74 -54
- data/lib/intel_hex/version.rb +3 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d15e5760f2d577b90bcb4265ce05878a505aa0ac0cc613d671324867a821ace
|
4
|
+
data.tar.gz: cc2dea512304abc088412f5c3e3338fec83aaf3208bec0d4a31bfa51b305864f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c39cddd4c1166c65d70b29fee0bb65ed0f6cb0a702d176c80bac7f3377925966cfeb73a1766beeb8dd05c4c4e0436e377e02d826031654ad88305108fce67b9
|
7
|
+
data.tar.gz: '04966a59f357db62b1197ee82d5c063f18b115c17496fa1bc8b9429055a6d1b17dc8d45791a20f108484b277f237063748cb32db15b80949b922b544b4ffe43a'
|
data/bin/intel_hex_reader
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require 'optparse'
|
4
5
|
require 'intel_hex'
|
@@ -9,30 +10,27 @@ require 'pp'
|
|
9
10
|
}
|
10
11
|
|
11
12
|
OptionParser.new do |opts|
|
12
|
-
opts.on('--help', 'Print this help.')
|
13
|
-
|
13
|
+
opts.on('--help', 'Print this help.') do
|
14
|
+
puts
|
15
|
+
puts opts
|
16
|
+
puts
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
opts.on('-f', '--filename=FILE', 'Load the specified file.') do |o|
|
20
|
+
@options[:file] = o
|
21
|
+
end
|
14
22
|
end.parse!
|
15
23
|
|
16
|
-
raise
|
24
|
+
raise 'No file specified' unless @options[:file]
|
17
25
|
|
18
26
|
intel_hex_file = IntelHex::FileReader.new(@options[:file])
|
19
27
|
|
20
28
|
intel_hex_file.each_record do |record|
|
21
|
-
|
22
|
-
when :data
|
23
|
-
record_data = "data=" + record.data.map { |b| "%02x" % b }.join(" ")
|
24
|
-
when :eof
|
25
|
-
record_data = ""
|
26
|
-
else
|
27
|
-
record_data = "value=" + record.send(data.type)
|
28
|
-
end
|
29
|
-
|
30
|
-
puts "Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s" % [
|
29
|
+
puts 'Record type=%-4s offset=%-4d length=%-4d checksum=%-4d %s' % [
|
31
30
|
record.type,
|
32
31
|
record.offset,
|
33
32
|
record.length,
|
34
33
|
record.checksum,
|
35
|
-
|
34
|
+
record.value_s,
|
36
35
|
]
|
37
36
|
end
|
38
|
-
|
data/lib/intel_hex.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IntelHex
|
2
4
|
class FileReader
|
3
5
|
def initialize(filename)
|
@@ -12,7 +14,7 @@ module IntelHex
|
|
12
14
|
|
13
15
|
def each_record
|
14
16
|
return to_enum(:each_record) unless block_given?
|
15
|
-
|
17
|
+
|
16
18
|
file = File.open(@filename, 'r')
|
17
19
|
|
18
20
|
begin
|
@@ -25,10 +27,10 @@ module IntelHex
|
|
25
27
|
|
26
28
|
nil
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
def each_byte_with_address
|
30
32
|
return to_enum(:each_byte_with_address) unless block_given?
|
31
|
-
|
33
|
+
|
32
34
|
each_record do |record|
|
33
35
|
case record.type
|
34
36
|
when :data
|
@@ -49,4 +51,4 @@ module IntelHex
|
|
49
51
|
nil
|
50
52
|
end
|
51
53
|
end
|
52
|
-
end
|
54
|
+
end
|
data/lib/intel_hex/record.rb
CHANGED
@@ -1,23 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IntelHex
|
2
4
|
class MisformattedFileError < RuntimeError; end
|
3
|
-
|
4
|
-
class
|
5
|
-
class
|
6
|
-
class
|
7
|
-
class
|
5
|
+
|
6
|
+
class ValidationError < StandardError; end
|
7
|
+
class InvalidTypeError < ValidationError; end
|
8
|
+
class InvalidLengthError < ValidationError; end
|
9
|
+
class InvalidOffsetError < ValidationError; end
|
10
|
+
class InvalidDataError < ValidationError; end
|
11
|
+
class InvalidChecksumError < ValidationError; end
|
8
12
|
|
9
13
|
class Record
|
10
|
-
TYPES = [
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
]
|
18
|
-
|
19
|
-
TYPE_MAP = TYPES.each_with_index.
|
20
|
-
|
14
|
+
TYPES = %i[
|
15
|
+
data
|
16
|
+
eof
|
17
|
+
esa
|
18
|
+
ssa
|
19
|
+
ela
|
20
|
+
sla
|
21
|
+
].freeze
|
22
|
+
|
23
|
+
TYPE_MAP = TYPES.each_with_index.each_with_object({}) { |(k, i), h| h[k] = i }
|
24
|
+
|
21
25
|
TYPE_DATA_LENGTH = {
|
22
26
|
data: (1..255),
|
23
27
|
eof: 0,
|
@@ -25,14 +29,14 @@ module IntelHex
|
|
25
29
|
ssa: 4,
|
26
30
|
ela: 2,
|
27
31
|
sla: 4,
|
28
|
-
}
|
32
|
+
}.freeze
|
29
33
|
|
30
34
|
def self.parse(line)
|
31
|
-
raise MisformattedFileError
|
32
|
-
raise MisformattedFileError
|
35
|
+
raise MisformattedFileError, 'Expected \':\' at start of line' unless line[0] == ':'
|
36
|
+
raise MisformattedFileError, 'Line length incorrect' unless line.size >= (1 + 2 + 4 + 2)
|
33
37
|
|
34
38
|
length = line[1..2].to_i(16)
|
35
|
-
data_end = (9 + length*2)
|
39
|
+
data_end = (9 + length * 2)
|
36
40
|
|
37
41
|
offset = line[3..6].to_i(16)
|
38
42
|
type = TYPES[line[7..8].to_i(16)]
|
@@ -81,7 +85,8 @@ module IntelHex
|
|
81
85
|
attr_reader :checksum
|
82
86
|
attr_reader :line
|
83
87
|
|
84
|
-
|
88
|
+
# rubocop:disable Metrics/ParameterLists
|
89
|
+
def initialize(type, length = 0, offset = 0, data = [], checksum = nil, line: nil, validate: false)
|
85
90
|
@type = type
|
86
91
|
@length = length
|
87
92
|
@offset = offset
|
@@ -92,17 +97,28 @@ module IntelHex
|
|
92
97
|
|
93
98
|
self.validate if validate
|
94
99
|
end
|
100
|
+
# rubocop:enable Metrics/ParameterLists
|
101
|
+
|
102
|
+
def data_s
|
103
|
+
'[' + data.map { |b| '%02x' % b }.join(' ') + ']'
|
104
|
+
end
|
105
|
+
|
106
|
+
def value_s
|
107
|
+
return '' if type == :eof
|
108
|
+
|
109
|
+
"#{type}=#{type == :data ? data_s : send(type)}"
|
110
|
+
end
|
95
111
|
|
96
112
|
def to_s
|
97
|
-
"#<#{self.class.name} type=#{type} offset=#{offset} length=#{length}>"
|
113
|
+
"#<#{self.class.name} type=#{type} offset=#{offset} length=#{length} #{value_s}>"
|
98
114
|
end
|
99
115
|
|
100
116
|
def to_ascii
|
101
|
-
|
117
|
+
':%02X%04X%02X%s%02X' % [
|
102
118
|
length,
|
103
119
|
offset,
|
104
120
|
TYPE_MAP[type],
|
105
|
-
data.map { |b|
|
121
|
+
data.map { |b| '%02X' % b }.join,
|
106
122
|
checksum,
|
107
123
|
]
|
108
124
|
end
|
@@ -111,7 +127,7 @@ module IntelHex
|
|
111
127
|
return @calculated_checksum if @calculated_checksum
|
112
128
|
|
113
129
|
sum = length + ((offset & 0xff00) >> 8) + (offset & 0x00ff) + TYPE_MAP[type]
|
114
|
-
sum += data.
|
130
|
+
sum += data.sum
|
115
131
|
|
116
132
|
@calculated_checksum = (((sum & 0xff) ^ 0xff) + 1) & 0xff
|
117
133
|
end
|
@@ -122,12 +138,10 @@ module IntelHex
|
|
122
138
|
end
|
123
139
|
|
124
140
|
def valid?
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
return false
|
130
|
-
end
|
141
|
+
validate
|
142
|
+
true
|
143
|
+
rescue ValidationError
|
144
|
+
false
|
131
145
|
end
|
132
146
|
|
133
147
|
def validate
|
@@ -139,12 +153,14 @@ module IntelHex
|
|
139
153
|
end
|
140
154
|
|
141
155
|
def validate_type
|
142
|
-
|
156
|
+
return if TYPE_MAP.include?(type)
|
157
|
+
|
158
|
+
raise InvalidTypeError, "Type #{type} is invalid"
|
143
159
|
end
|
144
160
|
|
145
161
|
def validate_offset
|
146
|
-
raise InvalidOffsetError
|
147
|
-
raise InvalidOffsetError
|
162
|
+
raise InvalidOffsetError, "Offset #{offset} is negative" unless offset >= 0
|
163
|
+
raise InvalidOffsetError, "Offset #{offset} is too large" unless offset < 2**16
|
148
164
|
end
|
149
165
|
|
150
166
|
def validate_length
|
@@ -157,20 +173,24 @@ module IntelHex
|
|
157
173
|
return if valid_length.include?(length)
|
158
174
|
end
|
159
175
|
|
160
|
-
raise InvalidLengthError
|
176
|
+
raise InvalidLengthError, "Length for type #{type} must be #{valid_length}; #{length} given"
|
161
177
|
end
|
162
178
|
|
163
179
|
def validate_data
|
164
|
-
|
180
|
+
return if data.size == length
|
181
|
+
|
182
|
+
raise InvalidDataError, "Data length #{data.size} does not match length #{length}"
|
165
183
|
end
|
166
184
|
|
167
185
|
def validate_checksum
|
168
|
-
|
186
|
+
return if calculate_checksum == checksum
|
187
|
+
|
188
|
+
raise InvalidChecksumError, "Checksum value #{checksum} does not match expected checksum #{calculate_checksum}"
|
169
189
|
end
|
170
190
|
|
171
191
|
def each_byte_with_address
|
172
192
|
return Enumerator.new(self, :each_byte_with_address) unless block_given?
|
173
|
-
|
193
|
+
|
174
194
|
data.each_with_index do |byte, i|
|
175
195
|
yield byte, offset + i
|
176
196
|
end
|
@@ -179,12 +199,12 @@ module IntelHex
|
|
179
199
|
end
|
180
200
|
|
181
201
|
def data=(value)
|
182
|
-
raise
|
183
|
-
raise InvalidLengthError
|
202
|
+
raise 'Incorrect data type' unless value.is_a?(Array)
|
203
|
+
raise InvalidLengthError, "Data length #{value.size} too large" unless value.size <= 255
|
204
|
+
|
184
205
|
@data = value
|
185
206
|
@length = data.size
|
186
207
|
@checksum = recalculate_checksum
|
187
|
-
nil
|
188
208
|
end
|
189
209
|
|
190
210
|
def data_to_uint16(offset = 0)
|
@@ -192,9 +212,9 @@ module IntelHex
|
|
192
212
|
end
|
193
213
|
|
194
214
|
def uint16_to_data(value, offset = 0)
|
195
|
-
|
215
|
+
data[(offset...(offset + 2))] = [
|
196
216
|
(value & 0xff00) >> 8,
|
197
|
-
value & 0x00ff
|
217
|
+
value & 0x00ff,
|
198
218
|
]
|
199
219
|
@length = data.size
|
200
220
|
@checksum = recalculate_checksum
|
@@ -205,24 +225,24 @@ module IntelHex
|
|
205
225
|
end
|
206
226
|
|
207
227
|
def uint32_to_data(value, offset = 0)
|
208
|
-
|
228
|
+
data[(offset...(offset + 4))] = [
|
209
229
|
(value & 0xff000000) >> 24,
|
210
230
|
(value & 0x00ff0000) >> 16,
|
211
231
|
(value & 0x0000ff00) >> 8,
|
212
|
-
value & 0x000000ff
|
232
|
+
value & 0x000000ff,
|
213
233
|
]
|
214
234
|
@length = data.size
|
215
235
|
@checksum = recalculate_checksum
|
216
236
|
end
|
217
237
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
238
|
+
alias esa data_to_uint16
|
239
|
+
alias ssa data_to_uint32
|
240
|
+
alias ela data_to_uint16
|
241
|
+
alias sla data_to_uint32
|
222
242
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
243
|
+
alias esa= uint16_to_data
|
244
|
+
alias ssa= uint32_to_data
|
245
|
+
alias ela= uint16_to_data
|
246
|
+
alias sla= uint32_to_data
|
227
247
|
end
|
228
|
-
end
|
248
|
+
end
|
data/lib/intel_hex/version.rb
CHANGED