/**
  * @brief  Initializes the camera.
  * @param  uint32_t Resolution : camera sensor requested resolution (x, y) : standard resolution
  *         naming QQVGA, QVGA, VGA ...
  *
  * @retval Camera status
  */
uint8_t BSP_CAMERA_Init(uint32_t Resolution)
{ 
  DCMI_HandleTypeDef *phdcmi;
  uint8_t status = CAMERA_ERROR;

  /* Get the DCMI handle structure */
  phdcmi = &hDcmiEval;

  /*** Configures the DCMI to interface with the camera module ***/
  /* DCMI configuration */
  phdcmi->Init.CaptureRate      = DCMI_CR_ALL_FRAME;
  phdcmi->Init.HSPolarity       = DCMI_HSPOLARITY_HIGH;
  phdcmi->Init.SynchroMode      = DCMI_SYNCHRO_HARDWARE;
  phdcmi->Init.VSPolarity       = DCMI_VSPOLARITY_HIGH;
  phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  phdcmi->Init.PCKPolarity      = DCMI_PCKPOLARITY_RISING;
  phdcmi->Instance              = DCMI;

  /* Configure IO functionalities for CAMERA detect pin */
  BSP_IO_Init(); 
  
  /* Apply Camera Module hardware reset */
  BSP_CAMERA_HwReset();

  /* Check if the CAMERA Module is plugged on board */
  if(BSP_IO_ReadPin(CAM_PLUG_PIN) == BSP_IO_PIN_SET)
  {
    status = CAMERA_NOT_DETECTED;
    return status; /* Exit with error */
  }

  /* Read ID of Camera module via I2C */
  if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID)
  {
    /* Initialize the camera driver structure */
    camera_drv = &s5k5cag_drv;
    CameraHwAddress = CAMERA_I2C_ADDRESS;

    /* DCMI Initialization */
    BSP_CAMERA_MspInit(&hDcmiEval, NULL);
    HAL_DCMI_Init(phdcmi);

    /* Camera Module Initialization via I2C to the wanted 'Resolution' */
    camera_drv->Init(CameraHwAddress, Resolution);

    CameraCurrentResolution = Resolution;

    /* Return CAMERA_OK status */
    status = CAMERA_OK;
  }
  else
  {
    /* Return CAMERA_NOT_SUPPORTED status */
	status = CAMERA_NOT_SUPPORTED;
  }

  return status;
}
/**
  * @brief  Initializes the camera.
  * @param  Camera: Pointer to the camera configuration structure
  * @retval Camera status
  */
uint8_t BSP_CAMERA_Init(uint32_t Resolution)
{ 
  DCMI_HandleTypeDef *phdcmi;
  
  uint8_t ret = CAMERA_ERROR;
  
  /* Get the DCMI handle structure */
  phdcmi = &hDcmiEval;

  /*** Configures the DCMI to interface with the camera module ***/
  /* DCMI configuration */
  phdcmi->Init.CaptureRate      = DCMI_CR_ALL_FRAME;
  phdcmi->Init.HSPolarity       = DCMI_HSPOLARITY_HIGH;
  phdcmi->Init.SynchroMode      = DCMI_SYNCHRO_HARDWARE;
  phdcmi->Init.VSPolarity       = DCMI_VSPOLARITY_HIGH;
  phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  phdcmi->Init.PCKPolarity      = DCMI_PCKPOLARITY_RISING;
  phdcmi->Init.ByteSelectMode   = DCMI_BSM_ALL;
  phdcmi->Init.ByteSelectStart  = DCMI_OEBS_ODD;
  phdcmi->Init.LineSelectMode   = DCMI_LSM_ALL;
  phdcmi->Init.LineSelectStart  = DCMI_OELS_ODD; 
  phdcmi->Instance              = DCMI;

  /* Configure IO functionalities for CAMERA detect pin */
  BSP_IO_Init(); 
  
  /* Apply Camera hardware reset */
  BSP_CAMERA_HwReset();

  /* Check if the CAMERA is plugged */
  if(BSP_IO_ReadPin(CAM_PLUG_PIN))
  {
    return CAMERA_ERROR;
  }

  if (s5k5cag_ReadID(CAMERA_I2C_ADDRESS) == S5K5CAG_ID)
  {
    /* Initialize the camera driver structure */
    camera_drv = &s5k5cag_drv;
    cameraHwAddress = CAMERA_I2C_ADDRESS;

    /* DCMI Initialization */
    BSP_CAMERA_MspInit(&hDcmiEval, NULL);
    HAL_DCMI_Init(phdcmi);

    /* Camera Init */
    camera_drv->Init(cameraHwAddress, Resolution);

    currentResolution = Resolution;

    /* Return CAMERA_OK status */
    ret = CAMERA_OK;
  }
  else
  {
    /* No supported camera sensor found */
    ret = CAMERA_ERROR;
  }
  
  return ret;
}