pos机装驱动,linux LED设备驱动开发源代码

 新闻资讯  |   2023-04-27 09:38  |  投稿人:pos机之家

网上有很多关于pos机装驱动,linux LED设备驱动开发源代码的知识,也有很多人为大家解答关于pos机装驱动的问题,今天pos机之家(www.poszjia.com)为大家整理了关于这方面的知识,让我们一起来看下吧!

本文目录一览:

1、pos机装驱动

pos机装驱动

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/errno.h>

#include <linux/types.h>

//使用宋宝华推荐的普通字符设备框架

#include <linux/fcntl.h>

#include <linux/cdev.h>

#include <linux/version.h>

#include <linux/vmalloc.h>

#include <linux/ctype.h>

#include <linux/pagemap.h>

#include <linux/device.h>

#include <asm/io.h>

//#include <asm/arch/regs-gpio.h> //2.6.12

#include <mach/regs-gpio.h> // 2.6.32

//LED1

#define LED1_OUT __raw_writel(( __raw_readl(S3C2410_GPBCON) & (~(3<<10)))|(1<<10),S3C2410_GPBCON) //

#define LED1_H __raw_writel(__raw_readl(S3C2410_GPBDAT)|(1<<5),S3C2410_GPBDAT)

#define LED1_L __raw_writel(__raw_readl(S3C2410_GPBDAT)&(~(1<<5)),S3C2410_GPBDAT)

//LED2

#define LED2_OUT __raw_writel(( __raw_readl(S3C2410_GPBCON) & (~(3<<12)))|(1<<12),S3C2410_GPBCON) //

#define LED2_H __raw_writel(__raw_readl(S3C2410_GPBDAT)|(1<<6),S3C2410_GPBDAT)

#define LED2_L __raw_writel(__raw_readl(S3C2410_GPBDAT)&(~(1<<6)),S3C2410_GPBDAT)

//LED3

#define LED3_OUT __raw_writel(( __raw_readl(S3C2410_GPBCON) & (~(3<<14)))|(1<<14),S3C2410_GPBCON) //

#define LED3_H __raw_writel(__raw_readl(S3C2410_GPBDAT)|(1<<7),S3C2410_GPBDAT)

#define LED3_L __raw_writel(__raw_readl(S3C2410_GPBDAT)&(~(1<<7)),S3C2410_GPBDAT)

//LED4

#define LED4_OUT __raw_writel(( __raw_readl(S3C2410_GPBCON) & (~(3<<16)))|(1<<16),S3C2410_GPBCON) //

#define LED4_H __raw_writel(__raw_readl(S3C2410_GPBDAT)|(1<<8),S3C2410_GPBDAT)

#define LED4_L __raw_writel(__raw_readl(S3C2410_GPBDAT)&(~(1<<8)),S3C2410_GPBDAT)

#define LEDOFF 0

#define LEDON 1

//#define DEMO_MAJOR 235 //静态分配

#define DEMO_MAJOR 0 //动态分配

#define DEMO_MINOR 0

int devmajor = DEMO_MAJOR;

int devminor = DEMO_MINOR;

dev_t dev = 0;

//设备结构

//设备结构

struct DEMO_dev

{

struct cdev cdev; /* Char device structure*/

};

struct DEMO_dev *DEMO_devices;

struct class *led_class;

int DEMO_open(struct inode *inode, struct file *filp)

{

struct DEMO_dev *demo_dev;

unsigned int temp;

filp->private_data=DEMO_devices;

//5

//LED1_OUT;

temp=__raw_readl(S3C2410_GPBCON); //__raw_readl函数在#include <asm/io.h> 定义

temp &= (~(3<<10))|(1<<10);

__raw_writel(temp,S3C2410_GPBCON);

//printk("S3C2410_GPBCON=%x,S3C2410_GPBDAT=%x\",S3C2410_GPBCON,S3C2410_GPBDAT);

//S3C2410_GPBCON=fb000010,S3C2410_GPBDAT=fb000014

//6

//LED2_OUT;

temp=__raw_readl(S3C2410_GPBCON);

temp &= (~(3<<12))|(1<<12);

__raw_writel(temp,S3C2410_GPBCON);

//7

// LED3_OUT;

temp=__raw_readl(S3C2410_GPBCON);

temp &= (~(3<<14))|(1<<14);

__raw_writel(temp,S3C2410_GPBCON);

//8

//LED4_OUT;

temp=__raw_readl(S3C2410_GPBCON);

temp &= (~(3<<16))|(1<<16);

__raw_writel(temp,S3C2410_GPBCON);

return 0;

}

int DEMO_release(struct inode *inode, struct file *filp)

{

return 0;

}

int DEMO_ioctl(struct inode *inode, struct file *filp,unsigned int cmd, unsigned long arg)

{

unsigned int temp;

//printk("**********arg=%d**************\",arg);

switch(cmd)

{

case LEDOFF:

{

if(arg == 1)

{

//printk("ioctl LED1_OFF successfully\");

//LED1_H;

temp=__raw_readl(S3C2410_GPBDAT);

temp|=(1<<5);

__raw_writel(temp,S3C2410_GPBDAT);

}

if(arg == 2)

{

//printk("ioctl LED2__OFF successfully\");

LED2_H;

}

if(arg == 3)

{

//printk("ioctl LED3_OFF successfully\");

LED3_H;

}

if(arg == 4)

{

//printk("ioctl LED4_OFF successfully\");

LED4_H;

}

return 0;

}

case LEDON:

{

if(arg == 1)

{

//printk("ioctl LED1_ON successfully\");

//LED1_L;

temp=__raw_readl(S3C2410_GPBDAT);

temp&=~(1<<5);

__raw_writel(temp,S3C2410_GPBDAT);

}

if(arg == 2)

{

//printk("ioctl LED2_ON successfully\");

LED2_L;

}

if(arg == 3)

{

//printk("ioctl LED3_ON successfully\");

LED3_L;

}

if(arg == 4)

{

//printk("ioctl LED4_ON successfully\");

LED4_L;

}

return 0;

}

default:

printk("ioctl error successfully\");

return -EFAULT;

}

}

//也可以使用DEMO_write来替代DEMO_ioctl

ssize_t DEMO_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)

{

int cmd;

int temp;

if(copy_from_user(&cmd,buf,4))

{

printk("copy_from_user erro\");

return -EFAULT;

}

printk("cmd=%d\",cmd);

switch(cmd)

{

case LEDOFF:

{

//printk("ioctl LEDOFF successfully\");

//LED1_H;

temp=__raw_readl(S3C2410_GPBDAT);

temp|=(1<<5);

__raw_writel(temp,S3C2410_GPBDAT);

return 0;

}

case LEDON:

{

//printk("ioctl LEDON successfully\");

LED1_L;

return 0;

}

default:

// printk("ioctl error successfully\");

return -EFAULT;

}

}

ssize_t DEMO_read(struct file *filp,char __user *buff,size_t size,loff_t *offp)

{

int count;

count = sizeof(int);

int temp;

int status;

temp=__raw_readl(S3C2410_GPBDAT);

if(temp&(1<<5)==0)

status=0;

else

status=1;

if (copy_to_user(buff, &status, count))

{

return -EFAULT;

}

return 0;

}

struct file_operations DEMO_fops = {

.owner = THIS_MODULE,

.ioctl = DEMO_ioctl,

.open = DEMO_open,

.write = DEMO_write,

.read = DEMO_read,

.release=DEMO_release,

};

/*******************************************************

MODULE ROUTINE

*******************************************************/

//卸载

void DEMO_cleanup_module(void)

{

//dev_t devno = MKDEV(DEMO_MAJOR, DEMO_MINOR);

if (DEMO_devices)

{

cdev_del(&DEMO_devices->cdev);//5 从系统中移除一个字符设备

kfree(DEMO_devices);

}

device_destroy(led_class, MKDEV(devmajor, 0)); //delete device node under /dev

class_destroy(led_class); //delete class created by us

unregister_chrdev_region(dev,1);// 6 释放设备编号

}

//挂载

int DEMO_init_module(void)

{

int result;

if(devmajor)

{

dev = MKDEV(devmajor, devminor); // 1 获得设备号

result = register_chrdev_region(dev, 1, "led"); // 2 分配设备编号

}

else

{

result = alloc_chrdev_region(&dev, devminor, 1, "led_driver");

// //2 动态分配设备编号

devmajor = MAJOR(dev);

}

if (result < 0)

{

printk(KERN_WARNING "scull: can't get major %d\", devmajor);

return result;

}

printk(KERN_WARNING "led get major: %d\", devmajor);

DEMO_devices = kmalloc(sizeof(struct DEMO_dev), GFP_KERNEL);//分配内存给本设备结构体

if (!DEMO_devices)

{

result = -ENOMEM;

goto fail;

}

memset(DEMO_devices, 0, sizeof(struct DEMO_dev));

cdev_init(&DEMO_devices->cdev, &DEMO_fops);//(1) // 字符设备的注册,即将结构嵌入到自己的设备中

DEMO_devices->cdev.owner = THIS_MODULE;

DEMO_devices->cdev.ops = &DEMO_fops; //(2)

result = cdev_add (&DEMO_devices->cdev, dev, 1);// 4 把本设备放内核中

if(result)

{

printk(KERN_NOTICE "Error %d adding DEMO\", result);

goto fail;

}

//创建节点方法2 使用函数自动创建 方法1是手动创建#mknod /dev/test c 235 0

// 下面方法2.6.32支持。2.6.18 2.6.12不支持

/* create your own class under /sysfs 2.6.32*/

led_class = class_create(THIS_MODULE, "led_class");

if(IS_ERR(led_class))

{

printk("Err: failed in creating class.\");

return -1;

}

/* register your own device in sysfs, and this will cause udev to create corresponding device node */

device_create( led_class, NULL, MKDEV(devmajor, 0), NULL, "led_driver");

return 0;

fail:

DEMO_cleanup_module(); //注销

return result;

}

module_init(DEMO_init_module); //注册

module_exit(DEMO_cleanup_module); //注销

MODULE_AUTHOR("hui");

MODULE_LICENSE("GPL");

/*****************************************************************

mini2440 应用程序测试源代码:

*****************************************************************/

//#bash-2.05b# insmod led.ko

//Using led.ko

//#bash-2.05b# ./user

//#bash-2.05b# mknod /dev/led_su c 235 0

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#define MYLED "/dev/led_driver"

void Delay_MS( unsigned int time) //50 ns

{

unsigned int i,j;

for ( i=0; i<time; i++)

{

for(j=0;j<30000;j++)

{

}

}

}

int main(void)

{

int fd,i=0;

int cmd;

int status;

fd = open(MYLED,O_RDWR,0666);

if (fd < 0)

{

perror("open device led_driver error\");

exit(1);

}

printf("open device led_driver success!\");

while(i<9)

{

ioctl(fd, 1, 1);

ioctl(fd, 1, 2);

ioctl(fd, 1, 3);

ioctl(fd, 1, 4);

//sleep(1);

Delay_MS(200);

ioctl(fd, 0, 1);

ioctl(fd, 0, 2);

ioctl(fd, 0, 3);

ioctl(fd, 0, 4);

i++;

}

close(fd);

return 0;

}

以上就是关于pos机装驱动,linux LED设备驱动开发源代码的知识,后面我们会继续为大家整理关于pos机装驱动的知识,希望能够帮助到大家!

转发请带上网址:http://www.poszjia.com/news/34387.html

你可能会喜欢:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 babsan@163.com 举报,一经查实,本站将立刻删除。