stm32f4xx_dcmi.c 17 KB

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