배우고픈 공돌이

JNA 개발 환경 구축 본문

ARM/BBBW

JNA 개발 환경 구축

내 마음 아홉수 2017. 11. 29. 10:15

자바에서 하드웨어를 다루기 위해서는 래핑된 c/c++ 과정을 거친다.


즉, 하드웨어를 다루는 low level에서는 c/c++로 컨트롤하고, 위의 레이어(자바)에서 사용할 수 있도록 셋팅한다.


그 것을 JNI(JAVA Native Interface)라 한다.


일단 호스트에서 arm i2c를 접근하기 위해 jna를 사용하여 이클립스로 개발한다.


1) JNA로 이클립스에서 ARM i2c(를 포함한 하드웨어 장치)를 개발할 수 있도록 환경 설정



jna.jar를 다운로드하고,


이클립스 자바 프로젝트에 넣는다.


jar의 압축을 풀고,


풀어진 라이브러리의 경로를 설정해주어야한다.



2) 이클립스에서 프로그램 코드를 작성하자.


2 - 1) src에 Main 패키지를 만들고, 그 안에다 클래스를 생성한다.





2 - 2 ) java 소스를 코딩한다.


package Main;


import com.sun.jna.Library;

import com.sun.jna.Native;

import com.sun.jna.Platform;


public class bBB_java_i2c_Main {

public static int O_RDWR = 0x00000002;


public static void main(String[] args) {

new bBB_java_i2c_Main();

}

public bBB_java_i2c_Main() {

System.setProperty("jna.nosys","true");

String fileName= "/dev/i2c-2";

int file = CLibrary.INSTANCE.open(fileName, O_RDWR);

if(file < 0) {

System.out.println("Failed to Open i2c file");

return;

}

else {

System.out.println("Open i2c file success");

System.out.println("fd : "+ file);

}

int address = 0x1b; 

int i2c_slave = 0x0703;

int ioctl = CLibrary.INSTANCE.ioctl(file, i2c_slave, address);

if(ioctl < 0) {

System.out.println("ioctl call failed");

return;

}

else {

System.out.println("ioctl call success");

}

byte[] turnOFF = {0x0b,0x00,0x00,0x00,0x00};

int writeReturn = CLibrary.INSTANCE.write(file, turnOFF, 5);

if(writeReturn != 5) {

System.out.println("write failed");

System.out.println("return" + writeReturn);

}

else {

System.out.println("write success");

}

byte[] turnON = {0x0c,0x00,0x00,0x00,0x1b};

writeReturn = CLibrary.INSTANCE.write(file, turnON, 5);

if(writeReturn != 5) {

System.out.println("write failed");

System.out.println("return" + writeReturn);

}

else {

System.out.println("write success");

}

/*

while(true) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

*/

}

public interface CLibrary extends Library{

CLibrary INSTANCE = (CLibrary)Native.loadLibrary(

(Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);

public int ioctl(int fd,int cmd, int arg);

public int open(String path, int flags);

public int close(int fd);

public int write(int fd, byte[] buffer, int count);

public int read(int fd, byte[] buffer, int count);

}

}



3) 실행하자


debian@beaglebone:~$ java -jar selpman.jar

Open i2c file success

fd : 17

ioctl call success

write success

write success




*** int i2c_slave = 0x0703; // 모드 설정.

/*

    i2c-dev.h - i2c-bus driver, char device interface


    Copyright (C) 1995-97 Simon G. Vogl

    Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>


    This program is free software; you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation; either version 2 of the License, or

    (at your option) any later version.


    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.


    You should have received a copy of the GNU General Public License

    along with this program; if not, write to the Free Software

    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

    MA 02110-1301 USA.

*/


#ifndef _LINUX_I2C_DEV_H

#define _LINUX_I2C_DEV_H


#include <linux/types.h>



/* /dev/i2c-X ioctl commands.  The ioctl's parameter is always an

 * unsigned long, except for:

 * - I2C_FUNCS, takes pointer to an unsigned long

 * - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data

 * - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data

 */

#define I2C_RETRIES 0x0701 /* number of times a device address should

   be polled when not acknowledging */

#define I2C_TIMEOUT 0x0702 /* set timeout in units of 10 ms */


/* NOTE: Slave address is 7 or 10 bits, but 10-bit addresses

 * are NOT supported! (due to code brokenness)

 */

#define I2C_SLAVE 0x0703 /* Use this slave address */

#define I2C_SLAVE_FORCE 0x0706 /* Use this slave address, even if it

   is already in use by a driver! */

#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */


#define I2C_FUNCS 0x0705 /* Get the adapter functionality mask */


#define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */


#define I2C_PEC 0x0708 /* != 0 to use PEC with SMBus */

#define I2C_SMBUS 0x0720 /* SMBus transfer */



/* This is the structure as used in the I2C_SMBUS ioctl call */

struct i2c_smbus_ioctl_data {

__u8 read_write;

__u8 command;

__u32 size;

union i2c_smbus_data *data;

};


/* This is the structure as used in the I2C_RDWR ioctl call */

struct i2c_rdwr_ioctl_data {

struct i2c_msg *msgs; /* pointers to i2c_msgs */

__u32 nmsgs; /* number of i2c_msgs */

};


#define  I2C_RDRW_IOCTL_MAX_MSGS 42



#endif /* _LINUX_I2C_DEV_H */



출처: 

Part 4: BeagleBone Black Java I2C Development1: BeagleBone Black Java I2C Development

https://www.youtube.com/watch?v=ly8emEJSzv0


'ARM > BBBW' 카테고리의 다른 글

pulseaudio source 설치 시 에러 해결  (0) 2017.12.16
Bluecove 개발 환경 구축  (0) 2017.12.04
java FX 개발 환경 구축  (0) 2017.11.28
JAVA 개발 환경 구축  (0) 2017.11.13
3. 모니터 사용하기  (0) 2017.10.16
Comments