This document describes how to add a native call to a monolithic application running on a mono-sandbox platform.
Important notice, In order to ease the reader’s understanding, the project from this HowTo is released in the final, working state one should obtain when following the instructions below.
The native code provided has been created for the STMicroelectronics STM32F746G-DISCO development board available from st.com.
It can be ported to any hardware with a LED.
This example has been tested on:
- MicroEJ SDK 5.1
- With a Platform Reference Platform that contains:
- EDC-1.2
- BON-1.2
- MicroEJ platform reference implementation STM32F746-DISCO Platform available from developer.microej.com
- Keil MDK-ARM version 5.25 available from keil.com
This tutorial is built upon an understanding of the MicroEJ SDK, MicroEJ architectures, MicroEJ Platforms and MicroEJ applications as well as at least one native BSP compiler and IDE.
The user of this tutorial should be able to create a “Hello World” application in Java, build the application, build the BSP and flash the firmware image onto the targeted board.
Other development boards, reference platforms and compilers can be used, however the instructions in this tutorial are specifically written for these items and will not apply without changes to other environments.
Import the example projects into the MicroEJ SDK:
- Click on File -> Import
- Select General -> Existing Project into Workspace
- Browse to root directory
- Check Search for nested projects checkbox
- Select all the projects
- Click on Finish
![]()
SNI-LEDcontains this READMEHelloWorldis a standalone applicationNativeAPIsis a project that defines the native functions to manage the LED
Modify the platform to add the capability to call a native C function from Java.
Configure modules
- Open the
[XXX]-configuration/[XXX].platformfile - Go to Content tab.
- Check Java to C interface -> SNI API module
- Open the
Build the platform
- Go to Overview tab
- Click on Build Platform in the Overview->**Build section**
- Once the platform is built, the
[Platform-name]-[version]project is created in the workspace
Add a native call to the HelloWorld Java application
- Native function definition
- The project
NativeAPIsis used to define the native functions - com.microej.Led
defines the native function to manage the LED
Led.initNative()is called at start-up to initialize the LEDprivate static native void initNative();Led.switchLed(boolean on)is called to set the state of the LEDpublic static void switchLed(boolean on);provides the APIs to the java code.private static native void switchLedNative(boolean on);
- Optional, but highly recommended: To use the simulator a Mock should be created. An example of a mock usage is provided in Example-Standalone-Java-C-Interface
- The project
- Call the native function in the HelloWorld application
- The project
HelloWorlddepends onNativeAPIs - com.microej.feature.HelloWorld
uses
LEDto toggle the LED
- The project
- Native function definition
Build the HelloWorld Java application
- Right-click on the HelloWorld project
- Select Run-As -> Run Configuration
- Right-click on MicroEJ Application
- Select New
- In Execution tab
- Set your platform that was built in step 3
- Check Execute on device
- Set Settings to Build and deploy
- Run will generated a
microejapp.oin the platform BSP folder
Add the native LED C implementation to the BSP in the third party C IDE
- LEDs.c provides the implementation of
the native C function defined in
NativeAPIs - This implementation is done for the STM32F746-DISCO board, to add
it to Keil IDE follow these steps:
- Open the Keil project in the platform
[XXX]-bspproject - Right-click on the
MicroEJ/Corefolder - Select Add Existing Files to Group ‘MicroEJ/Core’
- Browse to the file LEDs.c in the native repository
- Click Add
- Click Close
- Build the project by pressing F7
- Flash the firmware on the board pressing F8
- Verify the Green LED LD1 is blinking on for one second and off for one second
- Open the Keil project in the platform
- LEDs.c provides the implementation of
the native C function defined in
- Learn more about Java/C communication with this example: Example-Standalone-Java-C-Interface
- Adapt this HelloWorld to run into a Multi Sandbox kernel Single-App-to-Multi-App-Platform.
