static unsigned char iic_recvbyte (void) { unsigned char out, in; int i; out = (inb(IOC_CONTROL) & 0xfc) | 0xc0; outb(out, IOC_CONTROL); in = 0; for (i = 7; i >= 0; i--) { outb(out | 1, IOC_CONTROL); iic_delay(); outb(out | 3, IOC_CONTROL); iic_delay(); in = (in << 1) | (inb(IOC_CONTROL) & 1); outb(out | 1, IOC_CONTROL); iic_delay(); } outb(out, IOC_CONTROL); iic_delay(); outb(out | 2, IOC_CONTROL); iic_delay(); return in; }
static u8_t iic_recv_ack(void) { u8_t ret = 0; u8_t i; iic_sda_dir(0); iic_set_scl(0); iic_delay(20); iic_set_scl(1); iic_delay(20); for(i = 0; i < 5; i++) { if(! iic_get_sda()) { ret = 1; break; } } iic_delay(10); iic_set_scl(0); iic_delay(10); iic_set_sda(1); iic_sda_dir(1); return ret; }
void LCD_SendByte(Uint16 dat) // { Uint16 i=0; EALLOW; GpioMuxRegs.GPBDIR.bit.GPIOB7=1;//shuchu EDIS; for(i=0;i<8;i++) { SCL_LCD_0; iic_delay(); if( dat & 0x0080 ) { SDL_LCD_1; } else { SDL_LCD_0; } SCL_LCD_1; iic_delay(); dat <<= 1; SCL_LCD_0; } }
/******************************************************************************* * 函数名称: dru_iic_8873_read_byte * 功 能: 8873 的iic 读单个字节 * 参 数: * 参数名称 类型 描述 *data unsigned char * 存储8873 iic所读数据的地址 * 返回值: * 无 * 说 明: * 日 期 版本 作者 修改人 DEBUG * ----------------------------------------------------------------- * 2012/07/04 V1.0 H4 无 : *******************************************************************************/ void dru_iic_8873_read_byte(unsigned char *data) { unsigned char index=0; unsigned char tmp_data=0; unsigned int level=0; DRU_ICC_8873_SDA_DIR_IN(); iic_delay(IIC_DELAY_TIME); for(index=0;index<8;index++) { DRU_ICC_8873_SCL_OUT_HIGH(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SDA_IN(level); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_LOW(); iic_delay(2*IIC_DELAY_TIME); //printf("level=%x\n",level); if(level){ tmp_data|=(unsigned char)(1<<(7-index)); } } *data=tmp_data; //printf("read iic=%x\n",tmp_data); DRU_ICC_8873_SDA_DIR_OUT(); iic_delay(2*IIC_DELAY_TIME); return; }
/******************************************************************************* * 函数名称: dru_iic_8873_noack * 功 能: 8873 的 * 参 数: * 参数名称 类型 描述 * * 返回值: * 无 * 说 明: * 日 期 版本 作者 修改人 DEBUG * ----------------------------------------------------------------- * 2012/07/04 V1.0 H4 无 : *******************************************************************************/ void dru_iic_8873_noack(void) { DRU_ICC_8873_SDA_OUT_HIGH(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_HIGH(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_LOW(); iic_delay(IIC_DELAY_TIME); }
/*----------------------------------------------------------------------------*/ void iic_start(void) { iic_init_io(); iic_delay(); iic_data_l(); iic_delay(); iic_clk_l(); iic_delay(); iic_data_h(); }
static void iic_stop(void) { iic_set_scl(0); iic_set_sda(0); iic_delay(5); iic_set_scl(1); iic_delay(10); iic_set_sda(1); iic_delay(10); }
static void i2csoft_sendstop(YWGPIO_Handle_T SCL, YWGPIO_Handle_T SDA) { YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(2); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_OUTPUT); YWGPIO_Write(SDA, 0); //I2C_SDA(0); iic_delay(1); YWGPIO_Write(SCL, 1); //I2C_SCL(1); iic_delay(5); YWGPIO_Write(SDA, 1); //I2C_SDA(1); iic_delay(4); }
static void iic_send_ack(void) { iic_set_sda(0); iic_delay(10); iic_set_scl(0); iic_delay(10); iic_set_scl(1); iic_delay(30); iic_set_scl(0); iic_delay(20); }
static inline void iic_stop (void) { unsigned char out; out = inb(IOC_CONTROL) | 0xc3; iic_delay(); outb(out ^ 1, IOC_CONTROL); iic_delay(); outb(out, IOC_CONTROL); }
/*----------------------------------------------------------------------------*/ void iic_stop(void) { iic_data_out(); iic_data_l(); iic_delay(); iic_clk_h(); iic_delay(); iic_data_h(); iic_delay(); iic_data_l(); iic_clk_l(); }
/*----------------------------------------------------------------------------*/ void s_ack(u8 flag) { iic_data_out(); iic_clk_l(); if (flag) { iic_data_h(); } else { iic_data_l(); } iic_delay(); iic_clk_h(); iic_delay(); iic_clk_l(); }
static u8_t iic_send_byte(u8_t c) { u8_t i; for(i = 0; i < 8; i++) { iic_set_scl(0); iic_delay(10); iic_set_sda((c & (0x80 >> i)) ? 1 : 0); iic_delay(10); iic_set_scl(1); iic_delay(30); iic_set_scl(0); iic_delay(10); } return iic_recv_ack(); }
/******************************************************************************* * 函数名称: dru_iic_8873_ack * 功 能: 8873 的iic ack * 参 数: * 参数名称 类型 描述 * * 返回值: * 无 * 说 明: * 日 期 版本 作者 修改人 DEBUG * ----------------------------------------------------------------- * 2012/07/04 V1.0 H4 无 : *******************************************************************************/ int dru_iic_8873_ack(void) { unsigned int data; DRU_ICC_8873_SDA_DIR_IN(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_HIGH(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SDA_IN(data); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_LOW(); iic_delay(2*IIC_DELAY_TIME); DRU_ICC_8873_SDA_DIR_OUT(); iic_delay(2*IIC_DELAY_TIME); if(data!=0){ return 1; } return 0; }
static void i2csoft_sendack(YWGPIO_Handle_T SCL, YWGPIO_Handle_T SDA, int ack) { YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(3); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_OUTPUT); if (ack) YWGPIO_Write(SDA, 1); //I2C_SDA(1); else YWGPIO_Write(SDA, 0); //I2C_SDA(0); iic_delay(3); YWGPIO_Write(SCL, 1); //I2C_SCL(1); iic_delay(5); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_BIDIRECTIONAL); YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(2); }
/******************************************************************************* * 函数名称: dru_iic_8873_write_byte * 功 能: 8873 的iic 写单个字节 * 参 数: * 参数名称 类型 描述 *data unsigned char 8873 iic需要写的数据 * 返回值: * 无 * 说 明: * 日 期 版本 作者 修改人 DEBUG * ----------------------------------------------------------------- * 2012/07/04 V1.0 H4 无 : *******************************************************************************/ void dru_iic_8873_write_byte(unsigned char data) { unsigned char index=0,data_tmp; data_tmp=data; for(index=0;index<8;index++){ if(((data_tmp<<index)&0x80)!=0){ DRU_ICC_8873_SDA_OUT_HIGH(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_HIGH(); //printf("gpio->set_data01=%x\n",gpio_t->set_data01); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_LOW(); //printf("gpio->clr_data01=%x\n",gpio_t->clr_data01); iic_delay(IIC_DELAY_TIME); }else{ DRU_ICC_8873_SDA_OUT_LOW(); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_HIGH(); //printf("gpio->set_data01=%x\n",gpio_t->set_data01); iic_delay(IIC_DELAY_TIME); DRU_ICC_8873_SCL_OUT_LOW(); iic_delay(IIC_DELAY_TIME); //printf("gpio->clr_data01=%x\n",gpio_t->clr_data01); } //printf("write index=%x\n",index); } return; }
static U8 i2csoft_readbyte(YWGPIO_Handle_T SCL, YWGPIO_Handle_T SDA, int ack) { U8 data; int j; U8 Value; data = 0; YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_BIDIRECTIONAL); YWGPIO_Write(SDA, 1); //I2C_SDA(1); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_INPUT); //printk("!!!!:"); for (j = 0; j < 8; j++) { iic_delay(1); YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(5); YWGPIO_Write(SCL, 1); //I2C_SCL(1); iic_delay(3); //Value = I2C_READ(); YWGPIO_Read(SDA, &Value); iic_delay(2); data = data << 1; data = data | Value; //printk("%d ",Value); //iic_delay(100000); //I2C_SCL(0); } //printk("\n"); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_BIDIRECTIONAL); YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(2); //YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_OUTPUT); i2csoft_sendack(SCL, SDA, ack); return (data); }
static u8_t iic_recv_byte(void) { u8_t i; u8_t c = 0; iic_sda_dir(0); for (i = 0; i < 8; i++) { iic_set_scl(0); iic_delay(10); iic_set_scl(1); iic_delay(20); c |= (iic_get_sda() << (7 - i)); iic_delay(10); iic_set_scl(0); iic_delay(20); } iic_sda_dir(1); return c; }
/*----------------------------------------------------------------------------*/ bool r_ack(void) { bit nack; iic_data_in(); iic_delay(); iic_clk_h(); iic_delay(); iic_delay(); iic_delay(); iic_delay(); iic_delay(); nack = iic_data_r(); iic_clk_l(); iic_delay(); return nack; }
static int i2c_gpio_readnostop(U8 SlaveAddr, U8 *buffer, int len, int timeout, YWI2CSoft_Device_t *I2CSoftDevice) { //int shift; //int ack; int i; #if 0 while (timeout--) { i2csoft_sendstart(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle); //write_byte(SoftI2c.SlaveAddr +1); /* send cycle */ //if(write_byte(SlaveAddr+1)) //ACK if (!i2csoft_writebyte(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle, SlaveAddr + 1)) //ACK { break; } i2csoft_sendstop(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle); iic_delay(10); } if (timeout == 0) { printk("read error\n"); return (-1); } #endif i2csoft_sendstart(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle); i2csoft_writebyte(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle, SlaveAddr + 1); //printk("len = %d :",len); for (i = 0; i < (len - 1); i++) { buffer[i] = i2csoft_readbyte(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle, 0); //printk("0x%x ",buffer[i]); } buffer[len - 1] = i2csoft_readbyte(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle, 1); //printk("0x%x ",buffer[len-1]); //printk("\n"); //i2csoft_sendstop(I2CSoftDevice->SCLHandle, I2CSoftDevice->SDAHandle); return (0); }
static void iic_init(void) { /* * iic clk - output high level and pull up */ writel(S5PV210_GPD1CON, (readl(S5PV210_GPD1CON) & ~(0xf<<4)) | (0x1<<4)); writel(S5PV210_GPD1PUD, (readl(S5PV210_GPD1PUD) & ~(0x3<<2)) | (0x2<<2)); writel(S5PV210_GPD1DAT, (readl(S5PV210_GPD1DAT) & ~(0x1<<1)) | (0x1<<1)); /* * iic data - output high level and pull up */ writel(S5PV210_GPD1CON, (readl(S5PV210_GPD1CON) & ~(0xf<<0)) | (0x1<<0)); writel(S5PV210_GPD1PUD, (readl(S5PV210_GPD1PUD) & ~(0x3<<0)) | (0x2<<0)); writel(S5PV210_GPD1DAT, (readl(S5PV210_GPD1DAT) & ~(0x1<<0)) | (0x1<<0)); iic_set_sda(1); iic_set_scl(1); iic_delay(10); }
static int i2csoft_writebyte(YWGPIO_Handle_T SCL, YWGPIO_Handle_T SDA, U8 data) { int j; U8 nack; YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_OUTPUT); for (j = 0; j < 8; j++) { YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(1); if (data & 0x80) YWGPIO_Write(SDA, 1); //I2C_SDA(1); else YWGPIO_Write(SDA, 0); //I2C_SDA(0); iic_delay(1); YWGPIO_Write(SCL, 1); //I2C_SCL(1); iic_delay(5); //I2C_SCL(0); data <<= 1; } /* * Look for an <ACK>(negative logic) and return it. */ YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_BIDIRECTIONAL); YWGPIO_Write(SCL, 0); //I2C_SCL(0); iic_delay(2); YWGPIO_Write(SDA, 1); //I2C_SDA(1); iic_delay(2); YWGPIO_Write(SCL, 1); //I2C_SCL(1); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_INPUT); iic_delay(3); YWGPIO_Read(SDA, &nack); //nack = I2C_READ(); YWGPIO_SetIoMode(SDA, YWGPIO_IO_MODE_BIDIRECTIONAL); YWGPIO_Write(SCL, 0); //2C_SCL(0); iic_delay(2); return (nack); /* not a nack is an ack */ }
static int iic_sendbyte (unsigned char b) { unsigned char out, in; int i; out = (inb(IOC_CONTROL) & 0xfc) | 0xc0; outb(out, IOC_CONTROL); for (i = 7; i >= 0; i--) { unsigned char c; c = out | ((b & (1 << i)) ? 1 : 0); outb(c, IOC_CONTROL); iic_delay(); outb(c | 2, IOC_CONTROL); iic_delay(); outb(c, IOC_CONTROL); } outb(out | 1, IOC_CONTROL); iic_delay(); outb(out | 3, IOC_CONTROL); iic_delay(); in = inb(IOC_CONTROL) & 1; outb(out | 1, IOC_CONTROL); iic_delay(); outb(out, IOC_CONTROL); iic_delay(); if(in) { printk("No acknowledge from RTC\n"); return 1; } else return 0; }