Skip to content

Instantly share code, notes, and snippets.

@serpent213
Last active July 29, 2017 03:42
Show Gist options
  • Save serpent213/5ecd9324c0365ac19cd16ebfde38f832 to your computer and use it in GitHub Desktop.
Save serpent213/5ecd9324c0365ac19cd16ebfde38f832 to your computer and use it in GitHub Desktop.
defmodule NervesBBBEEPROM do
@moduledoc """
Documentation for NervesBbbEeprom.
"""
@doc """
Hello world.
## Examples
iex> NervesBbbEeprom.hello
:world
"""
# @eeprom_device "/sys/bus/i2c/devices/0-0050/eeprom"
# @ti_nvs_file "/lib/firmware/ti-connectivity/wl127x-nvs.bin"
@eeprom_device "../eeprom.bin"
@ti_nvs_file "../wl127x-nvs.bin"
defstruct header: nil, id: nil, version: nil, serial: nil, mac_hex: "000000000000", mac_bytes: <<0, 0, 0, 0, 0, 0>>
@spec read_and_parse_eeprom :: {:ok, %NervesBBBEEPROM{}} | {:error, :atom}
def read_and_parse_eeprom do
case File.open(@eeprom_device) do
{:ok, file} ->
eeprom = IO.binread(file, 28)
:file.position(file, 0x3c)
mac_hex = IO.binread(file, 12)
File.close(file)
<<header::binary-size(4),
id::binary-size(8),
version::binary-size(4),
serial::binary-size(12),
>> = eeprom
case Base.decode16(mac_hex) do
{:ok, mac_bytes} ->
%NervesBBBEEPROM{
header: header,
id: id,
version: version,
serial: serial,
mac_hex: mac_hex,
mac_bytes: mac_bytes
}
err -> err
end
err -> err
end
end
@spec write_mac_to_nvs_file(<<_::6>>) :: :ok | {:error, :atom}
def write_mac_to_nvs_file(<<0xff, 0xff, 0xff, 0xff, 0xff, 0xff>>), do: write_mac_to_nvs_file(<<0, 0, 0, 0, 0, 0>>)
def write_mac_to_nvs_file(<<b1, b2, b3, b4, b5, b6>>) do
case File.exists?(@ti_nvs_file) do
true ->
block1 = <<b6, b5, b4, b3>>
block2 = <<b2, b1>>
{:ok, nvs} = File.open(@ti_nvs_file, [:binary, :read, :write])
:file.position(nvs, 3)
IO.binwrite(nvs, block1)
:file.position(nvs, 10)
IO.binwrite(nvs, block2)
File.close(nvs) # returns :ok on success
false -> {:error, :enoent}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment