//This file for explain how to use a simple driver
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
// GPIO DEVICE MAJOR
#define SIMPLE_GPIO_MAJOR 151
#define OURS_GPIO_DEBUG
#define VERSION "PXA270RP-V1.00-090224"
//define GPIO STATUS
#define GPIO_ON 0
#define GPIO_OFF 1
struct led_dev
{
struct cdev cdev;
unsigned char value;
};
struct led_dev *led_devp;
static int led_major=SIMPLE_GPIO_MAJOR;
static long *ioremap_addr;
void showversion(void)
{
printk("*********************************************\n");
printk("\t %s \t\n", VERSION);
printk("*********************************************\n\n");
}
// ------------------- READ ------------------------
ssize_t SIMPLE_GPIO_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_read [ --kernel--]\n");
#endif
return count;
}
// ------------------- WRITE -----------------------
ssize_t SIMPLE_GPIO_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
int tmp_buf;
#ifdef OURS_LED_DEBUG
printk ("SIMPLE_LED_write [ --kernel--]\n");
#endif
// -------------------------------------------
tmp_buf = buf[1];
tmp_buf = tmp_buf<<8;
tmp_buf = tmp_buf | buf[0];
#ifdef OURS_LED_DEBUG
printk("tmp = %x\n",tmp_buf);
#endif
outw(tmp_buf,ioremap_addr);
return count;
}
// ------------------- IOCTL -----------------------
ssize_t SIMPLE_GPIO_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long data)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_ioctl [ --kernel--]\n");
#endif
return 0;
}
// ------------------- OPEN ------------------------
ssize_t SIMPLE_GPIO_open (struct inode * inode ,struct file * file)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_open [ --kernel--]\n");
#endif
return 0;
}
// ------------------- RELEASE/CLOSE ---------------
ssize_t SIMPLE_GPIO_release (struct inode * inode ,struct file * file)
{
#ifdef OURS_GPIO_DEBUG
printk ("SIMPLE_GPIO_release [ --kernel--]\n");
#endif
outw(0x0000,ioremap_addr); // close the led ary, all led off
return 0;
}
// -------------------------------------------------
struct file_operations led_fops ={
.open= SIMPLE_GPIO_open,
.read= SIMPLE_GPIO_read,
.write= SIMPLE_GPIO_write,
.ioctl= SIMPLE_GPIO_ioctl,
.release= SIMPLE_GPIO_release,
};
static void led_setup_cdev(struct led_dev *dev,int index)
{
int err, devno=MKDEV(led_major,index);
cdev_init(&dev->cdev,&led_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&led_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Error %d adding led%d",err,index);
}
int led_init(void)
{
int result;
static long *addr_status;
dev_t dev=MKDEV(led_major,0);
if(led_major)
result=register_chrdev_region(dev,1,"led");
else
{
result=alloc_chrdev_region(&dev,0,1,"led");
led_major=MAJOR(dev);
}
if(result<0)
return result;
led_devp =kmalloc (sizeof(struct led_dev),GFP_KERNEL);
if (!led_devp)
{ result= - ENOMEM;
goto fail_malloc;
}
memset(led_devp,0,sizeof(struct led_dev));
led_setup_cdev(led_devp,0);
addr_status=ioremap(0x08000018,0x0f);
outb(0x20,addr_status); //select cpld2
ioremap_addr=ioremap(0x0c00c000,0x0f); //
outw(0x00ff,ioremap_addr); // open led ary, all led on
#ifdef OURS_LED_DEBUG
printk("remap address = %x [ --kernel--]\n",ioremap_addr);
#endif
showversion();
return 0;
fail_malloc:unregister_chrdev_region(dev,1);
return result;
}
void led_cleanup(void)
{
static long *addr_status;
outw(0x0000,ioremap_addr);
addr_status=ioremap(0x08000018,0x0f);
outb(0x00,addr_status); //select cpld2
cdev_del(&led_devp->cdev);
kfree(led_devp);
unregister_chrdev_region(MKDEV(led_major,0),1);
printk("led_cleanup\n");
}
MODULE_DESCRIPTION("simple led driver module");
MODULE_AUTHOR("guliangzeng");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("led module");
module_init(led_init);
module_exit(led_cleanup);
//***********************马铁旦和刘星亮作品******************************
#include
#include
#include
#include // open() close()
#include // read() write()
#define DEVICE_NAME "/dev/led_ary_ctl"
//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
int fd;
int ret;
unsigned char buf[2] ;
unsigned char c,r;
int i,j;
// begin of led ary
c = 1;
r = 1;
printf("\nstart led_driver test\n\n");
fd = open(DEVICE_NAME, O_RDWR);
printf("fd = %d\n",fd);
if (fd == -1) {
printf("open device %s error\n",DEVICE_NAME);
}
else {
while(1){
c = 1;
r = 1;
/* for (i=1;i<=8;i++) {
buf[0]=c;
buf[1]=~r; // row
for (j=1;j<=8;j++) {
write(fd,buf,2);
printf ("buf[0],buf[1]: [%x,%x]\n",buf[0],buf[1]);
//usleep(200000); // sleep 0.2 second
c = c<<1;
buf[0]=c; // column
}
c = 1;
r = r<<1;
}
*/ // close
//***************************************************************************************
for(i=1;i<6;i=i+2){
c = c<<1;
buf[0]=c;
r = r<<1;
buf[1]=~r;
for(j=1;j<7;j++){
write(fd,buf,2);
//printf ("buf[0],buf[1]: [%x,%x]\n",buf[0],buf[1]);
//usleep(200000); // sleep 0.2 second
c = c<<1;
buf[0]=c;
}
c = 1;
r = r<<1;
}
//*************************************************************************************
c = 1;
r = 1;
for(i=2;i<5;i=i+2){
c = c<<1;
buf[0]=c;
r = r<<2;
buf[1]=~r;
for(j=1;j<7;j=j+5){
write(fd,buf,2);
//printf ("buf[0],buf[1]: [%x,%x]\n",buf[0],buf[1]);
//usleep(200000); // sleep 0.2 second
c = c<<5;
buf[0]=c;
}
c = 1;
}
//**************************************************************************************
c = 1;
r = 1;
r = r<<7;
buf[0]=c;
buf[1]=~r; // row
for (j=1;j<=8;j++) {
write(fd,buf,2);
//printf ("buf[0],buf[1]: [%x,%x]\n",buf[0],buf[1]);
//usleep(200000); // sleep 0.2 second
c = c<<1;
buf[0]=c; // column
}
}
//**************************************************************************************
ret = close(fd);
//printf ("ret=%d\n",ret);
//printf ("close led_driver test\n");
}
return 0;
}// end main
阅读(1143) | 评论(0) | 转发(0) |