|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
之前楼主在论坛中已经分享了简单驱动的编写,现在楼主再来教刚接触板子的新手们如何引用调用GPIO,不过这里楼主并没有将GPIO口的函数封装成库,然后在eclipse上调用,楼主这边的例子,只是简单的用adb工具进入板中,然后用一个测试程序使用端口,有兴趣想要在安卓界面调用端口的,可以参考楼主之前写的那编jni调用的帖子。 |
1.在/kernel/drivers下建个文件夹,自己创建并添加Kconfig和makefile,内容如下, |
3.返回drivers目录,在目录下修改Kconfig和makefile,修改内容如下 |
/*********************************************************************************** |
**********************************************************************************/ |
#include <linux/miscdevice.h> |
#include <linux/input.h> |
#include <linux/clk.h> |
#include <linux/delay.h> |
#include <asm/io.h> |
#include <asm/uaccess.h> |
#include <linux/module.h> |
#include <linux/init.h> |
#include <mach/gpio.h> |
#include <linux/gpio.h> |
#define DEVICE_NAME "rkpx2_GPIO" //定义设备名 |
#define RKPX2_GPIO_MAJOR 101 //定义设备号 |
static struct class *dev_class//定义设备结构体 |
static int gpio_open(struct inode *inode, struct file *file) |
err = gpio_request(RK30_PIN4_PD1, "GPIO4_D1")//申请端口gpio4_1,成功返回0,失败返回负的错误值 |
printk(KERN_ERR "RK_PX2 failed to request GPIO4_D1
") |
err = gpio_request(RK30_PIN4_PD2, "GPIO4_D2")//申请端口gpio4_2,成功返回0,失败返回负的错误值 |
printk(KERN_ERR "RK_PX2 failed to request GPIO4_D2
") |
err = gpio_request(RK30_PIN4_PD3, "GPIO4_D3")//申请端口gpio4_3,成功返回0,失败返回负的错误值 |
printk(KERN_ERR "RK_PX2 failed to request GPIO4_D3
") |
err = gpio_request(RK30_PIN4_PD4, "GPIO4_D4")//申请端口gpio4_4,成功返回0,失败返回负的错误值 |
printk(KERN_ERR "RK_PX2 failed to request GPIO4_D4
") |
printk(KERN_INFO "RK_PX2 GPIO opened 4 !
") |
gpio_direction_output(RK30_PIN4_PD1,1)//决定GPIO的方向,为输出 |
gpio_direction_output(RK30_PIN4_PD2,1) |
gpio_direction_output(RK30_PIN4_PD3,1) |
gpio_direction_output(RK30_PIN4_PD4,1) |
static int gpio_close(struct inode *inode, struct file *file) |
gpio_free(RK30_PIN4_PD4) //释放端口, |
printk(KERN_INFO "RK_PX2 GPIO driver successfully close
") |
static int gpio_read(struct file *file, char *buffer, size_t size, loff_t *pos) //从内核中读取GPIO引脚的值 |
if (gpio_get_value(RK30_PIN4_PD1)==0){ |
if (gpio_get_value(RK30_PIN4_PD2)==0){ |
if (gpio_get_value(RK30_PIN4_PD3)==0){ |
if (gpio_get_value(RK30_PIN4_PD4)==0){ |
ret=copy_to_user(buffer,key_buf,4)//拷贝数据到用户区 ,成功为 0,失败为字节数 |
static long gpio_ioctl(struct file *file ,unsigned int cmd,unsigned long arg){ |
case 00: gpio_set_value(RK30_PIN4_PD1,0) |
printk("GPIO_D1 is low!
") |
case 01: gpio_set_value(RK30_PIN4_PD1,1) |
printk("GPIO_D1 is high!
") |
case 10: gpio_set_value(RK30_PIN4_PD2,0) |
printk("GPIO_D2 is low!
") |
case 11: gpio_set_value(RK30_PIN4_PD2,1) |
printk("GPIO_D2 is high!
") |
case 20: gpio_set_value(RK30_PIN4_PD3,0) |
printk("GPIO_D3 is low!
") |
case 21: gpio_set_value(RK30_PIN4_PD3,1) |
printk("GPIO_D3 is high!
") |
case 30: gpio_set_value(RK30_PIN4_PD4,0) |
printk("GPIO_D4 is low!
") |
case 31: gpio_set_value(RK30_PIN4_PD4,1) |
printk("GPIO_D4 is high!
") |
static struct file_operations dev_fops = { |
//.unlocked_ioctl = tq210_gpio_ioctl, |
.unlocked_ioctl = gpio_ioctl, |
static int __init dev_init(void) |
ret = register_chrdev(RKPX2_GPIO_MAJOR,"rkpx2_GPIO",&dev_fops)//注册 |
printk("rkpx2 GPIO for test unable to get major%d
",ret) |
dev_class = class_create(THIS_MODULE,"rkpx2_GPIO")//初始化 |
unregister_chrdev(RKPX2_GPIO_MAJOR,"rkpx2_GPIO") |
return PTR_ERR(dev_class) |
device_create(dev_class,NULL,MKDEV(RKPX2_GPIO_MAJOR,0),NULL,"rkpx2_GPIO")//创建设备 |
printk(KERN_INFO "RKPX2 GPIO driver successfully probed!
") |
static void __exit dev_exit(void) |
//misc_deregister(&dev_misc) |
printk(KERN_INFO "RKPX2 gpio driver successfully exit
") |
MODULE_AUTHOR("Rayeager cyl") |
MODULE_DESCRIPTION("rkpx2 gpio Driver") |
MODULE_LICENSE("GPL") |
#include <stdlib.h> |
#include <string.h> |
#define DEVICE_NAME "/dev/rkpx2_GPIO" |
int main(int argc,char **argv){ |
printf("
start test gpio_drivers
") |
if(strcmp(argv[1],"open")==0){ |
fd=open(DEVICE_NAME,O_RDWR) |
printf("open devices %s error
",DEVICE_NAME) |
printf("input the pin you want to operate") |
scanf("%d",&pin) |
printf("it will be set ?(1=on or 0=off):") |
scanf("%d",&ch) |
case 0: (ch==1?ioctl(fd,1):ioctl(fd,2))break |
case 1: (ch==1?ioctl(fd,3):ioctl(fd,4))break |
case 2: (ch==1?ioctl(fd,5):ioctl(fd,6))break |
case 3: (ch==1?ioctl(fd,7):ioctl(fd,8))break |
if(strcmp(argv[1],"close")==0){ |
fd=open(DEVICE_NAME,O_RDWR) |
LOCAL_PATH := $(call my-dir) |
include$(BUILD_EXECUTABLE) |
编译测试程序的方法:在源码任意位置建个目录,把Android.mk和测试程序全扔进去,然后执行mm(现在根目录执行source build/envsetup.sh) |
实际操作,过程就不说了,亲测可行,正常操作的端口为GPIO4_1234 |
|
|