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
16 * ===================================================================
17 * How to use this driver
18 * ===================================================================
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.
26 * 1. Enable the clock for the DCMI and associated GPIOs using the following functions:
27 * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
28 * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
30 * 2. DCMI pins configuration
31 * - Connect the involved DCMI pins to AF13 using the following function
32 * GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_DCMI);
33 * - Configure these DCMI pins in alternate function mode by calling the function
36 * 3. Declare a DCMI_InitTypeDef structure, for example:
37 * DCMI_InitTypeDef DCMI_InitStructure;
38 * and fill the DCMI_InitStructure variable with the allowed values
39 * of the structure member.
41 * 4. Initialize the DCMI interface by calling the function
42 * DCMI_Init(&DCMI_InitStructure);
44 * 5. Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
45 * register to the destination memory buffer.
47 * 6. Enable DCMI interface using the function
50 * 7. Start the image capture using the function
51 * DCMI_CaptureCmd(ENABLE);
53 * 8. At this stage the DCMI interface waits for the first start of frame,
54 * then a DMA request is generated continuously/once (depending on the
55 * mode used, Continuous/Snapshot) to transfer the received data into
56 * the destination memory.
58 * @note If you need to capture only a rectangular window from the received
59 * image, you have to use the DCMI_CROPConfig() function to configure
60 * the coordinates and size of the window to be captured, then enable
61 * the Crop feature using DCMI_CROPCmd(ENABLE);
62 * In this case, the Crop configuration should be made before to enable
63 * and start the DCMI interface.
67 ******************************************************************************
70 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
71 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
72 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
73 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
74 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
75 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
77 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
78 ******************************************************************************
81 /* Includes ------------------------------------------------------------------*/
82 #include "stm32f4xx_dcmi.h"
83 #include "stm32f4xx_rcc.h"
85 /** @addtogroup STM32F4xx_StdPeriph_Driver
90 * @brief DCMI driver modules
94 /* Private typedef -----------------------------------------------------------*/
95 /* Private define ------------------------------------------------------------*/
96 /* Private macro -------------------------------------------------------------*/
97 /* Private variables ---------------------------------------------------------*/
98 /* Private function prototypes -----------------------------------------------*/
99 /* Private functions ---------------------------------------------------------*/
101 /** @defgroup DCMI_Private_Functions
105 /** @defgroup DCMI_Group1 Initialization and Configuration functions
106 * @brief Initialization and Configuration functions
109 ===============================================================================
110 Initialization and Configuration functions
111 ===============================================================================
118 * @brief Deinitializes the DCMI registers to their default reset values.
122 void DCMI_DeInit(void)
134 * @brief Initializes the DCMI according to the specified parameters in the DCMI_InitStruct.
135 * @param DCMI_InitStruct: pointer to a DCMI_InitTypeDef structure that contains
136 * the configuration information for the DCMI.
139 void DCMI_Init(DCMI_InitTypeDef* DCMI_InitStruct)
143 /* Check the parameters */
144 assert_param(IS_DCMI_CAPTURE_MODE(DCMI_InitStruct->DCMI_CaptureMode));
145 assert_param(IS_DCMI_SYNCHRO(DCMI_InitStruct->DCMI_SynchroMode));
146 assert_param(IS_DCMI_PCKPOLARITY(DCMI_InitStruct->DCMI_PCKPolarity));
147 assert_param(IS_DCMI_VSPOLARITY(DCMI_InitStruct->DCMI_VSPolarity));
148 assert_param(IS_DCMI_HSPOLARITY(DCMI_InitStruct->DCMI_HSPolarity));
149 assert_param(IS_DCMI_CAPTURE_RATE(DCMI_InitStruct->DCMI_CaptureRate));
150 assert_param(IS_DCMI_EXTENDED_DATA(DCMI_InitStruct->DCMI_ExtendedDataMode));
152 /* The DCMI configuration registers should be programmed correctly before
153 enabling the CR_ENABLE Bit and the CR_CAPTURE Bit */
154 DCMI->CR &= ~(DCMI_CR_ENABLE | DCMI_CR_CAPTURE);
156 /* Reset the old DCMI configuration */
159 temp &= ~((uint32_t)DCMI_CR_CM | DCMI_CR_ESS | DCMI_CR_PCKPOL |
160 DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_FCRC_0 |
161 DCMI_CR_FCRC_1 | DCMI_CR_EDM_0 | DCMI_CR_EDM_1);
163 /* Sets the new configuration of the DCMI peripheral */
164 temp |= ((uint32_t)DCMI_InitStruct->DCMI_CaptureMode |
165 DCMI_InitStruct->DCMI_SynchroMode |
166 DCMI_InitStruct->DCMI_PCKPolarity |
167 DCMI_InitStruct->DCMI_VSPolarity |
168 DCMI_InitStruct->DCMI_HSPolarity |
169 DCMI_InitStruct->DCMI_CaptureRate |
170 DCMI_InitStruct->DCMI_ExtendedDataMode);
176 * @brief Fills each DCMI_InitStruct member with its default value.
177 * @param DCMI_InitStruct : pointer to a DCMI_InitTypeDef structure which will
181 void DCMI_StructInit(DCMI_InitTypeDef* DCMI_InitStruct)
183 /* Set the default configuration */
184 DCMI_InitStruct->DCMI_CaptureMode = DCMI_CaptureMode_Continuous;
185 DCMI_InitStruct->DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
186 DCMI_InitStruct->DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
187 DCMI_InitStruct->DCMI_VSPolarity = DCMI_VSPolarity_Low;
188 DCMI_InitStruct->DCMI_HSPolarity = DCMI_HSPolarity_Low;
189 DCMI_InitStruct->DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
190 DCMI_InitStruct->DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
194 * @brief Initializes the DCMI peripheral CROP mode according to the specified
195 * parameters in the DCMI_CROPInitStruct.
196 * @note This function should be called before to enable and start the DCMI interface.
197 * @param DCMI_CROPInitStruct: pointer to a DCMI_CROPInitTypeDef structure that
198 * contains the configuration information for the DCMI peripheral CROP mode.
201 void DCMI_CROPConfig(DCMI_CROPInitTypeDef* DCMI_CROPInitStruct)
203 /* Sets the CROP window coordinates */
204 DCMI->CWSTRTR = (uint32_t)((uint32_t)DCMI_CROPInitStruct->DCMI_HorizontalOffsetCount |
205 ((uint32_t)DCMI_CROPInitStruct->DCMI_VerticalStartLine << 16));
207 /* Sets the CROP window size */
208 DCMI->CWSIZER = (uint32_t)(DCMI_CROPInitStruct->DCMI_CaptureCount |
209 ((uint32_t)DCMI_CROPInitStruct->DCMI_VerticalLineCount << 16));
213 * @brief Enables or disables the DCMI Crop feature.
214 * @note This function should be called before to enable and start the DCMI interface.
215 * @param NewState: new state of the DCMI Crop feature.
216 * This parameter can be: ENABLE or DISABLE.
219 void DCMI_CROPCmd(FunctionalState NewState)
221 /* Check the parameters */
222 assert_param(IS_FUNCTIONAL_STATE(NewState));
224 if (NewState != DISABLE)
226 /* Enable the DCMI Crop feature */
227 DCMI->CR |= (uint32_t)DCMI_CR_CROP;
231 /* Disable the DCMI Crop feature */
232 DCMI->CR &= ~(uint32_t)DCMI_CR_CROP;
237 * @brief Sets the embedded synchronization codes
238 * @param DCMI_CodesInitTypeDef: pointer to a DCMI_CodesInitTypeDef structure that
239 * contains the embedded synchronization codes for the DCMI peripheral.
242 void DCMI_SetEmbeddedSynchroCodes(DCMI_CodesInitTypeDef* DCMI_CodesInitStruct)
244 DCMI->ESCR = (uint32_t)(DCMI_CodesInitStruct->DCMI_FrameStartCode |
245 ((uint32_t)DCMI_CodesInitStruct->DCMI_LineStartCode << 8)|
246 ((uint32_t)DCMI_CodesInitStruct->DCMI_LineEndCode << 16)|
247 ((uint32_t)DCMI_CodesInitStruct->DCMI_FrameEndCode << 24));
251 * @brief Enables or disables the DCMI JPEG format.
252 * @note The Crop and Embedded Synchronization features cannot be used in this mode.
253 * @param NewState: new state of the DCMI JPEG format.
254 * This parameter can be: ENABLE or DISABLE.
257 void DCMI_JPEGCmd(FunctionalState NewState)
259 /* Check the parameters */
260 assert_param(IS_FUNCTIONAL_STATE(NewState));
262 if (NewState != DISABLE)
264 /* Enable the DCMI JPEG format */
265 DCMI->CR |= (uint32_t)DCMI_CR_JPEG;
269 /* Disable the DCMI JPEG format */
270 DCMI->CR &= ~(uint32_t)DCMI_CR_JPEG;
277 /** @defgroup DCMI_Group2 Image capture functions
278 * @brief Image capture functions
281 ===============================================================================
282 Image capture functions
283 ===============================================================================
290 * @brief Enables or disables the DCMI interface.
291 * @param NewState: new state of the DCMI interface.
292 * This parameter can be: ENABLE or DISABLE.
295 void DCMI_Cmd(FunctionalState NewState)
297 /* Check the parameters */
298 assert_param(IS_FUNCTIONAL_STATE(NewState));
300 if (NewState != DISABLE)
302 /* Enable the DCMI by setting ENABLE bit */
303 DCMI->CR |= (uint32_t)DCMI_CR_ENABLE;
307 /* Disable the DCMI by clearing ENABLE bit */
308 DCMI->CR &= ~(uint32_t)DCMI_CR_ENABLE;
313 * @brief Enables or disables the DCMI Capture.
314 * @param NewState: new state of the DCMI capture.
315 * This parameter can be: ENABLE or DISABLE.
318 void DCMI_CaptureCmd(FunctionalState NewState)
320 /* Check the parameters */
321 assert_param(IS_FUNCTIONAL_STATE(NewState));
323 if (NewState != DISABLE)
325 /* Enable the DCMI Capture */
326 DCMI->CR |= (uint32_t)DCMI_CR_CAPTURE;
330 /* Disable the DCMI Capture */
331 DCMI->CR &= ~(uint32_t)DCMI_CR_CAPTURE;
336 * @brief Reads the data stored in the DR register.
338 * @retval Data register value
340 uint32_t DCMI_ReadData(void)
348 /** @defgroup DCMI_Group3 Interrupts and flags management functions
349 * @brief Interrupts and flags management functions
352 ===============================================================================
353 Interrupts and flags management functions
354 ===============================================================================
361 * @brief Enables or disables the DCMI interface interrupts.
362 * @param DCMI_IT: specifies the DCMI interrupt sources to be enabled or disabled.
363 * This parameter can be any combination of the following values:
364 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
365 * @arg DCMI_IT_OVF: Overflow interrupt mask
366 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
367 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
368 * @arg DCMI_IT_LINE: Line interrupt mask
369 * @param NewState: new state of the specified DCMI interrupts.
370 * This parameter can be: ENABLE or DISABLE.
373 void DCMI_ITConfig(uint16_t DCMI_IT, FunctionalState NewState)
375 /* Check the parameters */
376 assert_param(IS_DCMI_CONFIG_IT(DCMI_IT));
377 assert_param(IS_FUNCTIONAL_STATE(NewState));
379 if (NewState != DISABLE)
381 /* Enable the Interrupt sources */
382 DCMI->IER |= DCMI_IT;
386 /* Disable the Interrupt sources */
387 DCMI->IER &= (uint16_t)(~DCMI_IT);
392 * @brief Checks whether the DCMI interface flag is set or not.
393 * @param DCMI_FLAG: specifies the flag to check.
394 * This parameter can be one of the following values:
395 * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
396 * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
397 * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
398 * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
399 * @arg DCMI_FLAG_LINERI: Line Raw flag mask
400 * @arg DCMI_FLAG_FRAMEMI: Frame capture complete Masked flag mask
401 * @arg DCMI_FLAG_OVFMI: Overflow Masked flag mask
402 * @arg DCMI_FLAG_ERRMI: Synchronization error Masked flag mask
403 * @arg DCMI_FLAG_VSYNCMI: VSYNC Masked flag mask
404 * @arg DCMI_FLAG_LINEMI: Line Masked flag mask
405 * @arg DCMI_FLAG_HSYNC: HSYNC flag mask
406 * @arg DCMI_FLAG_VSYNC: VSYNC flag mask
407 * @arg DCMI_FLAG_FNE: Fifo not empty flag mask
408 * @retval The new state of DCMI_FLAG (SET or RESET).
410 FlagStatus DCMI_GetFlagStatus(uint16_t DCMI_FLAG)
412 FlagStatus bitstatus = RESET;
413 uint32_t dcmireg, tempreg = 0;
415 /* Check the parameters */
416 assert_param(IS_DCMI_GET_FLAG(DCMI_FLAG));
418 /* Get the DCMI register index */
419 dcmireg = (((uint16_t)DCMI_FLAG) >> 12);
421 if (dcmireg == 0x01) /* The FLAG is in RISR register */
425 else if (dcmireg == 0x02) /* The FLAG is in SR register */
429 else /* The FLAG is in MISR register */
431 tempreg = DCMI->MISR;
434 if ((tempreg & DCMI_FLAG) != (uint16_t)RESET )
442 /* Return the DCMI_FLAG status */
447 * @brief Clears the DCMI's pending flags.
448 * @param DCMI_FLAG: specifies the flag to clear.
449 * This parameter can be any combination of the following values:
450 * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
451 * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
452 * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
453 * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
454 * @arg DCMI_FLAG_LINERI: Line Raw flag mask
457 void DCMI_ClearFlag(uint16_t DCMI_FLAG)
459 /* Check the parameters */
460 assert_param(IS_DCMI_CLEAR_FLAG(DCMI_FLAG));
462 /* Clear the flag by writing in the ICR register 1 in the corresponding
465 DCMI->ICR = DCMI_FLAG;
469 * @brief Checks whether the DCMI interrupt has occurred or not.
470 * @param DCMI_IT: specifies the DCMI interrupt source to check.
471 * This parameter can be one of the following values:
472 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
473 * @arg DCMI_IT_OVF: Overflow interrupt mask
474 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
475 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
476 * @arg DCMI_IT_LINE: Line interrupt mask
477 * @retval The new state of DCMI_IT (SET or RESET).
479 ITStatus DCMI_GetITStatus(uint16_t DCMI_IT)
481 ITStatus bitstatus = RESET;
482 uint32_t itstatus = 0;
484 /* Check the parameters */
485 assert_param(IS_DCMI_GET_IT(DCMI_IT));
487 itstatus = DCMI->MISR & DCMI_IT; /* Only masked interrupts are checked */
489 if ((itstatus != (uint16_t)RESET))
501 * @brief Clears the DCMI's interrupt pending bits.
502 * @param DCMI_IT: specifies the DCMI interrupt pending bit to clear.
503 * This parameter can be any combination of the following values:
504 * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
505 * @arg DCMI_IT_OVF: Overflow interrupt mask
506 * @arg DCMI_IT_ERR: Synchronization error interrupt mask
507 * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
508 * @arg DCMI_IT_LINE: Line interrupt mask
511 void DCMI_ClearITPendingBit(uint16_t DCMI_IT)
513 /* Clear the interrupt pending Bit by writing in the ICR register 1 in the
514 corresponding pending Bit position*/
534 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/