Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_dcmi.c
blob1f8dcdc019239af3a884fb185255378830dfc312
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_dcmi.c
4 * @author MCD Application Team
5 * @brief DCMI HAL module driver
6 * This file provides firmware functions to manage the following
7 * functionalities of the Digital Camera Interface (DCMI) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State and Error functions
13 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 The sequence below describes how to use this driver to capture image
19 from a camera module connected to the DCMI Interface.
20 This sequence does not take into account the configuration of the
21 camera module, which should be made before to configure and enable
22 the DCMI to capture images.
24 (#) Program the required configuration through following parameters:
25 horizontal and vertical polarity, pixel clock polarity, Capture Rate,
26 Synchronization Mode, code of the frame delimiter and data width
27 using HAL_DCMI_Init() function.
29 (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
30 register to the destination memory buffer.
32 (#) Program the required configuration through following parameters:
33 DCMI mode, destination memory Buffer address and the data length
34 and enable capture using HAL_DCMI_Start_DMA() function.
36 (#) Optionally, configure and Enable the CROP feature to select a rectangular
37 window from the received image using HAL_DCMI_ConfigCrop()
38 and HAL_DCMI_EnableCROP() functions
40 (#) The capture can be stopped using HAL_DCMI_Stop() function.
42 (#) To control DCMI state you can use the function HAL_DCMI_GetState().
44 *** DCMI HAL driver macros list ***
45 =============================================
46 [..]
47 Below the list of most used macros in DCMI HAL driver.
49 (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
50 (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
51 (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
52 (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
53 (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
54 (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
55 (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred or not.
57 [..]
58 (@) You can refer to the DCMI HAL driver header file for more useful macros
60 *** Callback registration ***
61 =============================
63 The compilation define USE_HAL_DCMI_REGISTER_CALLBACKS when set to 1
64 allows the user to configure dynamically the driver callbacks.
65 Use functions @ref HAL_DCMI_RegisterCallback() to register a user callback.
67 Function @ref HAL_DCMI_RegisterCallback() allows to register following callbacks:
68 (+) FrameEventCallback : DCMI Frame Event.
69 (+) VsyncEventCallback : DCMI Vsync Event.
70 (+) LineEventCallback : DCMI Line Event.
71 (+) ErrorCallback : DCMI error.
72 (+) MspInitCallback : DCMI MspInit.
73 (+) MspDeInitCallback : DCMI MspDeInit.
74 This function takes as parameters the HAL peripheral handle, the callback ID
75 and a pointer to the user callback function.
77 Use function @ref HAL_DCMI_UnRegisterCallback() to reset a callback to the default
78 weak (surcharged) function.
79 @ref HAL_DCMI_UnRegisterCallback() takes as parameters the HAL peripheral handle,
80 and the callback ID.
81 This function allows to reset following callbacks:
82 (+) FrameEventCallback : DCMI Frame Event.
83 (+) VsyncEventCallback : DCMI Vsync Event.
84 (+) LineEventCallback : DCMI Line Event.
85 (+) ErrorCallback : DCMI error.
86 (+) MspInitCallback : DCMI MspInit.
87 (+) MspDeInitCallback : DCMI MspDeInit.
89 By default, after the @ref HAL_DCMI_Init and if the state is HAL_DCMI_STATE_RESET
90 all callbacks are reset to the corresponding legacy weak (surcharged) functions:
91 examples @ref FrameEventCallback(), @ref HAL_DCMI_ErrorCallback().
92 Exception done for MspInit and MspDeInit callbacks that are respectively
93 reset to the legacy weak (surcharged) functions in the @ref HAL_DCMI_Init
94 and @ref HAL_DCMI_DeInit only when these callbacks are null (not registered beforehand).
95 If not, MspInit or MspDeInit are not null, the @ref HAL_DCMI_Init and @ref HAL_DCMI_DeInit
96 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
98 Callbacks can be registered/unregistered in READY state only.
99 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
100 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
101 during the Init/DeInit.
102 In that case first register the MspInit/MspDeInit user callbacks
103 using @ref HAL_DCMI_RegisterCallback before calling @ref HAL_DCMI_DeInit
104 or @ref HAL_DCMI_Init function.
106 When the compilation define USE_HAL_DCMI_REGISTER_CALLBACKS is set to 0 or
107 not defined, the callback registering feature is not available
108 and weak (surcharged) callbacks are used.
110 @endverbatim
111 ******************************************************************************
112 * @attention
114 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
115 * All rights reserved.</center></h2>
117 * This software component is licensed by ST under BSD 3-Clause license,
118 * the "License"; You may not use this file except in compliance with the
119 * License. You may obtain a copy of the License at:
120 * opensource.org/licenses/BSD-3-Clause
122 ******************************************************************************
125 /* Includes ------------------------------------------------------------------*/
126 #include "stm32f7xx_hal.h"
128 /** @addtogroup STM32F7xx_HAL_Driver
129 * @{
131 /** @defgroup DCMI DCMI
132 * @brief DCMI HAL module driver
133 * @{
136 #ifdef HAL_DCMI_MODULE_ENABLED
137 #if defined (DCMI)
139 /* Private typedef -----------------------------------------------------------*/
140 /* Private define ------------------------------------------------------------*/
141 #define HAL_TIMEOUT_DCMI_STOP ((uint32_t)1000) /* Set timeout to 1s */
143 /* Private macro -------------------------------------------------------------*/
144 /* Private variables ---------------------------------------------------------*/
145 /* Private function prototypes -----------------------------------------------*/
146 static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma);
147 static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
149 /* Exported functions --------------------------------------------------------*/
151 /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
152 * @{
155 /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
156 * @brief Initialization and Configuration functions
158 @verbatim
159 ===============================================================================
160 ##### Initialization and Configuration functions #####
161 ===============================================================================
162 [..] This section provides functions allowing to:
163 (+) Initialize and configure the DCMI
164 (+) De-initialize the DCMI
166 @endverbatim
167 * @{
171 * @brief Initializes the DCMI according to the specified
172 * parameters in the DCMI_InitTypeDef and create the associated handle.
173 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
174 * the configuration information for DCMI.
175 * @retval HAL status
177 HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
179 /* Check the DCMI peripheral state */
180 if(hdcmi == NULL)
182 return HAL_ERROR;
185 /* Check function parameters */
186 assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
187 assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
188 assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
189 assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
190 assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
191 assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
192 assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
193 assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
195 assert_param(IS_DCMI_BYTE_SELECT_MODE(hdcmi->Init.ByteSelectMode));
196 assert_param(IS_DCMI_BYTE_SELECT_START(hdcmi->Init.ByteSelectStart));
197 assert_param(IS_DCMI_LINE_SELECT_MODE(hdcmi->Init.LineSelectMode));
198 assert_param(IS_DCMI_LINE_SELECT_START(hdcmi->Init.LineSelectStart));
200 if(hdcmi->State == HAL_DCMI_STATE_RESET)
202 /* Init the DCMI Callback settings */
203 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
204 hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
205 hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
206 hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
207 hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
209 if(hdcmi->MspInitCallback == NULL)
211 /* Legacy weak MspInit Callback */
212 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
214 /* Initialize the low level hardware (MSP) */
215 hdcmi->MspInitCallback(hdcmi);
216 #else
217 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
218 HAL_DCMI_MspInit(hdcmi);
219 #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
222 /* Change the DCMI state */
223 hdcmi->State = HAL_DCMI_STATE_BUSY;
224 /* Configures the HS, VS, DE and PC polarity */
225 hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |\
226 DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |\
227 DCMI_CR_ESS | DCMI_CR_BSM_0 | DCMI_CR_BSM_1 | DCMI_CR_OEBS |\
228 DCMI_CR_LSM | DCMI_CR_OELS);
230 hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate |\
231 hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity |\
232 hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode |\
233 hdcmi->Init.JPEGMode | hdcmi->Init.ByteSelectMode |\
234 hdcmi->Init.ByteSelectStart | hdcmi->Init.LineSelectMode |\
235 hdcmi->Init.LineSelectStart);
237 if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
239 hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode) |\
240 ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << DCMI_ESCR_LSC_Pos)|\
241 ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << DCMI_ESCR_LEC_Pos) |\
242 ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << DCMI_ESCR_FEC_Pos));
246 /* Enable the Line, Vsync, Error and Overrun interrupts */
247 __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
249 /* Update error code */
250 hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
252 /* Initialize the DCMI state*/
253 hdcmi->State = HAL_DCMI_STATE_READY;
255 return HAL_OK;
259 * @brief Deinitializes the DCMI peripheral registers to their default reset
260 * values.
261 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
262 * the configuration information for DCMI.
263 * @retval HAL status
266 HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
268 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
269 if(hdcmi->MspDeInitCallback == NULL)
271 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
273 /* De-Initialize the low level hardware (MSP) */
274 hdcmi->MspDeInitCallback(hdcmi);
275 #else
276 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
277 HAL_DCMI_MspDeInit(hdcmi);
278 #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
280 /* Update error code */
281 hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
283 /* Initialize the DCMI state*/
284 hdcmi->State = HAL_DCMI_STATE_RESET;
286 /* Release Lock */
287 __HAL_UNLOCK(hdcmi);
289 return HAL_OK;
293 * @brief Initializes the DCMI MSP.
294 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
295 * the configuration information for DCMI.
296 * @retval None
298 __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
300 /* Prevent unused argument(s) compilation warning */
301 UNUSED(hdcmi);
303 /* NOTE : This function Should not be modified, when the callback is needed,
304 the HAL_DCMI_MspInit could be implemented in the user file
309 * @brief DeInitializes the DCMI MSP.
310 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
311 * the configuration information for DCMI.
312 * @retval None
314 __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
316 /* Prevent unused argument(s) compilation warning */
317 UNUSED(hdcmi);
319 /* NOTE : This function Should not be modified, when the callback is needed,
320 the HAL_DCMI_MspDeInit could be implemented in the user file
325 * @}
327 /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
328 * @brief IO operation functions
330 @verbatim
331 ===============================================================================
332 ##### IO operation functions #####
333 ===============================================================================
334 [..] This section provides functions allowing to:
335 (+) Configure destination address and data length and
336 Enables DCMI DMA request and enables DCMI capture
337 (+) Stop the DCMI capture.
338 (+) Handles DCMI interrupt request.
340 @endverbatim
341 * @{
345 * @brief Enables DCMI DMA request and enables DCMI capture
346 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
347 * the configuration information for DCMI.
348 * @param DCMI_Mode DCMI capture mode snapshot or continuous grab.
349 * @param pData The destination memory Buffer address (LCD Frame buffer).
350 * @param Length The length of capture to be transferred.
351 * @retval HAL status
353 HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
355 /* Initialize the second memory address */
356 uint32_t SecondMemAddress = 0;
358 /* Check function parameters */
359 assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
361 /* Process Locked */
362 __HAL_LOCK(hdcmi);
364 /* Lock the DCMI peripheral state */
365 hdcmi->State = HAL_DCMI_STATE_BUSY;
367 /* Enable DCMI by setting DCMIEN bit */
368 __HAL_DCMI_ENABLE(hdcmi);
370 /* Configure the DCMI Mode */
371 hdcmi->Instance->CR &= ~(DCMI_CR_CM);
372 hdcmi->Instance->CR |= (uint32_t)(DCMI_Mode);
374 /* Set the DMA memory0 conversion complete callback */
375 hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
377 /* Set the DMA error callback */
378 hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
380 /* Set the dma abort callback */
381 hdcmi->DMA_Handle->XferAbortCallback = NULL;
383 /* Reset transfer counters value */
384 hdcmi->XferCount = 0;
385 hdcmi->XferTransferNumber = 0;
387 if(Length <= 0xFFFF)
389 /* Enable the DMA Stream */
390 HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
392 else /* DCMI_DOUBLE_BUFFER Mode */
394 /* Set the DMA memory1 conversion complete callback */
395 hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAXferCplt;
397 /* Initialize transfer parameters */
398 hdcmi->XferCount = 1;
399 hdcmi->XferSize = Length;
400 hdcmi->pBuffPtr = pData;
402 /* Get the number of buffer */
403 while(hdcmi->XferSize > 0xFFFF)
405 hdcmi->XferSize = (hdcmi->XferSize/2);
406 hdcmi->XferCount = hdcmi->XferCount*2;
409 /* Update DCMI counter and transfer number*/
410 hdcmi->XferCount = (hdcmi->XferCount - 2);
411 hdcmi->XferTransferNumber = hdcmi->XferCount;
413 /* Update second memory address */
414 SecondMemAddress = (uint32_t)(pData + (4*hdcmi->XferSize));
416 /* Start DMA multi buffer transfer */
417 HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
420 /* Enable Capture */
421 hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
423 /* Release Lock */
424 __HAL_UNLOCK(hdcmi);
426 /* Return function status */
427 return HAL_OK;
431 * @brief Disable DCMI DMA request and Disable DCMI capture
432 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
433 * the configuration information for DCMI.
434 * @retval HAL status
436 HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
438 register uint32_t count = HAL_TIMEOUT_DCMI_STOP * (SystemCoreClock /8/1000);
439 HAL_StatusTypeDef status = HAL_OK;
441 /* Process locked */
442 __HAL_LOCK(hdcmi);
444 /* Lock the DCMI peripheral state */
445 hdcmi->State = HAL_DCMI_STATE_BUSY;
447 /* Disable Capture */
448 hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
450 /* Check if the DCMI capture effectively disabled */
453 if (count-- == 0)
455 /* Update error code */
456 hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
458 status = HAL_TIMEOUT;
459 break;
462 while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
464 /* Disable the DCMI */
465 __HAL_DCMI_DISABLE(hdcmi);
467 /* Disable the DMA */
468 HAL_DMA_Abort(hdcmi->DMA_Handle);
470 /* Update error code */
471 hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
473 /* Change DCMI state */
474 hdcmi->State = HAL_DCMI_STATE_READY;
476 /* Process Unlocked */
477 __HAL_UNLOCK(hdcmi);
479 /* Return function status */
480 return status;
484 * @brief Suspend DCMI capture
485 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
486 * the configuration information for DCMI.
487 * @retval HAL status
489 HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
491 register uint32_t count = HAL_TIMEOUT_DCMI_STOP * (SystemCoreClock /8/1000);
492 HAL_StatusTypeDef status = HAL_OK;
494 /* Process locked */
495 __HAL_LOCK(hdcmi);
497 if(hdcmi->State == HAL_DCMI_STATE_BUSY)
499 /* Change DCMI state */
500 hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
502 /* Disable Capture */
503 hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
505 /* Check if the DCMI capture effectively disabled */
508 if (count-- == 0)
510 /* Update error code */
511 hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
513 /* Change DCMI state */
514 hdcmi->State = HAL_DCMI_STATE_READY;
516 status = HAL_TIMEOUT;
517 break;
520 while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
522 /* Process Unlocked */
523 __HAL_UNLOCK(hdcmi);
525 /* Return function status */
526 return status;
530 * @brief Resume DCMI capture
531 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
532 * the configuration information for DCMI.
533 * @retval HAL status
535 HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
537 /* Process locked */
538 __HAL_LOCK(hdcmi);
540 if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
542 /* Change DCMI state */
543 hdcmi->State = HAL_DCMI_STATE_BUSY;
545 /* Disable Capture */
546 hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
548 /* Process Unlocked */
549 __HAL_UNLOCK(hdcmi);
551 /* Return function status */
552 return HAL_OK;
556 * @brief Handles DCMI interrupt request.
557 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
558 * the configuration information for the DCMI.
559 * @retval None
561 void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
563 uint32_t isr_value = READ_REG(hdcmi->Instance->MISR);
565 /* Synchronization error interrupt management *******************************/
566 if((isr_value & DCMI_FLAG_ERRRI) == DCMI_FLAG_ERRRI)
568 /* Clear the Synchronization error flag */
569 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
571 /* Update error code */
572 hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
574 /* Change DCMI state */
575 hdcmi->State = HAL_DCMI_STATE_ERROR;
577 /* Set the synchronization error callback */
578 hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
580 /* Abort the DMA Transfer */
581 HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
583 /* Overflow interrupt management ********************************************/
584 if((isr_value & DCMI_FLAG_OVRRI) == DCMI_FLAG_OVRRI)
586 /* Clear the Overflow flag */
587 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
589 /* Update error code */
590 hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
592 /* Change DCMI state */
593 hdcmi->State = HAL_DCMI_STATE_ERROR;
595 /* Set the overflow callback */
596 hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
598 /* Abort the DMA Transfer */
599 HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
601 /* Line Interrupt management ************************************************/
602 if((isr_value & DCMI_FLAG_LINERI) == DCMI_FLAG_LINERI)
604 /* Clear the Line interrupt flag */
605 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
607 /* Line interrupt Callback */
608 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
609 /*Call registered DCMI line event callback*/
610 hdcmi->LineEventCallback(hdcmi);
611 #else
612 HAL_DCMI_LineEventCallback(hdcmi);
613 #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
615 /* VSYNC interrupt management ***********************************************/
616 if((isr_value & DCMI_FLAG_VSYNCRI) == DCMI_FLAG_VSYNCRI)
618 /* Clear the VSYNC flag */
619 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
621 /* VSYNC Callback */
622 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
623 /*Call registered DCMI vsync event callback*/
624 hdcmi->VsyncEventCallback(hdcmi);
625 #else
626 HAL_DCMI_VsyncEventCallback(hdcmi);
627 #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
629 /* FRAME interrupt management ***********************************************/
630 if((isr_value & DCMI_FLAG_FRAMERI) == DCMI_FLAG_FRAMERI)
632 /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
633 if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
635 /* Disable the Line, Vsync, Error and Overrun interrupts */
636 __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
639 /* Disable the Frame interrupt */
640 __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
642 /* Clear the End of Frame flag */
643 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
645 /* Frame Callback */
646 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
647 /*Call registered DCMI frame event callback*/
648 hdcmi->FrameEventCallback(hdcmi);
649 #else
650 HAL_DCMI_FrameEventCallback(hdcmi);
651 #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
656 * @brief Error DCMI callback.
657 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
658 * the configuration information for DCMI.
659 * @retval None
661 __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
663 /* Prevent unused argument(s) compilation warning */
664 UNUSED(hdcmi);
666 /* NOTE : This function Should not be modified, when the callback is needed,
667 the HAL_DCMI_ErrorCallback could be implemented in the user file
672 * @brief Line Event callback.
673 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
674 * the configuration information for DCMI.
675 * @retval None
677 __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
679 /* NOTE : This function Should not be modified, when the callback is needed,
680 the HAL_DCMI_LineEventCallback could be implemented in the user file
685 * @brief VSYNC Event callback.
686 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
687 * the configuration information for DCMI.
688 * @retval None
690 __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
692 /* Prevent unused argument(s) compilation warning */
693 UNUSED(hdcmi);
695 /* NOTE : This function Should not be modified, when the callback is needed,
696 the HAL_DCMI_VsyncEventCallback could be implemented in the user file
701 * @brief Frame Event callback.
702 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
703 * the configuration information for DCMI.
704 * @retval None
706 __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
708 /* Prevent unused argument(s) compilation warning */
709 UNUSED(hdcmi);
711 /* NOTE : This function Should not be modified, when the callback is needed,
712 the HAL_DCMI_FrameEventCallback could be implemented in the user file
717 * @}
720 /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
721 * @brief Peripheral Control functions
723 @verbatim
724 ===============================================================================
725 ##### Peripheral Control functions #####
726 ===============================================================================
727 [..] This section provides functions allowing to:
728 (+) Configure the CROP feature.
729 (+) Enable/Disable the CROP feature.
730 (+) Set embedded synchronization delimiters unmasks.
732 @endverbatim
733 * @{
737 * @brief Configure the DCMI CROP coordinate.
738 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
739 * the configuration information for DCMI.
740 * @param YSize DCMI Line number
741 * @param XSize DCMI Pixel per line
742 * @param X0 DCMI window X offset
743 * @param Y0 DCMI window Y offset
744 * @retval HAL status
746 HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
748 /* Process Locked */
749 __HAL_LOCK(hdcmi);
751 /* Lock the DCMI peripheral state */
752 hdcmi->State = HAL_DCMI_STATE_BUSY;
754 /* Check the parameters */
755 assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
756 assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
757 assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
758 assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
760 /* Configure CROP */
761 hdcmi->Instance->CWSIZER = (XSize | (YSize << DCMI_CWSIZE_VLINE_Pos));
762 hdcmi->Instance->CWSTRTR = (X0 | (Y0 << DCMI_CWSTRT_VST_Pos));
764 /* Initialize the DCMI state*/
765 hdcmi->State = HAL_DCMI_STATE_READY;
767 /* Process Unlocked */
768 __HAL_UNLOCK(hdcmi);
770 return HAL_OK;
774 * @brief Disable the Crop feature.
775 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
776 * the configuration information for DCMI.
777 * @retval HAL status
779 HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
781 /* Process Locked */
782 __HAL_LOCK(hdcmi);
784 /* Lock the DCMI peripheral state */
785 hdcmi->State = HAL_DCMI_STATE_BUSY;
787 /* Disable DCMI Crop feature */
788 hdcmi->Instance->CR &= ~(uint32_t)DCMI_CR_CROP;
790 /* Change the DCMI state*/
791 hdcmi->State = HAL_DCMI_STATE_READY;
793 /* Process Unlocked */
794 __HAL_UNLOCK(hdcmi);
796 return HAL_OK;
800 * @brief Enable the Crop feature.
801 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
802 * the configuration information for DCMI.
803 * @retval HAL status
805 HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
807 /* Process Locked */
808 __HAL_LOCK(hdcmi);
810 /* Lock the DCMI peripheral state */
811 hdcmi->State = HAL_DCMI_STATE_BUSY;
813 /* Enable DCMI Crop feature */
814 hdcmi->Instance->CR |= (uint32_t)DCMI_CR_CROP;
816 /* Change the DCMI state*/
817 hdcmi->State = HAL_DCMI_STATE_READY;
819 /* Process Unlocked */
820 __HAL_UNLOCK(hdcmi);
822 return HAL_OK;
826 * @}
829 /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
830 * @brief Peripheral State functions
832 @verbatim
833 ===============================================================================
834 ##### Peripheral State and Errors functions #####
835 ===============================================================================
836 [..]
837 This subsection provides functions allowing to
838 (+) Check the DCMI state.
839 (+) Get the specific DCMI error flag.
841 @endverbatim
842 * @{
846 * @brief Return the DCMI state
847 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
848 * the configuration information for DCMI.
849 * @retval HAL state
851 HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
853 return hdcmi->State;
857 * @brief Return the DCMI error code
858 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
859 * the configuration information for DCMI.
860 * @retval DCMI Error Code
862 uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
864 return hdcmi->ErrorCode;
867 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
869 * @brief DCMI Callback registering
870 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
871 * the configuration information for DCMI.
872 * @param CallbackID dcmi Callback ID
873 * @param pCallback pointer to DCMI_CallbackTypeDef structure
874 * @retval status
876 HAL_StatusTypeDef HAL_DCMI_RegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID, pDCMI_CallbackTypeDef pCallback)
878 HAL_StatusTypeDef status = HAL_OK;
880 if(pCallback == NULL)
882 /* update the error code */
883 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
884 /* update return status */
885 status = HAL_ERROR;
887 else
889 if(hdcmi->State == HAL_DCMI_STATE_READY)
891 switch (CallbackID)
893 case HAL_DCMI_FRAME_EVENT_CB_ID :
894 hdcmi->FrameEventCallback = pCallback;
895 break;
897 case HAL_DCMI_VSYNC_EVENT_CB_ID :
898 hdcmi->VsyncEventCallback = pCallback;
899 break;
901 case HAL_DCMI_LINE_EVENT_CB_ID :
902 hdcmi->LineEventCallback = pCallback;
903 break;
905 case HAL_DCMI_ERROR_CB_ID :
906 hdcmi->ErrorCallback = pCallback;
907 break;
909 case HAL_DCMI_MSPINIT_CB_ID :
910 hdcmi->MspInitCallback = pCallback;
911 break;
913 case HAL_DCMI_MSPDEINIT_CB_ID :
914 hdcmi->MspDeInitCallback = pCallback;
915 break;
917 default :
918 /* Return error status */
919 status = HAL_ERROR;
920 break;
923 else if(hdcmi->State == HAL_DCMI_STATE_RESET)
925 switch (CallbackID)
927 case HAL_DCMI_MSPINIT_CB_ID :
928 hdcmi->MspInitCallback = pCallback;
929 break;
931 case HAL_DCMI_MSPDEINIT_CB_ID :
932 hdcmi->MspDeInitCallback = pCallback;
933 break;
935 default :
936 /* update the error code */
937 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
938 /* update return status */
939 status = HAL_ERROR;
940 break;
943 else
945 /* update the error code */
946 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
947 /* update return status */
948 status = HAL_ERROR;
952 return status;
956 * @brief DCMI Callback Unregistering
957 * @param hdcmi dcmi handle
958 * @param CallbackID dcmi Callback ID
959 * @retval status
961 HAL_StatusTypeDef HAL_DCMI_UnRegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID)
963 HAL_StatusTypeDef status = HAL_OK;
965 if(hdcmi->State == HAL_DCMI_STATE_READY)
967 switch (CallbackID)
969 case HAL_DCMI_FRAME_EVENT_CB_ID :
970 hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
971 break;
973 case HAL_DCMI_VSYNC_EVENT_CB_ID :
974 hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
975 break;
977 case HAL_DCMI_LINE_EVENT_CB_ID :
978 hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
979 break;
981 case HAL_DCMI_ERROR_CB_ID :
982 hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
983 break;
985 case HAL_DCMI_MSPINIT_CB_ID :
986 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
987 break;
989 case HAL_DCMI_MSPDEINIT_CB_ID :
990 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
991 break;
993 default :
994 /* update the error code */
995 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
996 /* update return status */
997 status = HAL_ERROR;
998 break;
1001 else if(hdcmi->State == HAL_DCMI_STATE_RESET)
1003 switch (CallbackID)
1005 case HAL_DCMI_MSPINIT_CB_ID :
1006 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
1007 break;
1009 case HAL_DCMI_MSPDEINIT_CB_ID :
1010 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
1011 break;
1013 default :
1014 /* update the error code */
1015 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
1016 /* update return status */
1017 status = HAL_ERROR;
1018 break;
1021 else
1023 /* update the error code */
1024 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
1025 /* update return status */
1026 status = HAL_ERROR;
1029 return status;
1031 #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
1034 * @}
1036 /* Private functions ---------------------------------------------------------*/
1037 /** @defgroup DCMI_Private_Functions DCMI Private Functions
1038 * @{
1041 * @brief DMA conversion complete callback.
1042 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1043 * the configuration information for the specified DMA module.
1044 * @retval None
1046 static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
1048 uint32_t tmp = 0;
1050 DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1052 if(hdcmi->XferCount != 0)
1054 /* Update memory 0 address location */
1055 tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
1056 if(((hdcmi->XferCount % 2) == 0) && (tmp != 0))
1058 tmp = hdcmi->DMA_Handle->Instance->M0AR;
1059 HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY0);
1060 hdcmi->XferCount--;
1062 /* Update memory 1 address location */
1063 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0)
1065 tmp = hdcmi->DMA_Handle->Instance->M1AR;
1066 HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY1);
1067 hdcmi->XferCount--;
1070 /* Update memory 0 address location */
1071 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0)
1073 hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
1075 /* Update memory 1 address location */
1076 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0)
1078 tmp = hdcmi->pBuffPtr;
1079 hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4*hdcmi->XferSize));
1080 hdcmi->XferCount = hdcmi->XferTransferNumber;
1083 /* Check if the frame is transferred */
1084 if(hdcmi->XferCount == hdcmi->XferTransferNumber)
1086 /* Enable the Frame interrupt */
1087 __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
1089 /* When snapshot mode, set dcmi state to ready */
1090 if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
1092 hdcmi->State= HAL_DCMI_STATE_READY;
1098 * @brief DMA error callback
1099 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1100 * the configuration information for the specified DMA module.
1101 * @retval None
1103 static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
1105 DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1107 if(hdcmi->DMA_Handle->ErrorCode != HAL_DMA_ERROR_FE)
1109 /* Initialize the DCMI state*/
1110 hdcmi->State = HAL_DCMI_STATE_READY;
1112 /* Set DCMI Error Code */
1113 hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
1116 /* DCMI error Callback */
1117 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
1118 /*Call registered DCMI error callback*/
1119 hdcmi->ErrorCallback(hdcmi);
1120 #else
1121 HAL_DCMI_ErrorCallback(hdcmi);
1122 #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
1127 * @}
1131 * @}
1133 #endif /* DCMI */
1134 #endif /* HAL_DCMI_MODULE_ENABLED */
1136 * @}
1140 * @}
1143 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/