linux i2c数据 应用,linux下的I2C温度传感器应用

需求:

读嵌入式温度传感器的值。

已知条件:

*温度传感器已经在主板上焊好了。

*温度传感器型号为lm75

*linux自带驱动,在linux-2.6.20.21/drivers/hwmon/lm75.c

*温度传感器的电路

uid-22670933-id-1771674.html

问题与解答:

*linux自带的驱动怎么用的?

看lm75.c的代码,发现此温度传感器用的是I2C的总线。

代码中看到i2c驱动的接口定义如下,和以前做过的字符型驱动的接口不一样。

/* This is the driver that will be inserted */

staticstructi2c_driver lm75_driver = {

.driver = {

.name ="lm75",

},

.id  = I2C_DRIVERID_LM75,

.attach_adapter = lm75_attach_adapter,

.detach_client = lm75_detach_client,

};/* This is the driver that will be inserted */

static struct i2c_driver lm75_driver = {

.driver = {

.name = "lm75",

},

.id = I2C_DRIVERID_LM75,

.attach_adapter = lm75_attach_adapter,

.detach_client = lm75_detach_client,

};

在linux Device Driver这本书中搜,在此书中无介绍。

,讲得还不错。尤其是后者对路。I2C在读主板上的传感器的应用上应用比较多,但它是在比较新的版本的LINUX内核版本中才加进来的,难怪LDD这本书中没有讲了。

再看上面数据结构中的回调函数,具体干什么就比较清楚了。根据函数内容得知,驱动在加载的时候会自动检测I2C总线上的设备,因此会自动发现温度传感器。lm75_update_device()是对温度传感器进行轮询读数时用的函数。

根据文章指引,看设备的/sys/bus/i2c/。下面已经有了lm75传感器的数据,直接读是有数据的,而且数据会变化,如下:

root:~# cat /sys/bus/i2c/devices/0-0048/temp1_input

-31500

*读数为负。什么样的读数才是正常的?

在驱动代码里增加了调试信息,打印出传感器上读的寄存器的值,和文件系统上读出的不一样。

看 linux-2.6.20.21/Documentation/hwmon/lm75,了解到lm75是一个工业标准。所以linux里的lm75驱动是支持多种温度传感器的。

看了一下板子上的芯片,是lm75A.在网上下载了一个datasheet,上面显示一共9-bit来控制读数。头位为正负,后面8-bit为值。

在驱动代码里添加调试信息,只打印寄存器值的后8-bit,放到温度实验箱里测试的结果看起来满合理的。

@drivers/base/sys.c

staticssize_t

sysdev_show(structkobject * kobj,structattribute * attr,char* buffer)

{

structsys_device * sysdev = to_sysdev(kobj);

structsysdev_attribute * sysdev_attr = to_sysdev_attr(attr);

if(sysdev_attr->show)

returnsysdev_attr->show(sysdev, buffer);

return-EIO;

}@drivers/base/sys.c

static ssize_t

sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)

{

struct sys_device * sysdev = to_sysdev(kobj);

struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr);

if (sysdev_attr->show)

return sysdev_attr->show(sysdev, buffer);

return -EIO;

}

staticDEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);

/* 即truct device_attribute dev_attr_temp1_input =

__ATTR(temp1_input, S_IRUGO,

show_temp1_input, NULL)

*/

/*即truct device_attribute dev_attr_temp1_input = {

.attr = {.name = __stringify(temp1_input),

.mode = S_IRUGO,

.owner = THIS_MODULE },

.show   = show_temp1_input,

.store  = NULL,

}

*/static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);

/* 即truct device_attribute dev_attr_temp1_input =

__ATTR(temp1_input, S_IRUGO,

show_temp1_input, NULL)

*/

/*即truct device_attribute dev_attr_temp1_input = {

.attr = {.name = __stringify(temp1_input),

.mode = S_IRUGO,

.owner = THIS_MODULE },

.show= show_temp1_input,

.store= NULL,

}

*/

/* interface for exporting device attributes */

structdevice_attribute {

structattribute    attr;

ssize_t (*show)(structdevice *dev,structdevice_attribute *attr,

char*buf);

ssize_t (*store)(structdevice *dev,structdevice_attribute *attr,

constchar*buf,size_tcount);

};

#define show(value) /

staticssize_t show_##value(structdevice *dev,structdevice_attribute *attr,char*buf)       /

{                                   /

structlm75_data *data = lm75_update_device(dev);       /

returnsprintf(buf,"%d/n", LM75_TEMP_FROM_REG(data->value));    /

///sys/bus/i2c/devices/0-0048/temp1_input里面的东西就是通过上面打印到buffer中去的

}

show(temp_input);/* interface for exporting device attributes */

struct device_attribute {

struct attributeattr;

ssize_t (*show)(struct device *dev, struct device_attribute *attr,

char *buf);

ssize_t (*store)(struct device *dev, struct device_attribute *attr,

const char *buf, size_t count);

};

#define show(value)/

static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)/

{/

struct lm75_data *data = lm75_update_device(dev);/

return sprintf(buf, "%d/n", LM75_TEMP_FROM_REG(data->value));/

///sys/bus/i2c/devices/0-0048/temp1_input里面的东西就是通过上面打印到buffer中去的

}

show(temp_input);

show_temp_input);//@ show_##value()show_temp_input); //@ show_##value()

lm75_update_device(dev);

然后把内核的lm75.h的代码修正(只读后面8个比特并根据大小判断正负),测试通过。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MLX90614红外温度传感器_linux驱动源码,已经在产品中使用过。android 6.0 ,内核版本为3.4.39,可以做为你的学习设计参考。 #include <linux/kernel.h> #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/moduleparam.h> #include <linux/delay.h> #include <linux/fs.h> #include <linux/timer.h> #include <linux/ioctl.h> #include <linux/gpio.h>//__gpio_set_value #include <mach/sys_config.h>//script_item_u #include <linux/pinctrl/consumer.h>//pin_config_set #include <linux/pinctrl/pinconf-sunxi.h>//SUNXI_PINCFG_TYPE_* #include <linux/io.h> #include <mach/sys_config.h> #include <mach/platform.h> #include <linux/slab.h> #define ACK 0 #define NACK 1 #define SA 0x5a //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a #define RAM_ACCESS 0x00 //RAM access command #define EEPROM_ACCESS 0x20 //EEPROM access command #define RAM_TOBJ1 0x07 //To1 address in the eeprom #define RAM_TOBJ2 0x08 #define RAM_TA 0x06 #define DEVICE_NAME "mlx90614" //struct gpio_config { // u32 gpio; /* gpio global index, must be unique */ // u32 mul_sel; /* multi sel val: 0 - input, 1 - output... */ // u32 pull; /* pull val: 0 - pull up/down disable, 1 - pull up... */ // u32 drv_level; /* driver level val: 0 - level 0, 1 - level 1... */ // u32 data; /* data val: 0 - low, 1 - high, only vaild when mul_sel is input/output */ //}; #define SCL_NAME "sensor_sck" #define SDA_NAME "sensor_sda" struct gpio_func_desc { unsigned short pin; char *name; }; struct gpio_func_desc SCLK ={0,SCL_NAME};//蓝色 struct gpio_func_desc SDIN ={0,SDA_NAME};//白色 struct gpio_config *sclk_gpio_p = NULL; struct gpio_config *sdin_gpio_p = NULL; void SMBus_StartBit(void); void SMBus_StopBit(void);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值