?? accelerometer.c
字號:
/* * Accelerometer demo driver for Linux * * Copyright (C) 2002 Alvin Tang <alvin.tang@intel.com> * Copyright (C) 2007 Socrates <yangyj.ee@gmail.com> * * This driver allows you to access the contents of accelerometer sensor on * the Sitsang board. * * The device file for Accelerometer is "/dev/acce". You can create this file * handly by * mknod /dev/acce c 10 241 * * Example code for reading data in user space: * ------------------------------------------------------------------- * struct acce_point{ * unsigned int x; * unsigned int y; * }tmp; * * int main(int argc, char **argv) * { * .... * * fd=open("/dev/acce", O_RDONLY); * .... * * read(fd, &tmp, sizeof(acce_point)); * .... * * close(fd); //close device file * } * -------------------------------------------------------------------- * */#define SITSANG_ACCE_VERSION "1.1"#define SITSANG_ACCE_MINOR 241#include <linux/module.h>#include <linux/sched.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/fcntl.h>#include <linux/init.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/hardware.h>#include <asm/arch/pxa-regs.h>#include <asm/arch/sitsang.h>#include <asm/system.h>#include <linux/delay.h>static struct acce_point{ unsigned int x; unsigned int y;}acce_status;static ssize_t acce_read(struct file * file, char * buf, size_t count, loff_t *ppos ){ ssize_t ret = 0; while ( ret + sizeof (struct acce_point) <= count ) { acce_status.x = SITSANG_AXHR_RD & 0x0000ffff; acce_status.y = SITSANG_AYHR_RD & 0x0000ffff; if (copy_to_user(buf + ret, &acce_status, sizeof (struct acce_point))){ printk("copy to user failed\n"); return -EFAULT; } ret += sizeof (struct acce_point); } return ret;}static int acce_open( struct inode *inode, struct file *file ){ SITSANG_PCR_RW |= SITSANG_PCR_ACC_ON | SITSANG_PCR_PER_ON; mdelay(100); return 0;}static int acce_release( struct inode *inode, struct file *file ){ SITSANG_PCR_RW &= ~SITSANG_PCR_ACC_ON; return 0;}static struct file_operations acce_fops = { .owner = THIS_MODULE, .read = acce_read, .open = acce_open, .release = acce_release,};static struct miscdevice acce_dev = { SITSANG_ACCE_MINOR, "accelerometer", &acce_fops};static int __init acce_init(void){ int ret; ret = misc_register( &acce_dev ); if (ret) { printk(KERN_ERR "acce: can't misc_register on minor=%d\n", SITSANG_ACCE_MINOR); goto out; } ret = 0; printk(KERN_INFO "Sitsang Accelerometer Sensor driver v" SITSANG_ACCE_VERSION "\n");out: return( ret );}static void __exit acce_cleanup_module (void){ misc_deregister( &acce_dev );}module_init(acce_init);module_exit(acce_cleanup_module);MODULE_AUTHOR("Alvin Tang <alvin.tang@intel.com>");MODULE_DESCRIPTION("Accelerometer Sensor Driver for Sitsang Board");MODULE_LICENSE("GPL");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -