Skip to content

Commit

Permalink
Replace uf2 creation code with upstream uf2tool
Browse files Browse the repository at this point in the history
Replace `atomvm_uf2create_provider` uf2 creation code with upstream standalone
`uf2tool` from https://github.com/pguyot/uf2tool.

Signed-off-by: Winford <winford@object.stream>
  • Loading branch information
UncleGrumpy committed Sep 10, 2024
1 parent 8ab3e77 commit a2d06e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Using the `-s init` option is still supported but deprecated. Use the `--application` (or `-a`) option to generate OTP applications using AtomVM.
- Replace `atomvm_uf2create_provider` uf2 creation code with [upstream `uf2tool`](https://github.com/pguyot/uf2tool)

## [0.7.3] (2023.11.25)

Expand Down
5 changes: 4 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{erl_opts, [debug_info]}.

{deps, [{atomvm_packbeam, "0.7.2"}]}.
{deps, [
{atomvm_packbeam, "0.7.2"},
{uf2tool, {git, "https://github.com/pguyot/uf2tool.git", {tag, "1.0.0"}}}
]}.

{ex_doc, [
{source_url, <<"https://github.com/atomvm/atomvm_rebar3_plugin">>},
Expand Down
64 changes: 3 additions & 61 deletions src/atomvm_uf2create_provider.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
%
% This file is part of AtomVM.
%
% Copyright 2022 Paul Guyot <pguyot@kallisys.net>
%
% Adapted for atomvm_rebar3_plugin:
% Copyright 2023 Winford (UncleGrumpy) <winford@object.stream>
% Copyright 2023-24 Winford (UncleGrumpy) <winford@object.stream>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -139,62 +136,7 @@ parse_addr("16#" ++ AddrHex) ->
parse_addr(AddrDec) ->
list_to_integer(AddrDec).

%%% UF2 defines
-define(UF2_MAGIC_START0, 16#0A324655).
-define(UF2_MAGIC_START1, 16#9E5D5157).
-define(UF2_MAGIC_END, 16#0AB16F30).

-define(UF2_FLAG_FAMILY_ID_PRESENT, 16#00002000).

%%% Pico defines
-define(UF2_PICO_FLAGS, ?UF2_FLAG_FAMILY_ID_PRESENT).
-define(UF2_PICO_PAGE_SIZE, 256).
-define(UF2_PICO_FAMILY_ID, 16#E48BFF56).

%%%

do_uf2create(OutputPath, StartAddr, ImagePath) ->
{ok, ImageBin} = file:read_file(ImagePath),
BlocksCount0 = byte_size(ImageBin) div ?UF2_PICO_PAGE_SIZE,
BlocksCount =
BlocksCount0 +
if
byte_size(ImageBin) rem ?UF2_PICO_PAGE_SIZE =:= 0 -> 0;
true -> 1
end,
OutputBin = uf2create0(0, BlocksCount, StartAddr, ImageBin, []),
ok = file:write_file(OutputPath, OutputBin).

%% @private
uf2create0(_BlockIndex, _BlocksCount, _BaseAddr, <<>>, Acc) ->
lists:reverse(Acc);
uf2create0(BlockIndex, BlocksCount, BaseAddr, ImageBin, Acc) ->
{PageBin, Tail} =
if
byte_size(ImageBin) >= ?UF2_PICO_PAGE_SIZE ->
split_binary(ImageBin, ?UF2_PICO_PAGE_SIZE);
true ->
{ImageBin, <<>>}
end,
PaddedData = pad_binary(PageBin, 476),
Block = [
<<
?UF2_MAGIC_START0:32/little,
?UF2_MAGIC_START1:32/little,
?UF2_PICO_FLAGS:32/little,
BaseAddr:32/little,
?UF2_PICO_PAGE_SIZE:32/little,
BlockIndex:32/little,
BlocksCount:32/little,
?UF2_PICO_FAMILY_ID:32/little
>>,
PaddedData,
<<?UF2_MAGIC_END:32/little>>
],
uf2create0(BlockIndex + 1, BlocksCount, BaseAddr + ?UF2_PICO_PAGE_SIZE, Tail, [Block | Acc]).

%% @private
pad_binary(Bin, Len) ->
PadCount = Len - byte_size(Bin),
Pad = binary:copy(<<0>>, PadCount),
[Bin, Pad].
do_uf2create(OutputPath, StartAddr, ImagePath) ->
uf2tool:uf2create(OutputPath, StartAddr, ImagePath).

0 comments on commit a2d06e7

Please sign in to comment.