44
45
46
47
48
49
50
51
52
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/gdbflasher/mcu.rb', line 44
def program_ihex(ihex, = {})
sectors = []
sector_actions = []
sector_map = self.class.const_get :SECTORS
sector_map.each_index do |i|
sector_begin, sector_end = sector_map[i]
sector_segment = nil
ihex.segments.each do |segment|
intersection = segment.intersect sector_begin, sector_end
if intersection.size > 0
if sector_segment.nil?
sector_segment = IHex::Segment.new
sector_segment.base = sector_begin
sector_segment.data = blank_byte.chr * (sector_end - sector_begin + 1)
sectors << sector_segment
end
affected_range = intersection.base - sector_begin...intersection.base + intersection.size - sector_begin
sector_segment.data[affected_range] = intersection.data
end
end
end
if [:read_disallowed]
sector_actions = [ [ :erase, :program ] ] * sectors.count
else
puts "Checking sectors"
sectors.each do |sector|
data = @connection.read_memory sector.base, sector.size
if data == sector.data
sector_actions << [ ]
elsif is_blank(data)
sector_actions << [ :program, :verify ]
elsif is_blank(sector.data)
sector_actions << [ :erase, :verify ]
else
sector_actions << [ :erase, :program, :verify ]
end
end
end
puts "Programming sectors:"
sectors.each_index do |i|
sector = sectors[i]
actions = sector_actions[i]
next if actions.empty?
sector_id = sector_map.index { |i| i[0] == sector.base }
printf " - %08X - %08X: ", sector.base, sector.base + sector.size - 1
actions.each do |action|
case action
when :erase
print "erase, "
status = erase sector_id
if status != 0
puts "failure!"
warn "Unable to erase sector #{sector_id}, vendor-specific error #{status}"
return false
end
when :program
print "program, "
offset = 0
page_size = @symbol_table[:PAGE_SIZE]
buffer = @symbol_table[:page_buffer]
while offset < sector.size
@connection.write_memory buffer, sector.data[offset...offset + page_size]
status = program sector.base + offset
if status != 0
puts "failure!"
warn "Unable to program page #{(sector.base + offset).to_s 16}, vendor-specific error #{status}"
return false
end
offset += page_size
end
when :verify
print "verify, "
data = @connection.read_memory sector.base, sector.size
if data != sector.data
puts "failure!"
warn "Verification of sector #{sector_id} failed."
return false
end
end
end
print "all ok.\n"
end
true
end
|