GPIO핀을 JNI를 통해 ioctl로 컨트롤하려고 합니다
글쓴이: adiyoung / 작성시간: 화, 2014/08/26 - 3:38오후
GPIO 컨트롤을 위한 MISC 디바이스 드라이버를 등록하였고
안드로이드는 버튼의 리스너로 JNI함수의 init을 호출하도록 구현하였습니다
JNI에서 ioctl을 통해 디바이스 드라이버(/dev/d3gpio)를 접근하도록 되어있는데
드라이버단에서 open, release, read, write는 되는것을 확인했습니다만 d3_gpio_ioctl은 printk조차 찍히지 않고 아예 타질않습니다
그리고 JNI단의 ioctl은 리턴 -1을 뱉어내버립니다
원인이 뭔지 짐작이 가질않아서 소스와 함께 글을 남겨봅니다
-디바이스
#include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/fs.h> #include <linux/miscdevice.h> #include <linux/gpio.h> static ssize_t d3_gpio_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { return 0; } static ssize_t d3_gpio_write(struct file *file, char __user *buf, size_t size, loff_t *ppos) { return 0; } static long d3_gpio_ioctl (struct file *file, unsigned int cmd, unsigned long arg) { int val = -1; printk("[] d3_gpio_ioctl cmd(%d) arg(%d) \n", cmd, arg); switch(cmd) { case 1: //read val = gpio_get_value(D3_GPIO4); printk("[] ioctl read... GPIO4 is %d \n",val); return val; case 2: //write if(arg == 1) { gpio_set_value(D3_GPIO4, 1); //High printk("[] ioctl write... GPIO4 Set to HIGH."); } else if(arg == 0) { gpio_set_value(D3_GPIO4, 0); //Low printk("[] ioctl write... GPIO4 Set to LOW. "); } break; default: printk("[] ioctl unknown command...\n"); break; } return 0; } static int d3_gpio_open(struct inode *inode, struct file *file) { return 0; } static int d3_gpio_release(struct inode *inode, struct file *file) { return 0; } static const struct file_operations d3_gpio_fops = { .owner = THIS_MODULE, .read = d3_gpio_read, .write = d3_gpio_write, .unlocked_ioctl = d3_gpio_ioctl, .open = d3_gpio_open, .release = d3_gpio_release, }; static struct miscdevice d3_gpio_dev = { .minor = MISC_DYNAMIC_MINOR, .name = "d3gpio", .fops = &d3_gpio_fops, }; static int __init d3_gpio_init(void) { printk("[] d3_gpio_init\n"); return misc_register(&d3_gpio_dev); } static void __exit d3_gpio_exit(void) { misc_deregister(&d3_gpio_dev); } module_init(d3_gpio_init); module_exit(d3_gpio_exit); MODULE_DESCRIPTION("D3 GPIO driver"); MODULE_LICENSE("GPL");
- JNI
#include "jni.h" #include "nativeClass.h" #include <stdio.h> #include <sys/ioctl.h> #include <fcntl.h> #include <sys/stat.h> #include <android/log.h> JNIEXPORT void JNICALL Java_com_example_android_navigationdrawerexample_nativeClass_init(JNIEnv *env, jobject obj) { int dev = 0; int ret = 0; dev = open("/dev/d3gpio", O_RDWR); if(dev < 0) LOGE("gpio open fail : %d \n", dev); ret = ioctl(dev, 0x2, 0x1); LOGE("ioctl result : %d \n", ret); close(dev); return; }
Forums:
ioctl()이 -1 리턴했다면 errno도
ioctl()이 -1 리턴했다면 errno도 찍어보셨으면 하네요. errno에 들어가는 에러값이 단서가 될 수 있습니다.
Written By the Black Knight of Destruction
ERROR : 14, Bad address
ERROR : 14, Bad address 출력됩니다
그래도 모르겠네요 @_@ ㅎㅎ
이미 답을 찾았을것 같지만 .....
//driver
#define D3_GPIO_TEST _IO('z', 0x01)
static long d3_gpio_ioctl (struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case D3_GPIO_TEST:
.....
break;
}
}
//app
#define D3_GPIO _IO('z', 0x01)
ret = ioctl(dev, D3_GPIO_TEST, 0x1);
댓글 달기