Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12600 Discussions

alt_avalon_i2c_master_tx will always fail with -1 error code

PHeri1
Beginner
1,986 Views

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?

 

 

 

 

 

 

 

 

 

0 Kudos
6 Replies
PHeri1
Beginner
1,894 Views

Update from my side:

Error code is NOT -1 but -7 (ALT_AVALON_I2C_BUSY)

0 Kudos
druva1
New Contributor I
1,894 Views

I recommend to check this example of Master - slave communication: https://www.macnica.co.jp/business/semiconductor/articles/intel/129485/

even though is in Japanese (use google translate to read the pdf with the instructions) you can get the example files, they worked for me.

 

Cheers

0 Kudos
PHeri1
Beginner
1,894 Views

unfortunately, this example exits with error code -2 (ALT_AVALON_I2C_TIMEOUT)

0 Kudos
druva1
New Contributor I
1,894 Views

Which board do you have? Because I had to create a new project and then I replicated the qsys to my board, put the weak resistors, the 3.3 Voltage, and then it worked, re type the code due to the funny characters.

If you have an arduino board you can also used it as a Slave, is almost guarantee it will easily work with it, and it will help you out to understand things.

 

0 Kudos
PHeri1
Beginner
1,894 Views

I have a DE10-Lite board. I had to rename the pins so that they match with my board and I had to resize the memory size in the Platform Designer.

Maybe I am not using the appropriate pins, but I saw a post here that you could use every pin of the 10M50DAF484C7G for I2C purposes.

What I noticed is, when I select the MAX 10 DE10-Lite board in Quartus it selects a 10M50DAF484C6GES devices, but the board has an 10M50DAF484C7G. Maybe no big deal.

Now, when creating a new project I select a 10M50DAF484C7G manually.

 

2020_05_12_07_25_55_Quartus_Prime_Lite_Edition_C_Users_Peter_Downloads_i2c_slave_sample_1_i2c_sl.png

Normally I use these pins: PIN_AB5, PIN_AB6, but I also tried, to be compatible with an Arduino, ARDUINO_IO[14] (PIN_AB21) and ARDUINO_IO[15] (PIN_AA20) without any luck.

I do get a lot of warnings when compiling. One of them is that a bidirectional pin cannot be tri-stated.

 

2020_05_12_07_36_57_Quartus_Prime_Lite_Edition_C_Users_Peter_Downloads_i2c_slave_sample_1_i2c_sl.png

0 Kudos
PHeri1
Beginner
1,894 Views

I wil create the whole project from scratch today like you did. See if it helps.

0 Kudos
Reply