Browse Source

Initial commit

Signed-off-by: Magic_RB <magic_rb@redalder.org>
master
Magic_RB 1 year ago
commit
aaafcfc479
Signed by: Magic_RB
GPG Key ID: 08D5287CC5DDCA0E
  1. 1
      .ccls
  2. 1
      .envrc
  3. 106
      .gitignore
  4. 11
      cross.txt
  5. 38
      flake.lock
  6. 53
      flake.nix
  7. 45
      meson.build
  8. 48
      nix/xtensa-elf-binutils.nix
  9. 111
      nix/xtensa-elf-gcc.nix
  10. 65
      src/linker.ld
  11. 49
      src/main.c

1
.ccls

@ -0,0 +1 @@ @@ -0,0 +1 @@
xtensa-elf-gcc

1
.envrc

@ -0,0 +1 @@ @@ -0,0 +1 @@
use flake

106
.gitignore vendored

@ -0,0 +1,106 @@ @@ -0,0 +1,106 @@
# Created by https://www.toptal.com/developers/gitignore/api/meson,emacs,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=meson,emacs,vim
### Emacs ###
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
ltximg/**
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
*.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data
### Meson ###
# subproject directories
/subprojects/*
!/subprojects/*.wrap
# Meson Directories
meson-logs
meson-private
# Meson Files
meson_benchmark_setup.dat
meson_test_setup.dat
sanitycheckcpp.cc # C++ specific
sanitycheckcpp.exe # C++ specific
# Ninja
build.ninja
.ninja_deps
.ninja_logs
# Misc
compile_commands.json
### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
# End of https://www.toptal.com/developers/gitignore/api/meson,emacs,vim
result
builddir
.ccls-cache
.direnv

11
cross.txt

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
[binaries]
c = 'xtensa-esp32-elf-gcc'
cpp = 'xtensa-esp32-elf-g++'
ar = 'xtensa-esp32-elf-ar'
strip = 'xtensa-esp32-elf-strip'
[target_machine]
system = 'bare-metal'
cpu_family = 'xtensa-esp32'
cpu = 'lx6'
endian = 'little'

38
flake.lock

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1620332858,
"narHash": "sha256-ZoOfRdndJhX4QQumeUOYXynTuI8GzkSs9bh451Vezak=",
"path": "/nix/store/fpnasrzmnxgqw5h2raprm41sinq5lxmb-source",
"rev": "22612485a469d71df09b9434842767b1f4f2c063",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs-unstable": {
"locked": {
"lastModified": 1620340338,
"narHash": "sha256-Op/4K0+Z9Sp5jtFH0s/zMM4H7VFZxrekcAmjQ6JpQ4w=",
"path": "/nix/store/mzi2dvks0a52mmbhgxc43nlxa4pi6r7p-source",
"rev": "63586475587d7e0e078291ad4b49b6f6a6885100",
"type": "path"
},
"original": {
"id": "nixpkgs-unstable",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"nixpkgs-unstable": "nixpkgs-unstable"
}
}
},
"root": "root",
"version": 7
}

53
flake.nix

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
{
inputs.nixpkgs.url = "nixpkgs";
inputs.nixpkgs-unstable.url = "nixpkgs-unstable";
outputs = { self, nixpkgs, nixpkgs-unstable, ... }:
let
supportedSystems = [ "x86_64-linux" "i686-linux" "aarch64-linux" ];
forAllSystems' = systems: f: nixpkgs.lib.genAttrs systems f;
forAllSystems = forAllSystems' supportedSystems;
in
{
overlay = prev: final:
{
xtensa-elf-binutils = prev.callPackage ./nix/xtensa-elf-binutils.nix {};
xtensa-elf-gcc = prev.callPackage ./nix/xtensa-elf-gcc.nix {};
};
defaultPackage = forAllSystems (system:
let
pkgs = import nixpkgs
{ inherit system;
overlays = [ self.overlay ];
};
in
pkgs.xtensa-elf-gcc
);
packages = forAllSystems (system:
import nixpkgs
{ inherit system;
overlays = [ self.overlay ];
}
);
devShell = forAllSystems (system:
let
pkgs = import nixpkgs
{ inherit system;
overlays = [ self.overlay ];
};
in
pkgs.mkShell {
buildInputs = with pkgs; [
xtensa-elf-gcc
xtensa-elf-binutils
(import nixpkgs-unstable { inherit system; }).esptool
qemu
meson
ninja
];
}
);
};
}

45
meson.build

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
project('ossw-fw', 'c')
linker_script = [ '-T', meson.source_root() + '/src/linker.ld' ]
xtensa_args = [
'-mlongcalls',
'-mtext-section-literals',
'-fstrict-volatile-bitfields',
'-fno-strict-aliasing',
'-fdata-sections',
'-ffunction-sections'
]
ossw_fw_sources = [
'src/main.c',
]
ossw_fw = executable (
'ossw-fw',
ossw_fw_sources,
c_args: [ '-g', '-Wall', '-Wextra', '-nostdlib', xtensa_args ],
link_args: [ '-g', '-nostdlib', linker_script ],
install: false,
)
esptool = find_program('esptool.py', 'esptool')
image = custom_target (
'image',
input: ossw_fw,
output: 'ossw-fw.bin',
command: [
esptool,
'--chip', 'esp32',
'elf2image',
'--flash_mode=dio',
'--flash_freq', '40m',
'--flash_size', '4MB',
'-o', '@OUTPUT@',
'@INPUT@'
],
install: false,
)

48
nix/xtensa-elf-binutils.nix

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
{ lib
, stdenv
, libelf
}:
let
version = "2.35";
target = "xtensa-esp32-elf";
xtensa-overlay-commit = "4d8c98d";
xtensa-overlay = fetchTarball {
url = "https://codeload.github.com/espressif/xtensa-overlays/tar.gz/${xtensa-overlay-commit}";
sha256 = "sha256:16z9028mxfbzy4rmhcldi9rdlhi7lmcdxjhr630m0kr447gq3820";
};
in
with lib; stdenv.mkDerivation {
src = fetchTarball {
url = "https://ftp.gnu.org/gnu/binutils/binutils-${version}.tar.bz2";
sha256 = "sha256:0jk31l6w6bd2x067hcqa8zjvx202f85kk5khkidy6pig0p3yx8a8";
};
inherit version;
name = "${target}-binutils";
buildInputs = [
libelf
];
enableParallelBuilding = true;
preConfigure = ''
cp -r ${xtensa-overlay}/xtensa_esp32/binutils/* .
'';
configureFlags = [
"--target=${target}"
"--enable-multilib"
"--enable-interwork"
"--with-gnu-as"
"--with-gnu-ld"
"--with-sysroot"
"--disable-nls"
"--enable-ld=yes"
"--enable-gold"
"--enable-plugins"
"--enable-deterministic-archives"
];
}

111
nix/xtensa-elf-gcc.nix

@ -0,0 +1,111 @@ @@ -0,0 +1,111 @@
{ lib
, stdenv
, gmp, mpfr, libmpc
, isl, zlib, libelf
, xtensa-elf-binutils
, newlib ? null
}:
let
version = "10.2.0";
target = "xtensa-esp32-elf";
xtensa-overlay-commit = "4d8c98d";
xtensa-overlay = fetchTarball {
url = "https://codeload.github.com/espressif/xtensa-overlays/tar.gz/${xtensa-overlay-commit}";
sha256 = "sha256:16z9028mxfbzy4rmhcldi9rdlhi7lmcdxjhr630m0kr447gq3820";
};
in
with lib; stdenv.mkDerivation {
src = fetchTarball {
url = "https://gcc.gnu.org/pub/gcc/releases/gcc-${version}/gcc-${version}.tar.xz";
sha256 = "sha256:0l1n916az5ygp3jamrd6qj0j5kq3nfl8n79rvj94g3rj85jdpi64";
};
inherit version;
name = "${target}-gcc";
buildInputs = [
gmp mpfr libmpc
isl zlib
xtensa-elf-binutils
];
hardeningDisable = [ "format" "pie" ];
enableParallelBuilding = true;
configureFlags = [
"--target=${target}"
"--with-as=${xtensa-elf-binutils}/bin/${target}-as"
"--with-gnu-as"
"--with-ld=${xtensa-elf-binutils}/bin/${target}-ld"
"--with-gnu-ld"
"--with-host-libstdcxx=-static-libgcc,-Wl,-Bstatic,-lstdc++,-Bdynamic,-lm"
"--with-gmp-include=${gmp.dev}/include"
"--with-gmp-lib=${gmp.out}/lib"
"--with-mpfr-include=${mpfr.dev}/include"
"--with-mpfr-lib=${mpfr.out}/lib"
"--with-mpc=${libmpc}"
"--with-isl=${isl}"
"--with-libelf=${libelf}"
"--with-native-system-header-dir=/include"
"--with-newlib"
"--without-headers"
"--with-headers=$out/include"
"--with-sysroot=$out"
"--with-system-zlib"
"--without-libffi"
"--disable-__cxa_atexit"
"--disable-decimal-float"
"--disable-libgomp"
"--disable-libmpx"
"--disable-libmudflap"
"--disable-libquadmath"
"--disable-libquadmath-support"
"--disable-libssp"
"--disable-libstdcxx-pch"
"--disable-libstdcxx-verbose"
"--disable-nls"
"--disable-shared"
"--disable-threads"
"--disable-tls"
"--enable-gnu-indirect-function"
"--enable-languages=c,c++"
"--enable-lto"
"--enable-target-optspace"
# --with-python-dir=share/gcc-$_target \
# --with-sysroot=/usr/$_target \
# --with-native-system-header-dir=/include \
# --with-headers=/usr/$_target/include \
# --libexecdir=/usr/lib \
# --prefix=/usr \
];
preConfigure = ''
cp -r ${xtensa-overlay}/xtensa_esp32/gcc/* .
mkdir -p ./build && cd ./build
'';
configureScript = "../configure";
preBuild = ''
mkdir -p ./build && cd ./build
'';
buildPhase = ''
export CFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections -mlongcalls'
export CXXFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections -mlongcalls'
make all-gcc
make all-target-libgcc
'';
installPhase = ''
make install-gcc
make install-target-libgcc
'';
}

65
src/linker.ld

@ -0,0 +1,65 @@ @@ -0,0 +1,65 @@
/*
* GNU linker script for Espressif ESP32
*/
/* Default entry point */
ENTRY( call_start_cpu0 );
/* Specify main memory areas */
MEMORY
{
/* Use values from the ESP-IDF 'bootloader' component.
/* TODO: Use human-readable lengths */
/* TODO: Use the full memory map - this is just a test */
iram_seg ( RX ) : ORIGIN = 0x40080400, len = 0xFC00
dram_seg ( RW ) : ORIGIN = 0x3FFF0000, len = 0x1000
}
/* Define output sections */
SECTIONS {
/* The program code and other data goes into Instruction RAM */
.iram.text :
{
. = ALIGN(16);
KEEP(*(.entry.text))
*(.text)
*(.text*)
KEEP (*(.init))
KEEP (*(.fini))
*(.rodata)
*(.rodata*)
. = ALIGN(4);
_etext = .;
} >iram_seg
/* Initialized data goes into Data RAM */
_sidata = .;
.data : AT(_sidata)
{
. = ALIGN(4);
_sdata = .;
*(.data)
*(.data*)
. = ALIGN(4);
_edata = .;
} >dram_seg
/* Uninitialized data also goes into Data RAM */
.bss :
{
. = ALIGN(4);
_sbss = .;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >dram_seg
. = ALIGN(4);
PROVIDE ( end = . );
PROVIDE ( _end = . );
}

49
src/main.c

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
void *memset(void *s, int c, unsigned int len)
{
unsigned char* p=s;
while(len--)
{
*p++ = (unsigned char)c;
}
return s;
}
void *memmove(void *dest, void *src, int n)
{
// Typecast src and dest addresses to (char *)
char *csrc = (char *)src;
char *cdest = (char *)dest;
// Copy contents of src[] to dest[]
for (int i = 0; i < n; i++)
cdest[i] = csrc[i];
return dest;
}
void *memcpy(void *dest, void *src, int n)
{
return memmove(dest, src, n);
}
extern unsigned int _sbss, _ebss, _sidata, _sdata, _edata;
static volatile int sisyphus = 0;
int main(void) {
// Increment a variable.
while (1) {
++sisyphus;
}
return 0;
}
// Startup logic; this is the application entry point.
void __attribute__((noreturn)) call_start_cpu0() {
// Clear BSS.
memset(&_sbss, 0, (&_ebss - &_sbss) * sizeof(_sbss));
// Copy initialized data.
memmove(&_sdata, &_sidata, (&_edata - &_sdata) * sizeof(_sdata));
// Done, branch to main
main();
// (Should never be reached)
while (1) {
}
}
Loading…
Cancel
Save