Forum Discussion
Altera_Forum
Honored Contributor
20 years agohow to program use OpenCores I2C Master core in n2
hi all: I am useing the OpenCores I2C Master core in nios2 . But the core have no any documents to guide user. It just have a include file . I have writed the i2c read and write func...
Altera_Forum
Honored Contributor
20 years agoint32_t aic12_read_reg(uint8_t reg,uint8_t *data,uint8_t count)
{ uint32_t i; int32_t err=0; uint8_t status=0; uint8_t devadd=0x40; *data=0; // first init register index err=i2c_write_byte(CR_STA,(devadd<<1)|I2C_WR,&status); if(err) return -1; err=i2c_write_byte(CR_STO,reg,&status); if(err) return -1; // now read register index err=i2c_write_byte(CR_STA,(devadd<<1)|I2C_RD,&status); if(err) return -1; for(i=0;i<count-1;i++) { err=i2c_read_byte(0,data,&status); // CR_NACK = BIT3 = 0 --> ack is sent if(err) return -1; data++; } // create stop signal, no ack err=i2c_read_byte(CR_STO | CR_NACK ,data,&status); return err; } this is sample code (device specific) to read from a codec device (see your device datasheet), you have to give the correct ctrl value to the read function, the read is done in the "for loop", the last read is done with "CR_STO | CR_NACK" if BIT 3 = CR_NACK is set, no acknoledge is sent good luck