bool eeprom_read(uint32_t address, void* data, uint16_t length) { uint8_t control[3]; control[0] = (uint8_t)(0xa0 | 0 | 0); // clear read/write bit control[1] = (uint8_t)(address >> 8); control[2] = (uint8_t)address; if (address > 0xffff){ control[0] |= 0x08; } iic_start(EEPROM_IIC_CHANNEL); if (iic_write(EEPROM_IIC_CHANNEL, control, 3) == false){ iic_stop(EEPROM_IIC_CHANNEL); return false; } iic_repeated_start(EEPROM_IIC_CHANNEL); control[0] |= 1; // set read/write bit if (iic_write(EEPROM_IIC_CHANNEL, control, 1) == false){ iic_stop(EEPROM_IIC_CHANNEL); return false; } iic_read(EEPROM_IIC_CHANNEL, data, length); iic_stop(EEPROM_IIC_CHANNEL); return true; }
unsigned int ads_read(unsigned int channel) { unsigned short int result=0; /* unsigned char tmp[2]; */ if (channel > 4) channel = 0; /* Confige1115A(0); */ ads_confige(channel); /* Pointregister(); */ ads_point_reg(); /* delay_ads(100); */ #if 1 start(); iic_write_m(0x91); //地址+读 result|=iic_read_m()<<8; result|=iic_read_m(); /* result = ads_read_count(17); */ /* result = ads_read_count(16); */ stop(); #else iic_read(0x91, tmp, 2); result |= tmp[0] << 8; result |= tmp[1]; #endif return result; }
int main(int argc, char *argv[]) { int status; /* Read buffer to hold the data */ char *buffer = (char *)malloc(EEPROMSIZE * sizeof(char)); char data[] = "THIS IS A TEST MESSAGE FOR THE I2C PROTOCOL COMMUNICATION WITH A EEPROM. IT WAS WRITTEN FOR A REDPITAYA MEASURMENT TOOL."; size_t size = strlen(data); /* Sample offset inside an eeprom */ int offset = 0x100; /* * Open the device. */ fd = open("/dev/i2c-0", O_RDWR); if(fd < 0) { printf("Cannot open the IIC device\n"); return 1; } status = ioctl(fd, I2C_SLAVE_FORCE, EEPROM_ADDR); if(status < 0) { printf("Unable to set the EEPROM address\n"); return -1; } /* Write to redpitaya eeprom */ status = iic_write((char *)data, offset, size); if(status) { fprintf(stderr, "Cannot Write to EEPROM\n"); close(fd); return -1; } /* Read from redpitaya eeprom */ status = iic_read(buffer, EEPROM_ADDR, EEPROMSIZE); if (status) { printf("Cannot Read from EEPROM \n"); close(fd); return 1; } printf("eerprom test successfull.\n"); /* Release allocations */ close(fd); free(buffer); return 0; }
void main() { TMOD=0x01; TH0=(65536-45678)/256; TL0=(65536-45678)%256; ET0=1; TR0=1; EA=1; dat=iic_read(1); while(1) { digit(2,dat/100); digit(3,dat%100/10); digit(4,dat%10); } }
int main( void ) { unsigned char buf[4]; alt_putstr("Boot\n"); // I2C Clock Setting IOWR( IIC_0_BASE, 1, 42 ); // 100kHz (50,000kHz / 100kHz / 12 = 41.6...) // IOWR( IIC_0_BASE, 1, 11 ); // 400kHz (50,000kHz / 400kHz / 12 = 10.4...) buf[0] = 0; iic_write( 0x1D, 1, buf ); iic_read( 0x1D, 1, buf ); alt_printf( "DEVID %x\n", buf[0] ); /* Event loop never exits. */ while( 1 ) { ; } return 0; }
/* Read the EEPROM on the RPX-Lite board. */ void rpx_cfg(bd_t *bd) { u_char eebuf[256], *cp; /* Read the first 256 bytes of the EEPROM. I think this * is really all there is, and I hope if it gets bigger the * info we want is still up front. */ #if 1 iic_read(0xa8, eebuf, 0, 128); iic_read(0xa8, &eebuf[128], 128, 128); { int i; cp = (u_char *)0xfa000000; for (i=0; i<256; i++) *cp++ = eebuf[i]; } /* We look for two things, the Ethernet address and the * serial baud rate. The records are separated by * newlines. */ cp = eebuf; for (;;) { if (*cp == 'E') { cp++; if (*cp == 'A') { cp += 2; rpx_eth(bd, cp); } } if (*cp == 'S') { cp++; if (*cp == 'B') { cp += 2; rpx_brate(bd, cp); } } if (*cp == 'D') { cp++; if (*cp == '1') { cp += 2; rpx_memsize(bd, cp); } } if (*cp == 'H') { cp++; if (*cp == 'Z') { cp += 2; rpx_cpuspeed(bd, cp); } } /* Scan to the end of the record. */ while ((*cp != '\n') && (*cp != 0xff)) cp++; /* If the next character is a 0 or ff, we are done. */ cp++; if ((*cp == 0) || (*cp == 0xff)) break; } bd->bi_memstart = 0; #else /* For boards without initialized EEPROM. */ bd->bi_memstart = 0; bd->bi_memsize = (8 * 1024 * 1024); bd->bi_intfreq = 48; bd->bi_busfreq = 48; bd->bi_baudrate = 9600; #endif }
static u8 gsl_read_interface(u8 reg,u8 *buf,u16 num) { iic_read(reg,buf,num); return 1; }