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 ...
随机推荐
- PHP中多IP段权限控制方案
在某些项目中我们可能会用到根据IP段进行权限校验,比如不在我们配置的IP段内的用户访问某些页面或功能模块时,将提示其权限不够并禁止访问该页面的内容.鉴于项目中需求各异,下面只说下大致思路以及我个人的实 ...
- 淘宝网前端开发面试题(二)--JS 面试题
所有答案仅供参考,不负责答案对错(^_^) 1.js 是什么,js 和 html 的开发如何结合? js是javascript的缩写,是一种基于对象的.事件驱动的脚本语言.它一共由三个部分组成:分别是 ...
- Python编程-基础知识-python项目包和文件的管理以及如何引用相对路径的包和模块
目录 结构: core |____ __init__.py |____ basic |____ __init__.py |____ database |____ __init__. ...
- ASP.NET MVC4 Jquer 日期控件 测试范例
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&q ...
- Matlab interpgui
function interpgui(arg1,arg2) %INTERPGUI Behavior of interpolating functions. % Demonstrates interpo ...
- javascript&jquery 判断滚动到页面底部
js 判断滚动到页面底部 CreateTime--2018年4月14日10:13:07 Author:Marydon 1.使用场景: 滚动到屏幕底部,触发加载分页数据请求(qq空间,手机端) 2. ...
- Ubuntu14.04上深度学习Caffe库安装指南(CUDA7.5 + opencv3.1)
Ubuntu14.04上Caffe安装指南 安装的准备工作 首先,安装官方版Caffe时.假设要使用Cuda.须要确认自己确实有NVIDIA GPU. 安装Ubuntu时,将/boot 分区分大概20 ...
- javascript设计模式:构造器模式学习一
javascript 设计模式1.简介javascript是一种弱类型语言,不过类可以通过函数模拟出来最常见的实现方法如下:function Car(model){ this.model = mode ...
- ISO镜像安装UbuntuKylin 13.04 64位,启动菜单制作实例
1.将光盘镜像中的vmlinuz.efi.initrd.lz,和镜像本身(ubuntu....iso) 三个文件复制到U盘根目录下.如果下面的方法没成功启动,你可能要把U盘格式化为USB-HDD FA ...
- RBAC权限管理(转)
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...