1.首先在驱动文件开头定义用于保存带操作IOK口的unsigned long变量,并且要用volatile修饰。
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
2.在入口函数中对寄存器进行映射。
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
//该函数的第一个参数为物理起始地址,第二个参数位长度。
gpfdat = gpfcon + 1;
3.在入口函数中建立了映射,就要在出口函数中取消映射。
iounremap(gpfcon);
4.要在open函数中配置引脚。
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
5.怎样根据应用程序的参数来控制外设(LED)?
write(fd,&val,4);
第一个参数是打开的文件,fd = open("xxx");
第二个参数是&val相当于驱动write函数的buff。
第三个参数是长度。
write函数格式定义如下:
static ssize_t first_drv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
copy_from_user(&val,buf,count);
通过copy_from_user这个函数将用户空间的数据拷贝到内核空间中。
copy_to_user(&val,buf,count);
将内核空间的数据拷贝到用户空间。
然后对val的值进行判断
if(val == 1)
{
//点灯
}
else
{
//熄灯
}
}
测试程序如下:
int main(int argc,char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz",O_RDWR);
if(fd < 0)
printf("file can't open!
");
if(argc != 2)
printf("two parameter
");
write(xxx);
}