source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dcmi.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 37.8 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_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
12 *
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.
23
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.
28
29 (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
30 register to the destination memory buffer.
31
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.
35
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
39
40 (#) The capture can be stopped using HAL_DCMI_Stop() function.
41
42 (#) To control DCMI state you can use the function HAL_DCMI_GetState().
43
44 *** DCMI HAL driver macros list ***
45 =============================================
46 [..]
47 Below the list of most used macros in DCMI HAL driver.
48
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.
56
57 [..]
58 (@) You can refer to the DCMI HAL driver header file for more useful macros
59
60 *** Callback registration ***
61 =============================
62
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.
66
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.
76
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.
88
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).
97
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.
105
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.
109
110 @endverbatim
111 ******************************************************************************
112 * @attention
113 *
114 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
115 * All rights reserved.</center></h2>
116 *
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
121 *
122 ******************************************************************************
123 */
124
125/* Includes ------------------------------------------------------------------*/
126#include "stm32f4xx_hal.h"
127
128/** @addtogroup STM32F4xx_HAL_Driver
129 * @{
130 */
131/** @defgroup DCMI DCMI
132 * @brief DCMI HAL module driver
133 * @{
134 */
135
136#ifdef HAL_DCMI_MODULE_ENABLED
137
138#if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) ||\
139 defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) ||\
140 defined(STM32F479xx)
141/* Private typedef -----------------------------------------------------------*/
142/* Private define ------------------------------------------------------------*/
143#define HAL_TIMEOUT_DCMI_STOP 14U /* Set timeout to 1s */
144/* Private macro -------------------------------------------------------------*/
145/* Private variables ---------------------------------------------------------*/
146/* Private function prototypes -----------------------------------------------*/
147static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma);
148static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
149
150/* Exported functions --------------------------------------------------------*/
151
152/** @defgroup DCMI_Exported_Functions DCMI Exported Functions
153 * @{
154 */
155
156/** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
157 * @brief Initialization and Configuration functions
158 *
159@verbatim
160 ===============================================================================
161 ##### Initialization and Configuration functions #####
162 ===============================================================================
163 [..] This section provides functions allowing to:
164 (+) Initialize and configure the DCMI
165 (+) De-initialize the DCMI
166
167@endverbatim
168 * @{
169 */
170
171/**
172 * @brief Initializes the DCMI according to the specified
173 * parameters in the DCMI_InitTypeDef and create the associated handle.
174 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
175 * the configuration information for DCMI.
176 * @retval HAL status
177 */
178__weak HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
179{
180 /* Check the DCMI peripheral state */
181 if(hdcmi == NULL)
182 {
183 return HAL_ERROR;
184 }
185
186 /* Check function parameters */
187 assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
188 assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
189 assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
190 assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
191 assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
192 assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
193 assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
194 assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
195
196 if(hdcmi->State == HAL_DCMI_STATE_RESET)
197 {
198 /* Allocate lock resource and initialize it */
199 hdcmi->Lock = HAL_UNLOCKED;
200 /* Init the low level hardware */
201 /* Init the DCMI Callback settings */
202#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
203 hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
204 hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
205 hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
206 hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
207
208 if(hdcmi->MspInitCallback == NULL)
209 {
210 /* Legacy weak MspInit Callback */
211 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
212 }
213 /* Initialize the low level hardware (MSP) */
214 hdcmi->MspInitCallback(hdcmi);
215#else
216 /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
217 HAL_DCMI_MspInit(hdcmi);
218#endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
219 HAL_DCMI_MspInit(hdcmi);
220 }
221
222 /* Change the DCMI state */
223 hdcmi->State = HAL_DCMI_STATE_BUSY;
224
225 /* Set DCMI parameters */
226 /* Configures the HS, VS, DE and PC polarity */
227 hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |
228 DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |
229 DCMI_CR_ESS);
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);
234
235 if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
236 {
237 hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode) |
238 ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << DCMI_POSITION_ESCR_LSC)|
239 ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << DCMI_POSITION_ESCR_LEC) |
240 ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << DCMI_POSITION_ESCR_FEC));
241 }
242
243 /* Enable the Line, Vsync, Error and Overrun interrupts */
244 __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
245
246 /* Update error code */
247 hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
248
249 /* Initialize the DCMI state*/
250 hdcmi->State = HAL_DCMI_STATE_READY;
251
252 return HAL_OK;
253}
254
255/**
256 * @brief Deinitializes the DCMI peripheral registers to their default reset
257 * values.
258 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
259 * the configuration information for DCMI.
260 * @retval HAL status
261 */
262
263HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
264{
265#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
266 if(hdcmi->MspDeInitCallback == NULL)
267 {
268 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
269 }
270 /* De-Initialize the low level hardware (MSP) */
271 hdcmi->MspDeInitCallback(hdcmi);
272#else
273 /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
274 HAL_DCMI_MspDeInit(hdcmi);
275#endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
276
277 /* Update error code */
278 hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
279
280 /* Initialize the DCMI state*/
281 hdcmi->State = HAL_DCMI_STATE_RESET;
282
283 /* Release Lock */
284 __HAL_UNLOCK(hdcmi);
285
286 return HAL_OK;
287}
288
289/**
290 * @brief Initializes the DCMI MSP.
291 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
292 * the configuration information for DCMI.
293 * @retval None
294 */
295__weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
296{
297 /* Prevent unused argument(s) compilation warning */
298 UNUSED(hdcmi);
299 /* NOTE : This function Should not be modified, when the callback is needed,
300 the HAL_DCMI_MspInit could be implemented in the user file
301 */
302}
303
304/**
305 * @brief DeInitializes the DCMI MSP.
306 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
307 * the configuration information for DCMI.
308 * @retval None
309 */
310__weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
311{
312 /* Prevent unused argument(s) compilation warning */
313 UNUSED(hdcmi);
314 /* NOTE : This function Should not be modified, when the callback is needed,
315 the HAL_DCMI_MspDeInit could be implemented in the user file
316 */
317}
318
319/**
320 * @}
321 */
322/** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
323 * @brief IO operation functions
324 *
325@verbatim
326 ===============================================================================
327 ##### IO operation functions #####
328 ===============================================================================
329 [..] This section provides functions allowing to:
330 (+) Configure destination address and data length and
331 Enables DCMI DMA request and enables DCMI capture
332 (+) Stop the DCMI capture.
333 (+) Handles DCMI interrupt request.
334
335@endverbatim
336 * @{
337 */
338
339/**
340 * @brief Enables DCMI DMA request and enables DCMI capture
341 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
342 * the configuration information for DCMI.
343 * @param DCMI_Mode DCMI capture mode snapshot or continuous grab.
344 * @param pData The destination memory Buffer address (LCD Frame buffer).
345 * @param Length The length of capture to be transferred.
346 * @retval HAL status
347 */
348HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
349{
350 /* Initialize the second memory address */
351 uint32_t SecondMemAddress = 0U;
352
353 /* Check function parameters */
354 assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
355
356 /* Process Locked */
357 __HAL_LOCK(hdcmi);
358
359 /* Lock the DCMI peripheral state */
360 hdcmi->State = HAL_DCMI_STATE_BUSY;
361
362 /* Enable DCMI by setting DCMIEN bit */
363 __HAL_DCMI_ENABLE(hdcmi);
364
365 /* Configure the DCMI Mode */
366 hdcmi->Instance->CR &= ~(DCMI_CR_CM);
367 hdcmi->Instance->CR |= (uint32_t)(DCMI_Mode);
368
369 /* Set the DMA memory0 conversion complete callback */
370 hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
371
372 /* Set the DMA error callback */
373 hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
374
375 /* Set the dma abort callback */
376 hdcmi->DMA_Handle->XferAbortCallback = NULL;
377
378 /* Reset transfer counters value */
379 hdcmi->XferCount = 0U;
380 hdcmi->XferTransferNumber = 0U;
381
382 if(Length <= 0xFFFFU)
383 {
384 /* Enable the DMA Stream */
385 HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
386 }
387 else /* DCMI_DOUBLE_BUFFER Mode */
388 {
389 /* Set the DMA memory1 conversion complete callback */
390 hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAXferCplt;
391
392 /* Initialize transfer parameters */
393 hdcmi->XferCount = 1U;
394 hdcmi->XferSize = Length;
395 hdcmi->pBuffPtr = pData;
396
397 /* Get the number of buffer */
398 while(hdcmi->XferSize > 0xFFFFU)
399 {
400 hdcmi->XferSize = (hdcmi->XferSize/2U);
401 hdcmi->XferCount = hdcmi->XferCount*2U;
402 }
403
404 /* Update DCMI counter and transfer number*/
405 hdcmi->XferCount = (hdcmi->XferCount - 2U);
406 hdcmi->XferTransferNumber = hdcmi->XferCount;
407
408 /* Update second memory address */
409 SecondMemAddress = (uint32_t)(pData + (4U*hdcmi->XferSize));
410
411 /* Start DMA multi buffer transfer */
412 HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
413 }
414
415 /* Enable Capture */
416 hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
417
418 /* Release Lock */
419 __HAL_UNLOCK(hdcmi);
420
421 /* Return function status */
422 return HAL_OK;
423}
424
425/**
426 * @brief Disable DCMI DMA request and Disable DCMI capture
427 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
428 * the configuration information for DCMI.
429 * @retval HAL status
430 */
431HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
432{
433 __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
434 HAL_StatusTypeDef status = HAL_OK;
435
436 /* Process locked */
437 __HAL_LOCK(hdcmi);
438
439 /* Lock the DCMI peripheral state */
440 hdcmi->State = HAL_DCMI_STATE_BUSY;
441
442 /* Disable Capture */
443 hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
444
445 /* Check if the DCMI capture effectively disabled */
446 do
447 {
448 if (count-- == 0U)
449 {
450 /* Update error code */
451 hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
452
453 status = HAL_TIMEOUT;
454 break;
455 }
456 }
457 while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0U);
458
459 /* Disable the DCMI */
460 __HAL_DCMI_DISABLE(hdcmi);
461
462 /* Disable the DMA */
463 HAL_DMA_Abort(hdcmi->DMA_Handle);
464
465 /* Update error code */
466 hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
467
468 /* Change DCMI state */
469 hdcmi->State = HAL_DCMI_STATE_READY;
470
471 /* Process Unlocked */
472 __HAL_UNLOCK(hdcmi);
473
474 /* Return function status */
475 return status;
476}
477
478/**
479 * @brief Suspend DCMI capture
480 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
481 * the configuration information for DCMI.
482 * @retval HAL status
483 */
484HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
485{
486 __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
487 HAL_StatusTypeDef status = HAL_OK;
488
489 /* Process locked */
490 __HAL_LOCK(hdcmi);
491
492 if(hdcmi->State == HAL_DCMI_STATE_BUSY)
493 {
494 /* Change DCMI state */
495 hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
496
497 /* Disable Capture */
498 hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
499
500 /* Check if the DCMI capture effectively disabled */
501 do
502 {
503 if (count-- == 0U)
504 {
505 /* Update error code */
506 hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
507
508 /* Change DCMI state */
509 hdcmi->State = HAL_DCMI_STATE_READY;
510
511 status = HAL_TIMEOUT;
512 break;
513 }
514 }
515 while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
516 }
517 /* Process Unlocked */
518 __HAL_UNLOCK(hdcmi);
519
520 /* Return function status */
521 return status;
522}
523
524/**
525 * @brief Resume DCMI capture
526 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
527 * the configuration information for DCMI.
528 * @retval HAL status
529 */
530HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
531{
532 /* Process locked */
533 __HAL_LOCK(hdcmi);
534
535 if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
536 {
537 /* Change DCMI state */
538 hdcmi->State = HAL_DCMI_STATE_BUSY;
539
540 /* Disable Capture */
541 hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
542 }
543 /* Process Unlocked */
544 __HAL_UNLOCK(hdcmi);
545
546 /* Return function status */
547 return HAL_OK;
548}
549
550/**
551 * @brief Handles DCMI interrupt request.
552 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
553 * the configuration information for the DCMI.
554 * @retval None
555 */
556void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
557{
558 uint32_t isr_value = READ_REG(hdcmi->Instance->MISR);
559
560 /* Synchronization error interrupt management *******************************/
561 if((isr_value & DCMI_FLAG_ERRRI) == DCMI_FLAG_ERRRI)
562 {
563 /* Clear the Synchronization error flag */
564 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
565
566 /* Update error code */
567 hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
568
569 /* Change DCMI state */
570 hdcmi->State = HAL_DCMI_STATE_ERROR;
571
572 /* Set the synchronization error callback */
573 hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
574
575 /* Abort the DMA Transfer */
576 HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
577 }
578 /* Overflow interrupt management ********************************************/
579 if((isr_value & DCMI_FLAG_OVRRI) == DCMI_FLAG_OVRRI)
580 {
581 /* Clear the Overflow flag */
582 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
583
584 /* Update error code */
585 hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
586
587 /* Change DCMI state */
588 hdcmi->State = HAL_DCMI_STATE_ERROR;
589
590 /* Set the overflow callback */
591 hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
592
593 /* Abort the DMA Transfer */
594 HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
595 }
596 /* Line Interrupt management ************************************************/
597 if((isr_value & DCMI_FLAG_LINERI) == DCMI_FLAG_LINERI)
598 {
599 /* Clear the Line interrupt flag */
600 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
601
602 /* Line interrupt Callback */
603#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
604 /*Call registered DCMI line event callback*/
605 hdcmi->LineEventCallback(hdcmi);
606#else
607 HAL_DCMI_LineEventCallback(hdcmi);
608#endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
609 }
610 /* VSYNC interrupt management ***********************************************/
611 if((isr_value & DCMI_FLAG_VSYNCRI) == DCMI_FLAG_VSYNCRI)
612 {
613 /* Clear the VSYNC flag */
614 __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
615
616 /* VSYNC Callback */
617#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
618 /*Call registered DCMI vsync event callback*/
619 hdcmi->VsyncEventCallback(hdcmi);
620#else
621 HAL_DCMI_VsyncEventCallback(hdcmi);
622#endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
623 }
624 /* FRAME interrupt management ***********************************************/
625 if((isr_value & DCMI_FLAG_FRAMERI) == DCMI_FLAG_FRAMERI)
626 {
627 /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
628 if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
629 {
630 /* Disable the Line, Vsync, Error and Overrun interrupts */
631 __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
632 }
633
634 /* Disable the Frame interrupt */
635 __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
636
637 /* Frame Callback */
638#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
639 /*Call registered DCMI frame event callback*/
640 hdcmi->FrameEventCallback(hdcmi);
641#else
642 HAL_DCMI_FrameEventCallback(hdcmi);
643#endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
644 }
645}
646
647/**
648 * @brief Error DCMI callback.
649 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
650 * the configuration information for DCMI.
651 * @retval None
652 */
653__weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
654{
655 /* Prevent unused argument(s) compilation warning */
656 UNUSED(hdcmi);
657 /* NOTE : This function Should not be modified, when the callback is needed,
658 the HAL_DCMI_ErrorCallback could be implemented in the user file
659 */
660}
661
662/**
663 * @brief Line Event callback.
664 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
665 * the configuration information for DCMI.
666 * @retval None
667 */
668__weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
669{
670 /* Prevent unused argument(s) compilation warning */
671 UNUSED(hdcmi);
672 /* NOTE : This function Should not be modified, when the callback is needed,
673 the HAL_DCMI_LineEventCallback could be implemented in the user file
674 */
675}
676
677/**
678 * @brief VSYNC Event callback.
679 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
680 * the configuration information for DCMI.
681 * @retval None
682 */
683__weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
684{
685 /* Prevent unused argument(s) compilation warning */
686 UNUSED(hdcmi);
687 /* NOTE : This function Should not be modified, when the callback is needed,
688 the HAL_DCMI_VsyncEventCallback could be implemented in the user file
689 */
690}
691
692/**
693 * @brief Frame Event callback.
694 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
695 * the configuration information for DCMI.
696 * @retval None
697 */
698__weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
699{
700 /* Prevent unused argument(s) compilation warning */
701 UNUSED(hdcmi);
702 /* NOTE : This function Should not be modified, when the callback is needed,
703 the HAL_DCMI_FrameEventCallback could be implemented in the user file
704 */
705}
706
707/**
708 * @}
709 */
710
711/** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
712 * @brief Peripheral Control functions
713 *
714@verbatim
715 ===============================================================================
716 ##### Peripheral Control functions #####
717 ===============================================================================
718[..] This section provides functions allowing to:
719 (+) Configure the CROP feature.
720 (+) Enable/Disable the CROP feature.
721
722@endverbatim
723 * @{
724 */
725
726/**
727 * @brief Configure the DCMI CROP coordinate.
728 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
729 * the configuration information for DCMI.
730 * @param X0 DCMI window X offset
731 * @param Y0 DCMI window Y offset
732 * @param XSize DCMI Pixel per line
733 * @param YSize DCMI Line number
734 * @retval HAL status
735 */
736HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
737{
738 /* Process Locked */
739 __HAL_LOCK(hdcmi);
740
741 /* Lock the DCMI peripheral state */
742 hdcmi->State = HAL_DCMI_STATE_BUSY;
743
744 /* Check the parameters */
745 assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
746 assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
747 assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
748 assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
749
750 /* Configure CROP */
751 hdcmi->Instance->CWSIZER = (XSize | (YSize << DCMI_POSITION_CWSIZE_VLINE));
752 hdcmi->Instance->CWSTRTR = (X0 | (Y0 << DCMI_POSITION_CWSTRT_VST));
753
754 /* Initialize the DCMI state*/
755 hdcmi->State = HAL_DCMI_STATE_READY;
756
757 /* Process Unlocked */
758 __HAL_UNLOCK(hdcmi);
759
760 return HAL_OK;
761}
762
763/**
764 * @brief Disable the Crop feature.
765 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
766 * the configuration information for DCMI.
767 * @retval HAL status
768 */
769HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
770{
771 /* Process Locked */
772 __HAL_LOCK(hdcmi);
773
774 /* Lock the DCMI peripheral state */
775 hdcmi->State = HAL_DCMI_STATE_BUSY;
776
777 /* Disable DCMI Crop feature */
778 hdcmi->Instance->CR &= ~(uint32_t)DCMI_CR_CROP;
779
780 /* Change the DCMI state*/
781 hdcmi->State = HAL_DCMI_STATE_READY;
782
783 /* Process Unlocked */
784 __HAL_UNLOCK(hdcmi);
785
786 return HAL_OK;
787}
788
789/**
790 * @brief Enable the Crop feature.
791 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
792 * the configuration information for DCMI.
793 * @retval HAL status
794 */
795HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
796{
797 /* Process Locked */
798 __HAL_LOCK(hdcmi);
799
800 /* Lock the DCMI peripheral state */
801 hdcmi->State = HAL_DCMI_STATE_BUSY;
802
803 /* Enable DCMI Crop feature */
804 hdcmi->Instance->CR |= (uint32_t)DCMI_CR_CROP;
805
806 /* Change the DCMI state*/
807 hdcmi->State = HAL_DCMI_STATE_READY;
808
809 /* Process Unlocked */
810 __HAL_UNLOCK(hdcmi);
811
812 return HAL_OK;
813}
814
815/**
816 * @brief Set embedded synchronization delimiters unmasks.
817 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
818 * the configuration information for DCMI.
819 * @param SyncUnmask pointer to a DCMI_SyncUnmaskTypeDef structure that contains
820 * the embedded synchronization delimiters unmasks.
821 * @retval HAL status
822 */
823HAL_StatusTypeDef HAL_DCMI_ConfigSyncUnmask(DCMI_HandleTypeDef *hdcmi, DCMI_SyncUnmaskTypeDef *SyncUnmask)
824{
825 /* Process Locked */
826 __HAL_LOCK(hdcmi);
827
828 /* Lock the DCMI peripheral state */
829 hdcmi->State = HAL_DCMI_STATE_BUSY;
830
831 /* Write DCMI embedded synchronization unmask register */
832 hdcmi->Instance->ESUR = (((uint32_t)SyncUnmask->FrameStartUnmask) |\
833 ((uint32_t)SyncUnmask->LineStartUnmask << DCMI_ESUR_LSU_Pos)|\
834 ((uint32_t)SyncUnmask->LineEndUnmask << DCMI_ESUR_LEU_Pos)|\
835 ((uint32_t)SyncUnmask->FrameEndUnmask << DCMI_ESUR_FEU_Pos));
836
837 /* Change the DCMI state*/
838 hdcmi->State = HAL_DCMI_STATE_READY;
839
840 /* Process Unlocked */
841 __HAL_UNLOCK(hdcmi);
842
843 return HAL_OK;
844}
845
846/**
847 * @}
848 */
849
850/** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
851 * @brief Peripheral State functions
852 *
853@verbatim
854 ===============================================================================
855 ##### Peripheral State and Errors functions #####
856 ===============================================================================
857 [..]
858 This subsection provides functions allowing to
859 (+) Check the DCMI state.
860 (+) Get the specific DCMI error flag.
861
862@endverbatim
863 * @{
864 */
865
866/**
867 * @brief Return the DCMI state
868 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
869 * the configuration information for DCMI.
870 * @retval HAL state
871 */
872HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
873{
874 return hdcmi->State;
875}
876
877/**
878 * @brief Return the DCMI error code
879 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
880 * the configuration information for DCMI.
881 * @retval DCMI Error Code
882 */
883uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
884{
885 return hdcmi->ErrorCode;
886}
887
888#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
889/**
890 * @brief DCMI Callback registering
891 * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
892 * the configuration information for DCMI.
893 * @param CallbackID dcmi Callback ID
894 * @param pCallback pointer to DCMI_CallbackTypeDef structure
895 * @retval status
896 */
897HAL_StatusTypeDef HAL_DCMI_RegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID, pDCMI_CallbackTypeDef pCallback)
898{
899 HAL_StatusTypeDef status = HAL_OK;
900
901 if(pCallback == NULL)
902 {
903 /* update the error code */
904 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
905 /* update return status */
906 status = HAL_ERROR;
907 }
908 else
909 {
910 if(hdcmi->State == HAL_DCMI_STATE_READY)
911 {
912 switch (CallbackID)
913 {
914 case HAL_DCMI_FRAME_EVENT_CB_ID :
915 hdcmi->FrameEventCallback = pCallback;
916 break;
917
918 case HAL_DCMI_VSYNC_EVENT_CB_ID :
919 hdcmi->VsyncEventCallback = pCallback;
920 break;
921
922 case HAL_DCMI_LINE_EVENT_CB_ID :
923 hdcmi->LineEventCallback = pCallback;
924 break;
925
926 case HAL_DCMI_ERROR_CB_ID :
927 hdcmi->ErrorCallback = pCallback;
928 break;
929
930 case HAL_DCMI_MSPINIT_CB_ID :
931 hdcmi->MspInitCallback = pCallback;
932 break;
933
934 case HAL_DCMI_MSPDEINIT_CB_ID :
935 hdcmi->MspDeInitCallback = pCallback;
936 break;
937
938 default :
939 /* Return error status */
940 status = HAL_ERROR;
941 break;
942 }
943 }
944 else if(hdcmi->State == HAL_DCMI_STATE_RESET)
945 {
946 switch (CallbackID)
947 {
948 case HAL_DCMI_MSPINIT_CB_ID :
949 hdcmi->MspInitCallback = pCallback;
950 break;
951
952 case HAL_DCMI_MSPDEINIT_CB_ID :
953 hdcmi->MspDeInitCallback = pCallback;
954 break;
955
956 default :
957 /* update the error code */
958 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
959 /* update return status */
960 status = HAL_ERROR;
961 break;
962 }
963 }
964 else
965 {
966 /* update the error code */
967 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
968 /* update return status */
969 status = HAL_ERROR;
970 }
971 }
972
973 return status;
974}
975
976/**
977 * @brief DCMI Callback Unregistering
978 * @param hdcmi dcmi handle
979 * @param CallbackID dcmi Callback ID
980 * @retval status
981 */
982HAL_StatusTypeDef HAL_DCMI_UnRegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID)
983{
984 HAL_StatusTypeDef status = HAL_OK;
985
986 if(hdcmi->State == HAL_DCMI_STATE_READY)
987 {
988 switch (CallbackID)
989 {
990 case HAL_DCMI_FRAME_EVENT_CB_ID :
991 hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
992 break;
993
994 case HAL_DCMI_VSYNC_EVENT_CB_ID :
995 hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
996 break;
997
998 case HAL_DCMI_LINE_EVENT_CB_ID :
999 hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
1000 break;
1001
1002 case HAL_DCMI_ERROR_CB_ID :
1003 hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
1004 break;
1005
1006 case HAL_DCMI_MSPINIT_CB_ID :
1007 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
1008 break;
1009
1010 case HAL_DCMI_MSPDEINIT_CB_ID :
1011 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
1012 break;
1013
1014 default :
1015 /* update the error code */
1016 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
1017 /* update return status */
1018 status = HAL_ERROR;
1019 break;
1020 }
1021 }
1022 else if(hdcmi->State == HAL_DCMI_STATE_RESET)
1023 {
1024 switch (CallbackID)
1025 {
1026 case HAL_DCMI_MSPINIT_CB_ID :
1027 hdcmi->MspInitCallback = HAL_DCMI_MspInit;
1028 break;
1029
1030 case HAL_DCMI_MSPDEINIT_CB_ID :
1031 hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
1032 break;
1033
1034 default :
1035 /* update the error code */
1036 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
1037 /* update return status */
1038 status = HAL_ERROR;
1039 break;
1040 }
1041 }
1042 else
1043 {
1044 /* update the error code */
1045 hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
1046 /* update return status */
1047 status = HAL_ERROR;
1048 }
1049
1050 return status;
1051}
1052#endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
1053
1054/**
1055 * @}
1056 */
1057/* Private functions ---------------------------------------------------------*/
1058/** @defgroup DCMI_Private_Functions DCMI Private Functions
1059 * @{
1060 */
1061
1062/**
1063 * @brief DMA conversion complete callback.
1064 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1065 * the configuration information for the specified DMA module.
1066 * @retval None
1067 */
1068static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
1069{
1070 uint32_t tmp = 0U;
1071
1072 DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1073
1074 if(hdcmi->XferCount != 0U)
1075 {
1076 /* Update memory 0 address location */
1077 tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
1078 if(((hdcmi->XferCount % 2U) == 0U) && (tmp != 0U))
1079 {
1080 tmp = hdcmi->DMA_Handle->Instance->M0AR;
1081 HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY0);
1082 hdcmi->XferCount--;
1083 }
1084 /* Update memory 1 address location */
1085 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
1086 {
1087 tmp = hdcmi->DMA_Handle->Instance->M1AR;
1088 HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY1);
1089 hdcmi->XferCount--;
1090 }
1091 }
1092 /* Update memory 0 address location */
1093 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0U)
1094 {
1095 hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
1096 }
1097 /* Update memory 1 address location */
1098 else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
1099 {
1100 tmp = hdcmi->pBuffPtr;
1101 hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4U*hdcmi->XferSize));
1102 hdcmi->XferCount = hdcmi->XferTransferNumber;
1103 }
1104
1105 /* Check if the frame is transferred */
1106 if(hdcmi->XferCount == hdcmi->XferTransferNumber)
1107 {
1108 /* Enable the Frame interrupt */
1109 __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
1110
1111 /* When snapshot mode, set dcmi state to ready */
1112 if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
1113 {
1114 hdcmi->State= HAL_DCMI_STATE_READY;
1115 }
1116 }
1117}
1118
1119/**
1120 * @brief DMA error callback
1121 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
1122 * the configuration information for the specified DMA module.
1123 * @retval None
1124 */
1125static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
1126{
1127 DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1128
1129 if(hdcmi->DMA_Handle->ErrorCode != HAL_DMA_ERROR_FE)
1130 {
1131 /* Initialize the DCMI state*/
1132 hdcmi->State = HAL_DCMI_STATE_READY;
1133 }
1134
1135 /* DCMI error Callback */
1136#if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
1137 /*Call registered DCMI error callback*/
1138 hdcmi->ErrorCallback(hdcmi);
1139#else
1140 HAL_DCMI_ErrorCallback(hdcmi);
1141#endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
1142
1143}
1144
1145/**
1146 * @}
1147 */
1148
1149/**
1150 * @}
1151 */
1152#endif /* STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
1153 STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
1154 STM32F479xx */
1155#endif /* HAL_DCMI_MODULE_ENABLED */
1156/**
1157 * @}
1158 */
1159
1160/**
1161 * @}
1162 */
1163
1164/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.