Ok6410 Linux DS18B20 driver

Source: Internet
Author: User
// Note that you can use data = Data * 6.25 on bare metal to increase the temperature by 100 times, but not in the kernel, however, data = Data * 625/100 can be used;
// I checked it online. It seems that the kernel does not support floating point. I don't know if this is the case, but I don't need to use a floating point driver to load it. I cannot load it after use.
  
 
/*************************************** **************************************** * ********************************** File Name: DS18B20 b20_drive.c * Introduction: ok6410 DS18B20 driver * Author: different spirit yuan (cp1300@139.com) * Creation Time: 092/09/18 20:37 * modification time: 2012/09/18 * Description: ok6410 Development Board (s3c246410) DS18B20 (gpio) *************************************** **************************************** * ******************************* // system header file # include <Linux /Miscdevice. h> # include <Linux/delay. h> # include <ASM/IRQ. h> # include <Mach/hardware. h> # include <Linux/kernel. h> # include <Linux/module. h> # include <Linux/init. h> # include <Linux/mm. h> # include <Linux/Fs. h> # include <Linux/types. h> # include <Linux/delay. h> # include <Linux/moduleparam. h> # include <Linux/slab. h> # include <Linux/errno. h> # include <Linux/IOCTL. h> # include <Linux/cdev. h> # include <Linux/string. h> # Inc Lude <Linux/list. h> # include <Linux/PCI. h> # include <ASM/uaccess. h> # include <ASM/atomic. h> # include <ASM/unistd. h> # include <Linux/delay. h> # include <Linux/Io. h> // -------------------------- // # include <Mach/map. h> # include <Mach/regs-clock.h> # include <Mach/regs-gpio.h> // ------------------------ // # include <plat/gpio-cfg.h> # include <Mach/gpio-bank-e.h> # include <Mach/ gpio-bank-m.h> # define DS18B20 b20_error0xf 000 // initialization failed // system us latency definition # define delay_us (x) udelay (x) // set DS18B20 Io to push-pull output mode void set18b20ioout (void) {unsigned int reg; reg = readl (initi64xx_gpecon); Reg | = 1; writel (Reg, initi64xx_gpecon);} // read iounsigned char read18b20io (void) {unsigned int reg; reg = readl (initi64xx_gpedat); If (REG & 1) return 1; elsereturn 0 ;}// write iovoid write18b20io (unsigned char data) {unsigned int reg; reg = readl (initi64xx_gpedat); If (data) reg | = 1; els Ereg & = ~ 1; writel (Reg, initi64xx_gpedat);} // set it in the DS18B20 Io input mode void set18b20ioin (void) {unsigned int reg; Reg = readl (initi64xx_gpecon); Reg & = (~ 0xf); writel (Reg, initi64xx_gpecon);} // reset DS18B20 b20u8 DS18B20 reset (void) {u8 I = 0; reset (); write18b20io (1); delay_us (1 ); write18b20io (0); delay_us (500); write18b20io (1); delay_us (2); set18b20ioin (); While (read18b20io () {I ++; delay_us (1 ); if (I> 100) return 1;} delay_us (250); Return 0x00;} // read DS18B20 data u8 DS18B20 b20_readdata (void) {u8 I, Data = 0; for (I = 0; I <8; I ++) {set18b20ioout (); write18b20io (0); Data >>=1; Delay _ Us (12); write18b20io (1); set18b20ioin (); delay_us (1); If (read18b20io () Data | = 0x80; delay_us (42 );} return data;} // write the DS18B20 data void DS18B20 b20_writedata (u8 data) {u8 I; set18b20ioout (); for (I = 0; I <8; I ++) {write18b20io (0); delay_us (12); write18b20io (Data & 0x01); delay_us (30); write18b20io (1); data >>= 1; delay_us (2) ;}}// read temperature int DS18B20 (void) {u8 th, Tl; int data; If (DS18B20 b20_reset () {return DS18B20 b20_error ;} Round (0xcc); DS18B20 (0x44); DS18B20 (); DS18B20 (); DS18B20 (0xcc); Round (0xbe); TL = DS18B20 (readdata); Th = DS18B20 (readdata (); data = th; Data <= 8; Data | = TL; Data = Data * 625/100; return data ;} //////////////////////////////////////// //// // Driver Module name # define device_name "ok6410_ds18b20" // function declaration ////////////////// /// // static long ok6410_ds18b20 _ IOCTL (struct file * file, unsigned int cmd, unsigned long Arg); static ssize_t ok6410_ds18b20 b20_write (struct file * file, const char _ User * buff, size_t size, loff_t * Loff); static ssize_t ok6410_ds18b20 b20_read (struct file * file, char _ User * buff, size_t size, loff_t * Loff ); //////////////////////////////////////// //// // * this structure is the core of the character device driver * When the application operates the open provided by the device file, read, write, and other functions * will eventually call the corresponding function in this structure */static struct File _ Operations dev_fops = {. owner = this_module, // This is a macro that points to the _ this_module variable automatically created during compilation. unlocked_ioctl = ok6410_ds18b20 b20_ioctl ,. read = ok6410_ds18b20 _ read ,. write = ok6410_ds18b20 b20_write}; // register the information used by the driver static struct miscdevice MISC = {. minor = misc_dynamic_minor ,. name = device_name, // Driver Module name. foPs = & dev_fops,}; // access semaphores of the DS18B20 device struct semaphore DS18B20 b20_sem; /*************************************** ***************** **************************************** * ***************** Function Name: static int _ init ok6410_ds18b20 b20_init (void) * function: DS18B20 module initialization function * parameter: none * returned: 0: Successful; <0: Failed * dependency: underlying Linux macro definition * Author: abnormal spirit yuan (cp1300@139.com) * Creation Time: * last modification time: * Description: Initialize DS18B20 hardware, register the DS18B20 driver ************************************* **************************************** * **********************************/static int __ init ok6410_ds18 B20_init (void) {int ret; ret = misc_register (& MISC); // register the driver if (Ret <0) {printk (device_name "can't initialized DS18B20! \ N "); return ret;} init_mutex (& DS18B20 _ SEM); // register the semaphore printk (device_name" initialized \ n "); Return 0; // return success }/********************************** **************************************** * ************************************ Function name: static long ok6410_ds18b20 b20_ioctl (struct file * file, unsigned int cmd, unsigned long Arg) * function: send a command to the DS18B20 driver module, without any practical effect, directly return the 0 * parameter: no function * return: 0 * dependency: none * Author: different spirit yuan (cp1300@139.com) * Creation Time: * last modification time: * Note: no *************************************** **************************************** * ******************************/static long ok6410_ds18b20 b20_ioctl (struct File * file, unsigned int cmd, unsigned long Arg) {return 0 ;} /*************************************** **************************************** * ********************************** Function Name: static ssize_t ok6410_ds18b20 b20_write (struct file * file, const char _ User * buff, size_t size, loff_t * Loff) * function: write data to the DS18B20 driver module, invalid function, return 0 * parameter: file pointer (no function); Buff: data buffer pointer; Buff: data quantity; Loff: no function * return: 0: success; <0: failure * dependency: Linux underlying macro * Author: Alien (cp1300@139.com) * Creation Time: * last modification time: * Note: invalid write ************************************** **************************************** * *******************************/static ssize_t ok6410_ds18b20 b20_write (struct file * file, const char _ User * buff, size_t size, loff_t * Loff) {return 0 ;} /*************************************** **************************************** * ********************************** Function Name: static ssize_t ok6410_ds18b20 b20_read (struct file * file, char _ User * buff, size_t size, loff_t * Loff) * function: Read DS18B20 status, low-level light on * parameter: file: file pointer (ineffective); Buff: data buffer pointer; Buff: data quantity; Loff: ineffective * return: 0: Successful; <0: Failed * dependency: linux underlying macro * Author: different spirit yuan (cp1300@139.com) * Creation Time: * last modification time: * Note: it reads a 16-Bit Signed temperature ***************************** **************************************** **************************************** * **/static ssize_t ok6410_ds18b20 b20_read (struct file * file, char _ User * buff, size_t size, loff_t * Loff) {int temp; int * P; If (down_interruptible (& DS18B20 b20_sem) // obtain the semaphore return-erestartsys; temp = DS18B20 b20_readtemper (); // read temperature if (temp = DS18B20 b20_error) // return-1 for DS18B20 initialization failure; // Return Error P = (int *) buff; * P = temp; // write the temperature to the buffer up (& DS18B20 _ SEM); // release the semaphore return 0 ;} /*************************************** **************************************** * ********************************** Function Name: static void _ exit ok6410_ds18b20 b20_exit (void) * function: uninstall the DS18B20 driver * parameter: No * returned: No * dependency: Linux underlying macro * Author: different spirit (cp1300@139.com) * Creation Time: 2012/09/18 20:38 * last modification time: 2012/09/18 20:38 * description: uninstall the driver ************************************** **************************************** * *********************************/static void _ exit ok6410_ds18b20 b20_exit (void) {misc_deregister (& MISC); // uninstall driver} // dynamic loading Driver Interface (required) module_init (ok6410_ds18b20 b20_init); module_exit (ok6410_ds18b20 b20_exit); // other information (not required) module_author ("cp1300@139.com"); // driver author module_description ("ok6410 (initi6410) DS18B20 driver"); // module_license ("GPL "); // The following agreement

Makefile

ARCH=armCROSS_COMPILE=arm-linux-obj-m := ds18b20_drive.o#ds18b20_drive-objs := ds18b20_drive.oKDIR :=/home/cfan/linux/linux-3.0.1 PWD :=$(shell pwd)all:$(MAKE) -C $(KDIR) M=$(PWD) modulesclean:$(MAKE) -C $(KDIR) M=$(PWD) clean

Test

/*************************************** **************************************** * ********************************** File Name: DS18B20 b20_teset.c * Introduction: ok6410 DS18B20 driver test program * Author: different spirit yuan (cp1300@139.com) * Creation Time: * modification time: * Description: ok6410 Development Board (s3c246410) DS18B20 (gpio) ************************************* **************************************** * *********************************/# include <stdi O. h> # include <stdlib. h> # include <unistd. h> # include <fcntl. h> # include <sys/IOCTL. h> # include <unistd. h> int main (void) {int FD; int data; // DS18B20 test printf ("DS18B20 test... \ n "); FD = open ("/dev/ok6410_ds18b20 ", o_rdonly); // open DS18B20 20if (FD =-1) {printf (" Open DS18B20 error! \ N "); exit (-1);} else {printf (" Open DS18B20 OK! \ N ") ;}while (1) {If (read (FD, & data, (size_t) 2) printf (" read error! \ N "); printf (" DS18B20 = % d \ n ", data); usleep (1000*1000); // 1000 ms }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.