Forum Discussion

druva1's avatar
druva1
Icon for Occasional Contributor rankOccasional Contributor
5 years ago

i2c Master- i2c to slave (avalon mm bridge) error

Hi,

I have a Max10 Board, first I set it up as a Master (i2c core) and an Arduino Board as a slave, I successfully send and received data.

But when I wanted to add a RAM memory connected to a i2c slave to Avalon MM master bridge it does not work.

When I am trying to transmit the data I keep getting the error [-7], busy.

Here is my code, if someone can help me out:

int main(){

printf("Hello from Nios II!\n");

ALT_AVALON_I2C_DEV_t *i2c_dev; //pointer to instance structure

ALT_AVALON_I2C_MASTER_CONFIG_t cfg;

alt_u8 retry = 0;

alt_u32 *chip_id = 0;

int i;

alt_u8 *ocram[2]; //create slave RAM

ocram[0] = (alt_u8*)(I2C_MEM_0_BASE | 0x80000000);

ocram[1] = (alt_u8*)(I2C_MEM_1_BASE | 0x80000000);

// fill block of memory

for(i=0;i<2;i++){

memset((void*)ocram[i],i, I2C_MEM_0_SIZE_VALUE); // i2c slave memory clear, 256 bytes

}

// Get a pointer to the Avalon i2c block instance

// NOTE: Must match the name on Platform Designer

i2c_dev = alt_avalon_i2c_open("/dev/i2c");

if(NULL==i2c_dev){

printf("Error: Cannot find /dev/i2c\n");

return FALSE;

}

usleep(100);

// Speed settings

alt_avalon_i2c_master_config_speed_set(i2c_dev,&cfg,100000);

alt_avalon_i2c_master_config_set(i2c_dev,&cfg);

//set the address of the device using (32 bits address)

alt_avalon_i2c_master_target_set(i2c_dev,0x50);

// Function: Get i2c Slave ID

while((retry++ < 2)){

alt_avalon_i2c_master_target_get(i2c_dev, chip_id);

printf("Read chip ID: ");

printf("0x%04x\n" , chip_id[0]);

usleep(100);

}

for(i=0;i<2;i++){

i2c_write(i2c_dev,DEV_ADR+i);

}

for(i=0;i<2;i++){

printf("\nI2CSLAVE%d:\n",i);

dump(ocram[i],256);

}

return 0;

}

int i2c_write(ALT_AVALON_I2C_DEV_t *i2c_dev, unsigned char adr){

int i;

alt_u8 txbuffer[MEM_SIZE+BYTE_MODE];

ALT_AVALON_I2C_STATUS_CODE status;

printf("\nADR=%x\n", adr);

usleep(1000000);

//start address of register

txbuffer[0] = WRITE_ADR;

//set value in send buffer

for(i = 0;i<MEM_SIZE;i++){

txbuffer[i+BYTE_MODE] = i|0x80; // some data

}

alt_avalon_i2c_disable(i2c_dev);

dump(txbuffer,MEM_SIZE+BYTE_MODE);

status=alt_avalon_i2c_enable(i2c_dev);

printf("Status[%d]\n",(int)status);

status = alt_avalon_i2c_master_tx(i2c_dev,txbuffer,MEM_SIZE+1, ALT_AVALON_I2C_NO_INTERRUPTS);

if(status !=ALT_AVALON_I2C_SUCCESS){

printf("Error:Tx Fail[%d]\n",(int)status);

return FALSE;

}

printf("data send");

return TRUE;

}

3 Replies