itop4412学习记录
本章的目的: 掌握gpio的申请、配置、取值和释放。
(1)gpio申请:gpio_request
(2)gpio配置-设置为输入:s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT);
(3)gpio配置-设置为非上下拉:s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
(4)gpio取值:gpio_get_value
(5)gpio释放:gpio_free(EXYNOS4_GPC0(3));
利用拨码开关来实现GPIO的输入,通过原理图理清楚拨码开关的3和4对应的IO编号:
管脚编号 → GPIO编号 → 对应GPIO虚拟地址宏
AP_SLEEP→GPC0_3→EXYNOS4_GPC0(3)
XEINT6→GPX0_6→EXYNOS4_GPX0(6)
开关3和4,向内是低电平,输入0。向外是高电平1.8v,输入1。
开关3和4对应IO口需要做的准备: 1.设置为输入;2.不上拉,不下拉;3. 读寄存器
(1)gpio申请:
gpio_request(EXYNOS4_GPC0(3),"GPC0_3");
gpio_request(EXYNOS4_GPX0(6),"GPX0_6");
(2)gpio配置-设置为输入:
s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT);
s3c_gpio_cfgpin(EXYNOS4_GPX0(6),S3C_GPIO_INPUT);
(3)gpio配置-设置为非上下拉:
s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
s3c_gpio_setpull(EXYNOS4_GPX0(6),S3C_GPIO_PULL_NONE);
(4)gpio取值:
gpio_get_value(EXYNOS4_GPC0(3));
gpio_get_value(EXYNOS4_GPX0(6));
(5)gpio释放:
gpio_free(EXYNOS4_GPC0(3));
gpio_free(EXYNOS4_GPX0(6));
方法一: 在4412平台文件注册设备结构体:
iTop4412_Kernel_3.0/arch/arm/mach-exynos/mach-itop4412.c
struct platform_device s3c_device_read_gpio_ctl = {
.name = "read_gpio_ctl",
.id = -1,
};
&s3c_device_read_gpio_ctl,
具体参考:06_设备注册
方法二:使用platform_device_register注册设备
#define DRIVER_NAME "gpio_read"
#define DEVICE_NAME "gpio_read_dev"
static int gpio_read_dev_release(struct platform_device *dev)
{
printk("\tgpio read device.dec.release!!\n");
}
static struct platform_device s3c_device_gpio_read_dev = {
.name = DRIVER_NAME,
.id = -1,
.dev = {
.release = gpio_read_dev_release,
}
};
Devicestate = platform_device_register(&s3c_device_gpio_read_dev);
参考:linux以module方式注册设备和注册驱动、以及杂项设备
推荐使用方法二,因为通用性强。
模块编译命令:make
应用程序编译命令:arm-none-linux-gnueabi-gcc -o user_read_gpio user_read_gpio.c
(1)加载模块,查看设备节点
[root@iTOP-4412]# insmod gpio_read.ko
[ 454.284761] start gpio init!
[ 454.309519] device state is 0
[ 454.311541] gpio initialized
[ 454.371292] gpio init OK,DriverState is 0!
通过命令 ls /dev,可以发现设备节点:gpio_read_dev。
(2)运行程序程序gpio_read_dev
命令格式:./gpio_read_dev cmd
cmd=0时,读EXYNOS4_GPC0(3)
cmd=1时,读EXYNOS4_GPX0(6)
将开关4接地:
[root@iTOP-4412]# ./user_read_gpio 1
[ 466.340700] gpio open
[ 466.341593] cmd is 1,arg is 0
[ 466.345198] gpio release
cmd is 1,gpio_read is 0
将开关4接1.8V:
[root@iTOP-4412]# ./user_read_gpio 1
[ 503.834045] gpio open
[ 503.834934] cmd is 1,arg is 0
[ 503.838668] gpio release
cmd is 1,gpio_read is 1
#include
#include
/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include
/*注册杂项设备头文件*/
#include
/*注册设备节点的文件结构体*/
#include
/*Linux中申请GPIO的头文件*/
#include
/*三星平台的GPIO配置函数头文件*/
/*三星平台EXYNOS系列平台,GPIO配置参数宏定义头文件*/
#include
#include
#define DRIVER_NAME "gpio_read"
#define DEVICE_NAME "gpio_read_dev"
static long gpio_ioctl( struct file *files, unsigned int cmd, unsigned long arg){
printk("cmd is %d,arg is %d\n",cmd,arg);
if(cmd > 1){
printk(KERN_EMERG "cmd is 0 or 1\n");
}
if(arg > 1){
printk(KERN_EMERG "arg is only 1\n");
}
if(cmd==0)
return gpio_get_value(EXYNOS4_GPC0(3));
if(cmd==1)
return gpio_get_value(EXYNOS4_GPX0(6));
}
static int gpio_release(struct inode *inode, struct file *file){
printk(KERN_EMERG "gpio release\n");
return 0;
}
static int gpio_open(struct inode *inode, struct file *file){
printk(KERN_EMERG "gpio open\n");
return 0;
}
static struct file_operations gpio_ops = {
.owner = THIS_MODULE,
.open = gpio_open,
.release = gpio_release,
.unlocked_ioctl = gpio_ioctl,
};
static struct miscdevice gpio_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &gpio_ops,
};
static int gpio_probe(struct platform_device *pdv){
int ret;
printk(KERN_EMERG "\tgpio initialized\n");
ret = gpio_request(EXYNOS4_GPC0(3),"GPC0_3");
if(ret < 0){
printk(KERN_EMERG "gpio_request EXYNOS4_GPC0(3) failed!\n");
return ret;
}
ret = gpio_request(EXYNOS4_GPX0(6),"GPX0_6");
if(ret < 0){
printk(KERN_EMERG "gpio_request EXYNOS4_GPX0(6) failed!\n");
return ret;
}
s3c_gpio_cfgpin(EXYNOS4_GPC0(3),S3C_GPIO_INPUT);
s3c_gpio_setpull(EXYNOS4_GPC0(3),S3C_GPIO_PULL_NONE);
s3c_gpio_cfgpin(EXYNOS4_GPX0(6),S3C_GPIO_INPUT);
s3c_gpio_setpull(EXYNOS4_GPX0(6),S3C_GPIO_PULL_NONE);
misc_register(&gpio_dev);
return 0;
}
static int gpio_remove(struct platform_device *pdv){
printk(KERN_EMERG "\tgpio removed!\n");
misc_deregister(&gpio_dev);
return 0;
}
static void gpio_shutdown(struct platform_device *pdv){
;
}
static int gpio_suspend(struct platform_device *pdv,pm_message_t pmt){
return 0;
}
static int gpio_resume(struct platform_device *pdv){
return 0;
}
static int gpio_read_dev_release(struct platform_device *dev)
{
printk("\tgpio read device.dec.release!!\n");
}
static struct platform_device s3c_device_gpio_read_dev = {
.name = DRIVER_NAME,
.id = -1,
.dev = {
.release = gpio_read_dev_release,
}
};
struct platform_driver gpio_driver = {
.probe = gpio_probe,
.remove = gpio_remove,
.shutdown = gpio_shutdown,
.suspend = gpio_suspend,
.resume = gpio_resume,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
static int gpio_init(void)
{
int DriverState=0,Devicestate=0;
printk(KERN_EMERG "\tstart gpio init!\n");
Devicestate = platform_device_register(&s3c_device_gpio_read_dev);
printk(KERN_EMERG "device state is %d \n",Devicestate);
DriverState = platform_driver_register(&gpio_driver);
if(DriverState==0)
{
printk(KERN_EMERG "\tgpio init OK,DriverState is %d!\n",DriverState);
}
return 0;
}
static void gpio_exit(void)
{
printk(KERN_EMERG "gpio module exit!\n");
platform_device_unregister(&s3c_device_gpio_read_dev);
platform_driver_unregister(&gpio_driver);
gpio_free(EXYNOS4_GPC0(3));
gpio_free(EXYNOS4_GPX0(6));
}
module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");
#include
#include
#include
#include
#include
#include
int main(int agc,char **argv){
int fd,cmd;
char *hello_node = "/dev/gpio_read_dev";
if(strcmp(argv[1],"0")==0)
cmd=0;
if(strcmp(argv[1],"1")==0)
cmd=1;
if(cmd > 1)printf("cmd must be 0 or 1!\n");
/*O_RDWR只读打开,O_NDELAY非阻塞方式*/
if((cmd ==0)|(cmd ==1)){
if((fd = open(hello_node,O_RDWR|O_NDELAY))<0){
printf("APP open %s failed\n",hello_node);
}
else{
printf("cmd is %d,gpio_read is %d\n",cmd,ioctl(fd,cmd,0));
}
close(fd);
}
}
#!/bin/bash
obj-m += gpio_read.o
KDIR := /home/topeet/iTop4412_Kernel_3.0
PWD ?= $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o
学会使用函数platform_device_register在模块中注册设备