Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO_L4R9I-LCD-demo
stm32l4r9i_discovery_camera.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4r9i_discovery_camera.c 00004 * @author MCD Application Team 00005 * @brief This file includes the driver for Camera modules mounted on 00006 * STM32L4R9I_DISCOVERY board. 00007 @verbatim 00008 How to use this driver: 00009 ------------------------ 00010 - This driver is used to drive the camera. 00011 - The OV9655 component driver MUST be included with this driver. 00012 00013 Driver description: 00014 ------------------- 00015 + Initialization steps: 00016 o Initialize the camera using the BSP_CAMERA_Init() function. 00017 o Start the camera capture/snapshot using the CAMERA_Start() function. 00018 o Suspend, resume or stop the camera capture using the following functions: 00019 - BSP_CAMERA_Suspend() 00020 - BSP_CAMERA_Resume() 00021 - BSP_CAMERA_Stop() 00022 00023 + Options 00024 o Increase or decrease on the fly the brightness and/or contrast 00025 using the following function: 00026 - BSP_CAMERA_ContrastBrightnessConfig 00027 o Add a special effect on the fly using the following functions: 00028 - BSP_CAMERA_BlackWhiteConfig() 00029 - BSP_CAMERA_ColorEffectConfig() 00030 @endverbatim 00031 ****************************************************************************** 00032 * @attention 00033 * 00034 * <h2><center>© Copyright (c) 2017 STMicroelectronics. 00035 * All rights reserved.</center></h2> 00036 * 00037 * This software component is licensed by ST under BSD 3-Clause license, 00038 * the "License"; You may not use this file except in compliance with the 00039 * License. You may obtain a copy of the License at: 00040 * opensource.org/licenses/BSD-3-Clause 00041 * 00042 ****************************************************************************** 00043 */ 00044 00045 /* Includes ------------------------------------------------------------------*/ 00046 #include "stm32l4r9i_discovery_camera.h" 00047 #include "stm32l4r9i_discovery.h" 00048 #include "stm32l4r9i_discovery_io.h" 00049 00050 /** @addtogroup BSP 00051 * @{ 00052 */ 00053 00054 /** @addtogroup STM32L4R9I_DISCOVERY 00055 * @{ 00056 */ 00057 00058 /** @defgroup STM32L4R9I_DISCOVERY_CAMERA STM32L4R9I_DISCOVERY CAMERA 00059 * @{ 00060 */ 00061 00062 /** @defgroup STM32L4R9I_DISCOVERY_CAMERA_Private_Defines Private Defines 00063 * @{ 00064 */ 00065 #define CAMERA_VGA_RES_X 640 00066 #define CAMERA_VGA_RES_Y 480 00067 #define CAMERA_480x272_RES_X 480 00068 #define CAMERA_480x272_RES_Y 272 00069 #define CAMERA_QVGA_RES_X 320 00070 #define CAMERA_QVGA_RES_Y 240 00071 #define CAMERA_QQVGA_RES_X 160 00072 #define CAMERA_QQVGA_RES_Y 120 00073 /** 00074 * @} 00075 */ 00076 00077 /** @defgroup STM32L4R9I_DISCOVERY_CAMERA_Private_Variables Private Variables 00078 * @{ 00079 */ 00080 DCMI_HandleTypeDef hDcmiHandler; 00081 CAMERA_DrvTypeDef *camera_drv; 00082 /* Camera current resolution naming (QQVGA, VGA, ...) */ 00083 static uint32_t CameraCurrentResolution; 00084 00085 /* Camera module I2C HW address */ 00086 static uint32_t CameraHwAddress; 00087 00088 /** 00089 * @} 00090 */ 00091 00092 /** @defgroup STM32L4R9I_DISCOVERY_CAMERA_Private_FunctionPrototypes Private Function Prototypes 00093 * @{ 00094 */ 00095 static uint32_t GetSize(uint32_t resolution); 00096 /** 00097 * @} 00098 */ 00099 00100 /** @defgroup STM32L4R9I_DISCOVERY_CAMERA_Exported_Functions Exported Functions 00101 * @{ 00102 */ 00103 00104 /** 00105 * @brief Initializes the camera. 00106 * @param Resolution : camera sensor requested resolution (x, y) : standard resolution 00107 * naming QQVGA, QVGA, VGA ... 00108 * @retval Camera status 00109 */ 00110 uint8_t BSP_CAMERA_Init(uint32_t Resolution) 00111 { 00112 DCMI_HandleTypeDef *phdcmi; 00113 uint8_t status = CAMERA_ERROR; 00114 00115 /* Get the DCMI handle structure */ 00116 phdcmi = &hDcmiHandler; 00117 00118 00119 /* Initialize the IO functionalities */ 00120 BSP_IO_Init(); 00121 00122 /* Set up the Camera */ 00123 BSP_CAMERA_PwrUp(); 00124 00125 /*** Configures the DCMI to interface with the camera module ***/ 00126 /* DCMI configuration */ 00127 phdcmi->Init.CaptureRate = DCMI_CR_ALL_FRAME; 00128 phdcmi->Init.HSPolarity = DCMI_HSPOLARITY_HIGH; 00129 phdcmi->Init.SynchroMode = DCMI_SYNCHRO_HARDWARE; 00130 phdcmi->Init.VSPolarity = DCMI_VSPOLARITY_HIGH; 00131 phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B; 00132 phdcmi->Init.PCKPolarity = DCMI_PCKPOLARITY_RISING; 00133 phdcmi->Init.ByteSelectMode = DCMI_BSM_ALL; 00134 phdcmi->Init.ByteSelectStart = DCMI_OEBS_ODD; 00135 phdcmi->Init.LineSelectMode = DCMI_LSM_ALL; 00136 phdcmi->Instance = DCMI; 00137 00138 /* Camera initialization */ 00139 BSP_CAMERA_MspInit(&hDcmiHandler, NULL); 00140 00141 /* Read ID of Camera module via I2C */ 00142 if(ov9655_ReadID(CAMERA_I2C_ADDRESS) == OV9655_ID) 00143 { 00144 /* Initialize the camera driver structure */ 00145 camera_drv = &ov9655_drv; 00146 CameraHwAddress = CAMERA_I2C_ADDRESS; 00147 00148 /* DCMI Initialization */ 00149 HAL_DCMI_Init(phdcmi); 00150 00151 camera_drv->Init(CameraHwAddress, Resolution); 00152 00153 /* Set the RGB565 mode */ 00154 MFX_IO_Write(CameraHwAddress, 0x12 /*OV9655_COM7*/, 0x63); 00155 MFX_IO_Write(CameraHwAddress, 0x40 /*OV9655_COM15*/, 0x10); 00156 /* Invert the HRef signal */ 00157 MFX_IO_Write(CameraHwAddress, 0x15 /*OV9655_COM10*/, 0x08); 00158 00159 00160 CameraCurrentResolution = Resolution; 00161 00162 /* Return CAMERA_OK status */ 00163 status = CAMERA_OK; 00164 } 00165 else 00166 { 00167 /* Return CAMERA_NOT_SUPPORTED status */ 00168 status = CAMERA_NOT_SUPPORTED; 00169 } 00170 00171 return status; 00172 } 00173 00174 /** 00175 * @brief DeInitializes the camera. 00176 * @retval Camera status 00177 */ 00178 uint8_t BSP_CAMERA_DeInit(void) 00179 { 00180 hDcmiHandler.Instance = DCMI; 00181 00182 HAL_DCMI_DeInit(&hDcmiHandler); 00183 BSP_CAMERA_MspDeInit(&hDcmiHandler, NULL); 00184 00185 /* Set Camera in Power Down */ 00186 BSP_CAMERA_PwrDown(); 00187 00188 return CAMERA_OK; 00189 } 00190 00191 /** 00192 * @brief Starts the camera capture in continuous mode. 00193 * @param buff: pointer to the camera output buffer 00194 * @retval None 00195 */ 00196 void BSP_CAMERA_ContinuousStart(uint8_t *buff) 00197 { 00198 /* Start the camera capture */ 00199 HAL_DCMI_Start_DMA(&hDcmiHandler, DCMI_MODE_CONTINUOUS, (uint32_t)buff, GetSize(CameraCurrentResolution)); 00200 } 00201 00202 /** 00203 * @brief Starts the camera capture in snapshot mode. 00204 * @param buff: pointer to the camera output buffer 00205 * @retval None 00206 */ 00207 void BSP_CAMERA_SnapshotStart(uint8_t *buff) 00208 { 00209 /* Start the camera capture */ 00210 HAL_DCMI_Start_DMA(&hDcmiHandler, DCMI_MODE_SNAPSHOT, (uint32_t)buff, GetSize(CameraCurrentResolution)); 00211 } 00212 00213 /** 00214 * @brief Suspend the CAMERA capture 00215 * @retval None 00216 */ 00217 void BSP_CAMERA_Suspend(void) 00218 { 00219 /* Suspend the Camera Capture */ 00220 HAL_DCMI_Suspend(&hDcmiHandler); 00221 } 00222 00223 /** 00224 * @brief Resume the CAMERA capture 00225 * @retval None 00226 */ 00227 void BSP_CAMERA_Resume(void) 00228 { 00229 /* Start the Camera Capture */ 00230 HAL_DCMI_Resume(&hDcmiHandler); 00231 } 00232 00233 /** 00234 * @brief Stop the CAMERA capture 00235 * @retval Camera status 00236 */ 00237 uint8_t BSP_CAMERA_Stop(void) 00238 { 00239 uint8_t status = CAMERA_ERROR; 00240 00241 if(HAL_DCMI_Stop(&hDcmiHandler) == HAL_OK) 00242 { 00243 status = CAMERA_OK; 00244 } 00245 00246 return status; 00247 } 00248 00249 /** 00250 * @brief CANERA power up 00251 * @retval None 00252 */ 00253 void BSP_CAMERA_PwrUp(void) 00254 { 00255 /* De-assert the camera POWER_DOWN pin (active high) */ 00256 BSP_IO_WritePin(CAMERA_PWR_EN_PIN, GPIO_PIN_RESET); 00257 00258 HAL_Delay(3); /* POWER_DOWN de-asserted during 3ms */ 00259 } 00260 00261 /** 00262 * @brief CAMERA power down 00263 * @retval None 00264 */ 00265 void BSP_CAMERA_PwrDown(void) 00266 { 00267 /* Assert the camera POWER_DOWN pin (active high) */ 00268 BSP_IO_WritePin(CAMERA_PWR_EN_PIN, GPIO_PIN_SET); 00269 } 00270 00271 /** 00272 * @brief Configures the camera contrast and brightness. 00273 * @param contrast_level: Contrast level 00274 * This parameter can be one of the following values: 00275 * @arg CAMERA_CONTRAST_LEVEL4: for contrast +2 00276 * @arg CAMERA_CONTRAST_LEVEL3: for contrast +1 00277 * @arg CAMERA_CONTRAST_LEVEL2: for contrast 0 00278 * @arg CAMERA_CONTRAST_LEVEL1: for contrast -1 00279 * @arg CAMERA_CONTRAST_LEVEL0: for contrast -2 00280 * @param brightness_level: Contrast level 00281 * This parameter can be one of the following values: 00282 * @arg CAMERA_BRIGHTNESS_LEVEL4: for brightness +2 00283 * @arg CAMERA_BRIGHTNESS_LEVEL3: for brightness +1 00284 * @arg CAMERA_BRIGHTNESS_LEVEL2: for brightness 0 00285 * @arg CAMERA_BRIGHTNESS_LEVEL1: for brightness -1 00286 * @arg CAMERA_BRIGHTNESS_LEVEL0: for brightness -2 00287 * @retval None 00288 */ 00289 void BSP_CAMERA_ContrastBrightnessConfig(uint32_t contrast_level, uint32_t brightness_level) 00290 { 00291 if(camera_drv->Config != NULL) 00292 { 00293 camera_drv->Config(CameraHwAddress, CAMERA_CONTRAST_BRIGHTNESS, contrast_level, brightness_level); 00294 } 00295 } 00296 00297 /** 00298 * @brief Configures the camera white balance. 00299 * @param Mode: black_white mode 00300 * This parameter can be one of the following values: 00301 * @arg CAMERA_BLACK_WHITE_BW 00302 * @arg CAMERA_BLACK_WHITE_NEGATIVE 00303 * @arg CAMERA_BLACK_WHITE_BW_NEGATIVE 00304 * @arg CAMERA_BLACK_WHITE_NORMAL 00305 * @retval None 00306 */ 00307 void BSP_CAMERA_BlackWhiteConfig(uint32_t Mode) 00308 { 00309 if(camera_drv->Config != NULL) 00310 { 00311 camera_drv->Config(CameraHwAddress, CAMERA_BLACK_WHITE, Mode, 0); 00312 } 00313 } 00314 00315 /** 00316 * @brief Configures the camera color effect. 00317 * @param Effect: Color effect 00318 * This parameter can be one of the following values: 00319 * @arg CAMERA_COLOR_EFFECT_ANTIQUE 00320 * @arg CAMERA_COLOR_EFFECT_BLUE 00321 * @arg CAMERA_COLOR_EFFECT_GREEN 00322 * @arg CAMERA_COLOR_EFFECT_RED 00323 * @retval None 00324 */ 00325 void BSP_CAMERA_ColorEffectConfig(uint32_t Effect) 00326 { 00327 if(camera_drv->Config != NULL) 00328 { 00329 camera_drv->Config(CameraHwAddress, CAMERA_COLOR_EFFECT, Effect, 0); 00330 } 00331 } 00332 00333 /** 00334 * @brief Get the capture size in pixels unit. 00335 * @param resolution: the current resolution. 00336 * @retval capture size in pixels unit. 00337 */ 00338 static uint32_t GetSize(uint32_t resolution) 00339 { 00340 uint32_t size = 0; 00341 00342 /* Get capture size */ 00343 switch (resolution) 00344 { 00345 case CAMERA_R160x120: 00346 { 00347 size = 0x2580; 00348 } 00349 break; 00350 case CAMERA_R320x240: 00351 { 00352 size = 0x9600; 00353 } 00354 break; 00355 case CAMERA_R480x272: 00356 { 00357 size = 0xFF00; 00358 } 00359 break; 00360 case CAMERA_R640x480: 00361 { 00362 size = 0x25800; 00363 } 00364 break; 00365 default: 00366 { 00367 break; 00368 } 00369 } 00370 00371 return size; 00372 } 00373 00374 /** 00375 * @brief Initializes the DCMI MSP. 00376 * @param hdcmi: HDMI handle 00377 * @param Params 00378 * @retval None 00379 */ 00380 __weak void BSP_CAMERA_MspInit(DCMI_HandleTypeDef *hdcmi, void *Params) 00381 { 00382 static DMA_HandleTypeDef hdma_handler; 00383 GPIO_InitTypeDef gpio_init_structure; 00384 00385 /*** Enable peripherals and GPIO clocks ***/ 00386 /* Enable DCMI clock */ 00387 __HAL_RCC_DCMI_CLK_ENABLE(); 00388 00389 /* Enable DMA2 clock */ 00390 __HAL_RCC_DMA2_CLK_ENABLE(); 00391 /* Enable DMAmux1 clock to be able to use DMA_REQUEST_DCMI */ 00392 __HAL_RCC_DMAMUX1_CLK_ENABLE(); 00393 00394 /* Enable GPIO clocks */ 00395 __HAL_RCC_GPIOC_CLK_ENABLE(); 00396 __HAL_RCC_GPIOH_CLK_ENABLE(); 00397 __HAL_RCC_GPIOE_CLK_ENABLE(); 00398 __HAL_RCC_GPIOI_CLK_ENABLE(); 00399 __HAL_RCC_GPIOA_CLK_ENABLE(); 00400 00401 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_4); /* PA8 Camera modul input clock 20 MHz */ 00402 __HAL_RCC_HSI48_ENABLE(); 00403 HAL_Delay(10); // HSI48 should start in 10ms 00404 00405 /*** Configure the GPIO ***/ 00406 /* Configure DCMI GPIO as alternate function */ 00407 gpio_init_structure.Pin = GPIO_PIN_6 | GPIO_PIN_7; 00408 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00409 gpio_init_structure.Pull = GPIO_NOPULL; 00410 gpio_init_structure.Speed = GPIO_SPEED_HIGH; 00411 gpio_init_structure.Alternate = GPIO_AF10_DCMI; 00412 HAL_GPIO_Init(GPIOC, &gpio_init_structure); 00413 00414 gpio_init_structure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_5; 00415 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00416 gpio_init_structure.Pull = GPIO_NOPULL; 00417 gpio_init_structure.Speed = GPIO_SPEED_HIGH; 00418 gpio_init_structure.Alternate = GPIO_AF10_DCMI; 00419 HAL_GPIO_Init(GPIOH, &gpio_init_structure); 00420 00421 gpio_init_structure.Pin = GPIO_PIN_4; 00422 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00423 gpio_init_structure.Pull = GPIO_NOPULL; 00424 gpio_init_structure.Speed = GPIO_SPEED_HIGH; 00425 gpio_init_structure.Alternate = GPIO_AF10_DCMI; 00426 HAL_GPIO_Init(GPIOE, &gpio_init_structure); 00427 00428 gpio_init_structure.Pin = GPIO_PIN_4 | GPIO_PIN_7 | GPIO_PIN_5; 00429 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00430 gpio_init_structure.Pull = GPIO_NOPULL; 00431 gpio_init_structure.Speed = GPIO_SPEED_HIGH; 00432 gpio_init_structure.Alternate = GPIO_AF10_DCMI; 00433 HAL_GPIO_Init(GPIOI, &gpio_init_structure); 00434 00435 gpio_init_structure.Pin = GPIO_PIN_4; 00436 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00437 gpio_init_structure.Pull = GPIO_NOPULL; 00438 gpio_init_structure.Speed = GPIO_SPEED_HIGH; 00439 gpio_init_structure.Alternate = GPIO_AF10_DCMI; 00440 HAL_GPIO_Init(GPIOA, &gpio_init_structure); 00441 00442 /*** Configure the DMA ***/ 00443 /* Set the parameters to be configured */ 00444 hdma_handler.Instance = BSP_CAMERA_DMA_INSTANCE; 00445 00446 hdma_handler.Init.Request = DMA_REQUEST_DCMI; 00447 hdma_handler.Init.Direction = DMA_PERIPH_TO_MEMORY; 00448 hdma_handler.Init.PeriphInc = DMA_PINC_DISABLE; 00449 hdma_handler.Init.MemInc = DMA_MINC_ENABLE; /* Image captured by the DCMI is stored in memory */ 00450 hdma_handler.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; 00451 hdma_handler.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; 00452 hdma_handler.Init.Mode = DMA_CIRCULAR; 00453 hdma_handler.Init.Priority = DMA_PRIORITY_HIGH; 00454 00455 /* Associate the initialized DMA handle to the DCMI handle */ 00456 __HAL_LINKDMA(hdcmi, DMA_Handle, hdma_handler); 00457 00458 /*** Configure the NVIC for DCMI and DMA ***/ 00459 /* NVIC configuration for DCMI transfer complete interrupt */ 00460 HAL_NVIC_SetPriority(DCMI_IRQn, 0x0F, 0); 00461 HAL_NVIC_EnableIRQ(DCMI_IRQn); 00462 00463 /* NVIC configuration for DMA2D transfer complete interrupt */ 00464 HAL_NVIC_SetPriority(DMA2_Channel6_IRQn, 0x0F, 0); 00465 HAL_NVIC_EnableIRQ(DMA2_Channel6_IRQn); 00466 00467 /* Configure the DMA stream */ 00468 HAL_DMA_Init(hdcmi->DMA_Handle); 00469 } 00470 00471 00472 /** 00473 * @brief DeInitializes the DCMI MSP. 00474 * @param hdcmi: HDMI handle 00475 * @param Params 00476 * @retval None 00477 */ 00478 __weak void BSP_CAMERA_MspDeInit(DCMI_HandleTypeDef *hdcmi, void *Params) 00479 { 00480 /* Disable NVIC for DCMI transfer complete interrupt */ 00481 HAL_NVIC_DisableIRQ(DCMI_IRQn); 00482 00483 /* Disable NVIC for DMA2 transfer complete interrupt */ 00484 HAL_NVIC_DisableIRQ(DMA2_Channel6_IRQn); 00485 00486 /* Configure the DMA stream */ 00487 HAL_DMA_DeInit(hdcmi->DMA_Handle); 00488 00489 /* Disable DCMI clock */ 00490 __HAL_RCC_DCMI_CLK_DISABLE(); 00491 00492 /* GPIO pins clock and DMA clock can be shut down in the application 00493 by surcharging this __weak function */ 00494 } 00495 00496 /** 00497 * @brief Line event callback 00498 * @param hdcmi: pointer to the DCMI handle 00499 * @retval None 00500 */ 00501 void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi) 00502 { 00503 BSP_CAMERA_LineEventCallback(); 00504 } 00505 00506 /** 00507 * @brief Line Event callback. 00508 * @retval None 00509 */ 00510 __weak void BSP_CAMERA_LineEventCallback(void) 00511 { 00512 /* NOTE : This function should not be modified; when the callback is needed, 00513 the BSP_CAMERA_LineEventCallback can be implemented in the user file 00514 */ 00515 } 00516 00517 /** 00518 * @brief VSYNC event callback 00519 * @param hdcmi: pointer to the DCMI handle 00520 * @retval None 00521 */ 00522 void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi) 00523 { 00524 BSP_CAMERA_VsyncEventCallback(); 00525 } 00526 00527 /** 00528 * @brief VSYNC Event callback. 00529 * @retval None 00530 */ 00531 __weak void BSP_CAMERA_VsyncEventCallback(void) 00532 { 00533 /* NOTE : This function should not be modified; when the callback is needed, 00534 the BSP_CAMERA_VsyncEventCallback can be implemented in the user file 00535 */ 00536 } 00537 00538 /** 00539 * @brief Frame event callback 00540 * @param hdcmi: pointer to the DCMI handle 00541 * @retval None 00542 */ 00543 void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi) 00544 { 00545 BSP_CAMERA_FrameEventCallback(); 00546 } 00547 00548 /** 00549 * @brief Frame Event callback. 00550 * @retval None 00551 */ 00552 __weak void BSP_CAMERA_FrameEventCallback(void) 00553 { 00554 /* NOTE : This function should not be modified; when the callback is needed, 00555 the BSP_CAMERA_FrameEventCallback can be implemented in the user file 00556 */ 00557 } 00558 00559 /** 00560 * @brief Error callback 00561 * @param hdcmi: pointer to the DCMI handle 00562 * @retval None 00563 */ 00564 void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi) 00565 { 00566 BSP_CAMERA_ErrorCallback(); 00567 } 00568 00569 /** 00570 * @brief Error callback. 00571 * @retval None 00572 */ 00573 __weak void BSP_CAMERA_ErrorCallback(void) 00574 { 00575 /* NOTE : This function should not be modified; when the callback is needed, 00576 the BSP_CAMERA_ErrorCallback can be implemented in the user file 00577 */ 00578 } 00579 00580 00581 /** 00582 * @} 00583 */ 00584 00585 /** 00586 * @} 00587 */ 00588 00589 /** 00590 * @} 00591 */ 00592 00593 /** 00594 * @} 00595 */ 00596 00597 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 13 2022 19:15:17 by
