stm32f4xx_dcmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_dcmi.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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
  12. *
  13. * @verbatim
  14. *
  15. *
  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. * 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);
  29. *
  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
  34. * GPIO_Init();
  35. *
  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.
  40. *
  41. * 4. Initialize the DCMI interface by calling the function
  42. * DCMI_Init(&DCMI_InitStructure);
  43. *
  44. * 5. Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
  45. * register to the destination memory buffer.
  46. *
  47. * 6. Enable DCMI interface using the function
  48. * DCMI_Cmd(ENABLE);
  49. *
  50. * 7. Start the image capture using the function
  51. * DCMI_CaptureCmd(ENABLE);
  52. *
  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.
  57. *
  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.
  64. *
  65. * @endverbatim
  66. *
  67. ******************************************************************************
  68. * @attention
  69. *
  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.
  76. *
  77. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  78. ******************************************************************************
  79. */
  80. /* Includes ------------------------------------------------------------------*/
  81. #include "stm32f4xx_dcmi.h"
  82. #include "stm32f4xx_rcc.h"
  83. /** @addtogroup STM32F4xx_StdPeriph_Driver
  84. * @{
  85. */
  86. /** @defgroup DCMI
  87. * @brief DCMI driver modules
  88. * @{
  89. */
  90. /* Private typedef -----------------------------------------------------------*/
  91. /* Private define ------------------------------------------------------------*/
  92. /* Private macro -------------------------------------------------------------*/
  93. /* Private variables ---------------------------------------------------------*/
  94. /* Private function prototypes -----------------------------------------------*/
  95. /* Private functions ---------------------------------------------------------*/
  96. /** @defgroup DCMI_Private_Functions
  97. * @{
  98. */
  99. /** @defgroup DCMI_Group1 Initialization and Configuration functions
  100. * @brief Initialization and Configuration functions
  101. *
  102. @verbatim
  103. ===============================================================================
  104. Initialization and Configuration functions
  105. ===============================================================================
  106. @endverbatim
  107. * @{
  108. */
  109. /**
  110. * @brief Deinitializes the DCMI registers to their default reset values.
  111. * @param None
  112. * @retval None
  113. */
  114. void DCMI_DeInit(void)
  115. {
  116. DCMI->CR = 0x0;
  117. DCMI->IER = 0x0;
  118. DCMI->ICR = 0x1F;
  119. DCMI->ESCR = 0x0;
  120. DCMI->ESUR = 0x0;
  121. DCMI->CWSTRTR = 0x0;
  122. DCMI->CWSIZER = 0x0;
  123. }
  124. /**
  125. * @brief Initializes the DCMI according to the specified parameters in the DCMI_InitStruct.
  126. * @param DCMI_InitStruct: pointer to a DCMI_InitTypeDef structure that contains
  127. * the configuration information for the DCMI.
  128. * @retval None
  129. */
  130. void DCMI_Init(DCMI_InitTypeDef* DCMI_InitStruct)
  131. {
  132. uint32_t temp = 0x0;
  133. /* Check the parameters */
  134. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_InitStruct->DCMI_CaptureMode));
  135. assert_param(IS_DCMI_SYNCHRO(DCMI_InitStruct->DCMI_SynchroMode));
  136. assert_param(IS_DCMI_PCKPOLARITY(DCMI_InitStruct->DCMI_PCKPolarity));
  137. assert_param(IS_DCMI_VSPOLARITY(DCMI_InitStruct->DCMI_VSPolarity));
  138. assert_param(IS_DCMI_HSPOLARITY(DCMI_InitStruct->DCMI_HSPolarity));
  139. assert_param(IS_DCMI_CAPTURE_RATE(DCMI_InitStruct->DCMI_CaptureRate));
  140. assert_param(IS_DCMI_EXTENDED_DATA(DCMI_InitStruct->DCMI_ExtendedDataMode));
  141. /* The DCMI configuration registers should be programmed correctly before
  142. enabling the CR_ENABLE Bit and the CR_CAPTURE Bit */
  143. DCMI->CR &= ~(DCMI_CR_ENABLE | DCMI_CR_CAPTURE);
  144. /* Reset the old DCMI configuration */
  145. temp = DCMI->CR;
  146. temp &= ~((uint32_t)DCMI_CR_CM | DCMI_CR_ESS | DCMI_CR_PCKPOL |
  147. DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_FCRC_0 |
  148. DCMI_CR_FCRC_1 | DCMI_CR_EDM_0 | DCMI_CR_EDM_1);
  149. /* Sets the new configuration of the DCMI peripheral */
  150. temp |= ((uint32_t)DCMI_InitStruct->DCMI_CaptureMode |
  151. DCMI_InitStruct->DCMI_SynchroMode |
  152. DCMI_InitStruct->DCMI_PCKPolarity |
  153. DCMI_InitStruct->DCMI_VSPolarity |
  154. DCMI_InitStruct->DCMI_HSPolarity |
  155. DCMI_InitStruct->DCMI_CaptureRate |
  156. DCMI_InitStruct->DCMI_ExtendedDataMode);
  157. DCMI->CR = temp;
  158. }
  159. /**
  160. * @brief Fills each DCMI_InitStruct member with its default value.
  161. * @param DCMI_InitStruct : pointer to a DCMI_InitTypeDef structure which will
  162. * be initialized.
  163. * @retval None
  164. */
  165. void DCMI_StructInit(DCMI_InitTypeDef* DCMI_InitStruct)
  166. {
  167. /* Set the default configuration */
  168. DCMI_InitStruct->DCMI_CaptureMode = DCMI_CaptureMode_Continuous;
  169. DCMI_InitStruct->DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
  170. DCMI_InitStruct->DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
  171. DCMI_InitStruct->DCMI_VSPolarity = DCMI_VSPolarity_Low;
  172. DCMI_InitStruct->DCMI_HSPolarity = DCMI_HSPolarity_Low;
  173. DCMI_InitStruct->DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
  174. DCMI_InitStruct->DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
  175. }
  176. /**
  177. * @brief Initializes the DCMI peripheral CROP mode according to the specified
  178. * parameters in the DCMI_CROPInitStruct.
  179. * @note This function should be called before to enable and start the DCMI interface.
  180. * @param DCMI_CROPInitStruct: pointer to a DCMI_CROPInitTypeDef structure that
  181. * contains the configuration information for the DCMI peripheral CROP mode.
  182. * @retval None
  183. */
  184. void DCMI_CROPConfig(DCMI_CROPInitTypeDef* DCMI_CROPInitStruct)
  185. {
  186. /* Sets the CROP window coordinates */
  187. DCMI->CWSTRTR = (uint32_t)((uint32_t)DCMI_CROPInitStruct->DCMI_HorizontalOffsetCount |
  188. ((uint32_t)DCMI_CROPInitStruct->DCMI_VerticalStartLine << 16));
  189. /* Sets the CROP window size */
  190. DCMI->CWSIZER = (uint32_t)(DCMI_CROPInitStruct->DCMI_CaptureCount |
  191. ((uint32_t)DCMI_CROPInitStruct->DCMI_VerticalLineCount << 16));
  192. }
  193. /**
  194. * @brief Enables or disables the DCMI Crop feature.
  195. * @note This function should be called before to enable and start the DCMI interface.
  196. * @param NewState: new state of the DCMI Crop feature.
  197. * This parameter can be: ENABLE or DISABLE.
  198. * @retval None
  199. */
  200. void DCMI_CROPCmd(FunctionalState NewState)
  201. {
  202. /* Check the parameters */
  203. assert_param(IS_FUNCTIONAL_STATE(NewState));
  204. if (NewState != DISABLE)
  205. {
  206. /* Enable the DCMI Crop feature */
  207. DCMI->CR |= (uint32_t)DCMI_CR_CROP;
  208. }
  209. else
  210. {
  211. /* Disable the DCMI Crop feature */
  212. DCMI->CR &= ~(uint32_t)DCMI_CR_CROP;
  213. }
  214. }
  215. /**
  216. * @brief Sets the embedded synchronization codes
  217. * @param DCMI_CodesInitTypeDef: pointer to a DCMI_CodesInitTypeDef structure that
  218. * contains the embedded synchronization codes for the DCMI peripheral.
  219. * @retval None
  220. */
  221. void DCMI_SetEmbeddedSynchroCodes(DCMI_CodesInitTypeDef* DCMI_CodesInitStruct)
  222. {
  223. DCMI->ESCR = (uint32_t)(DCMI_CodesInitStruct->DCMI_FrameStartCode |
  224. ((uint32_t)DCMI_CodesInitStruct->DCMI_LineStartCode << 8)|
  225. ((uint32_t)DCMI_CodesInitStruct->DCMI_LineEndCode << 16)|
  226. ((uint32_t)DCMI_CodesInitStruct->DCMI_FrameEndCode << 24));
  227. }
  228. /**
  229. * @brief Enables or disables the DCMI JPEG format.
  230. * @note The Crop and Embedded Synchronization features cannot be used in this mode.
  231. * @param NewState: new state of the DCMI JPEG format.
  232. * This parameter can be: ENABLE or DISABLE.
  233. * @retval None
  234. */
  235. void DCMI_JPEGCmd(FunctionalState NewState)
  236. {
  237. /* Check the parameters */
  238. assert_param(IS_FUNCTIONAL_STATE(NewState));
  239. if (NewState != DISABLE)
  240. {
  241. /* Enable the DCMI JPEG format */
  242. DCMI->CR |= (uint32_t)DCMI_CR_JPEG;
  243. }
  244. else
  245. {
  246. /* Disable the DCMI JPEG format */
  247. DCMI->CR &= ~(uint32_t)DCMI_CR_JPEG;
  248. }
  249. }
  250. /**
  251. * @}
  252. */
  253. /** @defgroup DCMI_Group2 Image capture functions
  254. * @brief Image capture functions
  255. *
  256. @verbatim
  257. ===============================================================================
  258. Image capture functions
  259. ===============================================================================
  260. @endverbatim
  261. * @{
  262. */
  263. /**
  264. * @brief Enables or disables the DCMI interface.
  265. * @param NewState: new state of the DCMI interface.
  266. * This parameter can be: ENABLE or DISABLE.
  267. * @retval None
  268. */
  269. void DCMI_Cmd(FunctionalState NewState)
  270. {
  271. /* Check the parameters */
  272. assert_param(IS_FUNCTIONAL_STATE(NewState));
  273. if (NewState != DISABLE)
  274. {
  275. /* Enable the DCMI by setting ENABLE bit */
  276. DCMI->CR |= (uint32_t)DCMI_CR_ENABLE;
  277. }
  278. else
  279. {
  280. /* Disable the DCMI by clearing ENABLE bit */
  281. DCMI->CR &= ~(uint32_t)DCMI_CR_ENABLE;
  282. }
  283. }
  284. /**
  285. * @brief Enables or disables the DCMI Capture.
  286. * @param NewState: new state of the DCMI capture.
  287. * This parameter can be: ENABLE or DISABLE.
  288. * @retval None
  289. */
  290. void DCMI_CaptureCmd(FunctionalState NewState)
  291. {
  292. /* Check the parameters */
  293. assert_param(IS_FUNCTIONAL_STATE(NewState));
  294. if (NewState != DISABLE)
  295. {
  296. /* Enable the DCMI Capture */
  297. DCMI->CR |= (uint32_t)DCMI_CR_CAPTURE;
  298. }
  299. else
  300. {
  301. /* Disable the DCMI Capture */
  302. DCMI->CR &= ~(uint32_t)DCMI_CR_CAPTURE;
  303. }
  304. }
  305. /**
  306. * @brief Reads the data stored in the DR register.
  307. * @param None
  308. * @retval Data register value
  309. */
  310. uint32_t DCMI_ReadData(void)
  311. {
  312. return DCMI->DR;
  313. }
  314. /**
  315. * @}
  316. */
  317. /** @defgroup DCMI_Group3 Interrupts and flags management functions
  318. * @brief Interrupts and flags management functions
  319. *
  320. @verbatim
  321. ===============================================================================
  322. Interrupts and flags management functions
  323. ===============================================================================
  324. @endverbatim
  325. * @{
  326. */
  327. /**
  328. * @brief Enables or disables the DCMI interface interrupts.
  329. * @param DCMI_IT: specifies the DCMI interrupt sources to be enabled or disabled.
  330. * This parameter can be any combination of the following values:
  331. * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
  332. * @arg DCMI_IT_OVF: Overflow interrupt mask
  333. * @arg DCMI_IT_ERR: Synchronization error interrupt mask
  334. * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
  335. * @arg DCMI_IT_LINE: Line interrupt mask
  336. * @param NewState: new state of the specified DCMI interrupts.
  337. * This parameter can be: ENABLE or DISABLE.
  338. * @retval None
  339. */
  340. void DCMI_ITConfig(uint16_t DCMI_IT, FunctionalState NewState)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_DCMI_CONFIG_IT(DCMI_IT));
  344. assert_param(IS_FUNCTIONAL_STATE(NewState));
  345. if (NewState != DISABLE)
  346. {
  347. /* Enable the Interrupt sources */
  348. DCMI->IER |= DCMI_IT;
  349. }
  350. else
  351. {
  352. /* Disable the Interrupt sources */
  353. DCMI->IER &= (uint16_t)(~DCMI_IT);
  354. }
  355. }
  356. /**
  357. * @brief Checks whether the DCMI interface flag is set or not.
  358. * @param DCMI_FLAG: specifies the flag to check.
  359. * This parameter can be one of the following values:
  360. * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
  361. * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
  362. * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
  363. * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
  364. * @arg DCMI_FLAG_LINERI: Line Raw flag mask
  365. * @arg DCMI_FLAG_FRAMEMI: Frame capture complete Masked flag mask
  366. * @arg DCMI_FLAG_OVFMI: Overflow Masked flag mask
  367. * @arg DCMI_FLAG_ERRMI: Synchronization error Masked flag mask
  368. * @arg DCMI_FLAG_VSYNCMI: VSYNC Masked flag mask
  369. * @arg DCMI_FLAG_LINEMI: Line Masked flag mask
  370. * @arg DCMI_FLAG_HSYNC: HSYNC flag mask
  371. * @arg DCMI_FLAG_VSYNC: VSYNC flag mask
  372. * @arg DCMI_FLAG_FNE: Fifo not empty flag mask
  373. * @retval The new state of DCMI_FLAG (SET or RESET).
  374. */
  375. FlagStatus DCMI_GetFlagStatus(uint16_t DCMI_FLAG)
  376. {
  377. FlagStatus bitstatus = RESET;
  378. uint32_t dcmireg, tempreg = 0;
  379. /* Check the parameters */
  380. assert_param(IS_DCMI_GET_FLAG(DCMI_FLAG));
  381. /* Get the DCMI register index */
  382. dcmireg = (((uint16_t)DCMI_FLAG) >> 12);
  383. if (dcmireg == 0x01) /* The FLAG is in RISR register */
  384. {
  385. tempreg= DCMI->RISR;
  386. }
  387. else if (dcmireg == 0x02) /* The FLAG is in SR register */
  388. {
  389. tempreg = DCMI->SR;
  390. }
  391. else /* The FLAG is in MISR register */
  392. {
  393. tempreg = DCMI->MISR;
  394. }
  395. if ((tempreg & DCMI_FLAG) != (uint16_t)RESET )
  396. {
  397. bitstatus = SET;
  398. }
  399. else
  400. {
  401. bitstatus = RESET;
  402. }
  403. /* Return the DCMI_FLAG status */
  404. return bitstatus;
  405. }
  406. /**
  407. * @brief Clears the DCMI's pending flags.
  408. * @param DCMI_FLAG: specifies the flag to clear.
  409. * This parameter can be any combination of the following values:
  410. * @arg DCMI_FLAG_FRAMERI: Frame capture complete Raw flag mask
  411. * @arg DCMI_FLAG_OVFRI: Overflow Raw flag mask
  412. * @arg DCMI_FLAG_ERRRI: Synchronization error Raw flag mask
  413. * @arg DCMI_FLAG_VSYNCRI: VSYNC Raw flag mask
  414. * @arg DCMI_FLAG_LINERI: Line Raw flag mask
  415. * @retval None
  416. */
  417. void DCMI_ClearFlag(uint16_t DCMI_FLAG)
  418. {
  419. /* Check the parameters */
  420. assert_param(IS_DCMI_CLEAR_FLAG(DCMI_FLAG));
  421. /* Clear the flag by writing in the ICR register 1 in the corresponding
  422. Flag position*/
  423. DCMI->ICR = DCMI_FLAG;
  424. }
  425. /**
  426. * @brief Checks whether the DCMI interrupt has occurred or not.
  427. * @param DCMI_IT: specifies the DCMI interrupt source to check.
  428. * This parameter can be one of the following values:
  429. * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
  430. * @arg DCMI_IT_OVF: Overflow interrupt mask
  431. * @arg DCMI_IT_ERR: Synchronization error interrupt mask
  432. * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
  433. * @arg DCMI_IT_LINE: Line interrupt mask
  434. * @retval The new state of DCMI_IT (SET or RESET).
  435. */
  436. ITStatus DCMI_GetITStatus(uint16_t DCMI_IT)
  437. {
  438. ITStatus bitstatus = RESET;
  439. uint32_t itstatus = 0;
  440. /* Check the parameters */
  441. assert_param(IS_DCMI_GET_IT(DCMI_IT));
  442. itstatus = DCMI->MISR & DCMI_IT; /* Only masked interrupts are checked */
  443. if ((itstatus != (uint16_t)RESET))
  444. {
  445. bitstatus = SET;
  446. }
  447. else
  448. {
  449. bitstatus = RESET;
  450. }
  451. return bitstatus;
  452. }
  453. /**
  454. * @brief Clears the DCMI's interrupt pending bits.
  455. * @param DCMI_IT: specifies the DCMI interrupt pending bit to clear.
  456. * This parameter can be any combination of the following values:
  457. * @arg DCMI_IT_FRAME: Frame capture complete interrupt mask
  458. * @arg DCMI_IT_OVF: Overflow interrupt mask
  459. * @arg DCMI_IT_ERR: Synchronization error interrupt mask
  460. * @arg DCMI_IT_VSYNC: VSYNC interrupt mask
  461. * @arg DCMI_IT_LINE: Line interrupt mask
  462. * @retval None
  463. */
  464. void DCMI_ClearITPendingBit(uint16_t DCMI_IT)
  465. {
  466. /* Clear the interrupt pending Bit by writing in the ICR register 1 in the
  467. corresponding pending Bit position*/
  468. DCMI->ICR = DCMI_IT;
  469. }
  470. /**
  471. * @}
  472. */
  473. /**
  474. * @}
  475. */
  476. /**
  477. * @}
  478. */
  479. /**
  480. * @}
  481. */
  482. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/