stm32f4xx_dcmi.c 18 KB

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