stm32f4xx_dcmi.c 18 KB

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