msp430_bsl 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca5ed6ae2b592007808e73255f2f1de59c096f5b2e5702ad86fb7dea7c586981
4
- data.tar.gz: 6c289eaece5e0fe084ddba9f5847c87da017c51db2825c44d4463a6a10db7e7f
3
+ metadata.gz: 04be185fd24cfefaaf6f4d2ca3c0aa5fc919b912aebd0db25e0ac71a4308d10e
4
+ data.tar.gz: 228d4f5fe568b5d22e1072d169f8e5292803639b994a407872ed05ef93ad8ee5
5
5
  SHA512:
6
- metadata.gz: 327080fb207aabed5bdd0df53f84555268eaf251118c935ee9414547bd437df954b8704cfdd969e0a2cf60e296d9b01a9bf600222fd7a2c96a4f8ca423bf43bd
7
- data.tar.gz: 3e8b706fc043fa9048b191e44a5deab69790202418dbbeb51ce93e1ea388637b61847c8cc79129ac6ab1d0a5d0cf4f79f4941263f408e975e9d6341058ea1fec
6
+ metadata.gz: f3597deabec5b77da55ae9d5d89f577c09d351fe4bf376178927c3b8d90f957d3e64e6b8d8a0430a6249c1fb0cc27d9ff0c62d1d3620ee34c4c09aa57d3f8060
7
+ data.tar.gz: 88c10187a3049aa22a9a3083341399b683d5ad0475e4bbb6d292430f2fdf7ead9b5e723f359442e4706f31d828912a4ae8d5d1f105c19d09e5b5ee38cfb034cf
data/README.md CHANGED
@@ -20,19 +20,19 @@ Or install it yourself as:
20
20
 
21
21
  ## Compatibility
22
22
 
23
- Although it's not tested, this library should be compatibile with Ruby 2.7.0 or higher.
23
+ This Gem has been developed with Ruby 3, but has been tested down to Ruby 2.5.0
24
24
 
25
25
  ## Usage
26
26
 
27
27
  **Help is appreciated to write some good USAGE**
28
28
 
29
- In the `bin` folder there's the `upload_hex` executable.
29
+ In the `bin` folder there's the `upload_hex` executable. By installing the gem you'll have it available to use.
30
+ Just run `upload_hex -h` to show available options.
30
31
 
31
- TL;DR: the script can upload a .hex file to the target through a normal UART connection (`rts` and `dtr` pins required).
32
+ TL;DR: the script can upload a `.hex` file to the target through a normal UART connection (`rts` and `dtr` pins required).
32
33
 
33
- **The script has been tested only with CC430F5137**
34
+ **The script has been tested only with CC430F5137 - contributions for other chips are welcome**
34
35
 
35
- Just run `bin/upload_hex -h` to show available options.
36
36
 
37
37
  ## TODO
38
38
 
data/bin/upload_hex CHANGED
@@ -5,13 +5,23 @@ Bundler.require
5
5
  require 'slop'
6
6
  require_relative '../lib/msp430_bsl'
7
7
 
8
+ # Force sync on STDOUT write. This way the logger flushes its output after every write
9
+ STDOUT.sync = true
10
+
11
+ include Msp430Bsl::Utils
12
+
13
+ HIGH_SPEED_UART_BAUD = 115200.freeze
14
+ MAX_WRITE_ATTEMPTS = 3
15
+
16
+ @opts = {}
8
17
  begin
9
- opts = Slop.parse help: true do |o|
18
+ @opts = Slop.parse help: true do |o|
10
19
  o.string '-d', '--device', 'Mandatory: Path to serial programmer device', required: true
11
20
  o.string '-f', '--hexfile', 'Mandatory: Path to HEX file to load', required: true
12
21
  o.string '-g', '--logfile', 'Path to logfile'
13
22
  o.string '-l', '--loglevel', "Logger level. One of ['fatal', 'error', 'warn', 'info', 'debug']. Defaults to 'debug'", default: :debug
14
- o.bool '-c', '--check', 'Verify flash content after upload', default: true
23
+ o.bool '-e', '--erase_info', 'Erase info memory', default: false # TODO: implement
24
+ o.bool '-c', '--check', 'Verify flash content after each data block write', default: true
15
25
  o.bool '-h', '--help', 'Print this help' do
16
26
  puts "#{o}\n"
17
27
  exit
@@ -25,11 +35,9 @@ rescue Slop::UnknownOption => e
25
35
  exit
26
36
  end
27
37
 
28
- include Msp430Bsl::Utils
38
+ logger = build_logger_from @opts
29
39
 
30
- logger = build_logger_from opts
31
-
32
- @board = Msp430Bsl::Uart::Connection.new opts[:device], logger: logger
40
+ @board = Msp430Bsl::Uart::Connection.new @opts[:device], logger: logger
33
41
 
34
42
  # Enter BSL
35
43
  @board.enter_bsl
@@ -37,19 +45,49 @@ logger = build_logger_from opts
37
45
  logger.info 'Mass erasing FLASH'
38
46
  @board.send_command :mass_erase
39
47
  # Unlock BSL protected commands
40
- logger.info "'Unlocking BSL's password protected commands'"
48
+ logger.info "Unlocking BSL's password protected commands"
41
49
  @board.send_command :rx_password, data: Msp430Bsl::Configs::CMD_RX_PASSWORD
42
50
  # Switch UART to max speed
43
51
  logger.info 'Changing UART BAUD to 115200'
44
- @board.send_command :change_baud_rate, data: Msp430Bsl::Configs::BAUD_RATES[115200]
45
- @board.set_uart_speed 115200
52
+ @board.send_command :change_baud_rate, data: Msp430Bsl::Configs::BAUD_RATES[HIGH_SPEED_UART_BAUD]
53
+ @board.set_uart_speed HIGH_SPEED_UART_BAUD
46
54
 
47
55
  # If everything has gone well so far...
48
- hexfile = Msp430Bsl::HexFile.new opts[:hexfile]
56
+ hexfile = Msp430Bsl::HexFile.new @opts[:hexfile]
49
57
 
50
58
  # Group lines by contiguous memory addr
51
- logger.info 'Writing data into FLASH'
59
+ logger.info 'Writing data to FLASH'
52
60
  line_groups = hexfile.data_lines_grouped_by_contiguous_addr
61
+
62
+ def write_data_packet(lines_packet, check: true)
63
+ attempts = 0
64
+ loop do
65
+ # Write data to FLASH
66
+ resp = @board.send_command :rx_data_block, addr: lines_packet.first.addr, data: lines_packet.map { |line| line.data }.reduce(:+)
67
+
68
+ break resp unless check
69
+ # Check written data
70
+ data = lines_packet.map { |line| line.data }.flatten
71
+ start_addr = lines_packet.first.addr
72
+ data_len = data.size
73
+ our_crc = crc16(data)
74
+
75
+ resp = @board.send_command :crc_check, addr: start_addr, data: [data_len & 0xFF, (data_len >> 8) & 0xFF]
76
+ bsl_crc = resp.data[0] | resp.data[1] << 8
77
+
78
+ break resp if our_crc == bsl_crc
79
+
80
+ attempts += 1
81
+
82
+ logger.error "CRC mismatch at addr '0x#{start_addr.to_hex_str}' - Trying again"
83
+
84
+ if attempts >= MAX_WRITE_ATTEMPTS
85
+ logger.fatal "Tried #{attempts} times to write data at addr: 0x#{start_addr.to_hex_str}, but CRC keeps mismatching. Exiting"
86
+ exit
87
+ end
88
+ end
89
+ end
90
+
53
91
  # Try to optimize BSL writes
54
92
  # For each lines group, append as many lines as possible, given the BSL Core Commands buffer size
55
93
  line_groups.each do |group|
@@ -67,7 +105,7 @@ line_groups.each do |group|
67
105
  curr_data_size += line.data_length
68
106
  else
69
107
  # No room left, send packet
70
- @board.send_command :rx_data_block, addr: curr_data_packet.first.addr, data: curr_data_packet.map { |line| line.data }.reduce(:+)
108
+ write_data_packet curr_data_packet, check: @opts[:check]
71
109
  curr_data_packet = []
72
110
  curr_data_size = 0
73
111
  redo # Handle current line than would otherwise be skipped
@@ -76,7 +114,7 @@ line_groups.each do |group|
76
114
 
77
115
  # Send residual lines before handling next group
78
116
  if curr_data_packet.any?
79
- @board.send_command :rx_data_block, addr: curr_data_packet.first.addr, data: curr_data_packet.map { |line| line.data }.reduce(:+)
117
+ write_data_packet curr_data_packet, check: @opts[:check]
80
118
  end
81
119
  end
82
120
 
@@ -4,7 +4,7 @@ module Msp430Bsl
4
4
  class HexLine
5
5
  include Utils
6
6
 
7
- attr_reader :raw_data, :data_length, :addr, :type, :data, :crc, :number
7
+ attr_reader :raw_data, :data_length, :addr, :type, :data, :crc, :number, :end_addr
8
8
 
9
9
  RECORD_TYPES = {
10
10
  data: 0x00,
@@ -27,12 +27,13 @@ module Msp430Bsl
27
27
  @data = raw_data.slice 4, data_length
28
28
  @crc = raw_data[-1]
29
29
  @number = num
30
+ @end_addr = addr + data_length
30
31
  end
31
32
 
32
33
  # Checks if this line's address is contiguous with the one of the given line
33
34
  # Obviously this can be true only if the given line has an address that precedes this line's one
34
35
  def has_addr_contiguous_to?(another_line)
35
- another_line.addr + another_line.data_length == addr
36
+ another_line.end_addr == addr
36
37
  end
37
38
 
38
39
  def crc_ok?
@@ -27,7 +27,7 @@ module Msp430Bsl
27
27
  end
28
28
 
29
29
  def is_data?
30
- kind == Configs::CMD_KINDS[:data]
30
+ kind == Configs::CMD_KINDS[:data][:code]
31
31
  end
32
32
 
33
33
  def is_message?
@@ -127,6 +127,7 @@ module Msp430Bsl
127
127
  test_pin_go :low
128
128
  sleep 5.millis
129
129
  reset_pin_go :high
130
+ sleep 5.millis
130
131
  end
131
132
 
132
133
  private
@@ -1,3 +1,3 @@
1
1
  module Msp430Bsl
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
data/lib/msp430_bsl.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require 'zeitwerk'
2
2
  require 'serialport'
3
3
 
4
- loader = Zeitwerk::Loader.new
5
- loader.push_dir File.expand_path(__dir__)
4
+ @loader = Zeitwerk::Loader.new
5
+ @loader.push_dir File.expand_path(__dir__)
6
6
  core_ext = File.expand_path('core_ext', __dir__)
7
- loader.ignore core_ext
8
- loader.setup
7
+ @loader.ignore core_ext
8
+ @loader.setup
9
9
 
10
10
  # Require core_ext files
11
11
  Dir["#{core_ext}/**/*.rb"].each { |file| require file }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msp430_bsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-27 00:00:00.000000000 Z
11
+ date: 2023-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: 2.7.0
108
+ version: 2.5.0
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
111
  - - ">="