UART and other GPIO interrupt

Hi guys

I need to know how to use UART interrupt (when data receive ,do my job) .
I dont want use a thread with while(true) (forever loop) to read input stream.
we have this function in C
but i cant find how to implement this interface in java ?

the 2nd part is how to use input pins interrupt and interface them to java? is there any API for this??


Is there anybody that can help with this?
I am waiting for it .

Hi @Biftor,

Implementing the LLCOM_UART native interface will allow you to use ECOM COMM in Java.

Pins configuration and interrupts handling for UART are not done in Java but in C at the low-level API level, using the UART in Java only as a Java stream. You can find an example of use of the Java comm connectors in our GitHub [1].

As for implementing the LL_UART C side you can take a look at how it was done for an existing platform [2]. Inside the archive you will find an implementation of BSP for STM32F7476G-DISCO, this BSP contains a LL_UART implementation (in platformSource\\STM32F746GDISCO-846SI-3.1.5-bsp\Projects\STM32746G-Discovery\Applications\MicroEJ\src-comm).

For GPIOs we have a HAL library [3].



Thanks Gaëtan for answering.

Then there is no available java api for using UART and GPIO pins interrupt. Is this right?

I have done UART read and write stream but it is using the for ever loop for reading stream that will be problem with battery base devices.
Also i have done GPIO project input and output (digital or analog)
But the same thing from UART reading mode is true for GPIO input.

If we dont use interrupt we have to make some for ever loop to read pins or input stream that will consume battery and will be problem with battery base devices then i have to use interrupt.
In your examples in github you are using a for ever loop .

I pointed to you how to use the ECOM-COMM API and HAL APIs, these are Java APIs (cf. the links 1 and 3 of the previous message).

As of how you implement the LL_UART to match your need, this is a BSP issue but you can refer to link 2 that I posted that has an example of implementation.

We do not treat interrupts in Java as it is far too costly and interrupts should be processed as fast as possible.

The loop in Java reading the stream should be sleeping when nothing is to be read on the UART so to not consume actively.

Hope it’ll help.

Yes exactly i know how to use those api,
As what i know there is no delay for treat interrupts to Java as an interface. I have to make this myself as what i got from you :slight_smile:
I didn’t get you at the point you said we can sleep the thread(forever loop thread) , if we sleep the thread how to define when data receive?for example we have RFID reader how can we understand that RFID serial output is ready?and we get it from serial input (When user scanned a tag).

Instead of for ever loop or UART interrupt??
We need exactly serial interrupt here or forever loop to read stream ,to define exact time that data received

The UART interrupt is handled by the underlying BSP (in C) caching the data received in a buffer until the Java thread is awake to get back the data of the read. So there is no choice between an interrupt handling or a “loop forever”, it is:

  • interrupt handling in C, data read then cached into a buffer
  • Java thread periodically waking up and checking if there is data in the buffer

At the risk of repeating myself but you have an example of how the BSP part is implemented in the link 2 of my first answer.


Right,then how should i detect exact moment of there is data in UART from java to trigger something?
Nevermind, can you tell me how to call a java void function with some input value from c
Is there anything in microej?

in pure java and c continuation we have JNI and we can use that to find classes and function after that fill it .


If you need to trigger an action from a hardware event (could be a GPIO interrupt or UART character received either by polling or interrupt), you can check how to use the SNI library (in your case the best would probably be a Java thread that is resumed when your event is rising).

kind regards,

Thanks for answering.
Yes i should use SNI ,but i didn’t find how to call a java function in c part using SNI?
With JNI we have something like this

#include <stdio.h>
#include <jni.h>

JNIEnv* create_vm() {
	JavaVM* jvm;
	JNIEnv* env;
	JavaVMInitArgs args;
	JavaVMOption options[1];
	/* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
	args.version = JNI_VERSION_1_2;
	args.nOptions = 1;
	options[0].optionString = "-Djava.class.path=c:\\projects\\local\\inonit\\classes";
	args.options = options;
	args.ignoreUnrecognized = JNI_FALSE;

	JNI_CreateJavaVM(&jvm, (void **)&env, &args);
	return env;

void invoke_class(JNIEnv* env) {
	jclass helloWorldClass;
	jmethodID mainMethod;
	jobjectArray applicationArgs;
	jstring applicationArg0;

	helloWorldClass = (*env)->FindClass(env, "example/jni/InvocationHelloWorld");

	mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");

	applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
	applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
	(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

	(*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);

int main(int argc, char **argv) {
	JNIEnv* env = create_vm();
	invoke_class( env );

I need exact thing with SNI or give me full documents to read and will do my self .

To be clear, you can not call directly a Java method from a C function (note: you can call any C functions from Java) (see also the green thread architecture to understand better why), but you can:

Option 1: use SNI in asynchronous mode

  • create a Java thread that use SNI to call a C function
  • In the C function: if your data or IT is not ready/triggered yet, call in your code the function LLMJVM_suspendCurrentJavaThread from LLJVM.h (causes the current Java thread to pause its Java execution after the end of the current native method). This function is not blocking. Call LLMJVM_getCurrentJavaThreadID to get the Java thread ID.
  • when your data is ready or IT triggers, call LLMJVM_resumeJavaThread to resume the calling thread.
  • the Java thread reading the data is now resumed.

Option 2: use Shielded plug library, your code pushes data, that will be read asynchronously by the Java code.

Both options are implemented in this example:

kind regards,

1 Like

yeah finally a useful answer :slight_smile:

i was waiting for such an answer

To be clear, you can not call directly a Java method from a C function

thank you Stephane i will try the solutions