S3C6410 驱动18b20简单测试,时序

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/cdev.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/ctype.h>
#include <linux/pagemap.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>

#include <mach/regs-gpio.h>
#include <mach/gpio.h>
#include <mach/hardware.h>
#include <mach/map.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/signal.h>
#include <asm/uaccess.h>
#include <plat/regs-timer.h>
#include <plat/gpio-cfg.h>
#include <mach/gpio-bank-n.h>

#define DEVICE_NAME "yzh_temper"

#define DQ_input  s3c_gpio_cfgpin(S3C64XX_GPN(8),S3C64XX_GPN_INPUT(8))
#define DQ_output s3c_gpio_cfgpin(S3C64XX_GPN(8),S3C64XX_GPN_OUTPUT(8))

#define DQ_set_up s3c_gpio_cfgpin(S3C64XX_GPN(8),S3C_GPIO_PULL_UP)
#define DQ_set_down  s3c_gpio_cfgpin(S3C64XX_GPN(8),S3C_GPIO_PULL_DOWN)
#define DQ_set_none s3c_gpio_cfgpin(S3C64XX_GPN(8),S3C_GPIO_PULL_NONE)


unsigned int DQ_read(void)
{
 unsigned int temp;
 
 temp = __raw_readl(S3C64XX_GPNDAT);
 temp = temp & (1 << 8);
 
 return temp;
}

void DQ_set(unsigned int value)
{
 unsigned int temp;
 
 temp = __raw_readl(S3C64XX_GPNDAT);
 temp &= ~(1 << 8);
 temp |= (value << 8);
 __raw_writel(temp,S3C64XX_GPNDAT);

static void temper_reset(void)
{
 unsigned int level = 1;

 DQ_output;
 
 DQ_set(1);
 udelay(20);
 DQ_set(0);
 udelay(480);
 DQ_set(1);
 udelay(60);  
 
 DQ_input;
 level = DQ_read();
 printk("%x  \n\r",level);
 udelay(480);
 DQ_output;
 DQ_set(1); 
}


static void temper_byte_program(unsigned char dat)
{
 int i;
 DQ_output;
 
 for(i=0;i<8;i++)
 {
    
  DQ_set(1);  //start write
  udelay(2); 
  DQ_set(0);
      
  if(dat & 0x01)
  {
   DQ_set(1);
  }
  else
  {
   DQ_set(0);
  }
  udelay(60);
  dat >>= 1;  
 }
 DQ_set(1);
}
 
unsigned int temper_byte_read(void)
{
 int j;
 unsigned int result = 0;

 for(j=0;j<8;j++)
 {
  DQ_output;
  DQ_set(0);
  udelay(2);
  DQ_set(1);
  udelay(4);
  
  DQ_set(0);
  udelay(2); 
  DQ_set(1);
  udelay(8);
  
  result >>= 1;
  DQ_input;
  if(DQ_read() == 0)
  {
   result &= ~0x80;
  }
  else
  {
   result |= 0x80;
  }
  udelay(50);
 }
 DQ_output;
 DQ_set(1);
 return result;
}
  
int temper_init(void)
{
 unsigned int value_h;
 unsigned int value_l;
 unsigned int total;
 
 temper_reset();
 temper_byte_program(0xcc);
 temper_byte_program(0x44);
 mdelay(30);
 
 temper_reset();
 temper_byte_program(0xcc);
 temper_byte_program(0xbe);
 
 value_l = temper_byte_read();
 value_h = temper_byte_read();
  
// printk("value:  %x %x \n\r",value_l,value_h);
 
 value_h <<= 8;

 total = value_h | value_l;
// printk("the temper_value:  %x \n\r",total);
 
 return total;
  
}

void temper_test(void)
{
 int i;
 int ret;
 
 for(i=0;i<10;i++)
 {
  ret = temper_init();
   
  mdelay(500);
 }
}

 
static int temper_open(struct inode *inode, struct file *filp)
{
 temper_reset();
// temper_test();
 printk("the 18b20 has reset \n\r"); 
 return 0;
}


static int temper_release(struct inode *inode, struct file *filp)
{
 printk("the 18b20 has released \n\r");
 
 return 0;
}

ssize_t temper_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
{
 int value;
 int err;
 
 value = temper_init();

 err = copy_to_user(buffer,&value,sizeof(value));
 return min(sizeof(value),count);
 
}

static struct file_operations temper_fps = {
 .owner = THIS_MODULE,
 .open = temper_open,
 .read = temper_read,
 .release = temper_release,
};

static struct miscdevice temper_misc = {
 .minor = MISC_DYNAMIC_MINOR,
 .name = DEVICE_NAME,
 .fops = &temper_fps,
};

static int temper_init_up(void)
{
 int ret;
 
 ret = misc_register(&temper_misc);
 printk("18b20 module has up \n\r");
 
 return ret;
}

static void temper_exit_remove(void)
{
 misc_deregister(&temper_misc);
 printk("18b20 module has remove\n\r");
}


module_init(temper_init_up);
module_exit(temper_exit_remove);

MODULE_LICENSE("GPL");

你可能感兴趣的:(linux,驱动,ARM11,18b20)