平台:rk3288,android6.0
apk及jni开发:eclipse
说明:实现功能,定义两个gpio,一个人体感应检测,一个控制门禁,即检测到人来了就开门。
从底层往上层开始:
1.驱动
kernel/drivers/gpio/gpiocontrol.c
#define DOOR_GPIO 160//人体感应
#define HUMAN_GPIO 161//控制门禁
#define READ_HUMAN_GPIO 1//指令 1:读human_gpio,0:写door_gpio
#define WRITE_DOOR_GPIO 0
static int gpio_ioctl(struct file *filp, unsigned int cmd, unsigned int arg)
{
int ret;
printk("func: %s, cmd: %d, arg: %d\n", __func__,cmd,arg);
switch (cmd){
case READ_HUMAN_GPIO:
ret=gpio_get_value(HUMAN_GPIO);
//printk("func: %s, ret: %d\n", __func__,ret);
break;
case WRITE_DOOR_GPIO:
gpio_set_value(DOOR_GPIO,arg);
break;
default:
break;
}
return ret;
}
static int gpio_probe(struct platform_device *pdev)
{
int ret=-1;
ret = gpio_request(HUMAN_GPIO, NULL);
if (ret) {
printk("failed to request HUMAN_GPIO for you, ret:%d\n",ret);
return -EINVAL;
}else{
gpio_direction_input(HUMAN_GPIO);//初始化为输入
}
ret = gpio_request(DOOR_GPIO, NULL);
if (ret) {
printk("failed to request DOOR_GPIO for you, ret:%d\n",ret);
return -EINVAL;
}else{
gpio_direction_output(DOOR_GPIO,0);//初始化为输出
}
printk("func: %s\n", __func__);
return 0;
}
static int gpio_remove(struct platform_device *pdev)
{
//printk("func: %s\n", __func__);
return 0;
}
//与dts匹配
static const struct of_device_id gpio_match[] = {
{.compatible = "rockchip,gpio-control",},
{}
};
MODULE_DEVICE_TABLE(of, gpio_match);
static struct platform_driver gpio_driver = {
.driver = {
.name = "gpio-control",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(gpio_match),
},
.probe = gpio_probe,
.remove = gpio_remove,
};
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = gpio_ioctl,
};
static struct miscdevice gpio_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = "gpio-control",
.fops = &gpio_fops,
};
static int __init my_init(void)
{
int ret;
ret = misc_register(&gpio_dev); //注册misc设备,会在/dev下生成相应节点,/dev/gpio-control
if(ret)
printk("you init the driver failed!!");
//return ret;
return platform_driver_register(&gpio_driver);//注册驱动
}
static void __exit my_exit(void)
{
misc_deregister(&gpio_dev);
gpio_free(HUMAN_GPIO);
gpio_free(DOOR_GPIO);
platform_driver_unregister(&gpio_driver);
}
然后在dts里面配置 :
gpio-control{
compatible = "rockchip,gpio-control";
status = "okay";
};
底层配置完了,开始写上层.
2.jni
JNIEXPORT jint JNICALL Java_com_example_gpiocontrol_GpioControl_open
(JNIEnv *env, jobject thiz){
if(fd>0){
LOGD("already open the devices!!");
return 0;
}else{
fd = open("/dev/gpio-control", O_RDWR);
LOGD("fd = %d", fd);
if (fd == -1)
{
LOGE("Cannot open port");
close(fd);
return -1;
}
}
return 0;
}
JNIEXPORT jint JNICALL Java_com_example_gpiocontrol_GpioControl_read
(JNIEnv *env, jobject thiz){
int err;
err = ioctl(fd,1,0);//读human_gpio
return err;
}
JNIEXPORT void JNICALL Java_com_example_gpiocontrol_GpioControl_write
(JNIEnv *env, jobject thiz, jint ret){
ioctl(fd,0,ret);//写door_gpio
}
最后是apk.
3.APP
先定义一个gpio控制类
public class GpioControl {
static {
// The runtime will add "lib" on the front and ".o" on the end of
// the name supplied to loadLibrary.
System.loadLibrary("gpiocontrol");
}
public native int open();
public native int read();
public native void write(int ret);
}
最后就是具体调用控制gpio了。
完整源码下载地址:http://download.csdn.net/download/sea_biscuit/10214133