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

0 like 0 dislike
8 views
To use the SPI library BlackLib.

All steps should be done from java. With the Assembly etc etc no problems

C code:

JNIEXPORT jint JNICALL Java_BlackLib_BlackSPI__1transfer(JNIEnv *env, jclass obj, jobject writeBuffer, readBuffer jobject, jint bufferSize, jint wait_us) { return spi->transfer( reinterpret_cast(env->GetDirectBufferAddress(writeBuffer)), reinterpret_cast(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 the transfer and writeBuffer. If a piece of code from java to migrate 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);


The idea ByteBuffer assembled here. And couldn't understand why it's not working.

The question is how correctly to transfer in si uint8_t*?

This is the error you 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
by | 8 views

1 Answer

0 like 0 dislike
Just change everything to int

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

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
7 answers
0 like 0 dislike
2 answers
JNI
asked Mar 24, 2019 by sigito
110,608 questions
257,186 answers
0 comments
28,742 users