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 ...
随机推荐
- HTML5移动web横屏字体变大
html{ -webkit-text-size-adjust:none; -ms-text-size-adjust:none; -moz--text-size-adjust:none; text-si ...
- sql server 批量导出存储过程
sys.syscomments:包含数据库中每个视图.规则.默认值.触发器.CHECK 约束.DEFAULT 约束和存储过程的项.text 列包含原始的 SQL 定义语句.(简单点说,这个系统表存储了 ...
- 双硬盘Win7装Ubuntu 12.04经验并解决无线网络不能使用问题
RFKill Many computer systems contain radio transmitters, including Wi-Fi, Bluetooth, and 3G devices. ...
- python -c 处理shell字符串
$test="hello world" $python -c "print '$test'.split()[1]" world 或者 $test="h ...
- 自己主动化測试使用mybatis更新数据库信息实例
代码例如以下: mybatis配置文件: <? xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- android源码相关网站
https://android.googlesource.com/ google的android源码网站 http://source.android.com/ android网站 git://code ...
- CentOS 6.3系统安装配置KVM虚拟机
作业环境 服务器端 操作系统:CentOS 6.3 final x86_64 IP: 133.133.10.50 Hostname:myKVM KVM:qemu-kvm-0.12.1.2-2.29 ...
- Chrome内置的断网Javascript 小游戏脚本示范
//示范面向对象 this 作用域 闭包 单例模式很好的示范 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. // ...
- JS 校验,检测,验证,判断函数集合
http://jc-dreaming.iteye.com/blog/754690 /** *判断对象是否为空 *Check whether string s is empty. */ funct ...
- 【ASP.NET MVC系列】浅谈MVC
描述 本篇文章主要概述ASP.NET MVC,具体包括如下内容: 1.MVC模式概述 2.WebForm概述 3.WebForm与MVC区别 4.ASP.NET MVC发展历程 5.运用程序结构 6. ...