cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Timing parameter confusion

ZHama.1
Associate II

I'm interfacing a camera module (OV2640) with STM32F446RE. I've found a driver on github for this camera however the given example from the github repository is for an F7 chip. I've integrated all the files for my f446re chip and everything runs correctly however the i2c communication between the stm32 and camera doesn't look right. The camera doesnt send an acknowledgement (image from logic analyser software). In the image, this same signal of writing to address 60 is repeated everytime transmit/recieve function called.

0693W00000D0YK7QAN.jpg 

I believe the issue lies in the timing of the communication. In the f7 example the i2c init function has a parameter called timing which is set to 0x1060669A

static void MX_I2C1_Init(void) {
 
	/* USER CODE BEGIN I2C1_Init 0 */
 
	/* USER CODE END I2C1_Init 0 */
 
	/* USER CODE BEGIN I2C1_Init 1 */
 
	/* USER CODE END I2C1_Init 1 */
	hi2c1.Instance = I2C1;
	hi2c1.Init.Timing = 0x1060669A;
	hi2c1.Init.OwnAddress1 = 0;
	hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
	hi2c1.Init.OwnAddress2 = 0;
	hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
	hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
	if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
		Error_Handler();
	}

In my code for f446re chip below, the i2c init was created in cubeMX but f4 doesn't have this timing paramter but clock speed instead.

static void MX_I2C2_Init(void)
{
 
  /* USER CODE BEGIN I2C2_Init 0 */
 
  /* USER CODE END I2C2_Init 0 */
 
  /* USER CODE BEGIN I2C2_Init 1 */
 
  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.ClockSpeed = 100000;
  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }

What does the timing parameter in f7 HAL init function do and if this is the cause of the communication issue what can be done to fix it in f4 chip?

7 REPLIES 7
TDK
Guru

There is no issue with the clock speed here.

You're writing to address 0x30, not 0x60. Addresses need to be left-aligned in the upper 7 bits in the address parameter.

If you feel a post has answered your question, please click "Accept as Solution".
ZHama.1
Associate II

.

The camera datasheet says its slave address is 0x60 and the driver has it as 0x60 too? Are you saying I should be writing to 0x30 or that I'm mistakenly writing to 0x30?

I am saying you are mistakenly writing to 0x30 based on the I2C signals in your plot.

The slave address you pass to HAL_I2C_* should be 0xC0, which is 0x60 shifted left 1 bit.

If you feel a post has answered your question, please click "Accept as Solution".

The plot literally says 'write to 0x60', i didnt write that the analyser software figures it out. Plus that means I'd have to change all transmit/recieve function addresses from what they were originally in the example and the example is tested and works. Is there any other potential issue that is causing the communication not to work?

I looked at the data-sheet. The 0x60 is a 8-bit address (with the R/W bit included), so there's no addressing issue here. It is more common to list I2C addresses in 7-bit form.

If the device fails to ACK, look for wiring issue. Going to be hard to do on a BGA package. Verify power, verify SCL/SDA signals go to the right pins. If you can, verify the chip is using the expected amount of current.

I say there's no issue with timing here because your plot shows the clock rate is set to 100 kHz.

If you feel a post has answered your question, please click "Accept as Solution".

So I left it for a few hours and came back. I haven't touched or changed anything but its all somehow working now, its giving ack and transmitting/recieving fine, I then unplug the usb (using Nucleo) and plug it back in after a few times testing and its suddenly not working, sometimes theres an ack and sometimes there isnt, everytime i reset and test again I get a different outcome. This is so bizarre?