Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1531253
  • 博文数量: 329
  • 博客积分: 2773
  • 博客等级: 少校
  • 技术积分: 4219
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:17
个人简介

淡定从容,宁静致远

文章分类

全部博文(329)

文章存档

2016年(4)

2015年(50)

2014年(68)

2013年(45)

2012年(162)

分类: LINUX

2012-09-17 08:51:56

 
#include
#include
#include
#include
 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 
// SERIAL_LED DEVICE MAJOR
#define SERIAL_LED_MAJOR    105
#define OURS_SERIAL_LED_DEBUG  
 
#define VERSION         "PXA270RP-V1.00-090224"
 
struct serial_led_dev
{  
 struct cdev cdev;
   unsigned char value;
};
struct serial_led_dev *serial_led_devp;
 
static int serial_led_major=SERIAL_LED_MAJOR;
 
 
void showversion(void)
{
        printk("*********************************************\n");
        printk("\t %s \t\n", VERSION);
        printk("*********************************************\n\n");
 
}
void write_bit(int data)
{
 
        GPCR2 |= (0x1 << 27);   //CLOCK
 
        if((data & 0x80) == 0x80)
        {
                GPSR2 |= (0x1 << 26);      
        }
        else
        {
                GPCR2 |= (0x1 << 26);
        }
 
        GPSR2 |= (0x1 << 27);   //CLOCK
 
}
 
 
void write_byte(int data)
{
        int i;
        for(i=0;i<8;i++)
        {
                write_bit( data << i );
        }
}
 
 
// ------------------- READ ------------------------
ssize_t SERIAL_LED_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
    #ifdef OURS_SERIAL_LED_DEBUG
        printk ("SERIAL_LED_read [ --kernel--]\n");
    #endif               
 
    return count;
}    
 
// ------------------- WRITE -----------------------
ssize_t SERIAL_LED_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
    #ifdef OURS_SERIAL_LED_DEBUG
            printk ("SERIAL_LED_write [ --kernel--]\n");
        #endif
       write_byte(* buf);
    return count;
}    
 
// ------------------- IOCTL -----------------------
ssize_t SERIAL_LED_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long data)
{
//struct serial_led_dev *dev=file->private_data;
     
#ifdef OURS_SERIAL_LED_DEBUG
            printk ("SERIAL_LED_ioctl [ --kernel--]\n");
        #endif
 
 
    return 0;
}
 
// ------------------- OPEN ------------------------
ssize_t SERIAL_LED_open (struct inode * inode ,struct file * file)
{
    #ifdef OURS_SERIAL_LED_DEBUG
            printk ("SERIAL_LED_open [ --kernel--]\n");
        #endif
    
    
    return 0;
}    
 
// ------------------- RELEASE/CLOSE ---------------
ssize_t SERIAL_LED_release (struct inode  * inode ,struct file * file)
{
    #ifdef OURS_SERIAL_LED_DEBUG
            printk ("SERIAL_LED_release [ --kernel--]\n");
        #endif
 
 
    return 0;
}
 
// -------------------------------------------------
struct file_operations serial_led_fops ={
 
    
    .open=        SERIAL_LED_open,
    
    .read=        SERIAL_LED_read,
    
    .write=        SERIAL_LED_write,
    
    .ioctl=             SERIAL_LED_ioctl,
    
    .release=    SERIAL_LED_release,
    
};
 
void gpio_init(void)
{
 
 // init GPIO
        GAFR2_U &=0xff0fffff;//SET GPIO91,90 I/O MODE
 
        GPDR2 |= (1<<26); // SET GPIO90 OUTPUT MODE
        GPCR2 |= (1<<26); // SET LOW LEVEL
        GPDR2 |= (1<<27); // SET GPIO91 OUTPUT MODE
        GPCR2 |= (1<<27); // SET LOW LEVEL  
}
static void serial_led_setup_cdev(struct serial_led_dev *dev,int index)
{  
 int err, devno=MKDEV(serial_led_major,index);
    cdev_init(&dev->cdev,&serial_led_fops);
     dev->cdev.owner=THIS_MODULE;
   dev->cdev.ops=&serial_led_fops;
  err=cdev_add(&dev->cdev,devno,1);
    if(err)
    printk(KERN_NOTICE "Error %d adding serial_led%d",err,index);
}
 
int serial_led_init(void)
{
int result;
 
dev_t dev=MKDEV(serial_led_major,0);
if(serial_led_major)
 result=register_chrdev_region(dev,1,"serial_led");
else
{
result=alloc_chrdev_region(&dev,0,1,"serial_led");
serial_led_major=MAJOR(dev);
}
if(result<0)
return result;
 
 
serial_led_devp =kmalloc (sizeof(struct serial_led_dev),GFP_KERNEL);
if (!serial_led_devp)
{ result= - ENOMEM;
 goto fail_malloc;
}
memset(serial_led_devp,0,sizeof(struct serial_led_dev));
 
 serial_led_setup_cdev(serial_led_devp,0);
printk("serial_led_init\n");  
  //init SERIAL_LED
   gpio_init();
showversion();
                                                                                                                 
return 0;
 
fail_malloc:unregister_chrdev_region(dev,1);
return result;
}
void serial_led_cleanup(void)
{
 cdev_del(&serial_led_devp->cdev);
  kfree(serial_led_devp);
  unregister_chrdev_region(MKDEV(serial_led_major,0),1);
  printk("serial_led_cleanup\n");
}
 
MODULE_DESCRIPTION("simple serial_led driver module");
MODULE_AUTHOR("guliangzeng");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_ALIAS("serial_led module");
 
module_init(serial_led_init);
module_exit(serial_led_cleanup);

#include
#include
#include

#include       // open() close()
#include      // read() write()


#define DEVICE_NAME "/dev/serial_led"


//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
        int fd;
    int ret;
    int i,count;
      int  buf[10] ={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
                    //0   1   2   3   4    5    6   7    8    9
 

    int  data[10];

        printf("\nstart serial_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)
            {
/*                    for(count=0;count<10;count++)
            {
                data[0] = buf[count];
                            ret=write(fd,data,1);
                            sleep(1);
               
                    }
            memset(data,0,10);
            for(count=9;count>=0;count--)
            {
                data[0] = buf[count];
                            ret=write(fd,data,1);
                            usleep(500);
               
                    }
*/            data[0] = buf[1];
                        ret=write(fd,data,1);
                        sleep(1);
            data[1] = buf[5];
                        ret=write(fd,data,1);
                        sleep(1);
           
            }

        }
        ret = close(fd);

        printf ("ret=%d\n",ret);
        printf ("close serial_led driver test\n");

        return 0;
}// end main


阅读(902) | 评论(0) | 转发(0) |
0

上一篇:GPIO实验

下一篇:继电器

给主人留下些什么吧!~~