应用QQ2440(s3c2440)ARM开发板驱动MMA7455加速度计的linux设备驱动编写

课题水下机器人需要测定水下机器人的位姿,为此应用了加速度计MMA7455,该传感器可以用SPI或I2C读取数字信号到MCU。驱动MMA7455在atmega128上已经实现,但是由于mega128的速度、资源等瓶颈使得继续开发受到一定限制,故改用arm处理器。 在ar
摘要由CSDN通过智能技术生成


课题水下机器人需要测定水下机器人的位姿,为此应用了加速度计MMA7455,该传感器可以用SPI或I2C读取数字信号到MCU。

驱动MMA7455在atmega128上已经实现,但是由于mega128的速度、资源等瓶颈使得继续开发受到一定限制,故改用arm处理器。

 

在arm处理器上运行linux操作系统,要完成对MMA7455加速度计的驱动需要了解linux下的设备驱动程序,因此编写了相应的设备驱动程序。

在学习过程中Makefile的编写非常重要,对于我这样的菜鸟来说感觉makefile是设备驱动的一大障碍,试了好多各种各样的makefile,最终发现一个好用的makefile,而且似乎可以通用,于是记录下来了:http://blog.csdn.net/joygo007/article/details/6639368

我用的是SPI来驱动MMA7455,因为相对I2C来说,我觉得SPI还是稍微简单些。

先将代码附上,再在后面慢慢分享从中遇到的各种困难以及解决的过程。

 

Code1:linux 下针对MMA7455的SPI设备驱动程序

 

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/fs.h>//fops
#include <linux/module.h>
#include <linux/init.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/miscdevice.h>
#include <asm/arch/regs-gpio.h>//gpio寄存器地址头文件
#include <asm/hardware.h>//s3c2410_gpio_setpin等函数
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>

#define DEVICE_NAME "spi_gh"	//设备名称
#define SPI_MAJOR_NUM 236	//主设备号

//int whtaever=0;
volatile int *spi_gpecon=NULL;//GPG Part define  
volatile int *spi_gpedat=NULL;
volatile int *spi_gpeup=NULL; 
volatile int *s3c2440_clkcon=NULL;  
volatile int *spi_spcon0=NULL;//SPI Part define  
volatile int *spi_spsta0=NULL;  
volatile int *spi_sppin0=NULL;
volatile int *spi_sppre0=NULL;
volatile int *spi_sptdat0=NULL;  
volatile int *spi_sprdat0=NULL;

#define SPI_TXRX_READY      (((*spi_spsta0)&0x1) == 0x1)  
int loopChar=0x88;  

//定义SPI对应的引脚号
static unsigned long spi_pin_tab[]={
	S3C2410_GPE11,
	S3C2410_GPE12,
	S3C2410_GPE13,
	S3C2410_GPG2,

};

//配置SPI引脚功能
static unsigned long spi_pin_cfg_tab[]={
	S3C2410_GPE11_SPIMISO0,
	S3C2410_GPE12_SPIMOSI0,
	S3C2410_GPE13_SPICLK0,
	S3C2410_GPG2_OUTP,
};
/*======================================
驱动程序函数:__open
功能说明	:挂载模块退出程序
=======================================*/
static int spi_open(struct inode *inode,struct file *filp)  
{ 
	*s3c2440_clkcon |=0x40000;  
    printk("s3c2440_clkcon=%08X\n",*s3c2440_clkcon);  

    *spi_sppre0=0x18;       
    printk("spi_sppre0=%02X\n",*spi_sppre0);  
    *spi_spcon0=(0<<6)|(0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|(0<<0);  
    printk("spi_spcon0=%02X\n",*spi_spcon0);  
    *spi_sppin0=(0<<2)|(0<<0);  
	    printk("spi_sppin0=%02X\n",*spi_sppin0);  
	printk("KKK:OPEN OK\n");  
     return 0;  

}  

/*============================================
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值