-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsp_uart.ex
71 lines (60 loc) · 2.08 KB
/
dsp_uart.ex
1
2
3
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
37
38
39
40
41
42
43
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
defmodule DspUart do
def serial_baud_rate, do: 192000
def serial_timeout, do: 2
def mcu_command_prefix, do: <<0x04::8>>
def mcu_init_prefix, do: <<0x03::8>>
def mcu_message_prefix, do: <<0x55::8>>
def start_write_config_command, do: <<0x0201::16>>
def end_write_config_command, do: <<0x0206::16>>
def get_flash_status_command, do: <<0x02fe::16>>
def flash_address_at_position(position) do
{fa, _} = Integer.parse("#{position}000550#{position}", 16)
fa
end
@spec append_serial_message_checksum(binary) :: binary
@doc """
Sum the bytes in a message, and append the least significant byte of the sum
Effectively, rolling over the sum after 255.
"""
def append_serial_message_checksum(message) do
checksum = for <<i <- message>>, reduce: 0 do
acc -> acc + i
end
message <> <<checksum :: 8>>
end
@spec serialise_mcu_command(binary) :: binary
@doc """
Takes a binary command value and serialises it in to a form ready to sent over the
serial link.
"""
def serialise_mcu_command(command_value) do
message = mcu_command_prefix() <> command_value
serialise_message(message)
end
@spec serialise_mcu_init_command(binary) :: binary
def serialise_mcu_init_command(command_value) do
message = mcu_init_prefix() <> command_value
serialise_message(message)
end
def serialise_message(message) do
prefix = mcu_message_prefix() <> :binary.encode_unsigned(byte_size(message))
prefix <> append_serial_message_checksum(message)
end
def get_serial_connection(dev \\ "tnt0") do
{:ok, pid} = Circuits.UART.start_link()
:ok = Circuits.UART.open(pid, dev, speed: serial_baud_rate(), active: false)
pid
end
def rs232_test(pid) do
{:ok, <<0xff::8>>} = send_serial_message(pid, serialise_mcu_init_command(<<07>>))
end
def dsp_type_test(pid) do
{:ok, <<0x11::8>>} = send_serial_message(pid, serialise_mcu_init_command(<<08>>))
end
def send_serial_message(pid, message) do
IO.inspect(message)
Circuits.UART.flush(pid)
Circuits.UART.write(pid, message)
Circuits.UART.read(pid)
end
end