B
B
Billy Milligan2018-03-05 14:15:02
Java
Billy Milligan, 2018-03-05 14:15:02

How to pass value from java to native (c++)?

I use the BlackLib library for SPI .

All actions must be done from java. There are no problems with assembly, etc., etc.

Code in C:

JNIEXPORT jint JNICALL Java_BlackLib_BlackSPI__1transfer(JNIEnv *env, jclass obj, jobject writeBuffer, jobject readBuffer, jint bufferSize, jint wait_us)
{
    return spi->transfer(
        reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(writeBuffer)),
        reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(readBuffer)),
        bufferSize,
        wait_us
    );
}


Java code:

BlackSPI spi = new BlackSPI(BlackSPI.SpiName.SPI1_1.getValue(), 32, BlackSPI.SpiMode.MODE_0.getValue(), 5_000_000);

    spi.open(BlackSPI.OpenMode.ReadWrite.getValue() | BlackSPI.OpenMode.NonBlock.getValue());

    int val = 0, stub = 0;

    for(int i=30 ; i >= 0 ; i--) {
        val = val & (~(3 << i));
        val |= (0 << i);
    }

    val = val & (~(3 << 0));
    val |= (1 << 0);

    try {
        System.out.println(spi.transfer(ByteBuffer.allocate(4).putInt(val), ByteBuffer.allocate(4).putInt(stub), 4, 0));
    } catch (IIOException e) {
        e.printStackTrace();
    }


The problem is in transfer and writeBuffer. If part of the code from java is transferred to C, then everything works as it should.

int val, stub;

    for(int i=30 ; i >= 0 ; i--) {
        val = val & (~(3 << i));
        val |= (0 << i);
    }

    val = val & (~(3 << 0));
    val |= (1 << 0);

    return spi->transfer((uint8_t*) & val, (uint8_t*) & stub, sizeof (stub), 0);


I spotted the idea with ByteBuffer here . I still can't figure out why it doesn't work for me.

The question is how to correctly pass uint8_t* to C?

This is the error I get:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0xb6ed0f34, pid=4509, tid=0xb63fe470
#
# JRE version: Java(TM) SE Runtime Environment (8.0_161-b12) (build 1.8.0_161-b12)
# Java VM: Java HotSpot(TM) Client VM (25.161-b12 mixed mode linux-arm )
# Problematic frame:
# C  [libc.so.6+0x59f34]  memcpy+0xb4
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /root/hs_err_pid4509.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Aborted

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Billy Milligan, 2018-03-05
@Billy_Milligan

I just changed everything to int

JNIEXPORT jint JNICALL Java_BlackLib_BlackSPI__1transfer(JNIEnv *env, jclass obj, jint writeBuffer, jint readBuffer, jint bufferSize, jint wait_us)
{
    return spi->transfer((uint8_t*) & writeBuffer, (uint8_t*) & readBuffer, sizeof (readBuffer), 0);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question