We now have a Kotlin program to blink a LED on stm32f4_disco.

This commit is contained in:
Alexander Gorshenev
2018-02-16 19:42:04 +03:00
committed by alexander-gorshenev
parent c4111a836e
commit 0de9b3a9b6
11 changed files with 137 additions and 97 deletions
@@ -120,10 +120,10 @@ internal class LinkStage(val context: Context) {
hostLlvmTool("llvm-link", "-o", combinedBc, *bitcodeFiles.toTypedArray())
val optimizedBc = temporary("optimized", ".bc")
hostLlvmTool("opt", combinedBc, "-o=$optimizedBc", "-O3")
hostLlvmTool("opt", combinedBc, "-o=$optimizedBc", "-O3", "-internalize", "-globaldce")
val combinedO = temporary("combined", ".o")
hostLlvmTool("llc", combinedBc, "-filetype=obj", "-o", combinedO, "-function-sections", "-data-sections")
hostLlvmTool("llc", optimizedBc, "-filetype=obj", "-o", combinedO, "-function-sections", "-data-sections")
return combinedO
}
-30
View File
@@ -444,35 +444,5 @@ extern "C" {
RUNTIME_USED void Konan_abort(const char*) {
while(1) {}
}
/* Support the alias for the __aeabi_memset which may
assume memory alignment. */
RUNTIME_USED void __aeabi_memset4 (void *dest, size_t n, int c)
__attribute__ ((alias ("__aeabi_memset")));
RUNTIME_USED void __aeabi_memset8 (void *dest, size_t n, int c)
__attribute__ ((alias ("__aeabi_memset")));
/* Support the routine __aeabi_memset. Can't alias to memset
because it's not defined in the same translation unit. */
RUNTIME_USED void __aeabi_memset (void *dest, size_t n, int c)
{
/*Note that relative to ANSI memset, __aeabi_memset hase the order
of its second and third arguments reversed. */
memset (dest, c, n);
}
/* Support the alias for the __aeabi_memclr which may
assume memory alignment. */
RUNTIME_USED void __aeabi_memclr4 (void *dest, size_t n)
__attribute__ ((alias ("__aeabi_memclr")));
RUNTIME_USED void __aeabi_memclr8 (void *dest, size_t n)
__attribute__ ((alias ("__aeabi_memclr")));
/* Support the routine __aeabi_memclr. */
RUNTIME_USED void __aeabi_memclr (void *dest, size_t n)
{
__aeabi_memset (dest, n, 0);
}
#endif // KONAN_ZEPHYR
}
+7 -1
View File
@@ -1,4 +1,10 @@
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE)
target_sources(app PRIVATE build/program.o src/main.c src/blinky.c)
add_library(libsupcpp STATIC IMPORTED)
set_property(TARGET libsupcpp PROPERTY IMPORTED_LOCATION $ENV{GCCARMEMB_TOOLCHAIN_PATH}/arm-none-eabi/lib/thumb/libsupc++.a )
target_link_libraries(app libsupcpp)
target_sources(app PRIVATE src/empty.c build/program.o )
+23 -2
View File
@@ -22,14 +22,35 @@ fi
GCC_ARM="$KONAN_DEPS/$TOOLCHAIN"
rm -rf $DIR/build || exit 1
mkdir -p $DIR/build && cd $DIR/build
konanc $DIR/src/main.kt -target zephyr_$BOARD -linkerOpts -L$GCC_ARM/arm-none-eabi/lib/thumb -linkerOpts -lsupc++ -opt || exit 1
export ZEPHYR_GCC_VARIANT=gccarmemb
export GCCARMEMB_TOOLCHAIN_PATH=$GCC_ARM
# This is a kind of forward declaration for `program.o`. See below.
touch program.o
[ -f CMakeCache.txt ] || cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DBOARD=$BOARD .. || exit 1
make zephyr
. $DIR/c_interop/platforms/$BOARD.sh
# We need generated headers to be consumed by `cinterop`,
# so we preconfigure the project with `make zephyr`.
# But `program.o` should exist at that stage to be detected by the configuration tools.
# So we create an empty `program.o` above, and then override it with the compiler result below.
# TODO: do something with this mess.
rm -f program.o
konanc $DIR/src/main.kt \
-target zephyr_$BOARD \
-r $DIR/c_interop/platforms/build \
-l $BOARD \
-opt -g -o program || exit 1
make || exit 1
# make flash
@@ -0,0 +1,30 @@
# Copyright 2010-2018 JetBrains s.r.o.
#
# 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.
# Keep this one board independent.
headers = autoconf.h zephyr.h board.h device.h gpio.h
compilerOpts = -DKERNEL -DUSE_FULL_LL_DRIVER -DUSE_HAL_DRIVER -D_FORTIFY_SOURCE=2 -D__ZEPHYR__=1 -ffreestanding -std=c99 -nostdinc
---
// cinterop doesn't support `#define FOO "string"` constructs as of yet.
// so we provide this accessor for now. Because the port names
// are resolved to string literals.
extern const char* led0_gpio_port(void) {
return LED0_GPIO_PORT;
}
@@ -0,0 +1,46 @@
# Copyright 2010-2017 JetBrains s.r.o.
#
# 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 is board specific.
if [ x"$BOARD" == x ]; then
echo "This script is to be included by build.sh"
exit 0
fi
# TODO: we need a robust automated way to ask Zephyr for
# all these $ZEPHYR_BASE based paths and the proper defines.
# TODO: The D flag in `-copt -Dxxx` sequence is interpreted to be a jvm property,
# so we workaround the using -Xclang.
cinterop -def $DIR/c_interop/platforms/$BOARD.def \
-pkg platform.zephyr.$BOARD \
-copt '-Xclang -DSTM32F407xx' \
-copt -I$ZEPHYR_BASE/kernel/include \
-copt -I$ZEPHYR_BASE/arch/arm/include \
-copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/stm32f4 \
-copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/stm32f4/include \
-copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/include \
-copt -I$ZEPHYR_BASE/boards/arm/$BOARD \
-copt -I$ZEPHYR_BASE/include \
-copt -I$ZEPHYR_BASE/include/drivers \
-copt -I$ZEPHYR_BASE/ext/hal/cmsis/Include \
-copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/soc \
-copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include \
-copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include/Legacy \
-copt -I$ZEPHYR_BASE/drivers \
-copt -I$DIR/build/zephyr/include/generated \
-copt -I$DIR/build/zephyr/include/generated/syscalls \
-o $DIR/c_interop/platforms/build/$BOARD \
-target zephyr_$BOARD || exit 1
-36
View File
@@ -1,36 +0,0 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <board.h>
#include <device.h>
#include <gpio.h>
/* Change this if you have an LED connected to a custom port */
#define PORT LED0_GPIO_PORT
/* Change this if you have an LED connected to a custom pin */
#define LED LED0_GPIO_PIN
/* 1000 msec = 1 sec */
#define SLEEP_TIME 1000
void blinky(int value)
{
int cnt = 0;
struct device *dev;
dev = device_get_binding(PORT);
/* Set LED pin as output */
gpio_pin_configure(dev, LED, GPIO_DIR_OUT);
while (1) {
/* Set pin to HIGH/LOW every 1 second */
gpio_pin_write(dev, LED, cnt % 2);
cnt++;
k_sleep(SLEEP_TIME * value);
}
}
+2
View File
@@ -0,0 +1,2 @@
// This is a placeholder empty C file.
// It is needed for Zephyr to detect the project as C project.
-18
View File
@@ -1,18 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* 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.
*/
extern int Konan_main(int argc, const char** argv);
+20 -4
View File
@@ -14,11 +14,27 @@
* limitations under the License.
*/
@SymbolName("blinky")
external fun blinky(value: Int)
import platform.zephyr.stm32f4_disco.*
import kotlinx.cinterop.*
val x = 1
fun blinky(value: Int) {
// See the .def file for details of led0_gpio_port()
val port = led0_gpio_port()!!.toKString()
val led = LED0_GPIO_PIN
var toggler = false
val dev = device_get_binding(port)
gpio_pin_configure(dev, led, GPIO_DIR_OUT)
while (true) {
/* Set pin to HIGH/LOW every 1 second */
gpio_pin_write(dev, led, if (toggler) 1 else 0);
toggler = !toggler
k_sleep(1000 * value);
}
}
fun main(args: Array<String>) {
blinky(x)
blinky(1)
}
@@ -90,10 +90,13 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con
"-fno-pie",
"-fno-pic",
"-nostdinc",
// TODO: make it a libGcc property?
// We need to get rid of wasm sysroot first.
"-isystem $targetToolchain/../lib/gcc/arm-none-eabi/7.2.1/include",
"-isystem $targetToolchain/../lib/gcc/arm-none-eabi/7.2.1/include-fixed",
"-isystem$absoluteTargetSysRoot/include/libcxx",
"-isystem$absoluteTargetSysRoot/lib/libcxxabi/include",
"-isystem$absoluteTargetSysRoot/include/compat",
"-isystem$absoluteTargetSysRoot/include/libc") +
"-isystem$absoluteTargetSysRoot/include/libc"
) +
(configurables as ZephyrConfigurables).boardSpecificClangFlags
}
@@ -140,7 +143,7 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con
is KonanTarget.ZEPHYR ->
listOf( "-DKONAN_ZEPHYR=1", "-DKONAN_NO_FFI=1", "-DKONAN_NO_THREADS=1", "-DKONAN_NO_EXCEPTIONS=1",
"-DKONAN_NO_MATH=1", "-DKONAN_INTERNAL_SNPRINTF=1", "-DKONAN_INTERNAL_NOW=1",
"-DKONAN_NO_MEMMEM=1", "-DKONAN_NO_CTORS_SECTION")
"-DKONAN_NO_MEMMEM=1", "-DKONAN_NO_CTORS_SECTION=1")
}
private val host = HostManager.host