Skip to content

Commit c51b268

Browse files
authored
Minimize mbedos5 target (#4905)
* Removed hardware specific implementations * Added simple 'hello world' demo code to be aligned to other targets * Added ability to use external resources from outside JerryScript folder JerryScript-DCO-1.0-Signed-off-by: Roland Takacs [email protected]
1 parent d650390 commit c51b268

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+230
-2828
lines changed

.github/workflows/gh-actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ jobs:
255255
python-version: '3.8' # needed due to 'intelhex' module
256256
- run: sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
257257
- run: sudo apt update
258-
- run: sudo apt install gcc-arm-embedded python3-setuptools
258+
- run: sudo apt install gcc-arm-embedded python3-setuptools mercurial
259259
- run: make -f ./targets/os/mbedos5/Makefile.travis install
260260
- run: make -f ./targets/os/mbedos5/Makefile.travis script
261261

targets/os/mbedos5/.gitignore

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
mbed-os
2-
mbed-events
3-
.build
41
.mbed
5-
.temp/
6-
mbed_settings.py
7-
js/pins.js
8-
source/pins.cpp
9-
source/jerry-targetjs.h

targets/os/mbedos5/template-mbedignore.txt renamed to targets/os/mbedos5/.mbedignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ cmake/*
22
docs/*
33
jerry-main/*
44
jerry-math/*
5-
jerry-port/default/default-date.c
6-
jerry-port/default/default-io.c
5+
jerry-port/*
76
targets/*
87
tests/*
98
third-party/*

targets/os/mbedos5/Makefile

Lines changed: 35 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,46 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# USAGE:
16-
# specify the board using the command line:
17-
# make BOARD=[mbed board name]
15+
# default board: STM32F4-Discovery
16+
BOARD ?= DISCO_F407VG
1817

19-
BOARD=$(subst [mbed] ,,$(shell mbed target))
20-
HEAPSIZE=16
18+
# defult toolchain
19+
TOOLCHAIN ?= GCC_ARM
2120

22-
DEBUG?=0
23-
NO_JS?=0
24-
MBED_VERBOSE?=0
21+
# path to jerryscript/targets/os/mbedos5
22+
JERRY_TARGET_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
2523

26-
MBED_CLI_FLAGS=-j0 --source . --source ../../../
24+
# path to the root jerryscript directory
25+
JERRY_ROOT_DIR = $(realpath $(JERRY_TARGET_DIR)/../../..)
2726

28-
EXTRA_SRC=
27+
# default build path
28+
BUILD_DIR ?= $(JERRY_ROOT_DIR)/build/mbed-os
2929

30-
ifneq ($(EXTRA_SRC),)
31-
EXTRA_SRC_MOD=--source $(subst :, --source ,$(EXTRA_SRC))
32-
MBED_CLI_FLAGS += $(EXTRA_SRC_MOD)
33-
endif
30+
# default jerry heap size (in KB)
31+
JERRY_HEAP_SIZE ?= 70
3432

35-
EXTERN_BUILD_DIR=
36-
37-
ifneq ($(EXTERN_BUILD_DIR),)
38-
MBED_CLI_FLAGS += --build $(EXTERN_BUILD_DIR)
39-
endif
40-
41-
ifeq ($(DEBUG), 1)
42-
MBED_CLI_FLAGS += --profile ./mbed-os/tools/profiles/debug.json
43-
endif
44-
45-
ifeq ($(MBED_VERBOSE), 1)
46-
MBED_CLI_FLAGS += -v
47-
else ifeq ($(MBED_VERBOSE), 2)
48-
MBED_CLI_FLAGS += -vv
49-
endif
50-
51-
MBED_CLI_FLAGS += -D "JERRY_GLOBAL_HEAP_SIZE=$(HEAPSIZE)"
52-
MBED_CLI_FLAGS += -t GCC_ARM
53-
54-
.PHONY: all js2c getlibs rebuild library
55-
all: source/jerry-targetjs.h source/pins.cpp .mbed ../../../.mbedignore
56-
mbed target $(BOARD)
57-
mbed compile $(MBED_CLI_FLAGS)
58-
59-
library: .mbed ../../../.mbedignore
60-
# delete encoded js code if it exists
61-
rm -f source/jerry-targetjs.h
62-
mbed target $(BOARD)
63-
mbed compile $(MBED_CLI_FLAGS) --library
33+
.PHONY: all
34+
all: .mbedignore-copy .mbed-build .mbedignore-remove
6435

36+
.PHONY: clean
6537
clean:
66-
rm -rf ./BUILD/$(BOARD)
67-
68-
js2c: js/main.js js/flash_leds.js
69-
python ../../../tools/js2c.py --ignore pins.js
70-
71-
source/pins.cpp:
72-
python tools/generate_pins.py ${BOARD}
73-
74-
ifeq ($(NO_JS),0)
75-
source/jerry-targetjs.h: js2c
76-
else
77-
source/jerry-targetjs.h: ;
78-
endif
79-
80-
getlibs: .mbed
81-
82-
.mbed:
83-
echo 'ROOT=.' > .mbed
84-
mbed config root .
85-
mbed toolchain GCC_ARM
86-
mbed target $(BOARD)
87-
mbed deploy
88-
89-
../../../.mbedignore:
90-
ifeq ($(OS),Windows_NT)
91-
copy template-mbedignore.txt ..\..\..\.mbedignore
92-
else
93-
cp ./template-mbedignore.txt ../../../.mbedignore
94-
endif
38+
rm -rf $(JERRY_ROOT_DIR)/build/mbed-os
39+
40+
.PHONY: .mbed-build
41+
.mbed-build:
42+
mbed config -G MBED_OS_DIR $(MBED_OS_DIR)
43+
mbed compile \
44+
--clean \
45+
--build $(BUILD_DIR) \
46+
--source $(MBED_OS_DIR) \
47+
--source $(JERRY_ROOT_DIR) \
48+
--source $(JERRY_TARGET_DIR) \
49+
--toolchain $(TOOLCHAIN) \
50+
--target $(BOARD) \
51+
--macro JERRY_GLOBAL_HEAP_SIZE=$(JERRY_HEAP_SIZE)
52+
53+
.mbedignore-copy:
54+
cp .mbedignore $(JERRY_ROOT_DIR)
55+
56+
.mbedignore-remove:
57+
rm $(JERRY_ROOT_DIR)/.mbedignore

targets/os/mbedos5/Makefile.travis

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ all:
2121

2222
## Targets for installing build dependencies of the Mbed OS 5 JerryScript target.
2323

24+
install-mbedos:
25+
git clone https://github.com/ARMmbed/mbed-os.git ../mbed-os -b mbed-os-5.15
26+
2427
# Deploy Mbed and install Mbed Python dependencies.
25-
install:
28+
install-mbedos-deps:
2629
pip install mbed-cli
27-
cd targets/os/mbedos5 && mbed deploy
28-
pip install idna==2.5 # FIXME: workaround
29-
pip install -r targets/os/mbedos5/mbed-os/requirements.txt
30-
pip install -r targets/os/mbedos5/tools/requirements.txt
30+
pip install -r ../mbed-os/requirements.txt
3131

32+
install: install-mbedos install-mbedos-deps
3233

3334
## Targets for building Mbed OS 5 with JerryScript.
3435

3536
# Build the firmware (Mbed OS 5 with JerryScript).
3637
script:
37-
# HACK: `EXTRA_SRC[_MOD]` are abused to pass `--library` to `mbed compile` in the `all` make target that builds an app
38-
# HACK: this is needed because the Mbed OS 5 target code does not contain any `main` function, so the `all` make target does not link
39-
# HACK: but the `library` make target does not build either because the launcher sources require `jerry-targetjs.h` that are explicitly not generated for libraries
40-
$(MAKE) -C targets/os/mbedos5 BOARD=K64F EXTRA_SRC=dummy EXTRA_SRC_MOD=--library
38+
$(MAKE) -C targets/os/mbedos5 BOARD=K64F MBED_OS_DIR=$(realpath ../mbed-os)

targets/os/mbedos5/README.md

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,89 @@
1-
# JerryScript with mbed OS 5
1+
### About
22

3-
TL;DR? jump straight to [quickstart](#quick-start)
3+
This folder contains files to run JerryScript on
4+
[STM32F4-Discovery board](https://www.st.com/en/evaluation-tools/stm32f4discovery.html) with
5+
[Mbed OS 5](https://os.mbed.com/).
6+
The document had been validated on Ubuntu 20.04 operating system.
47

5-
## Introduction
8+
#### 1. Setup the build environment
69

7-
This directory contains the necessary code to build JerryScript for devices
8-
capable of running mbed OS 5. It has been tested with the following boards
9-
so far:
10+
Clone the necessary projects into a `jerry-mbedos` directory.
11+
The latest tested working version of Mbed is `5.15`.
1012

11-
- [Nordic Semiconductor NRF52 Development Kit](https://developer.mbed.org/platforms/Nordic-nRF52-DK/)
12-
- [NXP Freedom K64F](https://developer.mbed.org/platforms/FRDM-K64F/)
13-
- [STM NUCLEO F401RE](https://developer.mbed.org/platforms/ST-Nucleo-F401RE/)
14-
- [Silicon Labs EFM32 Giant Gecko](https://developer.mbed.org/platforms/EFM32-Giant-Gecko/)
13+
```sh
14+
mkdir jerry-mbedos && cd jerry-mbedos
1515

16-
## Features
16+
git clone https://github.com/jerryscript-project/jerryscript.git
17+
git clone https://github.com/ARMmbed/mbed-os.git -b mbed-os-5.15
18+
```
1719

18-
### Peripheral Drivers
20+
The following directory structure has been created:
1921

20-
Peripheral Drivers are intended as a 1-to-1 mapping to mbed C++ APIs, with a few
21-
differences (due to differences between JavaScript and C++ like lack of operator
22-
overloading).
22+
```
23+
jerry-mbedos
24+
+ jerryscript
25+
| + targets
26+
| + os
27+
| + mbedos5
28+
+ mbed-os
29+
```
2330

24-
- [DigitalOut](https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/io/DigitalOut/)
25-
- [InterruptIn](https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/io/InterruptIn/)
26-
- [I2C](https://docs.mbed.com/docs/mbed-os-api-reference/en/5.1/APIs/interfaces/digital/I2C/)
27-
- setInterval and setTimeout using [mbed-event](https://github.com/ARMmbed/mbed-events)
31+
#### 2. Install dependencies of the projects
2832

29-
## Dependencies
33+
```sh
34+
# Assuming you are in jerry-mbedos folder.
35+
jerryscript/tools/apt-get-install-deps.sh
3036

31-
### mbed CLI
37+
sudo apt install stlink-tools
38+
pip install mbed-cli
39+
# Install Python dependencies of Mbed OS.
40+
pip install --user -r mbed-os/requirements.txt
41+
```
3242

33-
mbed CLI is used as the build tool for mbed OS 5. You can find out how to install
34-
it in the [official documentation](https://docs.mbed.com/docs/mbed-os-handbook/en/5.1/dev_tools/cli/#installing-mbed-cli).
43+
#### 4. Build Mbed OS (with JerryScript)
3544

36-
### arm-none-eabi-gcc
45+
```
46+
# Assuming you are in jerry-mbedos folder.
47+
make -C jerryscript/targets/os/mbedos5 MBED_OS_DIR=${PWD}/mbed-os
48+
```
3749

38-
arm-none-eabi-gcc is the only currently tested compiler for jerryscript on mbed,
39-
and instructions for building can be found as part of the mbed-cli installation
40-
instructions above.
50+
The created binary is a `mbed-os.bin` named file located in `jerryscript/build/mbed-os` folder.
4151

42-
### make
52+
#### 5. Flash
4353

44-
make is used to automate the process of fetching dependencies, and making sure that
45-
mbed-cli is called with the correct arguments.
54+
Connect Mini-USB for charging and flashing the device.
4655

47-
### nodejs
56+
```
57+
# Assuming you are in jerry-riot folder.
58+
sudo st-flash write jerryscript/build/mbed-os/mbed-os.bin 0x8000000
59+
```
60+
61+
#### 6. Connect to the device
4862

49-
npm is used to install the dependencies in the local node_modules folder.
63+
Use `USB To TTL Serial Converter` for serial communication. STM32F4-Discovery pins are mapped by Mbed OS as follows:
5064

51-
### gulp
65+
```
66+
STM32f4-Discovery PA2 pin is configured for TX.
67+
STM32f4-Discovery PA3 pin is configured for RX.
68+
```
5269

53-
gulp is used to automate tasks, like cloning repositories or generate source files.
54-
If you create an own project, for more info see [mbed-js-gulp](https://github.com/ARMmbed/mbed-js-gulp).
70+
* Connect `STM32f4-Discovery` **PA2** pin to **RX** pin of `USB To TTL Serial Converter`
71+
* Connect `STM32f4-Discovery` **PA3** pin to **TX** pin of `USB To TTL Serial Converter`
72+
* Connect `STM32f4-Discovery` **GND** pin to **GND** pin of `USB To TTL Serial Converter`
5573

56-
### (optional) jshint
74+
The device should be visible as `/dev/ttyUSB0`. Use `minicom` communication program with `115200`.
5775

58-
jshint is used to statically check your JavaScript code, as part of the build process.
59-
This ensures that pins you are using in your code are available on your chosen target
60-
platform.
76+
* In `minicom`, set `Add Carriage Ret` to `off` in by `CTRL-A -> Z -> U` key combinations.
77+
* In `minicom`, set `Hardware Flow Control` to `no` by `CTRL-A -> Z -> O -> Serial port setup -> F` key combinations.
6178

62-
## Quick Start
79+
```sh
80+
sudo minicom --device=/dev/ttyUSB0 --baud=115200
81+
```
6382

64-
Once you have all of your dependencies installed, you can build the example project as follows:
83+
Press `RESET` on the board to get the output of JerryScript application:
6584

66-
```bash
67-
git clone https://github.com/ARMmbed/mbed-js-example
68-
cd mbed-js-example
69-
npm install
70-
gulp --target=YOUR_TARGET_NAME
7185
```
86+
This test run the following script code: [print ('Hello, World!');]
7287
73-
The produced file (in build/out/YOUR_TARGET_NAME) can then be uploaded to your board, and will
74-
run when you press reset.
88+
Hello, World!
89+
```

targets/os/mbedos5/jerry-main.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "mbed.h"
17+
18+
#include "jerry-core/include/jerryscript.h"
19+
#include "jerry-core/include/jerryscript-port.h"
20+
#include "jerryscript-ext/handler.h"
21+
22+
/**
23+
* Standalone Jerry exit codes
24+
*/
25+
#define JERRY_STANDALONE_EXIT_CODE_OK (0)
26+
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
27+
28+
/**
29+
* Register a JavaScript function in the global object.
30+
*/
31+
static void
32+
register_js_function (const char *name_p, /**< name of the function */
33+
jerry_external_handler_t handler_p) /**< function callback */
34+
{
35+
jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);
36+
37+
if (jerry_value_is_exception (result_val))
38+
{
39+
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "[Warning] Failed to register '%s' method.", name_p);
40+
}
41+
42+
jerry_value_free (result_val);
43+
} /* register_js_function */
44+
45+
int main()
46+
{
47+
Serial device(USBTX, USBRX); // tx, rx
48+
device.baud(115200);
49+
50+
/* Initialize engine */
51+
jerry_init (JERRY_INIT_EMPTY);
52+
53+
jerry_value_t ret_value = jerry_undefined ();
54+
const jerry_char_t script[] = "print ('Hello, World!');";
55+
jerry_port_log (JERRY_LOG_LEVEL_DEBUG, "This test run the following script code: [%s]\n\n", script);
56+
57+
/* Register the print function in the global object. */
58+
register_js_function ("print", jerryx_handler_print);
59+
60+
/* Setup Global scope code */
61+
ret_value = jerry_parse (script, sizeof (script) - 1, NULL);
62+
63+
if (!jerry_value_is_exception (ret_value))
64+
{
65+
/* Execute the parsed source code in the Global scope */
66+
ret_value = jerry_run (ret_value);
67+
}
68+
69+
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
70+
71+
if (jerry_value_is_exception (ret_value))
72+
{
73+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "[Error] Script Error!");
74+
75+
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
76+
}
77+
78+
jerry_value_free (ret_value);
79+
80+
/* Cleanup engine */
81+
jerry_cleanup ();
82+
83+
return ret_code;
84+
}

0 commit comments

Comments
 (0)