⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 立宇泰嵌入式系统技术论坛-嵌入式操作系统相关-菜鸟写给菜鸟参考的44b0x_iic驱动程序.htm

📁 LINUX44B0I2C驱动验证过 现在可以用 是中断方式的驱动
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            style="COLOR: black; BACKGROUND-COLOR: #ffff66">44b0x</B>.h<BR><BR>*<BR><BR>*&nbsp;&nbsp;Sun 
            Jun&nbsp;&nbsp;5 16:53:19 
            2005<BR><BR>*&nbsp;&nbsp;Copyright&nbsp;&nbsp;2005&nbsp;&nbsp;Yong F 
            Wang<BR><BR>*&nbsp;&nbsp;Email&nbsp;&nbsp;&nbsp;&nbsp;com_wang@21cn.com<BR><BR>****************************************************************************/<BR><BR><BR><BR>/*<BR><BR>*&nbsp;&nbsp;This 
            program is free software; you can redistribute it and/or 
            modify<BR><BR>*&nbsp;&nbsp;it under the terms of the GNU General 
            Public License as published by<BR><BR>*&nbsp;&nbsp;the Free Software 
            Foundation; either version 2 of the License, 
            or<BR><BR>*&nbsp;&nbsp;(at your option) any later 
            version.<BR><BR>*<BR><BR>*&nbsp;&nbsp;This program is distributed in 
            the hope that it will be useful,<BR><BR>*&nbsp;&nbsp;but WITHOUT ANY 
            WARRANTY; without even the implied warranty 
            of<BR><BR>*&nbsp;&nbsp;MERCHANTABILITY or FITNESS FOR A PARTICULAR 
            PURPOSE.&nbsp;&nbsp;See the<BR><BR>*&nbsp;&nbsp;GNU Library General 
            Public License for more details.<BR><BR>*<BR><BR>*&nbsp;&nbsp;You 
            should have received a copy of the GNU General Public 
            License<BR><BR>*&nbsp;&nbsp;along with this program; if not, write 
            to the Free Software<BR><BR>*&nbsp;&nbsp;Foundation, Inc., 59 Temple 
            Place - Suite 330, Boston, MA 02111-1307, 
            USA.<BR><BR>*/<BR><BR><BR><BR>#define U32 unsigned 
            int<BR><BR>#define S32 int<BR><BR>#define U16 short unsigned 
            int<BR><BR>#define S16 short int<BR><BR>#define U8 unsigned 
            char<BR><BR>#define S8 char<BR><BR><BR><BR>// <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B> operation 
            mode define<BR><BR>#define <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_RECEIVE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<BR><BR>#define 
            <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_TRANSMIT&nbsp;&nbsp;&nbsp;&nbsp;2<BR><BR>#define 
            <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_RD_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3<BR><BR><BR><BR>// 
            IOCTL operation definition<BR><BR>#define <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_SLAVE_ADDR&nbsp;&nbsp;&nbsp;&nbsp;15<BR><BR><BR><BR>#define 
            I2C_MAJOR 89<BR><BR>#define INT_I2C 5<BR><BR>DECLARE_WAIT_QUEUE_HEAD 
            (i2c_queue);<BR><BR><BR><BR>/*<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_<B 
            style="COLOR: black; BACKGROUND-COLOR: #ffff66">44b0x</B>.c - I2C 
            support for Samsung S3C<B 
            style="COLOR: black; BACKGROUND-COLOR: #ffff66">44B0X</B>, char 
            device interface<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Copyright 
            (C) 2005 Yong F Wang 
            &lt;com_wang@21cn.com&gt;<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;This 
            program is free software; you can redistribute it and/or 
            modify<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;it under the terms of the GNU 
            General Public License as published 
            by<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;the Free Software Foundation; 
            either version 2 of the License, 
            or<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;(at your option) any later 
            version.<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;This program is 
            distributed in the hope that it will be 
            useful,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;but WITHOUT ANY WARRANTY; 
            without even the implied warranty 
            of<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;MERCHANTABILITY or FITNESS FOR A 
            PARTICULAR PURPOSE.&nbsp;&nbsp;See 
            the<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;GNU General Public License for 
            more details.<BR><BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;You should have 
            received a copy of the GNU General Public 
            License<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;along with this program; if 
            not, write to the Free 
            Software<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Foundation, Inc., 675 Mass 
            Ave, Cambridge, MA 02139, USA.<BR><BR>*/<BR><BR><BR><BR>/* $Id: 
            i2c_<B style="COLOR: black; BACKGROUND-COLOR: #ffff66">44b0x</B>.c,v 
            1.00 2005/06/05 11:28:01 mds Exp $ */<BR><BR><BR><BR>#include 
            &lt;linux/fs.h&gt;<BR><BR>#include 
            &lt;linux/init.h&gt;<BR><BR>#include 
            &lt;linux/slab.h&gt;<BR><BR>#include 
            &lt;linux/config.h&gt;<BR><BR>#include 
            &lt;linux/kernel.h&gt;<BR><BR>#include 
            &lt;linux/module.h&gt;<BR><BR>#include 
            &lt;linux/version.h&gt;<BR><BR>#if LINUX_KERNEL_VERSION &gt;= 
            KERNEL_VERSION(2,4,0)<BR><BR>#include 
            &lt;linux/smp_lock.h&gt;<BR><BR>#endif<BR><BR>#ifdef 
            CONFIG_DEVFS_FS<BR><BR>#include 
            &lt;linux/devfs_fs_kernel.h&gt;<BR><BR>#endif<BR><BR><BR><BR>#include 
            &lt;asm/uaccess.h&gt;<BR><BR>#include 
            &lt;asm/io.h&gt;<BR><BR>#include 
            &lt;asm/system.h&gt;<BR><BR>#include 
            &lt;asm/bitops.h&gt;<BR><BR>#include 
            &lt;asm/hardware.h&gt;<BR><BR>#include "hardware.h"<BR><BR>#include 
            "i2c_<B 
            style="COLOR: black; BACKGROUND-COLOR: #ffff66">44b0x</B>.h"<BR><BR><BR><BR>#ifdef 
            MODULE<BR><BR>extern int init_module(void);<BR><BR>extern int 
            cleanup_module(void);<BR><BR>char 
            kernel_version[]=UTS_RELEASE;<BR><BR>#endif<BR><BR><BR><BR>//#define 
            DEBUG<BR><BR><BR><BR>/* struct file_operations changed too often in 
            the 2.1 series for nice code */<BR><BR>static ssize_t i2c_read 
            (struct file *file, char *buf, size_t count, 
            <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <BR>loff_t *offset);<BR><BR>static ssize_t i2c_write (struct file 
            *file, const char *buf, size_t count, 
            <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>loff_t 
            *offset);<BR><BR>static int i2c_ioctl (struct inode *inode, struct 
            file *file, 
            <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>unsigned 
            int cmd, unsigned long arg);<BR><BR>static int i2c_open (struct 
            inode *inode, struct file *file);<BR><BR>static int i2c_release 
            (struct inode *inode, struct file *file);<BR><BR><BR><BR>#ifdef 
            MODULE<BR><BR>static<BR><BR>#else<BR><BR>extern<BR><BR>#endif<BR><BR><BR><BR>int 
            __init init_i2c_<B 
            style="COLOR: black; BACKGROUND-COLOR: #ffff66">44b0x</B>(void);<BR><BR>static 
            int i2c_exit(void);<BR><BR>void i2c_irq_service(int irq,void 
            *dev_id,struct pt_regs *regs);<BR><BR><BR><BR>static struct 
            file_operations i2c_fops = 
            {<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;read:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i2c_read,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;write:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i2c_write,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;ioctl:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i2c_ioctl,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;open:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i2c_open,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;release:&nbsp;&nbsp;&nbsp;&nbsp;i2c_release,<BR><BR>};<BR><BR><BR><BR>static 
            int i2c_initialized,i2c_opened = 0;<BR><BR>static U32 
            PCONF_Backup,PUPF_Backup;<BR><BR>static U8 
            i2c_buf[64],i2c_buf_ptr,i2c_buf_count,i2c_oper_mode;<BR><BR><BR><BR>static 
            ssize_t i2c_read (struct file *file, char *buf, size_t 
            count,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <BR>loff_t 
            *offset)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_buf_ptr = 
            0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_buf_count = 
            count;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_oper_mode = <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_RD_ADDR;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICSTAT 
            = 0xB0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICCON = 
            0xE0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;interruptible_sleep_on(&amp;i2c_queue);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            sleep and wait receive finish<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;count = 
            i2c_buf_count;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if(copy_to_user(buf, 
            i2c_buf, 
            count))<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk("error 
            reading, 
            copy_to_user\n");<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
            -EFAULT;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
            count;<BR><BR>}<BR><BR><BR><BR>static ssize_t i2c_write (struct file 
            *file, const char *buf, size_t 
            count,<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>loff_t 
            *offset)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if(copy_from_user(i2c_buf, 
            buf, 
            count))<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk("error 
            writing, 
            copy_from_user\n");<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
            -EFAULT;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if(count 
            &lt; 2) return -EFAULT;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_buf_count 
            = count;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_buf_ptr = 
            0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_oper_mode = <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_TRANSMIT;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICSTAT 
            = 
            0xF0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            Master Transmit mode<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICCON = 
            0xE0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            Clear panding bit<BR><BR>&nbsp;&nbsp; 
            <BR>interruptible_sleep_on(&amp;i2c_queue);&nbsp;&nbsp; 
            <BR>&nbsp;&nbsp;&nbsp;&nbsp;// sleep and wait transmit 
            finish<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;count = 
            i2c_buf_ptr;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
            count;<BR><BR>}<BR><BR><BR><BR>static int i2c_ioctl (struct inode 
            *inode, struct file *file, unsigned int cmd, 
            <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned 
            long 
            arg)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;switch(cmd)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 
            <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B>_SLAVE_ADDR:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IICDS 
            = 
            (U8)(arg&lt;&lt;1);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
            0;<BR><BR>}<BR><BR><BR><BR>static int i2c_open (struct inode *inode, 
            struct file 
            *file)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if(i2c_opened &gt;= 
            1)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk("I2C 
            device already 
            opened!\n");<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
            -ENODEV;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PCONF_Backup 
            = PCONF;&nbsp;&nbsp;&nbsp;&nbsp;// Backup 
            PCONF<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PCONF &amp;= 
            0xFFFFFFF0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PCONF |= 
            0x0A;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <BR>&nbsp;&nbsp;&nbsp;&nbsp;// Set GPF0 to I2C SCL, set GPF1 to I2C 
            SDA.<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PUPF_Backup = 
            PUPF;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Backup 
            PUPF<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PUPF |= 
            0x03;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <BR>&nbsp;&nbsp;&nbsp;&nbsp;// Disable pull up resistor of 
            GPF0&amp;GPF1<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICSTAT = 
            0x10;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            Enable RX/TX<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICCON = 
            0xE0;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_opened++;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return 
            0;<BR><BR>}<BR><BR><BR><BR>static int i2c_release (struct inode 
            *inode, struct file 
            *file)<BR><BR>{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;if(i2c_opened &lt; 
            1)<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printk("I2C 
            device already 
            closed!\n");<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PCONF 
            = PCONF_Backup;&nbsp;&nbsp;&nbsp;&nbsp;// Restore 
            PCONF<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;PUPF = 
            PUPF_Backup;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            Restore PUPF<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;IICSTAT = 
            0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
            Disable <B 
            style="COLOR: black; BACKGROUND-COLOR: #a0ffff">IIC</B><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;i2c_opened--;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;return 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -