ioctl函数驱动实例-接上一篇博客

1、命令码的封装

ioctl函数第二个参数封装一个命令码,只需要在驱动中解析这个命令码去操作对应的硬件的寄存器设置,可以先给用户空间内核空间建一个通用的命令码,相当于制定规则。

//命令码文件 ioctl.h,代码如下:
#ifndef __IOCTL_H__
#define __IOCTL_H__
//eg:封装一个控制灯亮灭的命令码,用户空间和内核空间无需读写数据,可以直接使用_IO
#define RED_ON _IO('a',0)   //灯开,参数不同就代表不同的功能
#define RED_OFF _IO('a',1)  //灯关

#endif

2、用户空间代码

在应用层代码中我需要告诉用户规则(命令码)是什么,需要声明上边封装的命令码,

//应用程序文件 ioctl_usr.c,代码如下:
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "ioctl.h"  //命令码定义声明

int main(int argc, const char *argv[])
{
	int fd;
	//rgb_led是我通过mknod创建的设备节点
    if((fd = open("/dev/rgb_led",O_RDWR))<0){
	   perror("open error\n");
	   return -1;
	}
	/*想要通过向驱动发送RED_ON、RED_OFF来控制灯亮灭,实现亮灭亮灭的效果*/
    while(1){
	 ioctl(fd,RED_ON);
	 sleep(1);
	 ioctl(fd,RED_OFF);
	 sleep(1);
	  
	}
	close(fd);
	return 0;
}

3、驱动代码

同样的在驱动代码中我需要告诉用户规则(命令码)是什么,需要声明上边封装的命令码

//驱动代码,
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include "ioctl.h"  //命令码定义声明

#define CDEV_NAME "cdev_name"
static unsigned int major = 0;

unsigned int *red_led_base;
unsigned int *green_led_base;
unsigned int *blue_led_base;
//对外设灯的初始化函数
void rgb_led_init(void)
{
  printk("%s----%s-----%d\n",__FILE__,__func__,__LINE__);
  //led init
  //close red led
  *red_led_base &= ~(1<<28);
  *(red_led_base + 1) |= (1<<28);
  *(red_led_base + 8) &= ~(0x3<<28);
  //close green led
  *green_led_base &= ~(1 <<13);
  *(green_led_base + 1) |=(1<<13);
  *(green_led_base + 7) &= ~(0x3<<26);
  //close blue led
  *blue_led_base &= ~(1<<12);
  *(blue_led_base + 1) |= (1 << 12);
  *(blue_led_base + 7) &= (0x3<< 24);
  *(blue_led_base + 7) |=(0x2 << 24);
  
}
int mycdev_open(struct inode *inode, struct file *file)
{
   printk("%s----%s-----%d\n",__FILE__,__func__,__LINE__);
   rgb_led_init();
   return 0;
}
ssize_t mycdev_read(struct file *file, char __user *user, size_t size, loff_t *offs)
{
   printk("%s----%s-----%d\n",__FILE__,__func__,__LINE__);
   return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *user, size_t size, loff_t *offs)
{
  printk("%s----%s-----%d\n",__FILE__,__func__,__LINE__);
  return 0;

}
long mycdev_ioctl(struct file *file, unsigned int cmd,unsigned long args)
{
//通过switch来判断命令码是什么,和我指定的规则相同的就去执行对应的功能(开灯、关灯)
   switch(cmd){
    case RED_ON:
		 *(red_led_base +0) |=(1<<28);
          break;
	case RED_OFF:
		*(red_led_base+0) &=~(1<<28);
		  break;
	default:
		 printk("input argument error\n");
		 return -EINVAL;	 	
   }
   return 0; 
}
int mycdev_close(struct inode *inode, struct file *file)
{
   printk("%s----%s-----%d\n",__FILE__,__func__,__LINE__);
   return 0;
}
struct file_operations fops = {
	.open = mycdev_open,
	.read = mycdev_read,
	.write = mycdev_write,
	.unlocked_ioctl = mycdev_ioctl,
	.release = mycdev_close,
};
static int __init mydev_init(void)
{
   //注册字符设备驱动
    major = register_chrdev(major,CDEV_NAME,&fops);
	if(major < 0)
	{
	   printk("register_chrdev error\n");
       return -EAGAIN;
	}
	printk("major = %d\n",major);
    //通过iormap函数将实际的物理地址转换为虚拟地址
	red_led_base = (unsigned int *)ioremap(0xc001a000,36);
	if(red_led_base ==NULL){
         printk("ioremap red led error\n");
		 return -ENOMEM;
	}
   	return 0;
}

static void __exit mydev_exit(void)
{
     //注销字符设备驱动
    unregister_chrdev(major,CDEV_NAME);
}

module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值