Skip to content

Commit

Permalink
Merge pull request #35854 from mansnils:renode
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 295777715
Change-Id: I5561aee7f8367d45cf55a717213da38a94867d3d
  • Loading branch information
tensorflower-gardener committed Feb 18, 2020
2 parents d9c9c92 + fde6c13 commit e087187
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 3 deletions.
25 changes: 25 additions & 0 deletions tensorflow/lite/micro/stm32f4/debug_log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/micro/debug_log.h"

extern "C" void DebugLog(const char* s) {
asm("mov r0, #0x04\n" // SYS_WRITE0
"mov r1, %[str]\n"
"bkpt #0xAB\n"
:
: [ str ] "r"(s)
: "r0", "r1");
}
2 changes: 1 addition & 1 deletion tensorflow/lite/micro/testing/Dockerfile.bluepill
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
21 changes: 21 additions & 0 deletions tensorflow/lite/micro/testing/Dockerfile.stm32f4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# This docker configuration file lets you emulate a stm32f4 board
# on an x86 desktop or laptop, which can be useful for debugging and
# automated testing.
FROM antmicro/renode:latest

LABEL maintainer="Pete Warden <petewarden@google.com>"
2 changes: 1 addition & 1 deletion tensorflow/lite/micro/testing/bluepill.resc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
33 changes: 33 additions & 0 deletions tensorflow/lite/micro/testing/stm32f4.resc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

using sysbus

mach create
machine LoadPlatformDescription @platforms/cpus/stm32f4.repl

# These lines are needed to show the results of DebugLog calls in the output.
machine LoadPlatformDescriptionFromString "uartSemihosting: UART.SemihostingUart @ cpu"
showAnalyzer cpu.uartSemihosting Antmicro.Renode.Analyzers.LoggingUartAnalyzer

logFile @/tmp/renode_stm32f4_log.txt

macro reset
"""
sysbus LoadELF $bin
"""

runMacro $reset

23 changes: 23 additions & 0 deletions tensorflow/lite/micro/testing/stm32f4.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
*** Settings ***
Suite Setup Setup
Suite Teardown Teardown
Test Setup Reset Emulation
Resource /opt/renode/tests/renode-keywords.robot

*** Variables ***
${UART} sysbus.cpu.uartSemihosting

*** Test Cases ***
Should Run Stm32f4 Test
[Documentation] Runs a Stm32f4 test and waits for a specific string on the semihosting UART
[Tags] stm32f4 uart tensorflow arm
${BIN} = Get Environment Variable BIN
${SCRIPT} = Get Environment Variable SCRIPT
${EXPECTED} = Get Environment Variable EXPECTED
Execute Command $bin = @${BIN}
Execute Script ${SCRIPT}

Create Terminal Tester ${UART} timeout=30
Start Emulation

Wait For Line On Uart ${EXPECTED}
60 changes: 60 additions & 0 deletions tensorflow/lite/micro/testing/test_stm32f4_binary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash -e
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
# Tests a 'stm32f4' STM32F4 ELF by parsing the log output of Renode emulation.
#
# First argument is the ELF location.
# Second argument is a regular expression that's required to be in the output logs
# for the test to pass.
#
# This script must be run from the top-level folder of the tensorflow github
# repository as it mounts `pwd` to the renode docker image (via docker run -v)
# and paths in the docker run command assume the entire tensorflow repo is mounted.

declare -r ROOT_DIR=`pwd`
declare -r TEST_TMPDIR=/tmp/test_stm32f4_binary/
declare -r MICRO_LOG_PATH=${TEST_TMPDIR}
declare -r MICRO_LOG_FILENAME=${MICRO_LOG_PATH}/logs.txt
mkdir -p ${MICRO_LOG_PATH}

docker build -t renode_stm32f4 \
-f ${ROOT_DIR}/tensorflow/lite/micro/testing/Dockerfile.stm32f4 \
${ROOT_DIR}/tensorflow/lite/micro/testing/

exit_code=0
# running in `if` to avoid setting +e
if ! docker run \
--log-driver=none -a stdout -a stderr \
-v ${ROOT_DIR}:/workspace \
-v /tmp:/tmp \
-e BIN=/workspace/$1 \
-e SCRIPT=/workspace/tensorflow/lite/micro/testing/stm32f4.resc \
-e EXPECTED="$2" \
-it renode_stm32f4 \
/bin/bash -c "/opt/renode/tests/test.sh /workspace/tensorflow/lite/micro/testing/stm32f4.robot 2>&1 >${MICRO_LOG_FILENAME}"
then
exit_code=1
fi

echo "LOGS:"
cat ${MICRO_LOG_FILENAME}
if [ $exit_code -eq 0 ]
then
echo "$1: PASS"
else
echo "$1: FAIL - '$2' not found in logs."
fi
exit $exit_code
4 changes: 4 additions & 0 deletions tensorflow/lite/micro/tools/ci_build/test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ tensorflow/lite/micro/tools/ci_build/test_sparkfun.sh
echo "Running x86 tests at `date`"
tensorflow/lite/micro/tools/ci_build/test_x86.sh

# TODO(b/149597202): Disabled until we can get Docker running inside Docker.
#echo "Running stm32f4 tests at `date`"
#tensorflow/lite/micro/tools/ci_build/test_stm32f4.sh

echo "Finished all micro tests at `date`"
40 changes: 40 additions & 0 deletions tensorflow/lite/micro/tools/ci_build/test_stm32f4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
# Tests the microcontroller code for stm32f4

set -e

TARGET=stm32f4
TAGS=cmsis-nn
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR=${SCRIPT_DIR}/../../../../..
cd ${ROOT_DIR}
pwd

source tensorflow/lite/micro/tools/ci_build/helper_functions.sh

readable_run make -f tensorflow/lite/micro/tools/make/Makefile clean

# TODO(b/143715361): downloading first to allow for parallel builds.
readable_run make -f tensorflow/lite/micro/tools/make/Makefile TAGS=${TAGS} TARGET=${TARGET} third_party_downloads

# Build test binaries first
readable_run make -j8 -f tensorflow/lite/micro/tools/make/Makefile TAGS=${TAGS} TARGET=${TARGET} build

# Parallell builds doesn't work very well with this
readable_run make -f tensorflow/lite/micro/tools/make/Makefile TAGS=${TAGS} TARGET=${TARGET} test

7 changes: 6 additions & 1 deletion tensorflow/lite/micro/tools/make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ CC_PREFIX :=
# runtime that can be linked in to other programs.
MICROLITE_LIB_NAME := libtensorflow-microlite.a

MICRO_LITE_EXAMPLE_TESTS := $(wildcard tensorflow/lite/micro/examples/*/Makefile.inc)

MICROLITE_TEST_SRCS := \
$(wildcard tensorflow/lite/micro/*test.cc) \
$(wildcard tensorflow/lite/micro/kernels/*test.cc) \
Expand Down Expand Up @@ -242,7 +244,7 @@ CC := $(CC_PREFIX)${TARGET_TOOLCHAIN_PREFIX}${CC_TOOL}
AR := $(CC_PREFIX)${TARGET_TOOLCHAIN_PREFIX}${AR_TOOL}

# Load the examples.
include $(wildcard tensorflow/lite/micro/examples/*/Makefile.inc)
include $(MICRO_LITE_EXAMPLE_TESTS)

# Create rules for downloading third-party dependencies.
THIRD_PARTY_TARGETS :=
Expand Down Expand Up @@ -310,6 +312,9 @@ $(eval $(call microlite_test,kernel_$(notdir $(basename $(TEST_TARGET))),$(TEST_

test: $(MICROLITE_TEST_TARGETS)

# Just build the test targets
build: $(MICROLITE_BUILD_TARGETS)

generate_projects: $(ALL_PROJECT_TARGETS)

generate_non_kernel_projects: $(filter-out generate_kernel%,$(ALL_PROJECT_TARGETS))
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/micro/tools/make/helper_functions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ test_$(1): $$($(1)_BINARY)
$$(TEST_SCRIPT) $$($(1)_BINARY) '~~~ALL TESTS PASSED~~~'
ifneq (,$(findstring _test,$(1)))
MICROLITE_TEST_TARGETS += test_$(1)
MICROLITE_BUILD_TARGETS += $$($(1)_BINARY)
endif
$(eval $(call generate_microlite_projects,$(1),$(call specialize,$(2)),$(3)))
endef
Expand Down
102 changes: 102 additions & 0 deletions tensorflow/lite/micro/tools/make/targets/stm32f4/stm32f4.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* Copyright 2020 Google Inc. All Rights Reserved.

Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

/* Copied and modified from: tensorflow/lite/micro/tools/make/targets/bluepill/bluepill.lds

*/

/*
* 0x00000000 - 0x07ffffff - aliased to flash or sys memory depending on BOOT jumpers.
* 0x08000000 - 0x0801ffff - Flash.
* 0x1ffff000 - 0x1ffff7ff - Boot firmware in system memory.
* 0x1ffff800 - 0x1fffffff - Option bytes.
* 0x20000000 - 0x20004fff - SRAM.
* 0x40000000 - 0x40023400 - Peripherals
*/

/* Define main entry point */
ENTRY(_main)

/* 32K of RAM and 256K of FLASH */
MEMORY {
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K
}

/* Compute where the stack ends rather than hard coding it */
_ld_stack_end_addr = ORIGIN(RAM) + LENGTH(RAM);
_ld_min_stack_size = 0x200;

SECTIONS {

/* interrupt vector goes to top of flash */

.interrupt_vector : {
. = ALIGN(4);
KEEP(*(.interrupt_vector))
. = ALIGN(4);
} >FLASH

/* read only .text and .rodata go to flash */

.text : {
. = ALIGN(4);
KEEP(*(.text.interrupt_handler))
*(.text*)
} >FLASH

.rodata : {
. = ALIGN(4);
*(.rodata*)
. = ALIGN(4);
} >FLASH

/* read mwrite data needs to be stored in flash but copied to ram */
.data : {
. = ALIGN(4);
_ld_data_load_dest_start = .; /* export where to load from */
*(.data*)
. = ALIGN(4);
_ld_data_load_dest_stop = .; /* export where to load from */
} >RAM AT> FLASH
_ld_data_load_source = LOADADDR(.data);

/* unitialized data section needs zero initialization */
.bss :
{
. = ALIGN(4);
_ld_bss_data_start = .;
*(.bss*)
. = ALIGN(4);
_ld_bss_data_stop = .;
} >RAM

._user_heap_stack :
{
. = ALIGN(8);
. += _ld_min_stack_size;
PROVIDE(end = .);
. = ALIGN(8);
} >RAM

/DISCARD/ :
{
libc.a (*)
libm.a (*)
libgcc.a (*)
}

} /* SECTIONS */

0 comments on commit e087187

Please sign in to comment.