2 ******************************************************************************
3 * @file stm32f4xx_dcmi.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the DCMI peripheral:
9 * + Initialization and Configuration
10 * + Image capture functions
11 * + Interrupts and flags management
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
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 (#) Enable the clock for the DCMI and associated GPIOs using the following
26 RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
27 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
29 (#) DCMI pins configuration
30 (++) Connect the involved DCMI pins to AF13 using the following function
31 GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_DCMI);
32 (++) Configure these DCMI pins in alternate function mode by calling
33 the function GPIO_Init();
35 (#) Declare a DCMI_InitTypeDef structure, for example:
36 DCMI_InitTypeDef DCMI_InitStructure;
37 and fill the DCMI_InitStructure variable with the allowed values
38 of the structure member.
40 (#) Initialize the DCMI interface by calling the function
41 DCMI_Init(&DCMI_InitStructure);
43 (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
44 register to the destination memory buffer.
46 (#) Enable DCMI interface using the function
49 (#) Start the image capture using the function
50 DCMI_CaptureCmd(ENABLE);
52 (#) At this stage the DCMI interface waits for the first start of frame,
53 then a DMA request is generated continuously/once (depending on the
54 mode used, Continuous/Snapshot) to transfer the received data into
55 the destination memory.
57 -@- If you need to capture only a rectangular window from the received
58 image, you have to use the DCMI_CROPConfig() function to configure
59 the coordinates and size of the window to be captured, then enable
60 the Crop feature using DCMI_CROPCmd(ENABLE);
61 In this case, the Crop configuration should be made before to enable
62 and start the DCMI interface.
65 ******************************************************************************
68 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
70 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
71 * You may not use this file except in compliance with the License.
72 * You may obtain a copy of the License at:
74 * http://www.st.com/software_license_agreement_liberty_v2
76 * Unless required by applicable law or agreed to in writing, software
77 * distributed under the License is distributed on an "AS IS" BASIS,
78 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
79 * See the License for the specific language governing permissions and
80 * limitations under the License.
82 ******************************************************************************
85 /* Includes ------------------------------------------------------------------*/
86 #include "stm32f4xx_dcmi.h"
87 #include "stm32f4xx_rcc.h"
89 /** @addtogroup STM32F4xx_StdPeriph_Driver
94 * @brief DCMI driver modules
98 /* Private typedef -----------------------------------------------------------*/
99 /* Private define ------------------------------------------------------------*/
100 /* Private macro -------------------------------------------------------------*/
101 /* Private variables ---------------------------------------------------------*/
102 /* Private function prototypes -----------------------------------------------*/
103 /* Private functions ---------------------------------------------------------*/
105 /** @defgroup DCMI_Private_Functions
109 /** @defgroup DCMI_Group1 Initialization and Configuration functions
110 * @brief Initialization and Configuration functions
113 ===============================================================================
114 ##### Initialization and Configuration functions #####
115 ===============================================================================
122 * @brief Deinitializes the DCMI registers to their default reset values.
126 void DCMI_DeInit(void)
138 * @brief Initializes the DCMI according to the specified parameters in the DCMI_InitStruct.
139 * @param DCMI_InitStruct: pointer to a DCMI_InitTypeDef structure that contains
140 * the configuration information for the DCMI.
143 void DCMI_Init(DCMI_InitTypeDef
* DCMI_InitStruct
)
147 /* Check the parameters */
148 assert_param(IS_DCMI_CAPTURE_MODE(DCMI_InitStruct
->DCMI_CaptureMode
));
149 assert_param(IS_DCMI_SYNCHRO(DCMI_InitStruct
->DCMI_SynchroMode
));
150 assert_param(IS_DCMI_PCKPOLARITY(DCMI_InitStruct
->DCMI_PCKPolarity
));
151 assert_param(IS_DCMI_VSPOLARITY(DCMI_InitStruct
->DCMI_VSPolarity
));
152 assert_param(IS_DCMI_HSPOLARITY(DCMI_InitStruct
->DCMI_HSPolarity
));
153 assert_param(IS_DCMI_CAPTURE_RATE(DCMI_InitStruct
->DCMI_CaptureRate
));
154 assert_param(IS_DCMI_EXTENDED_DATA(DCMI_InitStruct
->DCMI_ExtendedDataMode
));
156 /* The DCMI configuration registers should be programmed correctly before
157 enabling the CR_ENABLE Bit and the CR_CAPTURE Bit */
158 DCMI
->CR
&= ~(DCMI_CR_ENABLE
| DCMI_CR_CAPTURE
);
160 /* Reset the old DCMI configuration */
163 temp
&= ~((uint32_t)DCMI_CR_CM
| DCMI_CR_ESS
| DCMI_CR_PCKPOL
|
164 DCMI_CR_HSPOL
| DCMI_CR_VSPOL
| DCMI_CR_FCRC_0
|
165 DCMI_CR_FCRC_1
| DCMI_CR_EDM_0
| DCMI_CR_EDM_1
);
167 /* Sets the new configuration of the DCMI peripheral */
168 temp
|= ((uint32_t)DCMI_InitStruct
->DCMI_CaptureMode
|
169 DCMI_InitStruct
->DCMI_SynchroMode
|
170 DCMI_InitStruct
->DCMI_PCKPolarity
|
171 DCMI_InitStruct
->DCMI_VSPolarity
|
172 DCMI_InitStruct
->DCMI_HSPolarity
|
173 DCMI_InitStruct
->DCMI_CaptureRate
|
174 DCMI_InitStruct
->DCMI_ExtendedDataMode
);
180 * @brief Fills each DCMI_InitStruct member with its default value.
181 * @param DCMI_InitStruct : pointer to a DCMI_InitTypeDef structure which will
185 void DCMI_StructInit(DCMI_InitTypeDef
* DCMI_InitStruct
)
187 /* Set the default configuration */
188 DCMI_InitStruct
->DCMI_CaptureMode
= DCMI_CaptureMode_Continuous
;
189 DCMI_InitStruct
->DCMI_SynchroMode
= DCMI_SynchroMode_Hardware
;
190 DCMI_InitStruct
->DCMI_PCKPolarity
= DCMI_PCKPolarity_Falling
;
191 DCMI_InitStruct
->DCMI_VSPolarity
= DCMI_VSPolarity_Low
;
192 DCMI_InitStruct
->DCMI_HSPolarity
= DCMI_HSPolarity_Low
;
193 DCMI_InitStruct
->DCMI_CaptureRate
= DCMI_CaptureRate_All_Frame
;
194 DCMI_InitStruct
->DCMI_ExtendedDataMode
= DCMI_ExtendedDataMode_8b
;
198 * @brief Initializes the DCMI peripheral CROP mode according to the specified
199 * parameters in the DCMI_CROPInitStruct.
200 * @note This function should be called before to enable and start the DCMI interface.
201 * @param DCMI_CROPInitStruct: pointer to a DCMI_CROPInitTypeDef structure that
202 * contains the configuration information for the DCMI peripheral CROP mode.
205 void DCMI_CROPConfig(DCMI_CROPInitTypeDef
* DCMI_CROPInitStruct
)
207 /* Sets the CROP window coordinates */
208 DCMI
->CWSTRTR
= (uint32_t)((uint32_t)DCMI_CROPInitStruct
->DCMI_HorizontalOffsetCount
|
209 ((uint32_t)DCMI_CROPInitStruct
->DCMI_VerticalStartLine
<< 16));
211 /* Sets the CROP window size */
212 DCMI
->CWSIZER
= (uint32_t)(DCMI_CROPInitStruct
->DCMI_CaptureCount
|
213 ((uint32_t)DCMI_CROPInitStruct
->DCMI_VerticalLineCount
<< 16));
217 * @brief Enables or disables the DCMI Crop feature.
218 * @note This function should be called before to enable and start the DCMI interface.
219 * @param NewState: new state of the DCMI Crop feature.
220 * This parameter can be: ENABLE or DISABLE.
223 void DCMI_CROPCmd(FunctionalState NewState
)
225 /* Check the parameters */
226 assert_param(IS_FUNCTIONAL_STATE(NewState
));
228 if (NewState
!= DISABLE
)
230 /* Enable the DCMI Crop feature */
231 DCMI
->CR
|= (uint32_t)DCMI_CR_CROP
;
235 /* Disable the DCMI Crop feature */
236 DCMI
->CR
&= ~(uint32_t)DCMI_CR_CROP
;
241 * @brief Sets the embedded synchronization codes
242 * @param DCMI_CodesInitTypeDef: pointer to a DCMI_CodesInitTypeDef structure that
243 * contains the embedded synchronization codes for the DCMI peripheral.
246 void DCMI_SetEmbeddedSynchroCodes(DCMI_CodesInitTypeDef
* DCMI_CodesInitStruct
)
248 DCMI
->ESCR
= (uint32_t)(DCMI_CodesInitStruct
->DCMI_FrameStartCode
|
249 ((uint32_t)DCMI_CodesInitStruct
->DCMI_LineStartCode
<< 8)|
250 ((uint32_t)DCMI_CodesInitStruct
->DCMI_LineEndCode
<< 16)|
251 ((uint32_t)DCMI_CodesInitStruct
->DCMI_FrameEndCode
<< 24));
255 * @brief Enables or disables the DCMI JPEG format.
256 * @note The Crop and Embedded Synchronization features cannot be used in this mode.
257 * @param NewState: new state of the DCMI JPEG format.
258 * This parameter can be: ENABLE or DISABLE.
261 void DCMI_JPEGCmd(FunctionalState NewState
)
263 /* Check the parameters */
264 assert_param(IS_FUNCTIONAL_STATE(NewState
));
266 if (NewState
!= DISABLE
)
268 /* Enable the DCMI JPEG format */
269 DCMI
->CR
|= (uint32_t)DCMI_CR_JPEG
;
273 /* Disable the DCMI JPEG format */
274 DCMI
->CR
&= ~(uint32_t)DCMI_CR_JPEG
;
281 /** @defgroup DCMI_Group2 Image capture functions
282 * @brief Image capture functions
285 ===============================================================================
286 ##### Image capture functions #####
287 ===============================================================================
294 * @brief Enables or disables the DCMI interface.
295 * @param NewState: new state of the DCMI interface.
296 * This parameter can be: ENABLE or DISABLE.
299 void DCMI_Cmd(FunctionalState NewState
)
301 /* Check the parameters */
302 assert_param(IS_FUNCTIONAL_STATE(NewState
));
304 if (NewState
!= DISABLE
)
306 /* Enable the DCMI by setting ENABLE bit */
307 DCMI
->CR
|= (uint32_t)DCMI_CR_ENABLE
;
311 /* Disable the DCMI by clearing ENABLE bit */
312 DCMI
->CR
&= ~(uint32_t)DCMI_CR_ENABLE
;
317 * @brief Enables or disables the DCMI Capture.
318 * @param NewState: new state of the DCMI capture.
319 * This parameter can be: ENABLE or DISABLE.
322 void DCMI_CaptureCmd(FunctionalState NewState
)
324 /* Check the parameters */
325 assert_param(IS_FUNCTIONAL_STATE(NewState
));
327 if (NewState
!= DISABLE
)
329 /* Enable the DCMI Capture */
330 DCMI
->CR
|= (uint32_t)DCMI_CR_CAPTURE
;
334 /* Disable the DCMI Capture */
335 DCMI
->CR
&= ~(uint32_t)DCMI_CR_CAPTURE
;
340 * @brief Reads the data stored in the DR register.
342 * @retval Data register value
344 uint32_t DCMI_ReadData(void)
352 /** @defgroup DCMI_Group3 Interrupts and flags management functions
353 * @brief Interrupts and flags management functions
356 ===============================================================================
357 ##### Interrupts and flags management functions #####
358 ===============================================================================
365 * @brief Enables or disables the DCMI interface interrupts.
366 * @param DCMI_IT: specifies the DCMI interrupt sources to be enabled or disabled.
367 * This parameter can be any combination of the following values:
368 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
369 * @arg DCMI_IT_OVF: Overflow interrupt mask
370 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
371 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
372 * @arg DCMI_IT_LINE: Line interrupt mask
373 * @param NewState: new state of the specified DCMI interrupts.
374 * This parameter can be: ENABLE or DISABLE.
377 void DCMI_ITConfig(uint16_t DCMI_IT
, FunctionalState NewState
)
379 /* Check the parameters */
380 assert_param(IS_DCMI_CONFIG_IT(DCMI_IT
));
381 assert_param(IS_FUNCTIONAL_STATE(NewState
));
383 if (NewState
!= DISABLE
)
385 /* Enable the Interrupt sources */
386 DCMI
->IER
|= DCMI_IT
;
390 /* Disable the Interrupt sources */
391 DCMI
->IER
&= (uint16_t)(~DCMI_IT
);
396 * @brief Checks whether the DCMI interface flag is set or not.
397 * @param DCMI_FLAG: specifies the flag to check.
398 * This parameter can be one of the following values:
399 * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
400 * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
401 * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
402 * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
403 * @arg DCMI_FLAG_LINERI: Line Raw flag mask
404 * @arg DCMI_FLAG_FRAMEMI: Frame capture complete Masked flag mask
405 * @arg DCMI_FLAG_OVFMI: Overflow Masked flag mask
406 * @arg DCMI_FLAG_ERRMI: Synchronization error Masked flag mask
407 * @arg DCMI_FLAG_VSYNCMI: VSYNC Masked flag mask
408 * @arg DCMI_FLAG_LINEMI: Line Masked flag mask
409 * @arg DCMI_FLAG_HSYNC: HSYNC flag mask
410 * @arg DCMI_FLAG_VSYNC: VSYNC flag mask
411 * @arg DCMI_FLAG_FNE: Fifo not empty flag mask
412 * @retval The new state of DCMI_FLAG (SET or RESET).
414 FlagStatus
DCMI_GetFlagStatus(uint16_t DCMI_FLAG
)
416 FlagStatus bitstatus
= RESET
;
417 uint32_t dcmireg
, tempreg
= 0;
419 /* Check the parameters */
420 assert_param(IS_DCMI_GET_FLAG(DCMI_FLAG
));
422 /* Get the DCMI register index */
423 dcmireg
= (((uint16_t)DCMI_FLAG
) >> 12);
425 if (dcmireg
== 0x00) /* The FLAG is in RISR register */
429 else if (dcmireg
== 0x02) /* The FLAG is in SR register */
433 else /* The FLAG is in MISR register */
435 tempreg
= DCMI
->MISR
;
438 if ((tempreg
& DCMI_FLAG
) != (uint16_t)RESET
)
446 /* Return the DCMI_FLAG status */
451 * @brief Clears the DCMI's pending flags.
452 * @param DCMI_FLAG: specifies the flag to clear.
453 * This parameter can be any combination of the following values:
454 * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
455 * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
456 * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
457 * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
458 * @arg DCMI_FLAG_LINERI: Line Raw flag mask
461 void DCMI_ClearFlag(uint16_t DCMI_FLAG
)
463 /* Check the parameters */
464 assert_param(IS_DCMI_CLEAR_FLAG(DCMI_FLAG
));
466 /* Clear the flag by writing in the ICR register 1 in the corresponding
469 DCMI
->ICR
= DCMI_FLAG
;
473 * @brief Checks whether the DCMI interrupt has occurred or not.
474 * @param DCMI_IT: specifies the DCMI interrupt source to check.
475 * This parameter can be one of the following values:
476 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
477 * @arg DCMI_IT_OVF: Overflow interrupt mask
478 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
479 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
480 * @arg DCMI_IT_LINE: Line interrupt mask
481 * @retval The new state of DCMI_IT (SET or RESET).
483 ITStatus
DCMI_GetITStatus(uint16_t DCMI_IT
)
485 ITStatus bitstatus
= RESET
;
486 uint32_t itstatus
= 0;
488 /* Check the parameters */
489 assert_param(IS_DCMI_GET_IT(DCMI_IT
));
491 itstatus
= DCMI
->MISR
& DCMI_IT
; /* Only masked interrupts are checked */
493 if ((itstatus
!= (uint16_t)RESET
))
505 * @brief Clears the DCMI's interrupt pending bits.
506 * @param DCMI_IT: specifies the DCMI interrupt pending bit to clear.
507 * This parameter can be any combination of the following values:
508 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
509 * @arg DCMI_IT_OVF: Overflow interrupt mask
510 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
511 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
512 * @arg DCMI_IT_LINE: Line interrupt mask
515 void DCMI_ClearITPendingBit(uint16_t DCMI_IT
)
517 /* Clear the interrupt pending Bit by writing in the ICR register 1 in the
518 corresponding pending Bit position*/
538 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/