1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_hal_dcmi.c
4   * @author  MCD Application Team
5   * @version V1.0.1
6   * @date    25-June-2015
7   * @brief   DCMI HAL module driver
8   *          This file provides firmware functions to manage the following
9   *          functionalities of the Digital Camera Interface (DCMI) peripheral:
10   *           + Initialization and de-initialization functions
11   *           + IO operation functions
12   *           + Peripheral Control functions
13   *           + Peripheral State and Error functions
14   *
15   @verbatim
16   ==============================================================================
17                         ##### How to use this driver #####
18   ==============================================================================
19   [..]
20       The sequence below describes how to use this driver to capture image
21       from a camera module connected to the DCMI Interface.
22       This sequence does not take into account the configuration of the
23       camera module, which should be made before to configure and enable
24       the DCMI to capture images.
25 
26     (#) Program the required configuration through following parameters:
27         horizontal and vertical polarity, pixel clock polarity, Capture Rate,
28         Synchronization Mode, code of the frame delimiter and data width
29         using HAL_DCMI_Init() function.
30 
31     (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
32         register to the destination memory buffer.
33 
34     (#) Program the required configuration through following parameters:
35         DCMI mode, destination memory Buffer address and the data length
36         and enable capture using HAL_DCMI_Start_DMA() function.
37 
38     (#) Optionally, configure and Enable the CROP feature to select a rectangular
39         window from the received image using HAL_DCMI_ConfigCrop()
40         and HAL_DCMI_EnableCROP() functions
41 
42     (#) The capture can be stopped using HAL_DCMI_Stop() function.
43 
44     (#) To control DCMI state you can use the function HAL_DCMI_GetState().
45 
46      *** DCMI HAL driver macros list ***
47      =============================================
48      [..]
49        Below the list of most used macros in DCMI HAL driver.
50 
51       (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
52       (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
53       (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
54       (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
55       (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
56       (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
57       (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred or not.
58 
59      [..]
60        (@) You can refer to the DCMI HAL driver header file for more useful macros
61 
62   @endverbatim
63   ******************************************************************************
64   * @attention
65   *
66   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
67   *
68   * Redistribution and use in source and binary forms, with or without modification,
69   * are permitted provided that the following conditions are met:
70   *   1. Redistributions of source code must retain the above copyright notice,
71   *      this list of conditions and the following disclaimer.
72   *   2. Redistributions in binary form must reproduce the above copyright notice,
73   *      this list of conditions and the following disclaimer in the documentation
74   *      and/or other materials provided with the distribution.
75   *   3. Neither the name of STMicroelectronics nor the names of its contributors
76   *      may be used to endorse or promote products derived from this software
77   *      without specific prior written permission.
78   *
79   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
80   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
82   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
83   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
85   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
86   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
87   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
88   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
89   *
90   ******************************************************************************
91   */
92 
93 /* Includes ------------------------------------------------------------------*/
94 #include "stm32f7xx_hal.h"
95 
96 /** @addtogroup STM32F7xx_HAL_Driver
97   * @{
98   */
99 /** @defgroup DCMI DCMI
100   * @brief DCMI HAL module driver
101   * @{
102   */
103 
104 #ifdef HAL_DCMI_MODULE_ENABLED
105 
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
108 #define HAL_TIMEOUT_DCMI_STOP    ((uint32_t)1000)  /* 1s  */
109 /* Private macro -------------------------------------------------------------*/
110 /* Private variables ---------------------------------------------------------*/
111 /* Private function prototypes -----------------------------------------------*/
112 static void       DCMI_DMAConvCplt(DMA_HandleTypeDef *hdma);
113 static void       DCMI_DMAError(DMA_HandleTypeDef *hdma);
114 
115 /* Exported functions --------------------------------------------------------*/
116 
117 /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
118   * @{
119   */
120 
121 /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
122  *  @brief   Initialization and Configuration functions
123  *
124 @verbatim
125  ===============================================================================
126                 ##### Initialization and Configuration functions #####
127  ===============================================================================
128     [..]  This section provides functions allowing to:
129       (+) Initialize and configure the DCMI
130       (+) De-initialize the DCMI
131 
132 @endverbatim
133   * @{
134   */
135 
136 /**
137   * @brief  Initializes the DCMI according to the specified
138   *         parameters in the DCMI_InitTypeDef and create the associated handle.
139   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
140   *                the configuration information for DCMI.
141   * @retval HAL status
142   */
HAL_DCMI_Init(DCMI_HandleTypeDef * hdcmi)143 __weak HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
144 {
145     /* Check the DCMI peripheral state */
146     if (hdcmi == NULL) {
147         return HAL_ERROR;
148     }
149 
150     /* Check function parameters */
151     assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
152     assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
153     assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
154     assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
155     assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
156     assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
157     assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
158     assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
159 
160     if (hdcmi->State == HAL_DCMI_STATE_RESET) {
161         /* Allocate lock resource and initialize it */
162         hdcmi->Lock = HAL_UNLOCKED;
163         /* Init the low level hardware */
164         HAL_DCMI_MspInit(hdcmi);
165     }
166 
167     /* Change the DCMI state */
168     hdcmi->State = HAL_DCMI_STATE_BUSY;
169 
170     /* Set DCMI parameters */
171     /* Configures the HS, VS, DE and PC polarity */
172     hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL  | DCMI_CR_VSPOL  | DCMI_CR_EDM_0 |
173                              DCMI_CR_EDM_1  | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG  |
174                              DCMI_CR_ESS);
175     hdcmi->Instance->CR |=  (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate | \
176                                        hdcmi->Init.VSPolarity  | hdcmi->Init.HSPolarity  | \
177                                        hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode | \
178                                        hdcmi->Init.JPEGMode);
179 
180     if (hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED) {
181         DCMI->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode)    |
182                       ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << 8)|
183                       ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << 16) |
184                       ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << 24));
185     }
186 
187     /* Enable the Line interrupt */
188     __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE);
189 
190     /* Enable the VSYNC interrupt */
191     __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_VSYNC);
192 
193     /* Enable the Frame capture complete interrupt */
194     __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
195 
196     /* Enable the Synchronization error interrupt */
197     __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_ERR);
198 
199     /* Enable the Overflow interrupt */
200     __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_OVF);
201 
202     /* Enable DCMI by setting DCMIEN bit */
203     __HAL_DCMI_ENABLE(hdcmi);
204 
205     /* Update error code */
206     hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
207 
208     /* Initialize the DCMI state*/
209     hdcmi->State  = HAL_DCMI_STATE_READY;
210 
211     return HAL_OK;
212 }
213 
214 /**
215   * @brief  Deinitializes the DCMI peripheral registers to their default reset
216   *         values.
217   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
218   *                the configuration information for DCMI.
219   * @retval HAL status
220   */
221 
HAL_DCMI_DeInit(DCMI_HandleTypeDef * hdcmi)222 HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
223 {
224     /* DeInit the low level hardware */
225     HAL_DCMI_MspDeInit(hdcmi);
226 
227     /* Update error code */
228     hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
229 
230     /* Initialize the DCMI state*/
231     hdcmi->State = HAL_DCMI_STATE_RESET;
232 
233     /* Release Lock */
234     __HAL_UNLOCK(hdcmi);
235 
236     return HAL_OK;
237 }
238 
239 /**
240   * @brief  Initializes the DCMI MSP.
241   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
242   *                the configuration information for DCMI.
243   * @retval None
244   */
HAL_DCMI_MspInit(DCMI_HandleTypeDef * hdcmi)245 __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
246 {
247     /* NOTE : This function Should not be modified, when the callback is needed,
248               the HAL_DCMI_MspInit could be implemented in the user file
249      */
250 }
251 
252 /**
253   * @brief  DeInitializes the DCMI MSP.
254   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
255   *                the configuration information for DCMI.
256   * @retval None
257   */
HAL_DCMI_MspDeInit(DCMI_HandleTypeDef * hdcmi)258 __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
259 {
260     /* NOTE : This function Should not be modified, when the callback is needed,
261               the HAL_DCMI_MspDeInit could be implemented in the user file
262      */
263 }
264 
265 /**
266   * @}
267   */
268 /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
269  *  @brief   IO operation functions
270  *
271 @verbatim
272  ===============================================================================
273                       #####  IO operation functions  #####
274  ===============================================================================
275     [..]  This section provides functions allowing to:
276       (+) Configure destination address and data length and
277           Enables DCMI DMA request and enables DCMI capture
278       (+) Stop the DCMI capture.
279       (+) Handles DCMI interrupt request.
280 
281 @endverbatim
282   * @{
283   */
284 
285 /**
286   * @brief  Enables DCMI DMA request and enables DCMI capture
287   * @param  hdcmi:     pointer to a DCMI_HandleTypeDef structure that contains
288   *                    the configuration information for DCMI.
289   * @param  DCMI_Mode: DCMI capture mode snapshot or continuous grab.
290   * @param  pData:     The destination memory Buffer address (LCD Frame buffer).
291   * @param  Length:    The length of capture to be transferred.
292   * @retval HAL status
293   */
HAL_DCMI_Start_DMA(DCMI_HandleTypeDef * hdcmi,uint32_t DCMI_Mode,uint32_t pData,uint32_t Length)294 HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
295 {
296     /* Initialize the second memory address */
297     uint32_t SecondMemAddress = 0;
298 
299     /* Check function parameters */
300     assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
301 
302     /* Process Locked */
303     __HAL_LOCK(hdcmi);
304 
305     /* Lock the DCMI peripheral state */
306     hdcmi->State = HAL_DCMI_STATE_BUSY;
307 
308     /* Check the parameters */
309     assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
310 
311     /* Configure the DCMI Mode */
312     hdcmi->Instance->CR &= ~(DCMI_CR_CM);
313     hdcmi->Instance->CR |=  (uint32_t)(DCMI_Mode);
314 
315     /* Set the DMA memory0 conversion complete callback */
316     hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAConvCplt;
317 
318     /* Set the DMA error callback */
319     hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
320 
321     if (Length <= 0xFFFF) {
322         /* Enable the DMA Stream */
323         HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
324     } else { /* DCMI_DOUBLE_BUFFER Mode */
325         /* Set the DMA memory1 conversion complete callback */
326         hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAConvCplt;
327 
328         /* Initialize transfer parameters */
329         hdcmi->XferCount = 1;
330         hdcmi->XferSize = Length;
331         hdcmi->pBuffPtr = pData;
332 
333         /* Get the number of buffer */
334         while (hdcmi->XferSize > 0xFFFF) {
335             hdcmi->XferSize = (hdcmi->XferSize/2);
336             hdcmi->XferCount = hdcmi->XferCount*2;
337         }
338 
339         /* Update DCMI counter  and transfer number*/
340         hdcmi->XferCount = (hdcmi->XferCount - 2);
341         hdcmi->XferTransferNumber = hdcmi->XferCount;
342 
343         /* Update second memory address */
344         SecondMemAddress = (uint32_t)(pData + (4*hdcmi->XferSize));
345 
346         /* Start DMA multi buffer transfer */
347         HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
348     }
349 
350     /* Enable Capture */
351     DCMI->CR |= DCMI_CR_CAPTURE;
352 
353     /* Return function status */
354     return HAL_OK;
355 }
356 
357 /**
358   * @brief  Disable DCMI DMA request and Disable DCMI capture
359   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
360   *                the configuration information for DCMI.
361   * @retval HAL status
362   */
HAL_DCMI_Stop(DCMI_HandleTypeDef * hdcmi)363 HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
364 {
365     uint32_t tickstart = 0;
366 
367     /* Lock the DCMI peripheral state */
368     hdcmi->State = HAL_DCMI_STATE_BUSY;
369 
370     __HAL_DCMI_DISABLE(hdcmi);
371 
372     /* Disable Capture */
373     DCMI->CR &= ~(DCMI_CR_CAPTURE);
374 
375     /* Get tick */
376     tickstart = HAL_GetTick();
377 
378     /* Check if the DCMI capture effectively disabled */
379     while ((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0) {
380         if ((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DCMI_STOP) {
381             /* Process Unlocked */
382             __HAL_UNLOCK(hdcmi);
383 
384             /* Update error code */
385             hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
386 
387             /* Change DCMI state */
388             hdcmi->State = HAL_DCMI_STATE_TIMEOUT;
389 
390             return HAL_TIMEOUT;
391         }
392     }
393 
394     /* Disable the DMA */
395     HAL_DMA_Abort(hdcmi->DMA_Handle);
396 
397     /* Update error code */
398     hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
399 
400     /* Change DCMI state */
401     hdcmi->State = HAL_DCMI_STATE_READY;
402 
403     /* Process Unlocked */
404     __HAL_UNLOCK(hdcmi);
405 
406     /* Return function status */
407     return HAL_OK;
408 }
409 
410 /**
411   * @brief  Handles DCMI interrupt request.
412   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
413   *                the configuration information for the DCMI.
414   * @retval None
415   */
HAL_DCMI_IRQHandler(DCMI_HandleTypeDef * hdcmi)416 void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
417 {
418     /* Synchronization error interrupt management *******************************/
419     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_ERRRI) != RESET) {
420         if (__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_ERR) != RESET) {
421             /* Disable the Synchronization error interrupt */
422             __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_ERR);
423 
424             /* Clear the Synchronization error flag */
425             __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
426 
427             /* Update error code */
428             hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
429 
430             /* Change DCMI state */
431             hdcmi->State = HAL_DCMI_STATE_ERROR;
432 
433             /* Process Unlocked */
434             __HAL_UNLOCK(hdcmi);
435 
436             /* Abort the DMA Transfer */
437             HAL_DMA_Abort(hdcmi->DMA_Handle);
438 
439             /* Synchronization error Callback */
440             HAL_DCMI_ErrorCallback(hdcmi);
441         }
442     }
443     /* Overflow interrupt management ********************************************/
444     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_OVFRI) != RESET) {
445         if (__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_OVF) != RESET) {
446             /* Disable the Overflow interrupt */
447             __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_OVF);
448 
449             /* Clear the Overflow flag */
450             __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVFRI);
451 
452             /* Update error code */
453             hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVF;
454 
455             /* Change DCMI state */
456             hdcmi->State = HAL_DCMI_STATE_ERROR;
457 
458             /* Process Unlocked */
459             __HAL_UNLOCK(hdcmi);
460 
461             /* Abort the DMA Transfer */
462             HAL_DMA_Abort(hdcmi->DMA_Handle);
463 
464             /* Overflow Callback */
465             HAL_DCMI_ErrorCallback(hdcmi);
466         }
467     }
468     /* Line Interrupt management ************************************************/
469     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_LINERI) != RESET) {
470         if (__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_LINE) != RESET) {
471             /* Clear the Line interrupt flag */
472             __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
473 
474             /* Process Unlocked */
475             __HAL_UNLOCK(hdcmi);
476 
477             /* Line interrupt Callback */
478             HAL_DCMI_LineEventCallback(hdcmi);
479         }
480     }
481     /* VSYNC interrupt management ***********************************************/
482     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_VSYNCRI) != RESET) {
483         if (__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_VSYNC) != RESET) {
484             /* Disable the VSYNC interrupt */
485             __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_VSYNC);
486 
487             /* Clear the VSYNC flag */
488             __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
489 
490             /* Process Unlocked */
491             __HAL_UNLOCK(hdcmi);
492 
493             /* VSYNC Callback */
494             HAL_DCMI_VsyncEventCallback(hdcmi);
495         }
496     }
497     /* End of Frame interrupt management ****************************************/
498     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_FRAMERI) != RESET) {
499         if (__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_FRAME) != RESET) {
500             /* Disable the End of Frame interrupt */
501             __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
502 
503             /* Clear the End of Frame flag */
504             __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
505 
506             /* Process Unlocked */
507             __HAL_UNLOCK(hdcmi);
508 
509             /* End of Frame Callback */
510             HAL_DCMI_FrameEventCallback(hdcmi);
511         }
512     }
513 }
514 
515 /**
516   * @brief  Error DCMI callback.
517   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
518   *                the configuration information for DCMI.
519   * @retval None
520   */
HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef * hdcmi)521 __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
522 {
523     /* NOTE : This function Should not be modified, when the callback is needed,
524               the HAL_DCMI_ErrorCallback could be implemented in the user file
525      */
526 }
527 
528 /**
529   * @brief  Line Event callback.
530   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
531   *                the configuration information for DCMI.
532   * @retval None
533   */
HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef * hdcmi)534 __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
535 {
536     /* NOTE : This function Should not be modified, when the callback is needed,
537               the HAL_DCMI_LineEventCallback could be implemented in the user file
538      */
539 }
540 
541 /**
542   * @brief  VSYNC Event callback.
543   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
544   *                the configuration information for DCMI.
545   * @retval None
546   */
HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef * hdcmi)547 __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
548 {
549     /* NOTE : This function Should not be modified, when the callback is needed,
550               the HAL_DCMI_VsyncEventCallback could be implemented in the user file
551      */
552 }
553 
554 /**
555   * @brief  Frame Event callback.
556   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
557   *                the configuration information for DCMI.
558   * @retval None
559   */
HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef * hdcmi)560 __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
561 {
562     /* NOTE : This function Should not be modified, when the callback is needed,
563               the HAL_DCMI_FrameEventCallback could be implemented in the user file
564      */
565 }
566 
567 /**
568   * @}
569   */
570 
571 /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
572  *  @brief    Peripheral Control functions
573  *
574 @verbatim
575  ===============================================================================
576                     ##### Peripheral Control functions #####
577  ===============================================================================
578 [..]  This section provides functions allowing to:
579       (+) Configure the CROP feature.
580       (+) Enable/Disable the CROP feature.
581             (+) Enable/Disable the JPEG feature.
582 
583 @endverbatim
584   * @{
585   */
586 
587 /**
588   * @brief  Configure the DCMI CROP coordinate.
589   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
590   *                the configuration information for DCMI.
591   * @param  YSize: DCMI Line number
592   * @param  XSize: DCMI Pixel per line
593   * @param  X0:    DCMI window X offset
594   * @param  Y0:    DCMI window Y offset
595   * @retval HAL status
596   */
HAL_DCMI_ConfigCROP(DCMI_HandleTypeDef * hdcmi,uint32_t X0,uint32_t Y0,uint32_t XSize,uint32_t YSize)597 HAL_StatusTypeDef HAL_DCMI_ConfigCROP(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
598 {
599     /* Process Locked */
600     __HAL_LOCK(hdcmi);
601 
602     /* Lock the DCMI peripheral state */
603     hdcmi->State = HAL_DCMI_STATE_BUSY;
604 
605     /* Check the parameters */
606     assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
607     assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
608     assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
609     assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
610 
611     /* Configure CROP */
612     DCMI->CWSIZER = (XSize | (YSize << 16));
613     DCMI->CWSTRTR = (X0 | (Y0 << 16));
614 
615     /* Initialize the DCMI state*/
616     hdcmi->State  = HAL_DCMI_STATE_READY;
617 
618     /* Process Unlocked */
619     __HAL_UNLOCK(hdcmi);
620 
621     return HAL_OK;
622 }
623 
624 /**
625   * @brief  Disable the Crop feature.
626   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
627   *                the configuration information for DCMI.
628   * @retval HAL status
629   */
HAL_DCMI_DisableCROP(DCMI_HandleTypeDef * hdcmi)630 HAL_StatusTypeDef HAL_DCMI_DisableCROP(DCMI_HandleTypeDef *hdcmi)
631 {
632     /* Process Locked */
633     __HAL_LOCK(hdcmi);
634 
635     /* Lock the DCMI peripheral state */
636     hdcmi->State = HAL_DCMI_STATE_BUSY;
637 
638     /* Disable DCMI Crop feature */
639     DCMI->CR &= ~(uint32_t)DCMI_CR_CROP;
640 
641     /* Change the DCMI state*/
642     hdcmi->State = HAL_DCMI_STATE_READY;
643 
644     /* Process Unlocked */
645     __HAL_UNLOCK(hdcmi);
646 
647     return HAL_OK;
648 }
649 
650 /**
651   * @brief  Enable the Crop feature.
652   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
653   *                the configuration information for DCMI.
654   * @retval HAL status
655   */
HAL_DCMI_EnableCROP(DCMI_HandleTypeDef * hdcmi)656 HAL_StatusTypeDef HAL_DCMI_EnableCROP(DCMI_HandleTypeDef *hdcmi)
657 {
658     /* Process Locked */
659     __HAL_LOCK(hdcmi);
660 
661     /* Lock the DCMI peripheral state */
662     hdcmi->State = HAL_DCMI_STATE_BUSY;
663 
664     /* Enable DCMI Crop feature */
665     DCMI->CR |= (uint32_t)DCMI_CR_CROP;
666 
667     /* Change the DCMI state*/
668     hdcmi->State = HAL_DCMI_STATE_READY;
669 
670     /* Process Unlocked */
671     __HAL_UNLOCK(hdcmi);
672 
673     return HAL_OK;
674 }
675 
676 /**
677   * @}
678   */
679 
680 /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
681  *  @brief    Peripheral State functions
682  *
683 @verbatim
684  ===============================================================================
685                ##### Peripheral State and Errors functions #####
686  ===============================================================================
687     [..]
688     This subsection provides functions allowing to
689       (+) Check the DCMI state.
690       (+) Get the specific DCMI error flag.
691 
692 @endverbatim
693   * @{
694   */
695 
696 /**
697   * @brief  Return the DCMI state
698   * @param  hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
699   *                the configuration information for DCMI.
700   * @retval HAL state
701   */
HAL_DCMI_GetState(DCMI_HandleTypeDef * hdcmi)702 HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
703 {
704     return hdcmi->State;
705 }
706 
707 /**
708 * @brief  Return the DCMI error code
709 * @param  hdcmi : pointer to a DCMI_HandleTypeDef structure that contains
710   *               the configuration information for DCMI.
711 * @retval DCMI Error Code
712 */
HAL_DCMI_GetError(DCMI_HandleTypeDef * hdcmi)713 uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
714 {
715     return hdcmi->ErrorCode;
716 }
717 
718 /**
719   * @}
720   */
721 /* Private functions ---------------------------------------------------------*/
722 /** @defgroup DCMI_Private_Functions DCMI Private Functions
723   * @{
724   */
725 /**
726 * @brief  DMA conversion complete callback.
727 * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
728 *                the configuration information for the specified DMA module.
729 * @retval None
730 */
DCMI_DMAConvCplt(DMA_HandleTypeDef * hdma)731 static void DCMI_DMAConvCplt(DMA_HandleTypeDef *hdma)
732 {
733     uint32_t tmp = 0;
734 
735     DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
736     hdcmi->State= HAL_DCMI_STATE_READY;
737 
738     if (hdcmi->XferCount != 0) {
739         /* Update memory 0 address location */
740         tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
741         if (((hdcmi->XferCount % 2) == 0) && (tmp != 0)) {
742             tmp = hdcmi->DMA_Handle->Instance->M0AR;
743             HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY0);
744             hdcmi->XferCount--;
745         }
746         /* Update memory 1 address location */
747         else if ((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0) {
748             tmp = hdcmi->DMA_Handle->Instance->M1AR;
749             HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY1);
750             hdcmi->XferCount--;
751         }
752     }
753     /* Update memory 0 address location */
754     else if ((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0) {
755         hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
756     }
757     /* Update memory 1 address location */
758     else if ((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0) {
759         tmp = hdcmi->pBuffPtr;
760         hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4*hdcmi->XferSize));
761         hdcmi->XferCount = hdcmi->XferTransferNumber;
762     }
763 
764     if (__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_FRAMERI) != RESET) {
765         /* Process Unlocked */
766         __HAL_UNLOCK(hdcmi);
767 
768         /* FRAME Callback */
769         HAL_DCMI_FrameEventCallback(hdcmi);
770     }
771 }
772 
773 /**
774   * @brief  DMA error callback
775   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
776   *                the configuration information for the specified DMA module.
777   * @retval None
778   */
DCMI_DMAError(DMA_HandleTypeDef * hdma)779 static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
780 {
781     DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
782     hdcmi->State= HAL_DCMI_STATE_READY;
783     HAL_DCMI_ErrorCallback(hdcmi);
784 }
785 
786 /**
787   * @}
788   */
789 
790 /**
791   * @}
792   */
793 #endif /* HAL_DCMI_MODULE_ENABLED */
794 /**
795   * @}
796   */
797 
798 /**
799   * @}
800   */
801 
802 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
803