Skip to content

Commit

Permalink
Fix sample littlevgl/gui build issues on zephyr platform (bytecodeall…
Browse files Browse the repository at this point in the history
…iance#588)

Fix sample littlevgl/guil build issues on zephyr platform, modify code of zephyr platform by adding some macros to control some apis to fit different zephyr version, and align to zephyr-v2.3.0 for sample littlevgl/gui as the latest zephyr version (>=v2.4.0) requires that the thread stack for the thread to create must be defined by macro but not allocating stack memory dynamically.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
  • Loading branch information
wenyongh authored Mar 23, 2021
1 parent b814116 commit 91f5583
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 133 deletions.
13 changes: 12 additions & 1 deletion core/shared/platform/zephyr/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
#include <autoconf.h>
#include <zephyr.h>
#include <kernel.h>
#include <version.h>
#if KERNEL_VERSION_NUMBER >= 0x020200 /* version 2.2.0 */
#include <sys/printk.h>
#else
#include <misc/printk.h>
#endif
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
Expand Down Expand Up @@ -53,7 +58,13 @@ typedef struct korp_cond {
os_thread_wait_list thread_wait_list;
} korp_cond;

#define os_printf printf
#ifndef Z_TIMEOUT_MS
#define Z_TIMEOUT_MS(ms) ms
#endif

void abort(void);
size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);

/* math functions which are not provided by os */
double sqrt(double x);
Expand Down
54 changes: 53 additions & 1 deletion core/shared/platform/zephyr/zephyr_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ char_out(int c, void *ctx)
out_ctx->count++;
return _stdout_hook_iwasm(c);
}
#endif

int
os_vprintf(const char *fmt, va_list ap)
Expand All @@ -115,6 +114,59 @@ os_vprintf(const char *fmt, va_list ap)
return 0;
#endif
}
#endif

int
os_printf(const char *format, ...)
{
int ret = 0;
va_list ap;

va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);

return ret;
}

int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

#if KERNEL_VERSION_NUMBER <= 0x020400 /* version 2.4.0 */
void
abort(void)
{
int i = 0;
os_printf("%d\n", 1 / i);
}
#endif

#if KERNEL_VERSION_NUMBER <= 0x010E01 /* version 1.14.1 */
size_t
strspn(const char *s, const char *accept)
{
os_printf("## unimplemented function %s called", __FUNCTION__);
return 0;
}

size_t
strcspn(const char *s, const char *reject)
{
os_printf("## unimplemented function %s called", __FUNCTION__);
return 0;
}
#endif

void *
os_mmap(void *hint, size_t size, int prot, int flags)
Expand Down
5 changes: 5 additions & 0 deletions core/shared/platform/zephyr/zephyr_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ int os_mutex_lock(korp_mutex *mutex)

int os_mutex_unlock(korp_mutex *mutex)
{
#if KERNEL_VERSION_NUMBER >= 0x020200 /* version 2.2.0 */
return k_mutex_unlock(mutex);
#else
k_mutex_unlock(mutex);
return 0;
#endif
}

int os_cond_init(korp_cond *cond)
Expand Down
24 changes: 12 additions & 12 deletions product-mini/platforms/zephyr/simple/build_and_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

X86_TARGET="x86"
STM32_TARGET="stm32"
QEMU_CORTEX_A53="qemu_cortex_a53"
XTENSA_QEMU_TARGET="xtensa-qemu"
ESP32_TARGET="esp32"
QEMU_CORTEX_A53="qemu_cortex_a53"
QEMU_XTENSA_TARGET="qemu_xtensa"
QEMU_RISCV64_TARGET="qemu_riscv64"
QEMU_RISCV32_TARGET="qemu_riscv32"

usage ()
{
echo "USAGE:"
echo "$0 $X86_TARGET|$STM32_TARGET|$QEMU_CORTEX_A53|$XTENSA_QEMU_TARGET|$ESP32_TARGET|$QEMU_RISCV64_TARGET|$QEMU_RISCV32_TARGET"
echo "$0 $X86_TARGET|$STM32_TARGET|$ESP32_TARGET|$QEMU_CORTEX_A53|$QEMU_XTENSA_TARGET|$QEMU_RISCV64_TARGET|$QEMU_RISCV32_TARGET"
echo "Example:"
echo " $0 $X86_TARGET"
echo " $0 $STM32_TARGET"
echo " $0 $QEMU_CORTEX_A53"
echo " $0 $XTENSA_QEMU_TARGET"
echo " $0 $ESP32_TARGET"
echo " $0 $QEMU_CORTEX_A53"
echo " $0 $QEMU_XTENSA_TARGET"
echo " $0 $QEMU_RISCV64_TARGET"
echo " $0 $QEMU_RISCV32_TARGET"
exit 1
Expand All @@ -47,13 +47,6 @@ case $TARGET in
-DWAMR_BUILD_TARGET=THUMBV7
west flash
;;
$XTENSA_QEMU_TARGET)
west build -b qemu_xtensa \
. -p always -- \
-DCONF_FILE=prj_qemu_xtensa.conf \
-DWAMR_BUILD_TARGET=XTENSA
west build -t run
;;
$ESP32_TARGET)
# suppose you have set environment variable ESP_IDF_PATH
west build -b esp32 \
Expand All @@ -65,6 +58,13 @@ case $TARGET in
# the real name accordingly
west flash --esp-device /dev/ttyUSB1
;;
$QEMU_XTENSA_TARGET)
west build -b qemu_xtensa \
. -p always -- \
-DCONF_FILE=prj_qemu_xtensa.conf \
-DWAMR_BUILD_TARGET=XTENSA
west build -t run
;;
$QEMU_CORTEX_A53)
west build -b qemu_cortex_a53 \
. -p always -- \
Expand Down
125 changes: 72 additions & 53 deletions samples/gui/README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,96 @@
"gui" sample introduction
==============
This sample demonstrates that a graphic user interface application in WebAssembly programming with WAMR graphic library(WGL) which is part of WAMR app-framework.
This sample demonstrates that a graphic user interface application in WebAssembly programming with WAMR graphic library(WGL) which is part of WAMR app-framework.

Compared with the [littlevgl](../littlevgl) sample, WGL compiles LittlevGL source code into the WAMR runtime and defines a set of wrapper API's for exporting to Webassembly application.


Below picture shows the WASM application is running on an STM board with an LCD touch panel.


![WAMR UI SAMPLE](../../doc/pics/vgl_demo2.png "WAMR UI DEMO")

When users click the blue button, the WASM application increases the counter, and the latest counter value is displayed on the top banner of the touch panel. The number on top will plus one each second, and the number on the bottom will plus one when clicked.
When user clicks the blue button, the WASM application increases the counter, and the latest counter value is displayed on the top banner of the touch panel. The number on top will plus one each second, and the number on the bottom will plus one when clicked.

# Test on Linux

Install required SDK and libraries
--------------
- 32 bit SDL(simple directmedia layer) (Note: only necessary when `WAMR_BUILD_TARGET` is set to `X86_32` when building WAMR runtime)
Use apt-get:
`sudo apt-get install libsdl2-dev:i386`
```bash
sudo apt-get install libsdl2-dev:i386
```
Or download source from www.libsdl.org:
```
./configure C_FLAGS=-m32 CXX_FLAGS=-m32 LD_FLAGS=-m32
make
sudo make install
```
```bash
./configure C_FLAGS=-m32 CXX_FLAGS=-m32 LD_FLAGS=-m32
make
sudo make install
```
- 64 bit SDL(simple directmedia layer) (Note: only necessary when `WAMR_BUILD_TARGET` is set to `X86_64` when building WAMR runtime)
Use apt-get:
`sudo apt-get install libsdl2-dev`
Or download source from www.libsdl.org:
```
./configure
make
sudo make install
```

```bash
sudo apt-get install libsdl2-dev
```
Or download source from www.libsdl.org:
```bash
./configure
make
sudo make install
```

Build and Run
--------------

- Build</br>
`./build.sh`</br>
- Build
```bash
./build.sh
```
All binaries are in "out", which contains "host_tool", "ui_decrease.wasm", "ui_increase.wasm" and "wasm_runtime_wgl".

- Run WASM VM Linux applicaton & install WASM APP</br>
First start wasm_runtime_wgl in server mode.</br>
`./wasm_runtime_wgl -s`</br>
Then install wasm APP use host tool.</br>
`./host_tool -i inc -f ui_increase.wasm`</br>
`./host_tool -i dec -f ui_decrease.wasm`</br>


- Run WASM VM Linux applicaton & install WASM APP
First start wasm_runtime_wgl in server mode.
```bash
./wasm_runtime_wgl -s
```
Then install wasm APP by using host tool.
```bash
./host_tool -i inc -f ui_increase.wasm
# or
./host_tool -i dec -f ui_decrease.wasm
```

Test on Zephyr
================================

We can use a STM32 NUCLEO_F767ZI board with ILI9341 display and XPT2046 touch screen to run the test. Then use host_tool to remotely install wasm app into STM32.
- Build WASM VM into Zephyr system</br>
a. clone zephyr source code</br>
Refer to Zephyr getting started.</br>
https://docs.zephyrproject.org/latest/getting_started/index.html</br>
`west init zephyrproject`</br>
`cd zephyrproject`</br>
`west update`</br>
b. copy samples</br>
`cd zephyr/samples/`</br>
`cp -a <wamr_root>samples/gui/wasm-runtime-wgl wasm-runtime-wgl`</br>
`cd wasm-runtime-wgl/zephyr_build`</br>
c. create a link to wamr root dir</br>
` ln -s <wamr_root> wamr`</br>
d. build source code</br>
`mkdir build && cd build`</br>
`source ../../../../zephyr-env.sh`</br>
`cmake -GNinja -DBOARD=nucleo_f767zi ..`</br>
` ninja flash`</br>
- Build WASM VM into Zephyr system
a. clone zephyr source code
Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting_started/index.html).

```bash
west init zephyrproject
cd zephyrproject/zephyr
git checkout zephyr-v2.3.0
cd ..
west update
```
b. copy samples
```bash
cd zephyr/samples
cp -a <wamr_root>samples/gui/wasm-runtime-wgl wasm-runtime-wgl
cd wasm-runtime-wgl/zephyr_build
```
c. create a link to wamr root dir
```bash
ln -s <wamr_root> wamr
```
d. build source code
```bash
mkdir build && cd build
source ../../../../zephyr-env.sh
cmake -GNinja -DBOARD=nucleo_f767zi ..
ninja flash
```

- Hardware Connections

Expand Down Expand Up @@ -102,16 +120,17 @@ https://docs.zephyrproject.org/latest/getting_started/index.html</br>
+-------------------+-+------------------+
```


- Install WASM application to Zephyr using host_tool</br>
First, connect PC and STM32 with UART. Then install to use host_tool.</br>
`./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm`
- Install WASM application to Zephyr using host_tool
First, connect PC and STM32 with UART. Then install to use host_tool.
```bash
./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm
```

- Install AOT version WASM application
`wamrc --target=thumbv7 --target-abi=eabi --cpu=cortex-m7 -o ui_app.aot ui_increase.wasm`
`./host_tool -D /dev/ttyUSBXXX -i inc -f ui_app.aot`


```bash
wamrc --target=thumbv7 --target-abi=eabi --cpu=cortex-m7 -o ui_app.aot ui_increase.wasm
./host_tool -D /dev/ttyUSBXXX -i inc -f ui_app.aot
```

The graphic user interface demo photo:

Expand Down
Loading

0 comments on commit 91f5583

Please sign in to comment.