diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7a598..d3ecd75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/rebar.config b/rebar.config index 60ac517..d8e1834 100644 --- a/rebar.config +++ b/rebar.config @@ -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">>}, diff --git a/src/atomvm_uf2create_provider.erl b/src/atomvm_uf2create_provider.erl index f858ab5..95f306f 100644 --- a/src/atomvm_uf2create_provider.erl +++ b/src/atomvm_uf2create_provider.erl @@ -1,10 +1,7 @@ % % This file is part of AtomVM. % -% Copyright 2022 Paul Guyot -% -% Adapted for atomvm_rebar3_plugin: -% Copyright 2023 Winford (UncleGrumpy) +% Copyright 2023-24 Winford (UncleGrumpy) % % Licensed under the Apache License, Version 2.0 (the "License"); % you may not use this file except in compliance with the License. @@ -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, - <> - ], - 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).