cancel
Showing results for 
Search instead for 
Did you mean: 

Handling 16bit register address in i2c communication

PToma.14
Associate II

I am trying to use ov5640 camera with stm32l4r5 board nucleo-144. The ov5640 has SCCB bus which is similar to i2c. But ov5640 has register with 16bit address (0x300A, 0x300B like this). I used CUBEMX to initialise i2c pins and generated code. How can I communicate with camera using i2c. I am using following code but it is not working.

int ov5640_init(I2C_HandleTypeDef *p_hi2c)
{
  sp_hi2c = p_hi2c;
  int returnValue = 0;
  uint8_t buffer;
 
  //ov5640_write(0x31, 0x11);
  //reg = ov5640_write(0x3008, 0x82);
  HAL_Delay(30);
 
  returnValue = ov5640_read(OV5640_CHIPIDH, &buffer);
  if (buffer == 0x56)
  {
	  returnValue = ov5640_read(OV5640_CHIPIDL, &buffer);
	  if (buffer == 0x40)
	  {
		  return 0;
	  }
	  else
	  {
		  return 1;
	  }
  }
  else
  {
	  return 1;
  }
}
 
static int ov5640_write(uint16_t regAddr, uint8_t data)
{
  HAL_StatusTypeDef ret;
  do {
    ret = HAL_I2C_Mem_Write(sp_hi2c, OV5640_WR_ADDR, regAddr, I2C_MEMADD_SIZE_16BIT, &data, 1, 100);
  } while (ret != HAL_OK && 0);
  return ret;
}
 
 
static int ov5640_read(uint16_t regAddr, uint8_t *data)
{
  HAL_StatusTypeDef ret;
  uint8_t regAddrMSB = regAddr >> 8;
  uint8_t regAddrLSB = regAddr & 0xFF;
  do {
    ret = HAL_I2C_Master_Transmit(sp_hi2c, 0x78, &regAddrMSB, 1, 100);
    ret |= HAL_I2C_Master_Transmit(sp_hi2c, 0x78, &regAddrLSB, 1, 100);
    ret |= HAL_I2C_Master_Receive(sp_hi2c, 0x79, data, 1, 100);
  } while (ret != HAL_OK && 0);
  return ret;
}

1 ACCEPTED SOLUTION

Accepted Solutions

hello

Device slave address sould be shifted left by one bit,

A way to read data from a 16 bit address is

HAL_I2C_Mem_Read(sp_hi2c, 0x78<<1, regAddr, 2, data, datalen, timeout);

regAddr is 16 bit address and "2" is the size in bytes of regAddr.

View solution in original post

2 REPLIES 2

hello

Device slave address sould be shifted left by one bit,

A way to read data from a 16 bit address is

HAL_I2C_Mem_Read(sp_hi2c, 0x78<<1, regAddr, 2, data, datalen, timeout);

regAddr is 16 bit address and "2" is the size in bytes of regAddr.

PToma.14
Associate II

Thanks your suggestion worked for me. HAL_I2C_Mem_Read worked.