alt_avalon_i2c_master_tx will always fail with -1 error code
In basic I have this:
module top (
input clk,
output [7:0] led,
inout tri1 i2c_scl,
inout tri1 i2c_sda
);
wire i2c_scl_in;
wire i2c_sda_in;
wire i2c_scl_oe;
wire i2c_sda_oe;
assign i2c_scl_in = i2c_scl;
assign i2c_sda_in = i2c_sda;
assign i2c_scl = i2c_scl_oe ? 1'b0 : 1'bz;
assign i2c_sda = i2c_sda_oe ? 1'b0 : 1'bz;
pro13_i2c_port u0 (
.clk_clk (clk), // clk.clk
.led_external_connection_export (led),
.i2c_0_i2c_serial_scl_in (i2c_scl_in),
.i2c_0_i2c_serial_sda_in (i2c_sda_in),
.i2c_0_i2c_serial_scl_oe (i2c_scl_oe),
.i2c_0_i2c_serial_sda_oe (i2c_sda_oe)
);
endmodule
And here my Nios II code:
#include "sys/alt_stdio.h"
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_uart_regs.h"
#include "altera_avalon_i2c.h"
#include "altera_avalon_i2c_regs.h"
const alt_u8 SLAVE_ADDRESS = 0x40;
ALT_AVALON_I2C_DEV_t *i2c_dev; //pointer to instance structure
int I2C_INIT()
{
ALT_AVALON_I2C_STATUS_CODE status;
//get a pointer to the avalon i2c instance
i2c_dev = alt_avalon_i2c_open("/dev/i2c_0");
if (i2c_dev == NULL)
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x01);
return 1;
}
alt_avalon_i2c_disable(i2c_dev);
ALT_AVALON_I2C_MASTER_CONFIG_t* cfg;
cfg->addr_mode = ALT_AVALON_I2C_ADDR_MODE_7_BIT;
cfg->speed_mode = ALT_AVALON_I2C_SPEED_STANDARD;
alt_avalon_i2c_master_config_set(i2c_dev, cfg);
//set the address of the device using
alt_avalon_i2c_master_target_set(i2c_dev, SLAVE_ADDRESS);
if (i2c_dev == NULL)
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x02);
return 1;
}
status = alt_avalon_i2c_enable(i2c_dev);
if (status != ALT_AVALON_I2C_SUCCESS)
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x04);
return 1; //FAIL
}
return 0;
}
int I2C_SENT()
{
ALT_AVALON_I2C_STATUS_CODE status;
//write data to an I2C device
alt_u8 txbuffer[2];
txbuffer[0] = 0x00;
txbuffer[1] = 0x00;
status = alt_avalon_i2c_master_tx(i2c_dev, txbuffer, 2, ALT_AVALON_I2C_NO_INTERRUPTS);
if (status != ALT_AVALON_I2C_SUCCESS)
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, ~abs(status&0xff));
return 1; //FAIL
}
return 0;
}
int main()
{
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x00);
I2C_INIT();
/* Event loop never exits. */
while(1){
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x00);
delay(2);
I2C_SENT();
delay(2);
}
return 0;
}
Any idea why alt_avalon_i2c_master_tx will always fail with -1 error code?