2440_spi驱动学习

#include <linux/miscdevice.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>






#define DEVICE_NAME     "spi"   /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define SPI_MAJOR    231         /* 主设备号 */




static unsigned long *spi_gpacon;


static unsigned long *spi_gpadat;


static unsigned long *spi_clkcon;


static unsigned long *spi_spcon0;


static unsigned long *spi_spsta0;


static unsigned long *spi_gppin0;


static unsigned long *spi_gppre0;


static unsigned long *spi_sptdat0;


static unsigned long *spi_sprdat0;


static int __init s3c2440_spi_init(void)
{
spi_gpacon = ioremap(0x56000000,4);


spi_gpadat = ioremap(0x56000004,4);


spi_clkcon = ioremap(0x4c00000c,4);


spi_spcon0 = ioremap(0x59000000,4);




spi_spsta0 = ioremap(0x59000004,4);


spi_gppin0 = ioremap(0x59000008,4);


spi_gppre0 = ioremap(0x5900000c,4);


spi_sptdat0 = ioremap(0x59000010,4);


spi_sprdat0 = ioremap(0x59000014,4);




ret = resgister_chrdv(SPI_MAJOR,DEVICE_NAME,&spi_fops);


if(ret<0)
{ ptintk("cpld_io:can't get major number\n");
  return ret;
}


#ifdef CONFIG_DEVFS_FS


ret = devfs_mk_cdev(MKDEV(SPI_MAJOR,0),S_IFCHR|S_IRUGO|S_IWUSR,DEVICE_NAME);


if(ret)
{ unregister_chrdev(SPI_MAJOR,DEVICE_NAME);
  printk("s3c2440-spi0:cant make char device fo devfs\n");


  return  ret; 
}
#endif


printk("s3c2440-spi0 driver initial\n")
return 0;
}


static void __exit s3c2440_spi_exit(void)
  {
       #ifdef CONFIG_DEVFS_FS
       devfs_remove(DEVICE_NAME);
           
       #endif
       unregister_chrdev(SPI_MAJOR,DEVICE_NAME);
       printk("s3c2440-spi0 driver rm \n");


   }


static int s3c2440_spi_open(struct inode *inode, struct file *file)


{
*spi_gpacon = *spi_gpacon&(~(1<<15));
*spi_gpadat = *spi_gpadat|(1<<15);
 
 s3c2410_gpio_cfgpin(S3C2410_GPE11,S3C2410_GPE11_SPIMISO0);
 s3c2410_gpio_cfgpin(S3C2410_GPE12,S3C2410_GPE11_SPIMOSI0);


 s3c2410_gpio_cfgpin(S3C2410_GPE13,S3C2410_GPE11_SPICLK0);




 s3c2410_gpio_pullup(S3C2410_GPE11,1);
 s3c2410_gpio_pullup(S3C2410_GPE12,1);
 s3c2410_gpio_pullup(S3C2410_GPE13,1);
  




 s3c2410_gpio_setpin(S3C2410_GPE11,1);
 s3c2410_gpio_setpin(S3C2410_GPE12,1);
 s3c2410_gpio_setpin(S3C2410_GPE13,1);
    *spi_clkcon  |= (1<<18) ;
    *spi_sppre0 = 0x0f;
    *spi_spcon0 = (0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|(0<<0);
    *spi_sppin0 = (0<<2)|(1<<1)|(0<<0);
return 0;


}


static ssize_t s3c2440_spi_write(struct file *filp,char __user *buf,size_t count,loff_t *ops)
{
  unsigned char datatx[2]
  int x=0;


if(count>1024)
 count = 1024;
if(copy_from_user((void*)datatx,buf,count))
  { return -EFAULT;
     }
*spi_gpadat = *spi_gpadat&(~(1<<15));
for(x=0;x<count;x++)
 { while((*spi_spsta0&0x01)==0);
         *spi_sptdat0 = datatx[x];
                         udelay(8);
   }
*spi_gpadat = *spi_gpadat|(1<<15);
return x;




}






static ssize_t s3c2440_spi_read(struct file *filp,char __user *buf,size_t count,loff_t *ops)
{
unsigned char datarx[2]
  int x=0;


if(count>1024)
  count = 1024;




if(copy_to_user((void*)datarx,buf,count))
  { return -EFAULT;
     }


*spi_gpadat = *spi_gpadat&(~(1<<15));
for(x=0;x<count;x++)
 {       
         while((*spi_spsta0&0x01)==0);
          datarx[x]=*spi_sptdat0;
          *spi_sptdat0 = 0xff;
          udelay(8);
   }
*spi_gpadat = *spi_gpadat|(1<<15);
 copy_to_user(buf,(void*)datarx,count);
 return x;


}


static  s3c2440_spi_close(struct inode *inode, struct file *file)
{ *spi_gpadat = *spi_gpadat|(1<<15);
        return 0;
}


static struct file_operations  s3c2440_spi_fops={


  
    .owner   =   THIS_MODULE,    
    .open    =  s3c2440_spi_open,
    .read    =   s3c2440_spi_read,
    .write   =   s3c2440_spi_write,
    .release =  s3c2440_spi_close, 


};


module_init(s3c2440_spi_init);
module_exit(s3c2440_spi_exit);


/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("hello");             // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 SPI Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的协议

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值