分享

基于FS_S5PC100的LM75温度传感器驱动程序添加与测试

 dwlinux_gs 2014-08-14

 

基于FS_S5PC100的LM75温度传感器驱动程序添加与测试

作者:郑老师,华清远见嵌入式学院讲师。

【实验内容】

在平台代码中添加I2C设备LM75;
        在内核配置菜单中配置温度传感器LM75驱动程序。编写应用程序读取当前温度;
        温度传感器芯片位于COM3下方,紧靠蓝色的可调电阻旋钮。

【实验目的】

熟悉内核中I2C驱动的工作原理,了解sysfs接口的使用方法

【实验平台】

主机:Ubuntu 10.10
        目标板:FS_S5PC100
        目标内核版本:2.6.29
        交叉编译器版本:arm-unknown-linux-gnueabi-gcc-4.2.2

【实验步骤】

在主机ubuntu环境下:

1、 在内核S5PC100平台代码中添加一个I2C设备用来描述LM75:

打开平台代码源文件:
        $ vim (kernel_dir)/arch/arm/mach-s5pc100/mach-smdkc100.c

在结构体:
        static struct platform_device *smdkc100_devices[] __initdata = {
                …
        };
        的定义后面添加如下代码:
        static struct i2c_board_info i2c_devs0[] __initdata = {
                { I2C_BOARD_INFO("lm75", 0x90 >> 1 ), },
        };

在函数smdkc100_machine_init中的
        /* i2c */
        s3c_i2c0_set_platdata(NULL);
        s3c_i2c1_set_platdata(NULL);
        之后添加如下代码:
        i2c_register_board_info(0, i2c_devs0, ARRAY_SIZE(i2c_devs0));

2、在内核源码根目录下执行:

$ make menuconfig

进入配置菜单后选择lm75温度传感器的驱动程序:
        Location:
                -> Device Drivers
                        -> Hardware Monitoring support (HWMON [=y])

3、重新编译内核

$ make zImage

4、重新烧写或者下载内核到开发板

5、将文件夹s5pc100_lm75复制到linux环境中,如:/home/linux/test

$ cp s5pc100_lm75 /home/linux/test –a
        $ cd /home/linux/test/s5pc100_lm75

6、$ arm-linux-gcc read_temp.c -o read_temp //编译应用程序

7、$ sudo cp read_temp /source/rootfs

在开发板的串口终端控制台下:

8、运行应用程序

# ./read_temp

用手捂住传感器芯片,可以看到温度值会持续上升:

lm75 0-0048: Starting lm75 update
        Current temperature is: 27.000000
        Current temperature is: 27.000000
        lm75 0-0048: Starting lm75 update
        Current temperature is: 28.000000
        Current temperature is: 28.000000
        lm75 0-0048: Starting lm75 update
        Current temperature is: 29.000000
         Current temperature is: 29.000000

9、阅读驱动源码:driver/hwmon/lm75.c,理解i2c驱动的注册方法,lm75的工作原理以及sysfs接口的编程方法

附:

1、温度传感器接口原理图

2、测试程序代码:

#include <stdio.h>
        #include <fcntl.h>

int main()
        {
                int n, temp_int;
                float temp_float;
                char buf[10];
                int fd = open("/sys/bus/i2c/devices/0-0048/temp1_input", O_RDONLY);
                if(fd < 0) {
                        perror("open error");
                        return -1;
                }
                printf("fd = %d\n", fd);

        while(1) {
                        n = read(fd, buf, 10);
                        if(n < 0) {
                                perror("read error");
                                return -1;
                        }
                        if( 0 == n) {
                                printf("Nothing can be read now.\n");
                                perror("read zero");
                                return -1;
                        }
                        buf[n-1] = '\0';
                        temp_int = atoi(buf);
                        temp_float = temp_int / 1000; // translate into real temperature
                        printf("Current temperature is: %f\n", temp_float);
                        sleep(1);
                        close(fd);
                        fd = open("/sys/bus/i2c/devices/0-0048/temp1_input", O_RDONLY);
                }

        return 0;
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多