pos機裝驅(qū)動,linux LED設(shè)備驅(qū)動開發(fā)源代碼

 新聞資訊  |   2023-04-27 09:38  |  投稿人:pos機之家

網(wǎng)上有很多關(guān)于pos機裝驅(qū)動,linux LED設(shè)備驅(qū)動開發(fā)源代碼的知識,也有很多人為大家解答關(guān)于pos機裝驅(qū)動的問題,今天pos機之家(www.tjfsxbj.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、pos機裝驅(qū)動

pos機裝驅(qū)動

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/errno.h>

#include <linux/types.h>

//使用宋寶華推薦的普通字符設(shè)備框架

#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 //靜態(tài)分配

#define DEMO_MAJOR 0 //動態(tài)分配

#define DEMO_MINOR 0

int devmajor = DEMO_MAJOR;

int devminor = DEMO_MINOR;

dev_t dev = 0;

//設(shè)備結(jié)構(gòu)

//設(shè)備結(jié)構(gòu)

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函數(shù)在#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 從系統(tǒng)中移除一個字符設(shè)備

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 釋放設(shè)備編號

}

//掛載

int DEMO_init_module(void)

{

int result;

if(devmajor)

{

dev = MKDEV(devmajor, devminor); // 1 獲得設(shè)備號

result = register_chrdev_region(dev, 1, "led"); // 2 分配設(shè)備編號

}

else

{

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

// //2 動態(tài)分配設(shè)備編號

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);//分配內(nèi)存給本設(shè)備結(jié)構(gòu)體

if (!DEMO_devices)

{

result = -ENOMEM;

goto fail;

}

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

cdev_init(&DEMO_devices->cdev, &DEMO_fops);//(1) // 字符設(shè)備的注冊,即將結(jié)構(gòu)嵌入到自己的設(shè)備中

DEMO_devices->cdev.owner = THIS_MODULE;

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

result = cdev_add (&DEMO_devices->cdev, dev, 1);// 4 把本設(shè)備放內(nèi)核中

if(result)

{

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

goto fail;

}

//創(chuàng)建節(jié)點方法2 使用函數(shù)自動創(chuàng)建 方法1是手動創(chuàng)建#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;

}

以上就是關(guān)于pos機裝驅(qū)動,linux LED設(shè)備驅(qū)動開發(fā)源代碼的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機裝驅(qū)動的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://www.tjfsxbj.com/news/34387.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報,一經(jīng)查實,本站將立刻刪除。