Class: Msp430Bsl::Uart::PeripheralInterface

Inherits:
Object
  • Object
show all
Extended by:
Msp430Bsl::Utils
Includes:
Msp430Bsl::Utils
Defined in:
lib/msp430_bsl/uart/peripheral_interface.rb

Constant Summary collapse

MIN_PACKET_SIZE =

bytes

6.freeze
OK_HEADER =
0x80.freeze
HEADER_SIZE =
1.freeze
DATA_LEN_SIZE =
2.freeze
CRC_SIZE =
2.freeze
TOTAL_SIZE =
(HEADER_SIZE + DATA_LEN_SIZE + CRC_SIZE).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Msp430Bsl::Utils

build_logger_from, crc16, crc8, normalize_log_level

Constructor Details

#initialize(header: nil, data_len: nil, data: nil, crc: nil) ⇒ PeripheralInterface

Returns a new instance of PeripheralInterface.

Raises:

  • (Exceptions::PeripheralInterfaceDataNotArray)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 42

def initialize(header: nil, data_len: nil, data: nil, crc: nil)
  raise Exceptions::PeripheralInterfaceDataNotArray if (data && !data.is_a?(Array))

  @header = header
  @data_len = data_len
  @data = data
  @crc = crc ? crc : (data ? crc16(data) : nil)
  @cmd_code

  @partial_data = []
end

Instance Attribute Details

#cmd_kindObject (readonly)

Returns the value of attribute cmd_kind.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def cmd_kind
  @cmd_kind
end

#crcObject (readonly)

Returns the value of attribute crc.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def crc
  @crc
end

#dataObject (readonly)

Returns the value of attribute data.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def data
  @data
end

#data_lenObject (readonly)

Returns the value of attribute data_len.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def data_len
  @data_len
end

#errorsObject (readonly)

Returns the value of attribute errors.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def errors
  @errors
end

#headerObject (readonly)

Returns the value of attribute header.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def header
  @header
end

#packetObject (readonly)

Returns the value of attribute packet.



40
41
42
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40

def packet
  @packet
end

Class Method Details

.parse(raw_data) ⇒ Object

Raises:

  • (Exceptions::PeripheralInterfaceParseRawDataNotArray)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 27

def parse(raw_data)
  raise Exceptions::PeripheralInterfaceParseRawDataNotArray unless raw_data.is_a?(Array)
  raise Exceptions::PeripheralInterfaceSize, raw_data.size unless raw_data.size >= MIN_PACKET_SIZE

  header = raw_data[0]
  data_len = raw_data[2] << 8 | raw_data[1]
  data = raw_data[3, data_len]
  crc = raw_data[-1] << 8 | raw_data[-2]

  new header: header, data_len: data_len, data: data, crc: crc
end

.wrap(command) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 19

def wrap(command)
  unless command.is_a?(Command)
    raise Exceptions::PeripheralInterfaceWrapNotACommand, command
  end

  new header: OK_HEADER, data_len: command.packet.size, data: command.packet
end

Instance Method Details

#crc_ok?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 54

def crc_ok?
  data && crc == crc16(data)
end

#data_len_ok?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 62

def data_len_ok?
  data&.length == data_len
end

#header_ok?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 58

def header_ok?
  header == OK_HEADER
end

#lengthObject



66
67
68
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 66

def length
  packet.length
end

#push(val) ⇒ Object Also known as: <<



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 81

def push(val)
  @partial_data.append *val

  # If :header has not already been fetched
  if !header && @partial_data.size >= HEADER_SIZE
    @header = @partial_data.shift
  end
  # If :data_len has not already been fetched, and we have enough data
  if header && !data_len && @partial_data.size >= DATA_LEN_SIZE
    values = @partial_data.shift DATA_LEN_SIZE
    @data_len = values[0] | (values[1] << 8)
  end

  # If :data has not already been fetched, fetch it
  if data_len && (data.nil? || data.empty?) && @partial_data.size >= data_len
    @data = @partial_data.shift data_len
  end

  if data && !crc && @partial_data.size >= CRC_SIZE
    values = @partial_data.shift CRC_SIZE
    @crc = values[0] | (values[1] << 8)
  end
end

#to_hex_ary_strObject



106
107
108
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 106

def to_hex_ary_str
  packet.to_hex
end

#to_responseObject



114
115
116
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 114

def to_response
  Response.new data
end

#to_uartObject



110
111
112
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 110

def to_uart
  packet.to_chr_string
end

#valid?Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 118

def valid?
  @errors = []
  @errors << [:header, 'Header NOK'] unless header_ok?
  @errors << [:data_len, "'data_len' value (#{data_len}) differs from current data length (#{data&.length})"] unless data_len_ok?
  @errors << [:crc, 'CRC NOK'] unless crc_ok?

  @errors.empty?
end