Skip to content

Add support to ESP32C3 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
85fd3e9
add esp32 boards toolchain into dependencies
potsrevennil Jul 19, 2024
6d9cafd
refactor nix config for ci optimization
potsrevennil Jul 22, 2024
5109c80
wrap esp-idf static linked library in to nix derivation
potsrevennil Jul 24, 2024
cee9c0b
use setup hook to expose nix package path instead
potsrevennil Jul 24, 2024
fda925a
upgrade esp-idf to v5.3
potsrevennil Jul 26, 2024
0dbdd0d
only include static library used by bootloader in nix
potsrevennil Jul 30, 2024
8b15eb7
add esp32 gcc version info in ci
potsrevennil Aug 7, 2024
45aeb29
add riscv-pkgs into ci shell instead
potsrevennil Aug 7, 2024
32c9802
add esp32-c3 hal file
potsrevennil Aug 2, 2024
9eb8174
add esp32-c3.mk
potsrevennil Aug 5, 2024
b63c9fe
support esp32-c3 in the build system
potsrevennil Aug 6, 2024
7a8417c
fix keccak riscv macros
potsrevennil Aug 6, 2024
4ccabc3
use direct boot instead
potsrevennil Aug 6, 2024
dcf3809
add license
potsrevennil Aug 6, 2024
faa1d64
add esp32c3 to platform list in the tests script
potsrevennil Aug 7, 2024
98633e5
update license to MIT
potsrevennil Aug 7, 2024
c37112a
config the clock frequency for esp32c3
potsrevennil Aug 14, 2024
27d33f2
update some comment for hal-esp-idf.c
potsrevennil Aug 14, 2024
42f1b6f
use rng module instead
potsrevennil Aug 14, 2024
b72ffe6
set default baudrate to 38400 and make it configurable
potsrevennil Aug 14, 2024
beb81b4
add stack hal api
potsrevennil Aug 15, 2024
0bf8314
remove unused macros from stack.c
potsrevennil Aug 15, 2024
badb37b
only compile esp-idf hal with gnu17
potsrevennil Aug 16, 2024
56f5886
set esp32c3 default baudrate to 115200
potsrevennil Aug 16, 2024
4a45553
fix ld warning and make memory region match esp-idf document
potsrevennil Aug 16, 2024
3dde29b
use esp_fill_random instead of bootloader_fill_random
potsrevennil Aug 16, 2024
f5859fe
add comment for rng
potsrevennil Aug 16, 2024
8fb7b99
remove unneeded comment
potsrevennil Aug 16, 2024
bdde012
remove esp32-c3 from --list-platforms to avoid confusion
potsrevennil Aug 16, 2024
f5824fa
fix typo
potsrevennil Aug 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ if ! has nix_direnv_version || ! nix_direnv_version 3.0.3; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.3/direnvrc" "sha256-0EVQVNSRQWsln+rgPW3mXVmnF5sfcmKEYOmOSfLYxHg="
fi

watch_file mbed-os.nix
watch_file libopencm3.nix
watch_file esp-idf-lib.nix
use flake
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
script: |
astyle --version
arm-none-eabi-gcc --version
riscv32-esp-elf-gcc --version
qemu-system-arm --version
- name: Lint
run: |
Expand All @@ -43,6 +44,7 @@ jobs:

export -f make_platform
tests --list-platforms | grep -v "mps2" | xargs -I {} bash -c 'make_platform "$@" || exit 1' _ {}
make_platform "esp32-c3"

- name: Functional test
id: func_test
Expand Down
86 changes: 86 additions & 0 deletions esp-idf-lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# SPDX-License-Identifier: Apache-2.0

{ stdenvNoCC
, writeText
, esp-idf-esp32c3
, git
, python3Packages
, fetchPypi
}:
let
version = "v5.3";

# new dependency for v5.2.x
pyclang = python3Packages.buildPythonPackage rec {
pname = "pyclang";
version = "0.4.2";

src = fetchPypi {
inherit pname version;
sha256 = "sha256-vuDZ5yEhyDpCmkXoC+Gr2X5vMK5B46HnktcvBONjxXM=";
};

doCheck = false;
};

# new dependency for v5.3
esp-idf-nvs-partition-gen = python3Packages.buildPythonPackage rec {
pname = "esp_idf_nvs_partition_gen";
version = "0.1.2";
format = "pyproject";

src = fetchPypi {
inherit pname version;
sha256 = "sha256-HjW5RCKfy83LQgAs0tOW/f9LPVoLwHY1pyb6ar+AxwY=";
};

propagatedBuildInputs = builtins.attrValues {
inherit (python3Packages)
setuptools
cryptography;
};

doCheck = false;
};

esp-idf-esp32c3' = (esp-idf-esp32c3.overrideAttrs (old: {
propagatedBuildInputs = builtins.filter (p: p != git) old.propagatedBuildInputs ++
[
pyclang
esp-idf-nvs-partition-gen
];
patches = [ ];
})
).override {
rev = version;
sha256 = "sha256-w+xyva4t21STVtfYZOXY2xw6sDc2XvJXBZSx+wd1N6Y=";
};
in
stdenvNoCC.mkDerivation {
pname = "esp-idf-esp32c3-lib";
version = version;

dontUnpack = true; # start with empt src
dontConfigure = true;

# set environment variables for downstreams
setupHook = writeText "setup-hook.sh" ''
export ESP_IDF_DIR="${esp-idf-esp32c3'}"
export ESP_IDF_LIB="$1"
'';

buildInputs = [ esp-idf-esp32c3' git ];
propagatedBuildInputs = [ esp-idf-esp32c3' ];

buildPhase = ''
cp --no-preserve=mode,ownership -r "$IDF_PATH/examples/get-started/hello_world" ./
idf.py -C hello_world set-target esp32c3
idf.py -C hello_world build
'';
installPhase = ''
mkdir -p $out/esp32c3/bootloader
mv ./hello_world/build/esp-idf $out/esp32c3/
mv ./hello_world/build/bootloader/esp-idf $out/esp32c3/bootloader
mv ./hello_world/build/bootloader/config $out/esp32c3/bootloader
'';
}
8 changes: 4 additions & 4 deletions fips202/keccakf1600.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Please refer to LowLevel.build for the exact list of other files it must be comb
#define IS_BIG_ENDIAN 4321
#define IS_LITTLE_ENDIAN 1234

#if defined(__arm__)
# ifdef __BIG_ENDIAN
#if defined(__arm__) || defined(__riscv)
# if defined(__BIG_ENDIAN) || defined(__ORDER_BIG_ENDIAN__)
# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
# else
# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
Expand Down Expand Up @@ -152,7 +152,7 @@ void KeccakP1600_AddLanes(KeccakP1600_plain32_state *state, const unsigned char
| ((uint32_t)(laneAsBytes[5]) << 8)
| ((uint32_t)(laneAsBytes[6]) << 16)
| ((uint32_t)(laneAsBytes[7]) << 24);
uint32_t even, odd, temp, temp0, temp1;
uint32_t temp, temp0, temp1;
uint32_t *stateAsHalfLanes = state->A;
toBitInterleavingAndXOR(low, high, stateAsHalfLanes[lanePosition * 2 + 0], stateAsHalfLanes[lanePosition * 2 + 1], temp, temp0, temp1);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ static void KeccakP1600_ExtractLanes(const KeccakP1600_plain32_state *state, uns
#else
unsigned int lanePosition;
for (lanePosition = 0; lanePosition < laneCount; lanePosition++) {
uint32_t *stateAsHalfLanes = state->A;
const uint32_t *stateAsHalfLanes = state->A;
uint32_t low, high, temp, temp0, temp1;
fromBitInterleaving(stateAsHalfLanes[lanePosition * 2], stateAsHalfLanes[lanePosition * 2 + 1], low, high, temp, temp0, temp1);
uint8_t laneAsBytes[8];
Expand Down
56 changes: 56 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 28 additions & 17 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";

esp-dev = {
url = "github:mirrexagon/nixpkgs-esp-dev?rev=86a2bbe01fe0258887de7396af2a5eb0e37ac3be";
inputs.nixpkgs.follows = "nixpkgs";
};

flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
};

outputs = inputs@{ flake-parts, ... }:
outputs = inputs@{ flake-parts, esp-dev, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [ ];
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
perSystem = { pkgs, ... }:
perSystem = { pkgs, system, ... }:
let
libopencm3 = pkgs.callPackage ./libopencm3.nix {
targets = [ "stm32/f2" "stm32/f4" "stm32/f7" ];
};
mbed-os = pkgs.callPackage ./mbed-os.nix {
targets = [ "TARGET_MPS2_M3" "TARGET_MPS2_M4" "TARGET_MPS2_M7" ];
};

core = builtins.attrValues {
astyle = pkgs.astyle.overrideAttrs (old: rec {
version = "3.4.13";
Expand Down Expand Up @@ -52,12 +50,22 @@
};

arm-pkgs = builtins.attrValues {
libopencm3 = libopencm3;
mbed-os = mbed-os;
libopencm3 = pkgs.callPackage ./libopencm3.nix {
targets = [ "stm32/f2" "stm32/f4" "stm32/f7" ];
};

mbed-os = pkgs.callPackage ./mbed-os.nix {
targets = [ "TARGET_MPS2_M3" "TARGET_MPS2_M4" "TARGET_MPS2_M7" ];
};

inherit (pkgs)
gcc-arm-embedded-13; # arm-gnu-toolchain-13.2.rel1
};

riscv-pkgs = builtins.attrValues {
esp-idf-lib = pkgs.callPackage ./esp-idf-lib.nix { };
};

wrapShell = mkShell: attrs:
mkShell (attrs // {
shellHook = ''
Expand All @@ -66,23 +74,26 @@
});
in
{
_module.args = {
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ esp-dev.overlays.default ];
};
};

devShells.default = wrapShell pkgs.mkShellNoCC {
packages = core ++ arm-pkgs ++ builtins.attrValues {
packages = core ++ arm-pkgs ++ riscv-pkgs ++ builtins.attrValues {
inherit (pkgs)
direnv
nix-direnv

# debug dependencies
openocd; # 0.12.0
};
OPENCM3_DIR = ''${libopencm3}'';
MBED_OS_DIR = ''${mbed-os}'';
};

devShells.ci = wrapShell pkgs.mkShellNoCC {
packages = core ++ arm-pkgs;
OPENCM3_DIR = ''${libopencm3}'';
MBED_OS_DIR = ''${mbed-os}'';
packages = core ++ arm-pkgs ++ riscv-pkgs;
};
};
flake = {
Expand Down
68 changes: 68 additions & 0 deletions hal/esp32-c3.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*SPDX-License-Identifier: MIT*/
MEMORY {
irom (x): org = 0x42000000, len = 0x400000
drom (r): org = 0x3C000000, len = 0x400000
sram (rw): org = 0x3FC80000, len = 0x50000
rtc_ram (rx): org = 0x50000000, len = 0x2000
}

ENTRY(_start)

SECTIONS {
.header (READONLY): AT(0)
{
_iflash_start = .;
LONG(0xaedb041d)
LONG(0xaedb041d)
} > irom

.text.entry : {
KEEP(*(.text.entry))
} > irom

.text : {
*(.text .stub .text.* .gnu.linkonce.t.*)
} > irom

.iram : {
*(.iram .iram*)
} > irom

_iflash_end = .;
_iflash_size = _iflash_end - _iflash_start;

_dflash_start = ORIGIN(drom) + _iflash_size;
.rodata _dflash_start : AT(_iflash_size) {
*(.rodata .rodata* .srodata .srodata* .sdata2 .sdata2* .gnu.linkonce.r.*)
} > drom

_dflash_end = .;
_dflash_size = _dflash_end - _dflash_start;

.data ORIGIN(sram) : AT(_iflash_size + _dflash_size) {
_data_start = .;
*(.data .data* .gnu.linkonce.r.* .riscv.*)
} > sram

.sdata : {
_sdata_start = .;
*(.sdata .sdata.* .gnu.linkonce.s.*)
} > sram
_data_end = .;
_data_lma_start = ORIGIN(drom) + LOADADDR(.data);

.bss : {
_bss_start = .;
*(.bss .bss* .sbss .sbss* COMMON)
_bss_end = .;
} > sram

__global_pointer$ = _sdata_start + 0x800;

.stack : {
_stack_bottom = .;
_stack_top = ORIGIN(sram) + LENGTH(sram);
_stack_size_min = 0x4000;
ASSERT(_stack_bottom + _stack_size_min < _stack_top, "Error: no space for stack");
}
}
Loading