Skip to content

Commit 4b5747e

Browse files
Initial commit
0 parents  commit 4b5747e

File tree

7 files changed

+264
-0
lines changed

7 files changed

+264
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.build
2+
.cache
3+
.packages
4+
.pio
5+
.vscode
6+
.DS_Store
7+
compile_commands*
8+
due
9+
qemu

Makefile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.DEFAULT_GOAL := all
2+
dir_guard=@mkdir -p $(@D)
3+
4+
ifeq ($(DEBUG),1)
5+
opt_flags := -Og -g2 -ggdb2
6+
else
7+
opt_flags := -Os -g0 -ffunction-sections -fdata-sections
8+
endif
9+
10+
CFLAGS := -std=gnu17 -nostdlib $(opt_flags)
11+
CXXFLAGS := -std=gnu++17 -nostdlib $(opt_flags)
12+
13+
# QEMU doesn't support any ez-clang lib yet
14+
qemu/include/:
15+
$(dir_guard)
16+
qemu/lib/:
17+
$(dir_guard)
18+
19+
include_dirs := src include
20+
includes := $(addprefix -I,$(include_dirs))
21+
22+
due_flags := -mcpu=cortex-m3 -mthumb --config armv7m_soft_nofp_nosys
23+
due_defs := -D__SAM3X8E__ -DARDUINO_SAM_DUE -DARDUINO=10805 -DARDUINO_ARCH_SAM
24+
due_defs += -DF_CPU=84000000L -DUSBCON -DUSB_VID=0x2341 -DUSB_PID=0x003E
25+
26+
.build/due/ez/stdio/printf.cc.o: src/ez/stdio/printf.cc
27+
$(dir_guard)
28+
$(CXX) $(CXXFLAGS) $(due_flags) $(due_defs) $(includes) -c $< -o $@
29+
30+
due/lib/ez/stdio/printf.a: .build/due/ez/stdio/printf.cc.o
31+
$(dir_guard)
32+
$(AR) cr $@ $<
33+
34+
ifdef ARDUINO_DIR
35+
arduino_include_subdirs := cores/arduino variants/arduino_due_x
36+
arduino_include_subdirs += system/libsam system/CMSIS/CMSIS/Include system/CMSIS/Device/ATMEL
37+
arduino_includes := $(addprefix -I$(ARDUINO_DIR)/,$(arduino_include_subdirs))
38+
39+
arduino_c := $(shell find $(ARDUINO_DIR)/cores/arduino -name '*.c')
40+
arduino_cpp := $(shell find $(ARDUINO_DIR)/cores/arduino -name '*.cpp')
41+
arduino_cpp += $(ARDUINO_DIR)/variants/arduino_due_x/variant.cpp
42+
43+
arduino_c_o := $(arduino_c:$(ARDUINO_DIR)/%=.build/due/ez/framework/%.o)
44+
arduino_cpp_o := $(arduino_cpp:$(ARDUINO_DIR)/%=.build/due/ez/framework/%.o)
45+
endif
46+
47+
.build/due/ez/framework/%.c.o: $(ARDUINO_DIR)/%.c
48+
$(dir_guard)
49+
$(CC) $(CFLAGS) $(due_flags) $(due_defs) $(arduino_includes) -c $< -o $@
50+
51+
.build/due/ez/framework/%.cpp.o: $(ARDUINO_DIR)/%.cpp
52+
$(dir_guard)
53+
$(CXX) $(CXXFLAGS) $(due_flags) $(due_defs) $(arduino_includes) -c $< -o $@
54+
55+
due/lib/ez/framework/Arduino.a: $(arduino_cpp_o) $(arduino_c_o)
56+
$(dir_guard)
57+
$(AR) cr $@ $(arduino_cpp_o) $(arduino_c_o)
58+
59+
due/lib/: due/lib/ez/framework/Arduino.a due/lib/ez/stdio/printf.a
60+
61+
due/include/:
62+
$(dir_guard)
63+
cp -r include due/.
64+
mkdir -p due/include/ez/framework
65+
cp $(ARDUINO_DIR)/cores/arduino/Arduino.h due/include/ez/framework/
66+
67+
.PHONY: due
68+
due: due/include/ due/lib/
69+
70+
.PHONY: qemu
71+
qemu: qemu/include/ qemu/lib/
72+
73+
.PHONY: all
74+
all: due qemu
75+
76+
.PHONY: clean
77+
clean:
78+
rm -rf .build
79+
rm -rf due
80+
rm -rf qemu

build.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
if [ ! -d ".packages" ]; then
4+
echo "Downloading dependencies.."
5+
./setup.sh
6+
fi
7+
8+
export ARDUINO_DIR=$(pwd)/.packages/framework-arduino-sam
9+
export PATH=$PATH:$(pwd)/.packages/toolchain-clang-arm/bin
10+
export AR=llvm-ar
11+
export CC=clang
12+
export CXX=clang++
13+
14+
mkdir -p due
15+
mkdir -p qemu
16+
17+
make
18+
#make SHELL='sh -x'
19+
20+
function dump() {
21+
for f in "$@"; do echo " $f"; done
22+
}
23+
24+
echo "Arduino Due"
25+
echo "- archives:"
26+
dump $(find due -name '*.a')
27+
echo "- headers:"
28+
dump $(find due -name '*.h')
29+
30+
echo "LM3S811 (QEMU)"
31+
echo "- archives:"
32+
dump $(find qemu -name '*.a')
33+
echo "- headers:"
34+
dump $(find qemu -name '*.h')

include/ez/fixpoint/mapRange.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef EZ_FIXPOINT_MAPRANGE_H
2+
#define EZ_FIXPOINT_MAPRANGE_H
3+
4+
#include <cstdint>
5+
6+
namespace {
7+
8+
template <uint32_t Value>
9+
constexpr uint32_t max_shift() {
10+
static_assert((Value & 0xFF000000) == 0, "Not enough free leading bits");
11+
if constexpr (((Value << 4) & 0x80000000) != 0)
12+
return 4;
13+
if constexpr (((Value << 5) & 0x80000000) != 0)
14+
return 5;
15+
if constexpr (((Value << 6) & 0x80000000) != 0)
16+
return 6;
17+
if constexpr (((Value << 7) & 0x80000000) != 0)
18+
return 7;
19+
if constexpr (((Value << 8) & 0x80000000) != 0)
20+
return 8;
21+
if constexpr (((Value << 9) & 0x80000000) != 0)
22+
return 9;
23+
if constexpr (((Value << 10) & 0x80000000) != 0)
24+
return 10;
25+
if constexpr (((Value << 11) & 0x80000000) != 0)
26+
return 11;
27+
if constexpr (((Value << 12) & 0x80000000) != 0)
28+
return 12;
29+
if constexpr (((Value << 13) & 0x80000000) != 0)
30+
return 13;
31+
if constexpr (((Value << 14) & 0x80000000) != 0)
32+
return 14;
33+
if constexpr (((Value << 15) & 0x80000000) != 0)
34+
return 15;
35+
if constexpr (((Value << 16) & 0x80000000) != 0)
36+
return 16;
37+
if constexpr (((Value << 17) & 0x80000000) != 0)
38+
return 17;
39+
if constexpr (((Value << 18) & 0x80000000) != 0)
40+
return 18;
41+
if constexpr (((Value << 19) & 0x80000000) != 0)
42+
return 19;
43+
if constexpr (((Value << 20) & 0x80000000) != 0)
44+
return 20;
45+
if constexpr (((Value << 21) & 0x80000000) != 0)
46+
return 21;
47+
if constexpr (((Value << 22) & 0x80000000) != 0)
48+
return 22;
49+
if constexpr (((Value << 23) & 0x80000000) != 0)
50+
return 23;
51+
if constexpr (((Value << 24) & 0x80000000) != 0)
52+
return 24;
53+
if constexpr (((Value << 25) & 0x80000000) != 0)
54+
return 25;
55+
if constexpr (((Value << 26) & 0x80000000) != 0)
56+
return 26;
57+
if constexpr (((Value << 27) & 0x80000000) != 0)
58+
return 27;
59+
if constexpr (((Value << 28) & 0x80000000) != 0)
60+
return 28;
61+
if constexpr (((Value << 29) & 0x80000000) != 0)
62+
return 29;
63+
if constexpr (((Value << 30) & 0x80000000) != 0)
64+
return 30;
65+
if constexpr (((Value << 31) & 0x80000000) != 0)
66+
return 31;
67+
static_assert(Value > 0, "Zero is not a valid max value");
68+
}
69+
70+
template <uint32_t Value1, uint32_t Value2>
71+
constexpr uint32_t min_value() {
72+
return Value1 < Value2 ? Value1 : Value2;
73+
}
74+
75+
}
76+
77+
namespace ez {
78+
79+
template <uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max>
80+
uint32_t mapRange(uint32_t x) {
81+
constexpr uint32_t shift = min_value<max_shift<in_max + 1>(),
82+
max_shift<out_max + 1>()>();
83+
constexpr uint32_t factor =
84+
((out_max - out_min + 1) << shift) / (in_max - in_min + 1);
85+
return (((x - in_min) * factor) >> shift) + out_min;
86+
}
87+
88+
}
89+
90+
#endif // EZ_FIXPOINT_MAPRANGE_H

include/ez/stdio/printf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef EZ_STDIO_PRINTF_H
2+
#define EZ_STDIO_PRINTF_H
3+
4+
namespace ez {
5+
6+
void printf(const char* fmt, ...);
7+
8+
}
9+
10+
#endif // EZ_STDIO_PRINTF_H

setup.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
mkdir -p .packages
5+
6+
if [ ! -d ".packages/toolchain-clang-arm" ]; then
7+
wget -q --show-progress -O .packages/toolchain-clang-arm-13.tar.gz https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm/releases/download/release-13.0.0/LLVMEmbeddedToolchainForArm-13.0.0-linux.tar.gz
8+
tar zxf .packages/toolchain-clang-arm-13.tar.gz -C .packages
9+
mv .packages/LLVMEmbeddedToolchainForArm-13.0.0 .packages/toolchain-clang-arm
10+
fi
11+
12+
if [ ! -d ".packages/framework-arduino-sam" ]; then
13+
wget -q --show-progress -O .packages/framework-arduino-sam-clang.tar.gz https://github.com/echtzeit-dev/framework-arduino-sam/archive/refs/tags/ez-clang-0.0.5.tar.gz
14+
tar zxf .packages/framework-arduino-sam-clang.tar.gz -C .packages
15+
mv .packages/framework-arduino-sam-ez-clang-0.0.5 .packages/framework-arduino-sam
16+
fi

src/ez/stdio/printf.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "ez/stdio/printf.h"
2+
3+
#include <cstdarg>
4+
#include <cstddef>
5+
#include <cstdio>
6+
#include <cstring>
7+
8+
extern "C" {
9+
char *__ez_clang_inline_heap_acquire(size_t);
10+
void __ez_clang_report_string(const char *Data, size_t Size);
11+
}
12+
13+
namespace ez {
14+
15+
void printf(const char* Fmt, ...) {
16+
va_list Args;
17+
va_start(Args, Fmt);
18+
size_t Capacity = strlen(Fmt) + 20;
19+
char *Buffer = __ez_clang_inline_heap_acquire(Capacity);
20+
int Length = vsnprintf(Buffer, Capacity, Fmt, Args);
21+
__ez_clang_report_string(Buffer, Length);
22+
va_end(Args);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)