Class: GdbFlasher::MCU

Inherits:
Object
  • Object
show all
Defined in:
lib/gdbflasher/mcu.rb

Direct Known Subclasses

CortexM

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ MCU

Returns a new instance of MCU.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gdbflasher/mcu.rb', line 4

def initialize(connection)
  @connection = connection
  @symbol_table = {}

  helper_hex = File.join HELPERS, helper_name + ".hex"
  helper_sym = File.join HELPERS, helper_name + ".sym"

  File.open(helper_sym, "r") do |f|
    f.each_line do |line|
      line.rstrip!

      address, symbol = line.split(" ")

      @symbol_table[symbol.intern] = address.to_i 16
    end
  end

  helper = nil
  File.open(helper_hex, "r") { |f| helper = IHex.load f }

  connection.reset
  connection.get_stop_reply

  helper.segments.each do |segment|
    connection.write_memory segment.base, segment.data
  end

  initialize_environment

  @connection.insert_breakpoint 0, @symbol_table[:trap], breakpoint_kind

  call_helper :initialize
end

Instance Method Details

#finalizeObject



38
39
40
41
42
# File 'lib/gdbflasher/mcu.rb', line 38

def finalize
  call_helper :finalize

  @connection.remove_breakpoint 0, @symbol_table[:trap], breakpoint_kind
end

#program_ihex(ihex, extra = {}) ⇒ Object



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, extra = {})
  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 extra[: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