概述
Android 下通过jni读取i2c数据
Linux 平台:ubuntu 10.04(x86)
Android平台:AML18726-M3(Android4.0, linux kernel2.6.34)
JNI: Java Native Interface
原理:JNI允许在Java虚拟机上运行的Java代码操作其他语言编写的应用程序和本地库方法,从而能够直与接特定的操作系统和硬件平台进行交互。
方法:(这里是在linux下编译so库的方式)
一. Linux下jni编译环境搭建
1. 安装ndk.
a. 将c文件编译成so库的时候需要用到ndk-build命令,必须安装ndk
下载android-ndk-r6-linux-x86.tar,拷贝到ubuntu下,解压到/home/tony/sda6/m3tools目录
b. 添加ndk环境变量:
export PATH="$PATH:/home/tony/sda6/m3tools/android-ndk-r6"
添加完成后可以用ndk-build –v查看是否成功
二. Linux kernel 内核添加i2c设备
1. 根据原理图,i2c的mcu为i2c B, mcu芯片地址为0xa0
2. 在kernel中将设备注册到i2c B总线上,这样才能通过i2c访问到mcu上的数据。
修改kernel/inch/arch/arm/mach-meson3/board-m3-reff17.c,在i2c_board_info __initdata aml_i2c_bus_info_1[]结构体中注册设备(若是i2c A,则在i2c_bus_info_0[]中注册)
static struct i2c_board_info __initdata aml_i2c_bus_info_1[] = {
//elevator
{
I2C_BOARD_INFO("elevator ", 0xa0),
},
};
3. 重新编译内核。
make uImage 并更新uImage.
三. Jni程序编写
1. 以电梯应用程序amlogicAD为例,在amlogicAD目录下新建jni目录
2. 编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := i2c_elevator //name of so
LOCAL_SRC_FILES := i2c_elevator.c //c file
LOCAL_LDLIBS := -llog
LOCAL_C_INCLUDES := $(MY_ANDROID_SOURCE)/frameworks/base/core/jni/android/graphics
$(MY_ANDROID_SOURCE)/external/skia/include/core
$(MY_ANDROID_SOURCE)/external/skia/include/images
$(MY_ANDROID_SOURCE)/frameworks/base/include
$(MY_ANDROID_SOURCE)/system/core/include
include $(BUILD_SHARED_LIBRARY)
3. 利用javah生成jni头文件
a.用javah命令自动生成头文件需要在java代码中先声明该函数。(示例程序中 需要在SerialThread.java中调用jni来操作i2c,先用public native来声明open, read, write, colse函数,这里的open, read, write, close分别与i2c_elevator.c的函 数对应)
public static class I2c {
/**
* @param nodeName
* node path name
* @return return file hander else return <0 on fail
*/
public native int open(String nodeName);
/**
* @param fileHander
* @param i2c_adr
* slave addr
* @param buf
* @param Lenth
* of buf
* @return read length
*/
public native int read(int fileHander, int i2c_adr, byte buf[], int Length);
/**
* @param fileHander
* @param i2c_adr
* slave addr
* @param sub_adr
* sub addr
* @param buf
* @param Lenth
* of buf
* @return write length
*/
public native int write(int fileHander, int i2c_adr, int sub_adr, int buf[], int Length);
public native void close(int fileHander);
}
b.从linux下终端进入到项目目录
找到SerialThread.java对应的class文件
用javah命令自动生成头文件
javah -classpath bin/classes -d jni peter.amlogic.serial.SerialThread
其中:classpath 指定classes的目录,
-d :指定jni目录
运行此命令后,会在jni目录下生成符合jni规范的头文件
c.打开peter_amlogic_serial_SerialThread_I2c.h,里面有和前面在SerialThread.java中函数对应的函数名
在i2c_elevator.c中,函数名应该和peter_amlogic_serial_SerialThread_I2c.h一致,才能被识别
4. 在jni目录新建i2c_elevator.c 此c文件为jni核心,java层主要通过该文件中的函数来实现i2c的读写等操作。注意函数名字要用peter_amlogic_serial_SerialThread_I2c.h中的函数名
#include <jni.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <android/log.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#define CHIP_ADDR 0xa0 //mcu i2c addr
#define I2C_DEV "/dev/i2c-1" // register i2c B bus
#define LOG_TAG "i2c" //android logcat
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
static int read_eeprom(int fd, char buff[], int addr, int count)
{
int res;
int i;
for(i=0; i<PAGE_SIZE; i++)
{
buff[i]=0;
}
if(write(fd, &addr, 1) != 1)
return -1;
usleep(10000);
res=read(fd, buff, count);
LOGI("read %d byte at 0x%.2xn", res, addr);
for(i=0; i<PAGE_SIZE; i++)
{
LOGI("0x%.2x, ", buff[i]);
}
return res;
}
static int write_eeprom(int fd, char buff[], int addr, int count)
{
int res;
int i;
char sendbuffer[PAGE_SIZE+1];
memcpy(sendbuffer+1, buff, count);
sendbuffer[0]=addr;
res= write(fd, sendbuffer, count+1);
LOGI("write %d byte at 0x%.2xn", res ,addr);
usleep(10000);
for(i=0; i<PAGE_SIZE; i++)
{
LOGI("0x%.2x, ", buff[i]);
}
}
JNIEXPORT jint JNICALL Java_peter_amlogic_serial_SerialThread_00024I2c_open(JNIEnv *env, jobject obj, jstring file)
{
char fileName[64];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, file, NULL);
if (str == NULL) {
LOGI("Can't get file name!");
return -1;
}
sprintf(fileName, "%s", str);
LOGI("will open i2c device node %s", fileName);
(*env)->ReleaseStringUTFChars(env, file, str);
return open(fileName, O_RDWR);
}
JNIEXPORT jint JNICALL Java_peter_amlogic_serial_SerialThread_00024I2c_read(JNIEnv * env, jobject obj, jint fileHander, jint slaveAddr, jintArray bufArr, jint len)
{
jint *bufInt;
char *bufByte;
int res = 0, i = 0, j = 0;
if (len <= 0) {
LOGE("I2C: buf len <=0");
goto err0;
}
bufInt = (jint *) malloc(len * sizeof(int));
if (bufInt == 0) {
LOGE("I2C: nomem");
goto err0;
}
bufByte = (char*) malloc(len);
if (bufByte == 0) {
LOGE("I2C: nomem");
goto err1;
}
(*env)->GetIntArrayRegion(env, bufArr, 0, len, bufInt);
memset(bufByte, '