GPIO핀을 JNI를 통해 ioctl로 컨트롤하려고 합니다

adiyoung의 이미지

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;
}

Necromancer의 이미지

ioctl()이 -1 리턴했다면 errno도 찍어보셨으면 하네요. errno에 들어가는 에러값이 단서가 될 수 있습니다.

Written By the Black Knight of Destruction

adiyoung의 이미지

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);

adiyoung의 이미지

 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.