ad7888 linux driver
- /*
- ADCCONVERT.c :
- Generate ADC signals from SPI ports, as the A/D control signals.
- Author: Aaron Fu
- 2008-10-14
- */
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/slab.h> /* kmalloc */
- #include <linux/fs.h> /* struect file inode */
- #include <linux/errno.h> /* error codes */
- #include <linux/types.h> /* size_t */
- #include <linux/proc_fs.h> /* proc_fs */
- #include <linux/fcntl.h> /* O_ACCMODE */
- #include <linux/seq_file.h> /* seq_file */
- #include <linux/cdev.h> /* register char device */
- #include <asm/system.h> /* cli(), *_flags */
- #include <asm/uaccess.h> /* copy_*_user */
- #include <asm/delay.h> /* udelay */
- #include <asm/io.h> /* ioremap...*/
- #include "adconvert.h" /* local definitions */
- int adc_major = ADC_MAJOR;
- int adc_minor = 0;
- int adc_nr_devs = ADC_NR_DEVS;
- static int adcopen;
- struct cdev *adc_cdev;
- module_param( adc_major, int, S_IRUGO );
- module_param( adc_minor, int, S_IRUGO );
- module_param( adc_nr_devs, int, S_IRUGO );
- MODULE_LICENSE( "Aaron Fu" );
- MODULE_DESCRIPTION( "Iconic Shanghai Co.,LTD." );
- MODULE_AUTHOR( "GPL" );
- void adc_address_map(void){
- r_SPSTA0 = ioremap( rSPSTA0, 0x00000004 );
- r_SPPRE0 = ioremap( rSPPRE0, 0x00000004 ); // rSPPRE0
- r_SPCON0 = ioremap( rSPCON0, 0x00000004 ); // rSPCON0
- r_SPTDAT0 = ioremap( rSPTDAT0, 0x00000004 ); // rSPTDAT0
- r_SPRDAT0 = ioremap( rSPRDAT0, 0x00000004 );
- r_GPECON = ioremap( rGPECON, 0x00000004 ); //rGPECON
- r_GPEUP = ioremap( rGPEUP, 0x00000004 ); // rGPEUP
- r_GPHCON = ioremap( rGPHCON, 0x00000004 ); // rGPHCON
- r_GPHUP = ioremap( rGPHUP, 0x00000004 ); // rGPHUP
- r_GPHDAT = ioremap( rGPHDAT, 0x00000004 ); //rGPHDAT
- }
- void adc_cancel_add_map( void ){
- iounmap( r_SPSTA0 );
- iounmap( r_SPPRE0 );
- iounmap( r_SPCON0 );
- iounmap( r_SPTDAT0 );
- iounmap( r_SPRDAT0 );
- iounmap( r_GPECON );
- iounmap( r_GPEUP );
- iounmap( r_GPHCON );
- iounmap( r_GPHUP );
- iounmap( r_GPHDAT );
- }
- static void adc_spi_init( void ){
- int i;
- /*
- rSPPRE0 = 0x32;
- rSPCON0 = 0x1e;
- for( i = 0; i < 10; i++ )
- rSPTDAT0 = 0xff;
- rGPECON |= 0x0a800000;
- rGPECON &= ( ~0x05400000 );
- rGPEUP |= 0x3800;
- //GPH5----->CS
- rGPHCON |= 0x0400;
- rGPHCON &= ( ~0x0800 );
- rGPHUP &= ( ~0x20 );
- rGPHDAT |= 0x20;*/
- iowrite32( ioread32( r_SPSTA0 ) | 0x01, r_SPSTA0 );
- iowrite32( 0x4, r_SPPRE0 );
- iowrite32( 0x1e, r_SPCON0 );
- for( i = 0; i < 10; i++ ){
- iowrite32( 0xff, r_SPTDAT0 );
- }
- //for( i = 0; i < 10; i++ ){
- iowrite32( 0x00, r_SPRDAT0 );
- //}
- iowrite32( ioread32( r_GPECON ) | 0x0a800000, r_GPECON );
- iowrite32( ioread32( r_GPECON ) & ( ~0x05400000 ), r_GPECON );
- iowrite32( ioread32( r_GPEUP ) | 0x3800, r_GPEUP );
- iowrite32( ioread32( r_GPHCON ) | 0x0400, r_GPHCON );
- iowrite32( ioread32( r_GPHCON ) & ( ~0x0800 ), r_GPHCON );
- iowrite32( ioread32( r_GPHUP ) & ( ~0x20 ), r_GPHUP );
- iowrite32( ioread32( r_GPHDAT ) | ( 0x20 ), r_GPHDAT );
- }
- static struct file_operations adc_fops = { /* driver info */
- .owner = THIS_MODULE,
- .open = adc_open,
- .read = adc_read,
- .write = adc_write,
- .release = adc_release,
- };
- static ssize_t adc_write( struct file *file, const char __user *buf, size_t count, loff_t *offset ){
- int ret = 0;
- int i = 0;
- printk( "write count value = %d\n", count );
- dbuf = kmalloc( count * sizeof( unsigned char ), GFP_KERNEL);
- if ( dbuf == NULL ){
- printk( KERN_WARNING "write: dbuf alloc memory fail\n" );
- return -1;
- }
- memset( dbuf, 0, count *sizeof( unsigned char ) );
- copy_from_user( dbuf, buf, count );
- printk( "************** write bufx = %x *******************\n", buf );
- printk( "************* write bufs = %s *******************\n", buf );
- for( i = 0; i < count; i++ ){
- ADCTxdata[i] = dbuf[i];
- printk( "write adctx value [%d] = %c\n", i, ADCTxdata[i] );
- }
- kfree( dbuf );
- return ret;
- }
- static ssize_t adc_read( struct file *file, char __user *buf, size_t count, loff_t *offset ){
- int i = 0;
- int ret = 0;
- iowrite32( ioread32( r_SPCON0 ) | 0x1, r_SPCON0 );
- adc_convert();
- adc_convert();
- printk( "\n\n" );
- printk( "read count value = %d\n", count );
- dbuf = kmalloc( count * sizeof( unsigned char ), GFP_KERNEL );
- if ( dbuf == NULL ){
- printk( KERN_WARNING "read: dbuf alloc memory fail\n" );
- return -1;
- }
- memset( dbuf, 0, count * sizeof( unsigned char ) );
- for( i = 0; i < count; i++ ){
- dbuf[i] = ADCRxdata[i];
- printk( "read adctx value [%d] = %c\n", i, ADCTxdata[i] );
- }
- printk( "\n" );
- copy_to_user( buf, dbuf, count);
- printk( "************** read bufx = %x ************************\n", buf );
- printk( "************** read bufs = %s ************************\n", buf );
- kfree( dbuf );
- return ret;
- }
- void adc_convert( void ){
- /*rGPHDAT &= ( ~0x20 );
- adc_tx_data( ADCTxdata[0] );
- // ADCRXdata[0] = rSPRDATO;
- ADCRxdata[0] = r_SPRDAT0;
- adc_tx_data( 0xff );
- // ADCRXdata[1] = rSPRDATO;
- ADCRxdata[1] = r_SPRDAT0;
- rGPHDAT |= 0x20;*/
- iowrite32( ioread32( r_GPHDAT ) & ( 0x20 ), r_GPHDAT );
- adc_tx_data( ADCTxdata[0] );
- ADCRxdata[0] = r_SPRDAT0;
- adc_tx_data( 0xff );
- ADCRxdata[1] = r_SPRDAT0;
- iowrite32( ioread32( r_GPHDAT ) | ( 0x20 ), r_GPHDAT );
- }
- static void adc_tx_data( unsigned char data ){
- iowrite32( ioread32( r_SPSTA0 ) | 0x01, r_SPSTA0 );
- spi_poll_done();
- //rSPTDAT0 = data;
- iowrite32( data, r_SPTDAT0 );
- spi_poll_done();
- }
- void spi_poll_done( void ){
- //while ( !( rSPSTA0 & 0x01 ) )
- while ( !( ioread32( r_SPSTA0 ) & 0x01 ) )
- ;
- }
- int adc_release( struct inode *inode, struct file *filp ){
- adcopen--;
- module_put( THIS_MODULE );
- return 0 ;
- }
- int adc_init_module( void ){
- int result;
- dev_t dev = 0;
- if ( adc_major ){
- dev = MKDEV( adc_major, adc_minor );
- result = register_chrdev_region( dev, adc_nr_devs, "adc" );
- }else{
- result = alloc_chrdev_region( &dev, adc_minor, adc_nr_devs, "adc" );
- adc_major = MAJOR( dev );
- }
- adc_cdev = cdev_alloc();
- if ( adc_cdev == NULL ){
- return -1;
- }
- if ( result < 0 ){
- printk( KERN_WARNING "adc: can't get major %d\n", adc_major );
- return result;
- }
- printk( KERN_INFO "adc: this major is %d\n", adc_major );
- adcopen = 0;
- adc_cdev->ops = &adc_fops;
- cdev_init( adc_cdev, &adc_fops );
- cdev_add( adc_cdev, dev, 1 );
- adc_address_map();
- adc_spi_init();
- printk( KERN_INFO "*** adc_init() finished *** \n" );
- return 0;
- }
- static int adc_open( struct inode *inode, struct file *filp ){
- if ( adcopen == 0 )
- adcopen++;
- else{
- printk( KERN_ALERT "Another process open the char device\n" );
- return -1;
- }
- try_module_get( THIS_MODULE );
- return 0;
- }
- void adc_cleanup_module( void ){
- dev_t devno;
- adc_cancel_add_map();
- devno = MKDEV( adc_major, adc_minor );
- unregister_chrdev_region( devno, adc_nr_devs );
- cdev_del( adc_cdev );
- }
- module_init( adc_init_module );
- module_exit( adc_cleanup_module );
ad7888 linux driver的更多相关文章
- Linux driver 板级文件跟踪一般方法
/*********************************************************************************** * Linux driver ...
- OK335xS pwm buzzer Linux driver hacking
/**************************************************************************** * OK335xS pwm buzzer L ...
- linux driver开发
1 开发linux driver时的调试思路 基本上是打印调试,原因很简单,方便.或者使用工具挂住cpu.
- linux driver module
本文将对Linux系统中的sysfs进行简单的分析,要分析sysfs就必须分析内核的driver-model(驱动模型),两者是紧密联系的.在分析过程中,本文将以platform总线和spi主控制器的 ...
- linux driver ------ platform模型,驱动开发分析
一.platform总线.设备与驱动 在Linux 2.6 的设备驱动模型中,关心总线.设备和驱动3个实体,总线将设备和驱动绑定.在系统每注册一个设备的时候,会寻找与之匹配的驱动:相反的,在系统每注册 ...
- linux driver ------ GPIO的驱动编写和调用
判断哪些文件被编译进内核: 1.通过 make menuconfig 查看 2.比如查看gpio类型的文件,输入 ls drivers/gpio/*.o,有生成.o文件表示被编译进内核 在编写驱动程序 ...
- linux driver ------ platform模型,通过杂项设备(主设备号是10)注册设备节点
注册完设备和驱动之后,就需要注册设备节点 Linux杂项设备出现的意义在于:有很多简单的外围字符设备,它们功能相对简单,一个设备占用一个主设备号对于内核资源来说太浪费.所以对于这些简单的字符设备它们共 ...
- linux driver编译环境搭建和命令
首先将ubuntu14.04的内核升级到内核3.18.12. 其次,Ubuntu14.04上驱动编译命令 $ sudo make -C ~/linux-3.18.12/ M=`pwd` modules ...
- linux driver: input子系统
<韦东山Linux视频第2期_从零写驱动\第13课第1节 输入子系统概念介绍_P.wmv> 本视频对输入子系统的结构进行了详细的剖析,通过本视频,可以了解到input核心包括了设备和han ...
随机推荐
- Unity for Windows: II – Publishing Unity games to Windows Store
原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windo ...
- 使用RMAN方式清除
使用RMAN方式清除 RMAN清除方式会自动清除磁盘上的归档日志文件,同时会释放控制文件中对应的归档日志的归档信息. 可以基于不同的条件来清除归档日志,如基于SCN,基于SEQUENCE,基于TIME ...
- LDAP编辑器 LDAPAdmin
LDAPAdmin 是一个在 Windows 用来编辑 LDAP 账户信息的管理工具,采用 Delphi 开发.
- 本地ubuntu下pycharm 如何利用远程开发环境时显示图片
最近使用pycharm远程开发tensorflow,每次在想显示图像时,苦于不知怎么操作,就通过保存后再看结果,使得调试很不方便.今天打算解决这个问题,收获也是很多啊. 我首先参考了这两篇博客: ht ...
- Python操作redis字符串(String)详解 (三)
# -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...
- 使用zlib模块实现HTTP服务端与客户端实现传输数据压缩
现如今在处理http请求的时候,由于请求的资源较多,如果不启用压缩的话,那么页面请求的流量将会非常大.启用gzip压缩,在一定程度上会大大的提高页面性能. 因此这写一个使用Node.js实现在http ...
- centos 无法ping内网 Destination Host Unreachable
centos 突然无法ping内网了. 本来是一直是好好的. 在这之前,当前服务器(centos 192.168.1.30)大量的在操作内网192.168.1.20服务器的数据库.. 会不会是流量大了 ...
- atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval ....
atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval .... 1. 原因::: 不个object ...
- etcd+calico集群的部署
etcd单机模式 设置环境变量 1 export HostIP="192.168.12.50" 执行如下命令,打开etcd的客户端连接端口4001和2379.etcd互联端口238 ...
- 再谈API GateWay服务网关
前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...