Skip to content

Commit 5789915

Browse files
authored
Merge pull request #28 from All-Your-Locks-Are-Belong-To-Us/feature/example-nrf
2 parents 35e7c62 + f7827ef commit 5789915

File tree

12 files changed

+120
-5
lines changed

12 files changed

+120
-5
lines changed

examples/measurements/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
include(../../cmake/linker-map.cmake)
22

3-
set(measurement_dependencies gpio.c)
4-
53
function(add_measurement name)
6-
add_executable(${name} "${name}.c" ${measurement_dependencies})
7-
add_linker_map_for_target(${name})
8-
target_link_libraries(${name} ${PRODUCT_NAME})
4+
add_executable(${name} "common/${name}.c" common/gpio.c)
5+
target_include_directories(${name} PRIVATE common/)
6+
add_linker_map_for_target(${name})
7+
target_link_libraries(${name} ${PRODUCT_NAME})
98
endfunction()
109

1110
add_measurement("aes_gcm_measure")

examples/measurements/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Measurements
2+
3+
This folder contains several programs to measure the energy and time it takes for the different algorithms to complete.
4+
5+
## Compiling for ATmega
6+
7+
The measurement programs are compiled automatically, when compiling the `libmicrofido2`.
8+
9+
## Compiling for nRF52
10+
11+
The examples must be compiled explicitly for the nRF52.
12+
13+
First, navigate to the [nrf52](./nrf52) folder.
14+
Then, make sure to select the algorithm you want to measure in the `CMakeLists.txt` by modifying the `set(MEASURE_ALGORITHM aes_gcm)`.
15+
Finally, the build procedure is similar to the one in the [nrf52 example](../nrf52/README.md).
16+
To initially build the program, execute: `sudo docker run --rm -v $PWD/../../../:/libmicrofido2 nrf52-sdk west -v build -b nrf52840dk_nrf52840 -d /libmicrofido2/examples/measurements/nrf52/build /libmicrofido2/examples/measurements/nrf52`.
17+
To build the program and flash it, execute: `docker run --rm -v $PWD/../../../:/libmicrofido2 --privileged --device=/dev/ttyACM? nrf52-sdk west flash -d /libmicrofido2/examples/measurements/nrf52/build`.
File renamed without changes.
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ void delay(double ms) {
3131
}
3232
}
3333

34+
#elif defined(__ZEPHYR__)
35+
36+
#include <zephyr.h>
37+
#include <zephyr/kernel.h>
38+
#include <zephyr/drivers/gpio.h>
39+
40+
static struct device* dev;
41+
42+
int setup_pin() {
43+
dev = device_get_binding("GPIO_0");
44+
gpio_pin_configure(dev, 27, GPIO_OUTPUT);
45+
}
46+
47+
void pin_on() {
48+
gpio_pin_set(dev, 27, 1);
49+
}
50+
51+
void pin_off() {
52+
gpio_pin_set(dev, 27, 0);
53+
}
54+
55+
void delay(double ms) {
56+
k_sleep(K_MSEC(ms));
57+
}
58+
3459
#else
3560
// To make the examples compile, default to NOOP.
3661
#warning GPIO does not work on this system, defaulting to nop
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Set this to one of the following: aes_gcm, ed25519, inflate, sha256, sha512
2+
set(measure_algorithm aes_gcm)
3+
4+
#######################################
5+
# General
6+
cmake_minimum_required(VERSION 3.20.0)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(nrf52-libmicrofido C)
10+
11+
target_sources(app PRIVATE "src/${measure_algorithm}_measure.c" src/gpio.c)
12+
target_include_directories(app PRIVATE src/)
13+
14+
#######################################
15+
# libmicrofido2
16+
17+
# Necessary for building libmicrofido2
18+
include(ExternalProject)
19+
20+
# Extract the Zephyr compilation settings.
21+
zephyr_get_include_directories_for_lang_as_string( C includes)
22+
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
23+
zephyr_get_compile_definitions_for_lang_as_string( C definitions)
24+
zephyr_get_compile_options_for_lang_as_string( C options)
25+
26+
set(external_project_cflags
27+
"${includes} ${definitions} ${options} ${system_includes} --specs=nosys.specs"
28+
)
29+
30+
# Directory settings.
31+
set(libmicrofido2_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../../)
32+
set(libmicrofido2_build_dir ${CMAKE_CURRENT_BINARY_DIR}/libmicrofido2)
33+
34+
set(LIBMICROFIDO2_LIB_DIR ${libmicrofido2_build_dir})
35+
set(LIBMICROFIDO2_INCLUDE_DIR ${libmicrofido2_src_dir}/include)
36+
37+
ExternalProject_Add(
38+
libmicrofido2_project # Name for custom target
39+
PREFIX ${libmicrofido2_build_dir} # Root directory for the lib.
40+
SOURCE_DIR ${libmicrofido2_src_dir}
41+
BINARY_DIR ${libmicrofido2_build_dir}
42+
CMAKE_ARGS # Pass the necessary build flags.
43+
-DCMAKE_C_FLAGS=${external_project_cflags}
44+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
45+
-DCMAKE_AR=${CMAKE_AR}
46+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
47+
INSTALL_COMMAND "" # No installation necessary, just build it.
48+
BUILD_BYPRODUCTS ${LIBMICROFIDO2_LIB_DIR}/libmicrofido2.a
49+
)
50+
51+
# Create a wrapper CMake library that our app can link with.
52+
add_library(libmicrofido2_lib STATIC IMPORTED GLOBAL)
53+
add_dependencies(
54+
libmicrofido2_lib
55+
libmicrofido2_project
56+
)
57+
set_target_properties(libmicrofido2_lib PROPERTIES IMPORTED_LOCATION ${LIBMICROFIDO2_LIB_DIR}/libmicrofido2.a)
58+
file(MAKE_DIRECTORY ${LIBMICROFIDO2_INCLUDE_DIR}) # Hack to make the line below work on the first download.
59+
set_target_properties(libmicrofido2_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LIBMICROFIDO2_INCLUDE_DIR} )
60+
target_include_directories(libmicrofido2_lib INTERFACE ${libmicrofido2_src_dir}/external/tinf/include)
61+
62+
# Link with libmicrofido2
63+
target_link_libraries(app PUBLIC libmicrofido2_lib)

0 commit comments

Comments
 (0)