当前位置:网站首页>[andorid] realize SPI communication between kernel and app through JNI
[andorid] realize SPI communication between kernel and app through JNI
2022-04-23 13:19:00 【Johnny2004】
about kernel Layer code ,Linux There is a classic reference driver in the original code , Can imitate spi drive .
If you don't ask too much , Just modify the equipment number and node name , Then you can talk to dts If the match is successful , Finally, remember to modify the generated node permissions
path:kernel/drivers/spi/spidev.c
diff --git a/device/rockchip/common/ueventd.rockchip.rc b/device/rockchip/common/ueventd.rockchip.rc
index 8d5d28d..4b6ac2a 100644
--- a/device/rockchip/common/ueventd.rockchip.rc
+++ b/device/rockchip/common/ueventd.rockchip.rc
@@ -65,6 +65,8 @@
/dev/ttyS2 0666 system system
/dev/ttyS3 0666 system system
+/dev/thm36 0666 system system
+
# for radio
/dev/ttyUSB0 0660 radio radio
/dev/ttyUSB1 0660 radio radio
JNI Your code needs to be completed by yourself ( Unwanted HAL layer ), And then JNI Add to App Chinese compiler .
The following code just realizes the simple read-write function , complete app and kernel Communication between , The complex part is the conversion between arrays .
JNI file name :thm36_jni.c
#include <jni.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "android/log.h"
static const char *TAG = "thm36_jni";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
#define DEV_NAME "/dev/thm36"
static jint fd;
JNIEXPORT jint JNICALL Java_com_example_aaron_se_Thm36Native_thm36Open(JNIEnv *env, jclass clazz)
{
LOGD("JNI spi open ... ...");
fd = open(DEV_NAME, O_RDWR);
if (fd < 0)
{
LOGD("open device fail!");
return -1;
}
return 0;
}
JNIEXPORT void JNICALL Java_com_example_aaron_se_Thm36Native_thm36Close(JNIEnv *env, jclass clazz)
{
LOGD("JNI spi close ... ...");
close(fd);
}
JNIEXPORT jint JNICALL Java_com_example_aaron_se_Thm36Native_thm36Read(JNIEnv *env, jclass clazz, jbyteArray jread_arr, jint len)
{
jbyte *array = NULL;
jboolean *buf;
int i = 0;
LOGD("JNI spi read ... ...");
array = (*env)->GetByteArrayElements(env, jread_arr, NULL);
if (array == NULL)
{
LOGD("JNI spi read: GetByteArrayElements faid!");
return -1;
}
buf = (jboolean *)calloc(sizeof(*array), sizeof(jboolean));
if (buf == NULL)
{
LOGD("JNI spi read: calloc fail!");
return -1;
}
read(fd, buf, len);
for (i=0; i<len; i++)
{
LOGD("JNI spi read: buf: %#x", *(buf + i));
*(array + i) = (jchar)(*(buf + i));
}
(*env)->ReleaseByteArrayElements(env, jread_arr, array, 0);
free(buf);
return 0;
}
JNIEXPORT jint JNICALL Java_com_example_aaron_se_Thm36Native_thm36Write(JNIEnv *env, jclass clazz, jbyteArray jwrite_arr, jint len)
{
jbyte *array = NULL;
jboolean *buf;
int i = 0;
LOGD("JNI spi write ... ...");
array = (*env)->GetByteArrayElements(env, jwrite_arr, NULL);
if (array == NULL)
{
LOGD("JNI spi write: GetByteArrayElements fail!");
return -1;
}
buf = (jboolean *)calloc(sizeof(*array), sizeof(jboolean));
if(buf == NULL)
{
LOGD("JNI spi write: calloc fail!");
return -1;
}
for(i = 0; i < len; i++)
{
*(buf + i) = (jboolean)(*(array + i));
LOGD("JNI spi write: data : %#x\n",*(buf + i));
}
(*env)->ReleaseByteArrayElements(env, jwrite_arr, array, 0);
write(fd, buf, len);
free(buf);
return 0;
}
Compile the file :Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
TARGET_PLATFORM := android-3
LOCAL_MODULE := thm36_jni
LOCAL_SRC_FILES := thm36_jni.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
App The test code is also very simple , One is the main activity , One is with JNI link
MainActivity.java
// JNI The first part of the function name in should be the same as this
package com.example.aaron.se;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Thm36Native thm36 = new Thm36Native();
byte[] tx = {(byte)0xAA, 0x00, 0x06, 0x00, (byte)0xCA, 0x00, 0x00, 0x00, 0x00, (byte)0xCA};
byte[] rx = new byte[22];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn:
thm36.thm36Open();
thm36.thm36Write(tx, tx.length);
thm36.thm36Read(rx, rx.length);
thm36.thm36Close();
break;
default:
break;
}
}
}
Thm36Native.java
package com.example.aaron.se;
import android.util.Log;
public class Thm36Native {
private final String TAG = "Thm36Native";
public native int thm36Open();
public native void thm36Close();
public native int thm36Read(byte[] buf, int len);
public native int thm36Write(byte[] buf, int len);
static {
System.loadLibrary("thm36");
}
}
Reference resources :
版权声明
本文为[Johnny2004]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230608014834.html
边栏推荐
- nodeJs + websocket 循环小案例
- EMMC / SD learning notes
- Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
- XML
- Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
- You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
- [untitled] PID control TT encoder motor
- (personal) sorting out system vulnerabilities after recent project development
- AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
- AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
猜你喜欢
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
Solve the problem that Oracle needs to set IP every time in the virtual machine
[dynamic programming] 221 Largest Square
EMMC / SD learning notes
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
Learning notes of AMBA protocol
C语言之字符串与字符数组的区别
Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
随机推荐
「玩转Lighthouse」轻量应用服务器自建DNS解析服务器
MySQL 8.0.11 download, install and connect tutorials using visualization tools
【行走的笔记】
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
MySQL basic statement query
MySQL -- 16. Data structure of index
[point cloud series] so net: self organizing network for point cloud analysis
【动态规划】221. 最大正方形
XML
Vscode tips
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
nodeJs + websocket 循环小案例
[indicators] precision, recall
Stack protector under armcc / GCC
LeetCode_DFS_中等_695.岛屿的最大面积
Learning notes of AMBA protocol
4.22 study record (you only did water problems in one day, didn't you)
Common commands of ADB shell
初鉴canvas,展示个小小的小案例
CSDN高校俱乐部“名师高校行”——湖南师范大学站