[SOLVED] Program I2C using C on Grove Vision AI V2 (WE2 processor)

Hi PJ

Thanks a lot for your input!

So, I checked the samples again like an idiot for the 100th time :slightly_smiling_face:

Following your suggestion, let’s look at the first i2cs sample: (in hx_drv_iic.h)

*    Sample code: I2C slave 0 pin mux configuration and initialization
*      /// The output pin of I2C slave 0 is defined by the user application.
*      hx_drv_scu_set_PA2_pinmux(SCU_PA2_PINMUX_SB_I2C_S_SCL_0);
*      hx_drv_scu_set_PA3_pinmux(SCU_PA3_PINMUX_SB_I2C_S_SDA_0);
*   
*      /// initializes the I2C slave 0 
*       hx_drv_i2cs_init(USE_DW_IIC_SLV_0, HX_I2C_HOST_SLV_0_BASE);
*
*    Usage-1: Transmit data using interrupt mode with I2C slave 0
*      void i2cs_0_tx_cb()
*      {
*          xprintf("[%s] \n", __FUNCTION__);
*      }
*
*      uint8_t wbuffer[32] = {0};
*      uint32_t i, data_size = 32;
*      for(i = 0; i < data_size; i++)
*      {
*          wbuffer[i] = i;
*          xprintf("wbuffer[%d]:0x%02x \n", i, wbuffer[i]);
*      }
*
*      hx_drv_i2cs_interrupt_write(USE_DW_IIC_SLV_0, 0x62, wbuffer, data_size, i2cs_0_tx_cb);
*

So we add this example to the app_main function:

#include <hx_drv_iic.h>

void i2cs_0_tx_cb()
{
	xprintf("[%s] \n", __FUNCTION__);
}

int app_main(void) {
  
	/* i2c example from hx_drv_iic.h */
  hx_drv_scu_set_PA2_pinmux(SCU_PA2_PINMUX_SB_I2C_S_SCL_0);
  hx_drv_scu_set_PA3_pinmux(SCU_PA3_PINMUX_SB_I2C_S_SDA_0);
	
  hx_drv_i2cs_init(USE_DW_IIC_SLV_0, HX_I2C_HOST_SLV_0_BASE);
	
  uint8_t wbuffer[32] = {0};
  uint32_t i, data_size = 32;
  for(i = 0; i < data_size; i++)
  {
    wbuffer[i] = i;
    xprintf("wbuffer[%d]:0x%02x \n", i, wbuffer[i]);
  }

  hx_drv_i2cs_interrupt_write(USE_DW_IIC_SLV_0, 0x62, wbuffer, data_size, i2cs_0_tx_cb);

  // .. there is some default code between above and below ..

  app_start_state(APP_STATE_ALLON);
  return 0;
}

However, we can’t compile it, because HX_I2C_HOST_SLV_0_BASE is undefined. Only HX_I2C_HOST_SLV_1_BASE gets shown by auto-complete, which, when flashed, also doesn’t show up on the I2C scanner.

How to fix it

Searching for where HX_I2C_HOST_SLV_0_BASE or HX_I2C_HOST_SLV_1_BASE appear in code, we find the file WE_device_addr.h - the aforementioned undefined reference is dependent on a preprocessor definition: IP_INST_IIC_SLAVE0 - so… just define it somewhere. Either in code (where we use it - in this case in the same file that the app_main function is - at the very top before all includes) or in your build system, IDE setting, or whatever.

in my case to test it, I simply added this to allon_sensor_tflm.c after line 7: (after std* includes)

// try to get i2s #0
#define IP_INST_IIC_SLAVE0

When we now compile and flash this, we will see this output on the I2C scanner:

I2C device found at address 0x28 
I2C device found at address 0x62 
I2C device found at address 0x79 

Now I have to try to actually send/receive some data. If I fail on that, expect to see me again

Again, thanks a lot :+1:

Best regards