日期:2014-05-16  浏览次数:20723 次

gpio模拟i2c 读取24c02寄存器的值为0xff?
我把24c02的A0,A1,A2悬空,把wp接地,用的是hi3515板子,下面是我的代码:
  
#include <linux/module.h>  
#include <linux/config.h>  
#include <linux/errno.h>  
#include <linux/miscdevice.h>  
#include <linux/fcntl.h>  
  
#include <linux/init.h>  
#include <linux/delay.h>  
#include <linux/proc_fs.h>  
#include <linux/workqueue.h>  
  
#include <asm/uaccess.h>  
#include <asm/system.h>  
#include <asm/io.h>  
  
#include "gpio_i2c.h"  
  
  
/* GPIO0_0 */  
#define SCL (1 << 6) /* 6 indicated 6th bit*/  
  
/* GPIO0_1 */  
#define SDA (1 << 7)  
  
  
#define GPIO_I2C_BASE 0x20180000  
  
#define GPIO_I2C_DIR IO_ADDRESS(GPIO_I2C_BASE + 0x400)  
  
#define GPIO_I2C_SDA_REG IO_ADDRESS(GPIO_I2C_BASE + (SDA<<2)) /* data reg */  
#define GPIO_I2C_SCL_REG IO_ADDRESS(GPIO_I2C_BASE + (SCL<<2))  
  
#define GPIO_I2C_SCLSDA_REG IO_ADDRESS(GPIO_I2C_BASE + 0x300)  
  
#define HW_REG(reg) *((volatile unsigned int *)(reg))  
#define DELAY(us) time_delay_us(us)  
  
  

static void i2c_clr(unsigned char whichline)  
{  
  unsigned char regvalue;  
   
  if(whichline == SCL)  
  {  
  regvalue = HW_REG(GPIO_I2C_DIR);  
  regvalue |= SCL;  
  HW_REG(GPIO_I2C_DIR) = regvalue; /* set scl dir output */  
   
  HW_REG(GPIO_I2C_SCL_REG) = 0; /* set scl data 0 */  
  return;  
  }  
  else if(whichline == SDA)  
  {  
  regvalue = HW_REG(GPIO_I2C_DIR);  
  regvalue |= SDA;  
  HW_REG(GPIO_I2C_DIR) = regvalue;  
   
  HW_REG(GPIO_I2C_SDA_REG) = 0;  
  return;  
  }  
  else if(whichline == (SDA|SCL))  
  {  
  regvalue = HW_REG(GPIO_I2C_DIR);  
  regvalue |= (SDA|SCL);  
  HW_REG(GPIO_I2C_DIR) = regvalue;  
   
  HW_REG(GPIO_I2C_SCLSDA_REG) = 0;  
  return;  
  }  
  else  
  {  
  printk("Error input.\n");  
  return;  
  }  
   
}  
  

static void i2c_set(unsigned char whichline)  
{  
  unsigned char regvalue;  
   
  if(whichline == SCL)  
  {  
  regvalue = HW_REG(GPIO_I2C_DIR);  
  regvalue |= SCL;  
  HW_REG(GPIO_I2C_DIR) = regvalue;  
   
  HW_REG(GPIO_I2C_SCL_REG) = SCL;  
  return;  
  }  
  else if(whichline == SDA)  
  {  
  regvalue = HW_REG(GPIO_I2C_DIR);  
  regvalue |= SDA;   <