cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-H743ZI with Adafruit OV5640 camera board not reading/writing on I2C bus

amay_stm32
Visitor

Hello!

I recently bought a OV5640 Adafruit breakout board from here and I was having some issues with using the I2C bus. I downloaded the libraries from the stm32_ov5640 GitHub page. Whenever I run the HAL_I2C_Mem_Write or HAL_I2C_Mem_Read functions, an error is returned. I read online that configuring the registers is needed before reading the chip, but even writing doesn't work.

The following is my write register function:

static int32_t OV5640_IO_ReadReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
  HAL_StatusTypeDef status;
  uint8_t reg_addr[2];

  // OV5640 uses 16-bit register addresses (big-endian)
  reg_addr[0] = (Reg >> 8) & 0xFF;  // High byte
  reg_addr[1] = Reg & 0xFF;         // Low byte

  // Send register address then read data
  status = HAL_I2C_Mem_Read(&hi2c1, OV5640_I2C_ADDRESS, Reg, I2C_MEMADD_SIZE_16BIT, pData, Length, HAL_MAX_DELAY); // OV5640_I2C_ADDRESS = 0x3C but I tried with 0x3C << 1 and 0x79
  if (status != HAL_OK) {
  	return OV5640_ERROR;
    }

return OV5640_OK;
}

I run it using the following code

printf("Reading camera ID...\r\n");
uint8_t tmp = 0x0;
ret = OV5640_IO_ReadReg(OV5640_I2C_ADDRESS, OV5640_CHIP_ID_HIGH_BYTE, &tmp, 1);

if (ret != HAL_OK){
  printf("Failed to read register");
  BSP_LED_On(LED_RED);
} else {
  printf("Camera ID read: SUCCESS (ID=0x%hu)\r\n", &tmp);
}

Whenever I run it, it outputs the "Failed to read register line." I have connected all of the pins except the DCMI pins and the external clock pin. I soldered the internal clock jumper on as specified in the Adafruit pinout page here. The green light on the camera board is lighting up. I have tried a lot and looked through many forum posts, but nobody seems to have my exact issue since I tried with all their fixes and nothing worked. Let me know if you have anymore questions!

The link to the function on my GitHub repository is here

Thanks in advance!!

1 REPLY 1
amay_stm32
Visitor

Just wanted to post here to help anybody else having this issue as well. The solution that worked for me was to first edit my read and write functions to be like this:

static int32_t OV5640_IO_WriteReg(uint16_t DevAddr, uint16_t Reg, uint8_t *pData, uint16_t Length)
{
  HAL_StatusTypeDef status;

  // Use the DevAddr parameter, shifted left by 1 for HAL I2C functions
  status = HAL_I2C_Mem_Write(&hi2c1, (DevAddr << 1), Reg, I2C_MEMADD_SIZE_16BIT, pData, Length, HAL_MAX_DELAY);
  if (status != HAL_OK) {
    return OV5640_ERROR;
  }

  return OV5640_OK;
}

I also had to change the reset function. Before the function was written like this:

static void Camera_Reset(void)
{
  // Pull reset pin low for at least 1ms, then high
  HAL_GPIO_WritePin(GPIOA, Reset_Pin, GPIO_PIN_RESET);
  HAL_Delay(2);  // Hold reset for 2ms
  HAL_GPIO_WritePin(GPIOA, Reset_Pin, GPIO_PIN_SET);
  HAL_Delay(50); // Allow camera to boot up
}

I needed to just change the GPIO port since the pin is on GPIOF, and that is already defined by the code in the visual editor. So all I did was change GPIOA for Reset_GPIO_Port. It seems that it has something to do with the camera not turning on until the reset pin is in the "set" mode or, in other words, is toggled high. I don't know why this is the case, but if you're having a similar issue to mine, I would make sure to reset your camera.