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++
12618 Discussions

alt_avalon_i2c_master_tx_rx Returns Error

New Contributor I


Quartus Prime Lite Ver 21.1.0 Build 842 10/21/21 - Windows 10 Host system

Eclipse Mars.2 rel 4.5.2


My application is trying to access an I2C temperature sensor. The design has the NIOS 2 with the Avalon I2C (Master), onchip RAM, timer, JTAG Uart. I create a buffer for the Avalon I2C (Master) single to the SDA and SCL in a top level Verilog file:


module i2c_buffer_test (
input clk_50Mhz,
input SW1,
inout i2c_scl_pin, // i2c_scl_pin
inout i2c_sda_pin // i2c_sda_pin

wire m_sda_in;
wire m_scl_in;
wire m_sda_oe;
wire m_scl_oe;

assign i2c_sda = (m_sda_oe)? 1'b0 : 1'bz;
assign m_sda_in = i2c_sda;
assign i2c_scl = (m_scl_oe)? 1'b0 : 1'bz;
assign m_scl_in = i2c_scl;


NIOS2iicMCU soc_inst (
.clk_clk (clk_50Mhz), // clk.clk
.i2c_sda_in (m_sda_in), // i2c.sda_in
.i2c_scl_in (m_scl_in), // .scl_in
.i2c_sda_oe (m_sda_oe), // .sda_oe
.i2c_scl_oe (m_scl_oe), // .scl_oe
.reset_reset_n (SW1) // reset.reset_n



The sensor is a TMP102, Address 0x48 and the register to read is 0x00. I send the 0x00 and expect back 2 bytes. A Windows 10 .NET 6 application on an UP Board successfully tested the device. I can even see the signals on my Oscope. When I test the following application on the NIOS2 design, the alt_avalon_i2c_master_tx_rx comes back with a status of 4294967294 or some equivalent number.


#include "sys/alt_stdio.h"
#include "system.h"
#include "altera_avalon_i2c_regs.h"
#include "altera_avalon_i2c.h"

//Address of TMP102 and the Temp Register
const alt_u32 TEMP_TMP102_ADDR = 0x48;
const alt_u8 TempRegisterAddr = 0x00;

//TMP102 other calls not used in the example
const alt_u8 ConfigRegisterAddr = 0x01;
const alt_u8 TlowRegisterAddr = 0x02;
const alt_u8 ThighRegisterAddr = 0x03;


int main(){

alt_u8 ReadTempbuf[2];
alt_u8 TxBuffer[1]= { TempRegisterAddr };

//TxBuffer[0] = TempRegisterAddr;

ALT_AVALON_I2C_DEV_t *my_i2c;

cfg.addr_mode = 0;

my_i2c = alt_avalon_i2c_open(I2C_0_NAME);
if(my_i2c == NULL){
alt_printf("Failed to open I2C port\n");
return 1;
alt_avalon_i2c_master_target_set(my_i2c, TEMP_TMP102_ADDR ); //pointing to the TMP102 address
alt_avalon_i2c_master_config_speed_set(my_i2c, &cfg, 400000 ); //Set the speed
alt_avalon_i2c_master_config_set(my_i2c, &cfg); //configure
status = alt_avalon_i2c_enable(my_i2c);
if (status!=ALT_AVALON_I2C_SUCCESS){
alt_printf("Not Ready\n");
return 1; //FAIL

status = alt_avalon_i2c_master_tx_rx(my_i2c, TxBuffer, 1, ReadTempbuf, sizeof(ReadTempbuf),ALT_AVALON_I2C_NO_INTERRUPTS);
if (status!=ALT_AVALON_I2C_SUCCESS){
alt_printf("Read Failure\n");
return 1; //FAIL

return 0;


The code worked once, but after a change, nothing. Stepping through the driver code, yielded nothing obvious. any ideas on what could be going on? 






0 Kudos
0 Replies