Skip to content

Commit 2968023

Browse files
authored
Add support to ESP32C3 (#50)
* add esp32 boards toolchain into dependencies Signed-off-by: Thing-han, Lim <[email protected]> * refactor nix config for ci optimization Signed-off-by: Thing-han, Lim <[email protected]> * wrap esp-idf static linked library in to nix derivation Signed-off-by: Thing-han, Lim <[email protected]> * use setup hook to expose nix package path instead Signed-off-by: Thing-han, Lim <[email protected]> * upgrade esp-idf to v5.3 Signed-off-by: Thing-han, Lim <[email protected]> * only include static library used by bootloader in nix Signed-off-by: Thing-han, Lim <[email protected]> * add esp32 gcc version info in ci Signed-off-by: Thing-han, Lim <[email protected]> * add riscv-pkgs into ci shell instead Signed-off-by: Thing-han, Lim <[email protected]> * add esp32-c3 hal file Signed-off-by: Thing-han, Lim <[email protected]> * add esp32-c3.mk Signed-off-by: Thing-han, Lim <[email protected]> * support esp32-c3 in the build system Signed-off-by: Thing-han, Lim <[email protected]> * fix keccak riscv macros Signed-off-by: Thing-han, Lim <[email protected]> * use direct boot instead Signed-off-by: Thing-han, Lim <[email protected]> * add license Signed-off-by: Thing-han, Lim <[email protected]> * add esp32c3 to platform list in the tests script Signed-off-by: Thing-han, Lim <[email protected]> * update license to MIT Signed-off-by: Thing-han, Lim <[email protected]> * config the clock frequency for esp32c3 Signed-off-by: Thing-han, Lim <[email protected]> * update some comment for hal-esp-idf.c Signed-off-by: Thing-han, Lim <[email protected]> * use rng module instead Signed-off-by: Thing-han, Lim <[email protected]> * set default baudrate to 38400 and make it configurable Signed-off-by: Thing-han, Lim <[email protected]> * add stack hal api Signed-off-by: Thing-han, Lim <[email protected]> * remove unused macros from stack.c Signed-off-by: Thing-han, Lim <[email protected]> * only compile esp-idf hal with gnu17 Signed-off-by: Thing-han, Lim <[email protected]> * set esp32c3 default baudrate to 115200 Signed-off-by: Thing-han, Lim <[email protected]> * fix ld warning and make memory region match esp-idf document Signed-off-by: Thing-han, Lim <[email protected]> * use esp_fill_random instead of bootloader_fill_random Signed-off-by: Thing-han, Lim <[email protected]> * add comment for rng Signed-off-by: Thing-han, Lim <[email protected]> * remove unneeded comment Signed-off-by: Thing-han, Lim <[email protected]> * remove esp32-c3 from --list-platforms to avoid confusion Signed-off-by: Thing-han, Lim <[email protected]> * fix typo Signed-off-by: Thing-han, Lim <[email protected]> --------- Signed-off-by: Thing-han, Lim <[email protected]>
1 parent 777bb21 commit 2968023

18 files changed

+620
-62
lines changed

.envrc

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ if ! has nix_direnv_version || ! nix_direnv_version 3.0.3; then
33
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.3/direnvrc" "sha256-0EVQVNSRQWsln+rgPW3mXVmnF5sfcmKEYOmOSfLYxHg="
44
fi
55

6+
watch_file mbed-os.nix
7+
watch_file libopencm3.nix
8+
watch_file esp-idf-lib.nix
69
use flake

.github/workflows/build.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
script: |
2222
astyle --version
2323
arm-none-eabi-gcc --version
24+
riscv32-esp-elf-gcc --version
2425
qemu-system-arm --version
2526
- name: Lint
2627
run: |
@@ -43,6 +44,7 @@ jobs:
4344
4445
export -f make_platform
4546
tests --list-platforms | grep -v "mps2" | xargs -I {} bash -c 'make_platform "$@" || exit 1' _ {}
47+
make_platform "esp32-c3"
4648
4749
- name: Functional test
4850
id: func_test

esp-idf-lib.nix

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
{ stdenvNoCC
4+
, writeText
5+
, esp-idf-esp32c3
6+
, git
7+
, python3Packages
8+
, fetchPypi
9+
}:
10+
let
11+
version = "v5.3";
12+
13+
# new dependency for v5.2.x
14+
pyclang = python3Packages.buildPythonPackage rec {
15+
pname = "pyclang";
16+
version = "0.4.2";
17+
18+
src = fetchPypi {
19+
inherit pname version;
20+
sha256 = "sha256-vuDZ5yEhyDpCmkXoC+Gr2X5vMK5B46HnktcvBONjxXM=";
21+
};
22+
23+
doCheck = false;
24+
};
25+
26+
# new dependency for v5.3
27+
esp-idf-nvs-partition-gen = python3Packages.buildPythonPackage rec {
28+
pname = "esp_idf_nvs_partition_gen";
29+
version = "0.1.2";
30+
format = "pyproject";
31+
32+
src = fetchPypi {
33+
inherit pname version;
34+
sha256 = "sha256-HjW5RCKfy83LQgAs0tOW/f9LPVoLwHY1pyb6ar+AxwY=";
35+
};
36+
37+
propagatedBuildInputs = builtins.attrValues {
38+
inherit (python3Packages)
39+
setuptools
40+
cryptography;
41+
};
42+
43+
doCheck = false;
44+
};
45+
46+
esp-idf-esp32c3' = (esp-idf-esp32c3.overrideAttrs (old: {
47+
propagatedBuildInputs = builtins.filter (p: p != git) old.propagatedBuildInputs ++
48+
[
49+
pyclang
50+
esp-idf-nvs-partition-gen
51+
];
52+
patches = [ ];
53+
})
54+
).override {
55+
rev = version;
56+
sha256 = "sha256-w+xyva4t21STVtfYZOXY2xw6sDc2XvJXBZSx+wd1N6Y=";
57+
};
58+
in
59+
stdenvNoCC.mkDerivation {
60+
pname = "esp-idf-esp32c3-lib";
61+
version = version;
62+
63+
dontUnpack = true; # start with empt src
64+
dontConfigure = true;
65+
66+
# set environment variables for downstreams
67+
setupHook = writeText "setup-hook.sh" ''
68+
export ESP_IDF_DIR="${esp-idf-esp32c3'}"
69+
export ESP_IDF_LIB="$1"
70+
'';
71+
72+
buildInputs = [ esp-idf-esp32c3' git ];
73+
propagatedBuildInputs = [ esp-idf-esp32c3' ];
74+
75+
buildPhase = ''
76+
cp --no-preserve=mode,ownership -r "$IDF_PATH/examples/get-started/hello_world" ./
77+
idf.py -C hello_world set-target esp32c3
78+
idf.py -C hello_world build
79+
'';
80+
installPhase = ''
81+
mkdir -p $out/esp32c3/bootloader
82+
mv ./hello_world/build/esp-idf $out/esp32c3/
83+
mv ./hello_world/build/bootloader/esp-idf $out/esp32c3/bootloader
84+
mv ./hello_world/build/bootloader/config $out/esp32c3/bootloader
85+
'';
86+
}

fips202/keccakf1600.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Please refer to LowLevel.build for the exact list of other files it must be comb
3131
#define IS_BIG_ENDIAN 4321
3232
#define IS_LITTLE_ENDIAN 1234
3333

34-
#if defined(__arm__)
35-
# ifdef __BIG_ENDIAN
34+
#if defined(__arm__) || defined(__riscv)
35+
# if defined(__BIG_ENDIAN) || defined(__ORDER_BIG_ENDIAN__)
3636
# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
3737
# else
3838
# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
@@ -152,7 +152,7 @@ void KeccakP1600_AddLanes(KeccakP1600_plain32_state *state, const unsigned char
152152
| ((uint32_t)(laneAsBytes[5]) << 8)
153153
| ((uint32_t)(laneAsBytes[6]) << 16)
154154
| ((uint32_t)(laneAsBytes[7]) << 24);
155-
uint32_t even, odd, temp, temp0, temp1;
155+
uint32_t temp, temp0, temp1;
156156
uint32_t *stateAsHalfLanes = state->A;
157157
toBitInterleavingAndXOR(low, high, stateAsHalfLanes[lanePosition * 2 + 0], stateAsHalfLanes[lanePosition * 2 + 1], temp, temp0, temp1);
158158
}
@@ -203,7 +203,7 @@ static void KeccakP1600_ExtractLanes(const KeccakP1600_plain32_state *state, uns
203203
#else
204204
unsigned int lanePosition;
205205
for (lanePosition = 0; lanePosition < laneCount; lanePosition++) {
206-
uint32_t *stateAsHalfLanes = state->A;
206+
const uint32_t *stateAsHalfLanes = state->A;
207207
uint32_t low, high, temp, temp0, temp1;
208208
fromBitInterleaving(stateAsHalfLanes[lanePosition * 2], stateAsHalfLanes[lanePosition * 2 + 1], low, high, temp, temp0, temp1);
209209
uint8_t laneAsBytes[8];

flake.lock

+56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+28-17
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@
66
inputs = {
77
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
88

9+
esp-dev = {
10+
url = "github:mirrexagon/nixpkgs-esp-dev?rev=86a2bbe01fe0258887de7396af2a5eb0e37ac3be";
11+
inputs.nixpkgs.follows = "nixpkgs";
12+
};
13+
914
flake-parts = {
1015
url = "github:hercules-ci/flake-parts";
1116
inputs.nixpkgs-lib.follows = "nixpkgs";
1217
};
1318
};
1419

15-
outputs = inputs@{ flake-parts, ... }:
20+
outputs = inputs@{ flake-parts, esp-dev, ... }:
1621
flake-parts.lib.mkFlake { inherit inputs; } {
1722
imports = [ ];
1823
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
19-
perSystem = { pkgs, ... }:
24+
perSystem = { pkgs, system, ... }:
2025
let
21-
libopencm3 = pkgs.callPackage ./libopencm3.nix {
22-
targets = [ "stm32/f2" "stm32/f4" "stm32/f7" ];
23-
};
24-
mbed-os = pkgs.callPackage ./mbed-os.nix {
25-
targets = [ "TARGET_MPS2_M3" "TARGET_MPS2_M4" "TARGET_MPS2_M7" ];
26-
};
27-
2826
core = builtins.attrValues {
2927
astyle = pkgs.astyle.overrideAttrs (old: rec {
3028
version = "3.4.13";
@@ -52,12 +50,22 @@
5250
};
5351

5452
arm-pkgs = builtins.attrValues {
55-
libopencm3 = libopencm3;
56-
mbed-os = mbed-os;
53+
libopencm3 = pkgs.callPackage ./libopencm3.nix {
54+
targets = [ "stm32/f2" "stm32/f4" "stm32/f7" ];
55+
};
56+
57+
mbed-os = pkgs.callPackage ./mbed-os.nix {
58+
targets = [ "TARGET_MPS2_M3" "TARGET_MPS2_M4" "TARGET_MPS2_M7" ];
59+
};
60+
5761
inherit (pkgs)
5862
gcc-arm-embedded-13; # arm-gnu-toolchain-13.2.rel1
5963
};
6064

65+
riscv-pkgs = builtins.attrValues {
66+
esp-idf-lib = pkgs.callPackage ./esp-idf-lib.nix { };
67+
};
68+
6169
wrapShell = mkShell: attrs:
6270
mkShell (attrs // {
6371
shellHook = ''
@@ -66,23 +74,26 @@
6674
});
6775
in
6876
{
77+
_module.args = {
78+
pkgs = import inputs.nixpkgs {
79+
inherit system;
80+
overlays = [ esp-dev.overlays.default ];
81+
};
82+
};
83+
6984
devShells.default = wrapShell pkgs.mkShellNoCC {
70-
packages = core ++ arm-pkgs ++ builtins.attrValues {
85+
packages = core ++ arm-pkgs ++ riscv-pkgs ++ builtins.attrValues {
7186
inherit (pkgs)
7287
direnv
7388
nix-direnv
7489

7590
# debug dependencies
7691
openocd; # 0.12.0
7792
};
78-
OPENCM3_DIR = ''${libopencm3}'';
79-
MBED_OS_DIR = ''${mbed-os}'';
8093
};
8194

8295
devShells.ci = wrapShell pkgs.mkShellNoCC {
83-
packages = core ++ arm-pkgs;
84-
OPENCM3_DIR = ''${libopencm3}'';
85-
MBED_OS_DIR = ''${mbed-os}'';
96+
packages = core ++ arm-pkgs ++ riscv-pkgs;
8697
};
8798
};
8899
flake = {

hal/esp32-c3.ld

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*SPDX-License-Identifier: MIT*/
2+
MEMORY {
3+
irom (x): org = 0x42000000, len = 0x400000
4+
drom (r): org = 0x3C000000, len = 0x400000
5+
sram (rw): org = 0x3FC80000, len = 0x50000
6+
rtc_ram (rx): org = 0x50000000, len = 0x2000
7+
}
8+
9+
ENTRY(_start)
10+
11+
SECTIONS {
12+
.header (READONLY): AT(0)
13+
{
14+
_iflash_start = .;
15+
LONG(0xaedb041d)
16+
LONG(0xaedb041d)
17+
} > irom
18+
19+
.text.entry : {
20+
KEEP(*(.text.entry))
21+
} > irom
22+
23+
.text : {
24+
*(.text .stub .text.* .gnu.linkonce.t.*)
25+
} > irom
26+
27+
.iram : {
28+
*(.iram .iram*)
29+
} > irom
30+
31+
_iflash_end = .;
32+
_iflash_size = _iflash_end - _iflash_start;
33+
34+
_dflash_start = ORIGIN(drom) + _iflash_size;
35+
.rodata _dflash_start : AT(_iflash_size) {
36+
*(.rodata .rodata* .srodata .srodata* .sdata2 .sdata2* .gnu.linkonce.r.*)
37+
} > drom
38+
39+
_dflash_end = .;
40+
_dflash_size = _dflash_end - _dflash_start;
41+
42+
.data ORIGIN(sram) : AT(_iflash_size + _dflash_size) {
43+
_data_start = .;
44+
*(.data .data* .gnu.linkonce.r.* .riscv.*)
45+
} > sram
46+
47+
.sdata : {
48+
_sdata_start = .;
49+
*(.sdata .sdata.* .gnu.linkonce.s.*)
50+
} > sram
51+
_data_end = .;
52+
_data_lma_start = ORIGIN(drom) + LOADADDR(.data);
53+
54+
.bss : {
55+
_bss_start = .;
56+
*(.bss .bss* .sbss .sbss* COMMON)
57+
_bss_end = .;
58+
} > sram
59+
60+
__global_pointer$ = _sdata_start + 0x800;
61+
62+
.stack : {
63+
_stack_bottom = .;
64+
_stack_top = ORIGIN(sram) + LENGTH(sram);
65+
_stack_size_min = 0x4000;
66+
ASSERT(_stack_bottom + _stack_size_min < _stack_top, "Error: no space for stack");
67+
}
68+
}

0 commit comments

Comments
 (0)