msp430_bsl 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca5ed6ae2b592007808e73255f2f1de59c096f5b2e5702ad86fb7dea7c586981
4
- data.tar.gz: 6c289eaece5e0fe084ddba9f5847c87da017c51db2825c44d4463a6a10db7e7f
3
+ metadata.gz: 8513d03e26ca2b1c1950d4f47f3efe26bb96ac4a55a0b9e4495fed64fd2790e2
4
+ data.tar.gz: d52247ee6f8585a2fa22609e9fa4e4bfb24db01e16ba31348cfbf508ab3b9aa3
5
5
  SHA512:
6
- metadata.gz: 327080fb207aabed5bdd0df53f84555268eaf251118c935ee9414547bd437df954b8704cfdd969e0a2cf60e296d9b01a9bf600222fd7a2c96a4f8ca423bf43bd
7
- data.tar.gz: 3e8b706fc043fa9048b191e44a5deab69790202418dbbeb51ce93e1ea388637b61847c8cc79129ac6ab1d0a5d0cf4f79f4941263f408e975e9d6341058ea1fec
6
+ metadata.gz: 8096c5b78bed9798b8c3405d2b4301eab387fff7f5160270cd2234ff726c2933d67dd2428037158b9c33cff6da061ac2cdbea8c455bf225b15fef12d149af0d5
7
+ data.tar.gz: 1fbd3e41aa11c61ecbb11f3fcec51188e9e5b36c602f9b23de46d7834417ce91b0a6064693ebd8e48fff8e6152c0cac01f390997d1506af2c0b92f05c62d70e0
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,20 @@ Bundler.require
5
5
  require 'slop'
6
6
  require_relative '../lib/msp430_bsl'
7
7
 
8
+ include Msp430Bsl::Utils
9
+
10
+ HIGH_SPEED_UART_BAUD = 115200.freeze
11
+ MAX_WRITE_ATTEMPTS = 3
12
+
13
+ @opts = {}
8
14
  begin
9
- opts = Slop.parse help: true do |o|
15
+ @opts = Slop.parse help: true do |o|
10
16
  o.string '-d', '--device', 'Mandatory: Path to serial programmer device', required: true
11
17
  o.string '-f', '--hexfile', 'Mandatory: Path to HEX file to load', required: true
12
18
  o.string '-g', '--logfile', 'Path to logfile'
13
19
  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
20
+ o.bool '-e', '--erase_info', 'Erase info memory', default: false # TODO: implement
21
+ o.bool '-c', '--check', 'Verify flash content after each data block write', default: true
15
22
  o.bool '-h', '--help', 'Print this help' do
16
23
  puts "#{o}\n"
17
24
  exit
@@ -25,11 +32,9 @@ rescue Slop::UnknownOption => e
25
32
  exit
26
33
  end
27
34
 
28
- include Msp430Bsl::Utils
29
-
30
- logger = build_logger_from opts
35
+ logger = build_logger_from @opts
31
36
 
32
- @board = Msp430Bsl::Uart::Connection.new opts[:device], logger: logger
37
+ @board = Msp430Bsl::Uart::Connection.new @opts[:device], logger: logger
33
38
 
34
39
  # Enter BSL
35
40
  @board.enter_bsl
@@ -37,19 +42,49 @@ logger = build_logger_from opts
37
42
  logger.info 'Mass erasing FLASH'
38
43
  @board.send_command :mass_erase
39
44
  # Unlock BSL protected commands
40
- logger.info "'Unlocking BSL's password protected commands'"
45
+ logger.info "Unlocking BSL's password protected commands"
41
46
  @board.send_command :rx_password, data: Msp430Bsl::Configs::CMD_RX_PASSWORD
42
47
  # Switch UART to max speed
43
48
  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
49
+ @board.send_command :change_baud_rate, data: Msp430Bsl::Configs::BAUD_RATES[HIGH_SPEED_UART_BAUD]
50
+ @board.set_uart_speed HIGH_SPEED_UART_BAUD
46
51
 
47
52
  # If everything has gone well so far...
48
- hexfile = Msp430Bsl::HexFile.new opts[:hexfile]
53
+ hexfile = Msp430Bsl::HexFile.new @opts[:hexfile]
49
54
 
50
55
  # Group lines by contiguous memory addr
51
- logger.info 'Writing data into FLASH'
56
+ logger.info 'Writing data to FLASH'
52
57
  line_groups = hexfile.data_lines_grouped_by_contiguous_addr
58
+
59
+ def write_data_packet(lines_packet, check: true)
60
+ attempts = 0
61
+ loop do
62
+ # Write data to FLASH
63
+ resp = @board.send_command :rx_data_block, addr: lines_packet.first.addr, data: lines_packet.map { |line| line.data }.reduce(:+)
64
+
65
+ break resp unless check
66
+ # Check written data
67
+ data = lines_packet.map { |line| line.data }.flatten
68
+ start_addr = lines_packet.first.addr
69
+ data_len = data.size
70
+ our_crc = crc16(data)
71
+
72
+ resp = @board.send_command :crc_check, addr: start_addr, data: [data_len & 0xFF, (data_len >> 8) & 0xFF]
73
+ bsl_crc = resp.data[0] | resp.data[1] << 8
74
+
75
+ break resp if our_crc == bsl_crc
76
+
77
+ attempts += 1
78
+
79
+ logger.error "CRC mismatch at addr '0x#{start_addr.to_hex_str}' - Trying again"
80
+
81
+ if attempts >= MAX_WRITE_ATTEMPTS
82
+ logger.fatal "Tried #{attempts} times to write data at addr: 0x#{start_addr.to_hex_str}, but CRC keeps mismatching. Exiting"
83
+ exit
84
+ end
85
+ end
86
+ end
87
+
53
88
  # Try to optimize BSL writes
54
89
  # For each lines group, append as many lines as possible, given the BSL Core Commands buffer size
55
90
  line_groups.each do |group|
@@ -67,7 +102,7 @@ line_groups.each do |group|
67
102
  curr_data_size += line.data_length
68
103
  else
69
104
  # 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(:+)
105
+ write_data_packet curr_data_packet, check: @opts[:check]
71
106
  curr_data_packet = []
72
107
  curr_data_size = 0
73
108
  redo # Handle current line than would otherwise be skipped
@@ -76,7 +111,7 @@ line_groups.each do |group|
76
111
 
77
112
  # Send residual lines before handling next group
78
113
  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(:+)
114
+ write_data_packet curr_data_packet, check: @opts[:check]
80
115
  end
81
116
  end
82
117
 
@@ -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.0'.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.0
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
  - - ">="