Linux嵌入式学习-ds18b20驱动
ds18b20的时序图如下:
复位时序:
读写时序:
以下是程序代码:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm/irq.h>
#include <linux/random.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <mach/gpio.h>
#include <linux/mutex.h>
#define GPH3_0CON 0xE0200C60
#define GPH3_0DAT 0xE0200C64
#define GPH3_0PUD 0xE0200C68
unsigned int *gpio_config;
unsigned char *gpio_data;
unsigned int *gpio_pud;
static struct class *fog_class;     //´´½¨Àà
static struct class_device *fog_class_devs;   //´´½¨Àà¶ÔÓ¦µÄÉ豸
int major;
struct mutex res_mutex;
void Ds18b20_Pin_Init(void)
{
    unsigned int pin_val;
    gpio_request(S5PV210_GPH3(0),"my_ds1802");
    gpio_config = ioremap(GPH3_0CON,4);
    gpio_data = ioremap(GPH3_0DAT,1);
    gpio_pud = ioremap(GPH3_0PUD,2);
    pin_val = readl(gpio_pud);
    pin_val &=~(0x0003);
    pin_val |= 0x2;
    writel(pin_val,gpio_pud);
    pin_val = readl(gpio_data);
    writel(pin_val|0x1,gpio_data);
}
void DS18B20_OUT( unsigned char value)
{
    if( value == 1)
    {
        gpio_direction_output( S5PV210_GPH3(0), 1);
    }
    else
    {
        gpio_direction_output( S5PV210_GPH3(0), 0);
    }
}
unsigned char DS18B20_IN( void )
{
    unsigned int pin_val;
    gpio_direction_input( S5PV210_GPH3(0));
    pin_val = readl(gpio_data);
    return pin_val&0x1;
}
static void Init_DS18B20(void)
{
   gpio_direction_output( S5PV210_GPH3(0), 1);
    udelay(200);
    gpio_direction_output( S5PV210_GPH3(0), 0);
    udelay(600);
    gpio_direction_output( S5PV210_GPH3(0), 1);
    udelay(480);
}
static void WriteCode(unsigned char dat)
{
    unsigned char temp,i;
    for(i=0;i<8;i++)
    {
        temp = dat&0x01;
        gpio_direction_output( S5PV210_GPH3(0), 1);
        udelay(2);
        gpio_direction_output( S5PV210_GPH3(0), 0);
        if(temp == 0x01)
        {
            udelay(2);
            gpio_direction_output( S5PV210_GPH3(0), 1);
            udelay(100);
        }else{
            udelay(100);
            gpio_direction_output( S5PV210_GPH3(0), 1);
            udelay(3);
        }
        dat = dat>>1;
    }
}
static void Reset_DS18B20( void )
{
    gpio_direction_output( S5PV210_GPH3(0), 0);
    udelay(500);
    gpio_direction_output( S5PV210_GPH3(0), 1);
    udelay(480);
}
static unsigned int ReadData(void)
{
    unsigned int rec,data,i;
    data = 0;
    for(i=0;i<16;i++)
    {
        gpio_direction_output( S5PV210_GPH3(0), 0);
        udelay(5);
        udelay(3);
        rec = DS18B20_IN();
            udelay(20);
        if(rec){
        data |= 0x8000;
        }else{
        data &= 0x7fff;
        }
        if(i<15)
        data >>=1;
        udelay(20);
        gpio_direction_output( S5PV210_GPH3(0), 1);
        udelay(5);
    }
    return (data);
}
int ds18b20_open(struct inode *node, struct file *filp)
{
    return 0;
}
static int ds18b20_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
{
    int tem;
    int ds_value;
    mutex_lock_interruptible(&res_mutex);
    Ds18b20_Pin_Init();
    Init_DS18B20();
    WriteCode(0xcc);
    WriteCode(0x44);
    gpio_direction_input( S5PV210_GPH3(0));
    udelay(100);
    tem = DS18B20_IN();
    if(tem)
    {
        gpio_direction_output( S5PV210_GPH3(0), 1);
        Reset_DS18B20();
        WriteCode(0xcc);
        WriteCode(0xbe);
        ds_value = ReadData();
    }else{
        udelay(50);
        ds_value = 0xaaaa;
    }
    mutex_unlock(&res_mutex);
    copy_to_user(buffer, &ds_value, 4);
    return sizeof ds_value;
}
static struct file_operations ds18b20_fops =
{
    .open = ds18b20_open,
    .read = ds18b20_read,
};
static int Ds18b20_init(void)
{
    major = register_chrdev( 0,"ds18b20_drv", &ds18b20_fops );
    fog_class = class_create(THIS_MODULE,"ds18b20_class");
    fog_class_devs = device_create(fog_class,NULL,MKDEV(major,0),NULL,"my_ds1802");
    mutex_init(&res_mutex);
    printk("install module successed\n");
    return 0;
}
void Ds18b20_exit(void)
{
    unregister_chrdev( major, "ds18b20_drv" );
    device_unregister(fog_class_devs);
    class_destroy(fog_class);
}
module_init(Ds18b20_init);
module_exit(Ds18b20_exit);
MODULE_LICENSE("GPL");
Linux嵌入式学习-ds18b20驱动的更多相关文章
- Linux嵌入式学习-烟雾传感器驱动-字符设备驱动-按键驱动
		
MQ-2烟雾气敏传感器模块在X210v3开发板上的驱动. 现在需要一个MQ-2烟雾气敏传感器模块的驱动.其检测烟雾超过一定的标准后,会返回一个不同的电平,和按键驱动差不多. 但是在编写驱动的时候,需要 ...
 - Linux嵌入式学习-mount命令+nfs挂载失败原因【转】
		
NFS 挂载失败原因[待搜集] 1.挂载时若出现mount.nfs: Input/output error 解决:在客户端也需启动portmap服务 service portmap status[查看 ...
 - Linux嵌入式学习-USB端口号绑定
		
由于ubuntu USB设备号为从零开始依次累加,所以多个设备每次开机后设备号不固定,机器人每次开机都要蛋疼的按顺序插, 在网上找到一种方法:udev的规则 udev的规则说明,可以参考博客说明:ht ...
 - Linux嵌入式学习-交叉编译openssl
		
利用arm-none-linux-gnueabi-gcc交叉编译openssl,生成静态库文件libcrypto.a ,libssl.a 1.从openssl官网下载openssl最新版本,我下载的是 ...
 - Linux嵌入式学习-远程过程调用-Binder系统
		
Binder系统的C程序使用示例IPC : Inter-Process Communication, 进程间通信RPC : Remote Procedure Call, 远程过程调用 这里我们直接只用 ...
 - Linux嵌入式学习-网络配置-ping外网、主机和域名
		
之前用的nfs挂载的文件系统,今天用yaffs2制作的文件系统并写入到nandflash中.但是网络却无法使用了. 首先,我们配置网卡. ifconfig eth0 192.168.1.230 bro ...
 - Linux嵌入式学习-Mplayer交叉编译-undefined reference to `clock_gettime' MPlayer
		
編譯Mplayera. 配置.configure# ./configure --host-cc=gcc --cc=arm-linux-gcc --target=arm --enable-static ...
 - Linux嵌入式学习-交叉编译mplayer
		
http://bbs.gkong.com/archive.aspx?ID=286721
 - Linux内核调用I2C驱动_驱动嵌套驱动方法
		
禁止转载!!!! Linux内核调用I2C驱动_以MPU6050为例 0. 导语 最近一段时间都在恶补数据结构和C++,加上导师的事情比较多,Linux内核驱动的学习进程总是被阻碍.不过,十一假期终于 ...
 
随机推荐
- Beta冲刺随笔——Day_Five
			
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
 - 基于ARM64的Qemu/KVM学习环境搭建
			
作者:pengdonglin137@163.com 在没有aarch64架构的开发板的情况下,可以使用Qemu来模拟一个支持KVM的AArch64位的host,然后再在其上运行一个开启KVM加速的Qe ...
 - Python音视频开发:消除抖音短视频Logo和去电视台标
			
☞ ░ 前往老猿Python博文目录 ░ 一.引言 对于带Logo(如抖音Logo.电视台标)的视频,有三种方案进行Logo消除: 直接将对应区域用对应图像替换: 直接将对应区域模糊化: 通过变换将要 ...
 - PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件的tabBar、count、indexOf方法
			
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTabWidget的每个选项卡都有一个对应的页面部件对象,可用通过count方法获取选项卡个数,可 ...
 - 第14.14节  爬虫实战准备:csdn博文点赞过程http请求和响应信息分析
			
如果要对csdn博文点赞,首先要登录CSDN,然后打开一篇需要点赞的文章,如<第14.1节 通过Python爬取网页的学习步骤>按<第14.3节 使用google浏览器获取网站访问的 ...
 - scala&&spark学习参考文章
			
http://www.cnblogs.com/xing901022/p/5944297.html 牛逼
 - 在虚拟机中安装Linux系统CentOS7详细教程!!!超详细!!!!一看就会!!!手把手教学!!!
			
一.CentOS的下载 CentOS是免费版,推荐在官网上直接下载.https://www.centos.org/download/ DVD ISO:普通光盘完整安装版镜像,可离线安装到计算机硬盘上, ...
 - bbed工具安装
			
1.上传bbedus.msb bbedus.msg sbbdpt.o ssbbded.o四个文件到数据库服务器 [oracle@edgzrip1-PROD1 bbed_10g_src_x32]$ ...
 - apt-get  could not get lock /var/lib/dpkg/lock报错
			
用apt-get命令安装一些软件包时,报这个错 could not get lock /var/lib/dpkg/lock 出现这个问题的原因可能是有另外一个程序正在运行,导致资源被锁不可用.而导致资 ...
 - 前端开发超好用的截图、取色工具——snipaste
			
最近发现一个很好用的前端截图,取色工具,并且基本功能是免费使用的,可以提升开发效率,拿出来跟大家分享一下. 该工具主要能实现的功能就是截图,并且截图可以以窗口形式置顶在窗口: 第二个主要功能就是可以取 ...