Skip to main content
PToma.14
Associate II
March 5, 2019
Solved

Handling 16bit register address in i2c communication

  • March 5, 2019
  • 2 replies
  • 2000 views

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;
}

This topic has been closed for replies.
Best answer by Vangelis Fortounas

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.

2 replies

Vangelis Fortounas
Associate II
March 5, 2019

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
PToma.14Author
Associate II
March 6, 2019

Thanks your suggestion worked for me. HAL_I2C_Mem_Read worked.