//寄存器变量
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
//指针操作中,加减以指向的数据大小为单位
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
ioumap(gpfcon);
static int first_drv_open(struct inode *inode,struct file *file)
{
//printk("first_drv_open\n");
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2))|(0x1<<(5*2))|(0x1<<(6*2)));
return 0;
}
static ssize_t first_drv_write(struct file *file,const char __user *buf,size_t count,loff_t * ppos)
{
int val;
//得到数据
copy_from_user(&val,buf,count);
if(val ==1)
{
*gpfdat &= ~((1<<4)|(1<<5)|(1<<6));
}
else
{
*gpfdat |=(1<<4)|(1<<5)|(1<<6);
}
// printk("first_drv_write\n");
return 0;
}
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if(argc != 2)
{
printf("Usage :\n");
printf("%s " ,argv[0]);
return 0;
}
if(strcmp(argv[1],"on")== 0)
{
val=1;
}
else
{
val =0;
}
write(fd, &val, 4);
return 0;
}
/*
* 执行insmod命令时就会调用这个函数
*/
static int __init s3c24xx_leds_init(void)
//static int __init init_module(void)
{
int ret;
int minor = 0;
//映射地址
gpio_va = ioremap(0x56000000,0x100000);
if(!gpio_va)
{
return -EIO;
}
/* 注册字符设备
* 参数为:主设备号,设备名字,file_opreation结构
* 主设备号同file operation联系起来
* 当填入的主设备号为0的时候,系统自动分配主设备号
*/
//注册字符设备
ret = register_chrdev(LED_MAJOR,DEVICE_NAME,&s3c24xx_leds_fops);
if(ret <0)
{
printk(DEVICE_NAME"can`t be register\n");
return 0;
}
// 注册设备类;
leds_class = class_create(THIS_MODULE,"leds");
if(IS_ERR(leds_class))
return PTR_ERR(leds_class);
//注册led类型设备:
leds_class_devs[0] = class_device_create(leds_class,NULL,MKDEV(LED_MAJOR, 0), NULL, "leds");
//这一步,分别注册了led 1,2,3从而可以通过次设备号来控制设备
for(minor =1;minor <4;minor++)
{
leds_class_devs[minor] =class_device_create(leds_class,NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);
if(unlikely(IS_ERR(leds_class_devs[minor])))
return PTR_ERR(leds_class_devs[minor]);
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
/* 应用程序对设备文件/dev/leds执行open(...)时,
* 就会调用s3c24xx_leds_open函数
*/
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
//取次设备号:
int minor = MINOR(inode->i_rdev);
switch (minor)
{
//0 对所有灯的操作
case 0:
{
//配置3引脚为输出:
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
GPFCON &= ~(0x3<<(4*2));
GPFCON |= (1<<(4*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
GPFCON &= ~(0x3<<(5*2));
GPFCON |= (1<<(5*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
GPFCON &= ~(0x3<<(6*2));
GPFCON |= (1<<(6*2));
//初始化都为零
// 都输出0
//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
GPFDAT &= ~(1<<4);
//s3c2410_gpio_setpin(S3C2410_GPF5, 0);
GPFDAT &= ~(1<<5);
//s3c2410_gpio_setpin(S3C2410_GPF6, 0);
GPFDAT &= ~(1<<6);
//修改led状态为关闭
down(&leds_lock);
leds_status = 0x0;
up(&leds_lock);
}
case 1: /* /dev/led1 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF4, 0);
down(&leds_lock);
leds_status &= ~(1<<0);
up(&leds_lock);
break;
}
case 2: /* /dev/led2 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 0);
leds_status &= ~(1<<1);
break;
}
case 3: /* /dev/led3 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF6, 0);
down(&leds_lock);
leds_status &= ~(1<<2);
up(&leds_lock);
break;
}
}
return 0;
}
static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
//注意,这里传输的是文件,所以我们要注意路径
int minor = MINOR(file->f_dentry->d_inode->i_rdev);
char val;
copy_from_user(&val, buf, 1);
switch (minor)
{
case 0: /* /dev/leds */
{
s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
down(&leds_lock);
leds_status = val;
up(&leds_lock);
break;
}
case 1: /* /dev/led1 */
{
s3c2410_gpio_setpin(S3C2410_GPF4, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<0);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<0);
up(&leds_lock);
}
break;
}
case 2: /* /dev/led2 */
{
s3c2410_gpio_setpin(S3C2410_GPF5, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<1);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<1);
up(&leds_lock);
}
break;
}
case 3: /* /dev/led3 */
{
s3c2410_gpio_setpin(S3C2410_GPF6, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<2);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<2);
up(&leds_lock);
}
break;
}
}
return 1;
}
4 .编写测试函数:
/*
* ledtest
*/
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s \n" ,file);
printf("eg. \n");
printf("%s /dev/leds on\n", file);
printf("%s /dev/leds off\n", file);
printf("%s /dev/led1 on\n", file);
printf("%s /dev/led1 off\n", file);
}
int main(int argc, char **argv)
{
int fd;
char* filename;
char val;
if (argc != 3)
{
print_usage(argv[0]);
return 0;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("error, can't open %s\n", filename);
return 0;
}
if (!strcmp("on", argv[2]))
{
// 亮灯
val = 0;
write(fd, &val, 1);
}
else if (!strcmp("off", argv[2]))
{
// 灭灯
val = 1;
write(fd, &val, 1);
}
else
{
print_usage(argv[0]);
return 0;
}
return 0;
}
函数总结:
MINOR函数:取次设备号。
down 下拉
up 互斥锁
//定义互斥锁
static DECLARE_MUTEX(leds_lock); // 定义赋值