stm32f4xx_hal_dcmi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dcmi.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  7. * @brief DCMI HAL module driver
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Digital Camera Interface (DCMI) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State and Error functions
  14. *
  15. @verbatim
  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. (#) Program the required configuration through following parameters:
  26. horizontal and vertical polarity, pixel clock polarity, Capture Rate,
  27. Synchronization Mode, code of the frame delimiter and data width
  28. using HAL_DCMI_Init() function.
  29. (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
  30. register to the destination memory buffer.
  31. (#) Program the required configuration through following parameters:
  32. DCMI mode, destination memory Buffer address and the data length
  33. and enable capture using HAL_DCMI_Start_DMA() function.
  34. (#) Optionally, configure and Enable the CROP feature to select a rectangular
  35. window from the received image using HAL_DCMI_ConfigCrop()
  36. and HAL_DCMI_EnableCROP() functions
  37. (#) The capture can be stopped using HAL_DCMI_Stop() function.
  38. (#) To control DCMI state you can use the function HAL_DCMI_GetState().
  39. *** DCMI HAL driver macros list ***
  40. =============================================
  41. [..]
  42. Below the list of most used macros in DCMI HAL driver.
  43. (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
  44. (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
  45. (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
  46. (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
  47. (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
  48. (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
  49. (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred or not.
  50. [..]
  51. (@) You can refer to the DCMI HAL driver header file for more useful macros
  52. @endverbatim
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  57. *
  58. * Redistribution and use in source and binary forms, with or without modification,
  59. * are permitted provided that the following conditions are met:
  60. * 1. Redistributions of source code must retain the above copyright notice,
  61. * this list of conditions and the following disclaimer.
  62. * 2. Redistributions in binary form must reproduce the above copyright notice,
  63. * this list of conditions and the following disclaimer in the documentation
  64. * and/or other materials provided with the distribution.
  65. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  66. * may be used to endorse or promote products derived from this software
  67. * without specific prior written permission.
  68. *
  69. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  70. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  71. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  72. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  73. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  74. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  75. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  77. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  78. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32f4xx_hal.h"
  84. /** @addtogroup STM32F4xx_HAL_Driver
  85. * @{
  86. */
  87. /** @defgroup DCMI DCMI
  88. * @brief DCMI HAL module driver
  89. * @{
  90. */
  91. #ifdef HAL_DCMI_MODULE_ENABLED
  92. #if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) ||\
  93. defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) ||\
  94. defined(STM32F479xx)
  95. /* Private typedef -----------------------------------------------------------*/
  96. /* Private define ------------------------------------------------------------*/
  97. #define HAL_TIMEOUT_DCMI_STOP ((uint32_t)1000) /* 1s */
  98. /* Private macro -------------------------------------------------------------*/
  99. /* Private variables ---------------------------------------------------------*/
  100. /* Private function prototypes -----------------------------------------------*/
  101. static void DCMI_DMAConvCplt(DMA_HandleTypeDef *hdma);
  102. static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
  103. /* Exported functions --------------------------------------------------------*/
  104. /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
  105. * @{
  106. */
  107. /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
  108. * @brief Initialization and Configuration functions
  109. *
  110. @verbatim
  111. ===============================================================================
  112. ##### Initialization and Configuration functions #####
  113. ===============================================================================
  114. [..] This section provides functions allowing to:
  115. (+) Initialize and configure the DCMI
  116. (+) De-initialize the DCMI
  117. @endverbatim
  118. * @{
  119. */
  120. /**
  121. * @brief Initializes the DCMI according to the specified
  122. * parameters in the DCMI_InitTypeDef and create the associated handle.
  123. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  124. * the configuration information for DCMI.
  125. * @retval HAL status
  126. */
  127. __weak HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
  128. {
  129. /* Check the DCMI peripheral state */
  130. if(hdcmi == NULL)
  131. {
  132. return HAL_ERROR;
  133. }
  134. /* Check function parameters */
  135. assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
  136. assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
  137. assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
  138. assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
  139. assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
  140. assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
  141. assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
  142. assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
  143. if(hdcmi->State == HAL_DCMI_STATE_RESET)
  144. {
  145. /* Allocate lock resource and initialize it */
  146. hdcmi->Lock = HAL_UNLOCKED;
  147. /* Init the low level hardware */
  148. HAL_DCMI_MspInit(hdcmi);
  149. }
  150. /* Change the DCMI state */
  151. hdcmi->State = HAL_DCMI_STATE_BUSY;
  152. /* Configures the HS, VS, DE and PC polarity */
  153. hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |
  154. DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |
  155. DCMI_CR_ESS);
  156. hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate | \
  157. hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity | \
  158. hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode | \
  159. hdcmi->Init.JPEGMode);
  160. if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
  161. {
  162. DCMI->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode) |
  163. ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << 8)|
  164. ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << 16) |
  165. ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << 24));
  166. }
  167. /* Enable the Line interrupt */
  168. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE);
  169. /* Enable the VSYNC interrupt */
  170. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_VSYNC);
  171. /* Enable the Frame capture complete interrupt */
  172. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
  173. /* Enable the Synchronization error interrupt */
  174. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_ERR);
  175. /* Enable the Overflow interrupt */
  176. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_OVF);
  177. /* Enable DCMI by setting DCMIEN bit */
  178. __HAL_DCMI_ENABLE(hdcmi);
  179. /* Update error code */
  180. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  181. /* Initialize the DCMI state*/
  182. hdcmi->State = HAL_DCMI_STATE_READY;
  183. return HAL_OK;
  184. }
  185. /**
  186. * @brief Deinitializes the DCMI peripheral registers to their default reset
  187. * values.
  188. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  189. * the configuration information for DCMI.
  190. * @retval HAL status
  191. */
  192. HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
  193. {
  194. /* DeInit the low level hardware */
  195. HAL_DCMI_MspDeInit(hdcmi);
  196. /* Update error code */
  197. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  198. /* Initialize the DCMI state*/
  199. hdcmi->State = HAL_DCMI_STATE_RESET;
  200. /* Release Lock */
  201. __HAL_UNLOCK(hdcmi);
  202. return HAL_OK;
  203. }
  204. /**
  205. * @brief Initializes the DCMI MSP.
  206. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  207. * the configuration information for DCMI.
  208. * @retval None
  209. */
  210. __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
  211. {
  212. /* Prevent unused argument(s) compilation warning */
  213. UNUSED(hdcmi);
  214. /* NOTE : This function Should not be modified, when the callback is needed,
  215. the HAL_DCMI_MspInit could be implemented in the user file
  216. */
  217. }
  218. /**
  219. * @brief DeInitializes the DCMI MSP.
  220. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  221. * the configuration information for DCMI.
  222. * @retval None
  223. */
  224. __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
  225. {
  226. /* Prevent unused argument(s) compilation warning */
  227. UNUSED(hdcmi);
  228. /* NOTE : This function Should not be modified, when the callback is needed,
  229. the HAL_DCMI_MspDeInit could be implemented in the user file
  230. */
  231. }
  232. /**
  233. * @}
  234. */
  235. /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
  236. * @brief IO operation functions
  237. *
  238. @verbatim
  239. ===============================================================================
  240. ##### IO operation functions #####
  241. ===============================================================================
  242. [..] This section provides functions allowing to:
  243. (+) Configure destination address and data length and
  244. Enables DCMI DMA request and enables DCMI capture
  245. (+) Stop the DCMI capture.
  246. (+) Handles DCMI interrupt request.
  247. @endverbatim
  248. * @{
  249. */
  250. /**
  251. * @brief Enables DCMI DMA request and enables DCMI capture
  252. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  253. * the configuration information for DCMI.
  254. * @param DCMI_Mode: DCMI capture mode snapshot or continuous grab.
  255. * @param pData: The destination memory Buffer address (LCD Frame buffer).
  256. * @param Length: The length of capture to be transferred.
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
  260. {
  261. /* Initialize the second memory address */
  262. uint32_t SecondMemAddress = 0;
  263. /* Check function parameters */
  264. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  265. /* Process Locked */
  266. __HAL_LOCK(hdcmi);
  267. /* Lock the DCMI peripheral state */
  268. hdcmi->State = HAL_DCMI_STATE_BUSY;
  269. /* Check the parameters */
  270. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  271. /* Configure the DCMI Mode */
  272. hdcmi->Instance->CR &= ~(DCMI_CR_CM);
  273. hdcmi->Instance->CR |= (uint32_t)(DCMI_Mode);
  274. /* Set the DMA memory0 conversion complete callback */
  275. hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAConvCplt;
  276. /* Set the DMA error callback */
  277. hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
  278. if(Length <= 0xFFFF)
  279. {
  280. /* Enable the DMA Stream */
  281. HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
  282. }
  283. else /* DCMI_DOUBLE_BUFFER Mode */
  284. {
  285. /* Set the DMA memory1 conversion complete callback */
  286. hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAConvCplt;
  287. /* Initialize transfer parameters */
  288. hdcmi->XferCount = 1;
  289. hdcmi->XferSize = Length;
  290. hdcmi->pBuffPtr = pData;
  291. /* Get the number of buffer */
  292. while(hdcmi->XferSize > 0xFFFF)
  293. {
  294. hdcmi->XferSize = (hdcmi->XferSize/2);
  295. hdcmi->XferCount = hdcmi->XferCount*2;
  296. }
  297. /* Update DCMI counter and transfer number*/
  298. hdcmi->XferCount = (hdcmi->XferCount - 2);
  299. hdcmi->XferTransferNumber = hdcmi->XferCount;
  300. /* Update second memory address */
  301. SecondMemAddress = (uint32_t)(pData + (4*hdcmi->XferSize));
  302. /* Start DMA multi buffer transfer */
  303. HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
  304. }
  305. /* Enable Capture */
  306. DCMI->CR |= DCMI_CR_CAPTURE;
  307. /* Return function status */
  308. return HAL_OK;
  309. }
  310. /**
  311. * @brief Disable DCMI DMA request and Disable DCMI capture
  312. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  313. * the configuration information for DCMI.
  314. * @retval HAL status
  315. */
  316. HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
  317. {
  318. uint32_t tickstart = 0;
  319. /* Lock the DCMI peripheral state */
  320. hdcmi->State = HAL_DCMI_STATE_BUSY;
  321. __HAL_DCMI_DISABLE(hdcmi);
  322. /* Disable Capture */
  323. DCMI->CR &= ~(DCMI_CR_CAPTURE);
  324. /* Get tick */
  325. tickstart = HAL_GetTick();
  326. /* Check if the DCMI capture effectively disabled */
  327. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0)
  328. {
  329. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DCMI_STOP)
  330. {
  331. /* Process Unlocked */
  332. __HAL_UNLOCK(hdcmi);
  333. /* Update error code */
  334. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  335. /* Change DCMI state */
  336. hdcmi->State = HAL_DCMI_STATE_TIMEOUT;
  337. return HAL_TIMEOUT;
  338. }
  339. }
  340. /* Disable the DMA */
  341. HAL_DMA_Abort(hdcmi->DMA_Handle);
  342. /* Update error code */
  343. hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
  344. /* Change DCMI state */
  345. hdcmi->State = HAL_DCMI_STATE_READY;
  346. /* Process Unlocked */
  347. __HAL_UNLOCK(hdcmi);
  348. /* Return function status */
  349. return HAL_OK;
  350. }
  351. /**
  352. * @brief Handles DCMI interrupt request.
  353. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  354. * the configuration information for the DCMI.
  355. * @retval None
  356. */
  357. void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
  358. {
  359. /* Synchronization error interrupt management *******************************/
  360. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_ERRRI) != RESET)
  361. {
  362. if(__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_ERR) != RESET)
  363. {
  364. /* Disable the Synchronization error interrupt */
  365. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_ERR);
  366. /* Clear the Synchronization error flag */
  367. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
  368. /* Update error code */
  369. hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
  370. /* Change DCMI state */
  371. hdcmi->State = HAL_DCMI_STATE_ERROR;
  372. /* Process Unlocked */
  373. __HAL_UNLOCK(hdcmi);
  374. /* Abort the DMA Transfer */
  375. HAL_DMA_Abort(hdcmi->DMA_Handle);
  376. /* Synchronization error Callback */
  377. HAL_DCMI_ErrorCallback(hdcmi);
  378. }
  379. }
  380. /* Overflow interrupt management ********************************************/
  381. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_OVFRI) != RESET)
  382. {
  383. if(__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_OVF) != RESET)
  384. {
  385. /* Disable the Overflow interrupt */
  386. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_OVF);
  387. /* Clear the Overflow flag */
  388. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVFRI);
  389. /* Update error code */
  390. hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVF;
  391. /* Change DCMI state */
  392. hdcmi->State = HAL_DCMI_STATE_ERROR;
  393. /* Process Unlocked */
  394. __HAL_UNLOCK(hdcmi);
  395. /* Abort the DMA Transfer */
  396. HAL_DMA_Abort(hdcmi->DMA_Handle);
  397. /* Overflow Callback */
  398. HAL_DCMI_ErrorCallback(hdcmi);
  399. }
  400. }
  401. /* Line Interrupt management ************************************************/
  402. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_LINERI) != RESET)
  403. {
  404. if(__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_LINE) != RESET)
  405. {
  406. /* Clear the Line interrupt flag */
  407. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
  408. /* Process Unlocked */
  409. __HAL_UNLOCK(hdcmi);
  410. /* Line interrupt Callback */
  411. HAL_DCMI_LineEventCallback(hdcmi);
  412. }
  413. }
  414. /* VSYNC interrupt management ***********************************************/
  415. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_VSYNCRI) != RESET)
  416. {
  417. if(__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_VSYNC) != RESET)
  418. {
  419. /* Disable the VSYNC interrupt */
  420. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_VSYNC);
  421. /* Clear the VSYNC flag */
  422. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
  423. /* Process Unlocked */
  424. __HAL_UNLOCK(hdcmi);
  425. /* VSYNC Callback */
  426. HAL_DCMI_VsyncEventCallback(hdcmi);
  427. }
  428. }
  429. /* End of Frame interrupt management ****************************************/
  430. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_FRAMERI) != RESET)
  431. {
  432. if(__HAL_DCMI_GET_IT_SOURCE(hdcmi, DCMI_IT_FRAME) != RESET)
  433. {
  434. /* Disable the End of Frame interrupt */
  435. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
  436. /* Clear the End of Frame flag */
  437. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
  438. /* Process Unlocked */
  439. __HAL_UNLOCK(hdcmi);
  440. /* End of Frame Callback */
  441. HAL_DCMI_FrameEventCallback(hdcmi);
  442. }
  443. }
  444. }
  445. /**
  446. * @brief Error DCMI callback.
  447. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  448. * the configuration information for DCMI.
  449. * @retval None
  450. */
  451. __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  452. {
  453. /* Prevent unused argument(s) compilation warning */
  454. UNUSED(hdcmi);
  455. /* NOTE : This function Should not be modified, when the callback is needed,
  456. the HAL_DCMI_ErrorCallback could be implemented in the user file
  457. */
  458. }
  459. /**
  460. * @brief Line Event callback.
  461. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  462. * the configuration information for DCMI.
  463. * @retval None
  464. */
  465. __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  466. {
  467. /* Prevent unused argument(s) compilation warning */
  468. UNUSED(hdcmi);
  469. /* NOTE : This function Should not be modified, when the callback is needed,
  470. the HAL_DCMI_LineEventCallback could be implemented in the user file
  471. */
  472. }
  473. /**
  474. * @brief VSYNC Event callback.
  475. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  476. * the configuration information for DCMI.
  477. * @retval None
  478. */
  479. __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  480. {
  481. /* Prevent unused argument(s) compilation warning */
  482. UNUSED(hdcmi);
  483. /* NOTE : This function Should not be modified, when the callback is needed,
  484. the HAL_DCMI_VsyncEventCallback could be implemented in the user file
  485. */
  486. }
  487. /**
  488. * @brief Frame Event callback.
  489. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  490. * the configuration information for DCMI.
  491. * @retval None
  492. */
  493. __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  494. {
  495. /* Prevent unused argument(s) compilation warning */
  496. UNUSED(hdcmi);
  497. /* NOTE : This function Should not be modified, when the callback is needed,
  498. the HAL_DCMI_FrameEventCallback could be implemented in the user file
  499. */
  500. }
  501. /**
  502. * @}
  503. */
  504. /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
  505. * @brief Peripheral Control functions
  506. *
  507. @verbatim
  508. ===============================================================================
  509. ##### Peripheral Control functions #####
  510. ===============================================================================
  511. [..] This section provides functions allowing to:
  512. (+) Configure the CROP feature.
  513. (+) Enable/Disable the CROP feature.
  514. @endverbatim
  515. * @{
  516. */
  517. /**
  518. * @brief Configure the DCMI CROP coordinate.
  519. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  520. * the configuration information for DCMI.
  521. * @param X0: DCMI window X offset
  522. * @param Y0: DCMI window Y offset
  523. * @param XSize: DCMI Pixel per line
  524. * @param YSize: DCMI Line number
  525. * @retval HAL status
  526. */
  527. HAL_StatusTypeDef HAL_DCMI_ConfigCROP(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
  528. {
  529. /* Process Locked */
  530. __HAL_LOCK(hdcmi);
  531. /* Lock the DCMI peripheral state */
  532. hdcmi->State = HAL_DCMI_STATE_BUSY;
  533. /* Check the parameters */
  534. assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
  535. assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
  536. assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
  537. assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
  538. /* Configure CROP */
  539. DCMI->CWSIZER = (XSize | (YSize << 16));
  540. DCMI->CWSTRTR = (X0 | (Y0 << 16));
  541. /* Initialize the DCMI state*/
  542. hdcmi->State = HAL_DCMI_STATE_READY;
  543. /* Process Unlocked */
  544. __HAL_UNLOCK(hdcmi);
  545. return HAL_OK;
  546. }
  547. /**
  548. * @brief Disable the Crop feature.
  549. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  550. * the configuration information for DCMI.
  551. * @retval HAL status
  552. */
  553. HAL_StatusTypeDef HAL_DCMI_DisableCROP(DCMI_HandleTypeDef *hdcmi)
  554. {
  555. /* Process Locked */
  556. __HAL_LOCK(hdcmi);
  557. /* Lock the DCMI peripheral state */
  558. hdcmi->State = HAL_DCMI_STATE_BUSY;
  559. /* Disable DCMI Crop feature */
  560. DCMI->CR &= ~(uint32_t)DCMI_CR_CROP;
  561. /* Change the DCMI state*/
  562. hdcmi->State = HAL_DCMI_STATE_READY;
  563. /* Process Unlocked */
  564. __HAL_UNLOCK(hdcmi);
  565. return HAL_OK;
  566. }
  567. /**
  568. * @brief Enable the Crop feature.
  569. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  570. * the configuration information for DCMI.
  571. * @retval HAL status
  572. */
  573. HAL_StatusTypeDef HAL_DCMI_EnableCROP(DCMI_HandleTypeDef *hdcmi)
  574. {
  575. /* Process Locked */
  576. __HAL_LOCK(hdcmi);
  577. /* Lock the DCMI peripheral state */
  578. hdcmi->State = HAL_DCMI_STATE_BUSY;
  579. /* Enable DCMI Crop feature */
  580. DCMI->CR |= (uint32_t)DCMI_CR_CROP;
  581. /* Change the DCMI state*/
  582. hdcmi->State = HAL_DCMI_STATE_READY;
  583. /* Process Unlocked */
  584. __HAL_UNLOCK(hdcmi);
  585. return HAL_OK;
  586. }
  587. /**
  588. * @}
  589. */
  590. /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
  591. * @brief Peripheral State functions
  592. *
  593. @verbatim
  594. ===============================================================================
  595. ##### Peripheral State and Errors functions #####
  596. ===============================================================================
  597. [..]
  598. This subsection provides functions allowing to
  599. (+) Check the DCMI state.
  600. (+) Get the specific DCMI error flag.
  601. @endverbatim
  602. * @{
  603. */
  604. /**
  605. * @brief Return the DCMI state
  606. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  607. * the configuration information for DCMI.
  608. * @retval HAL state
  609. */
  610. HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
  611. {
  612. return hdcmi->State;
  613. }
  614. /**
  615. * @brief Return the DCMI error code
  616. * @param hdcmi : pointer to a DCMI_HandleTypeDef structure that contains
  617. * the configuration information for DCMI.
  618. * @retval DCMI Error Code
  619. */
  620. uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
  621. {
  622. return hdcmi->ErrorCode;
  623. }
  624. /**
  625. * @}
  626. */
  627. /* Private functions ---------------------------------------------------------*/
  628. /** @defgroup DCMI_Private_Functions DCMI Private Functions
  629. * @{
  630. */
  631. /**
  632. * @brief DMA conversion complete callback.
  633. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  634. * the configuration information for the specified DMA module.
  635. * @retval None
  636. */
  637. static void DCMI_DMAConvCplt(DMA_HandleTypeDef *hdma)
  638. {
  639. uint32_t tmp = 0;
  640. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  641. hdcmi->State= HAL_DCMI_STATE_READY;
  642. if(hdcmi->XferCount != 0)
  643. {
  644. /* Update memory 0 address location */
  645. tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
  646. if(((hdcmi->XferCount % 2) == 0) && (tmp != 0))
  647. {
  648. tmp = hdcmi->DMA_Handle->Instance->M0AR;
  649. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY0);
  650. hdcmi->XferCount--;
  651. }
  652. /* Update memory 1 address location */
  653. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0)
  654. {
  655. tmp = hdcmi->DMA_Handle->Instance->M1AR;
  656. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8*hdcmi->XferSize)), MEMORY1);
  657. hdcmi->XferCount--;
  658. }
  659. }
  660. /* Update memory 0 address location */
  661. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0)
  662. {
  663. hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
  664. }
  665. /* Update memory 1 address location */
  666. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0)
  667. {
  668. tmp = hdcmi->pBuffPtr;
  669. hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4*hdcmi->XferSize));
  670. hdcmi->XferCount = hdcmi->XferTransferNumber;
  671. }
  672. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_FRAMERI) != RESET)
  673. {
  674. /* Process Unlocked */
  675. __HAL_UNLOCK(hdcmi);
  676. /* FRAME Callback */
  677. HAL_DCMI_FrameEventCallback(hdcmi);
  678. }
  679. }
  680. /**
  681. * @brief DMA error callback
  682. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  683. * the configuration information for the specified DMA module.
  684. * @retval None
  685. */
  686. static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
  687. {
  688. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  689. hdcmi->State= HAL_DCMI_STATE_READY;
  690. HAL_DCMI_ErrorCallback(hdcmi);
  691. }
  692. /**
  693. * @}
  694. */
  695. /**
  696. * @}
  697. */
  698. #endif /* STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  699. STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
  700. STM32F479xx */
  701. #endif /* HAL_DCMI_MODULE_ENABLED */
  702. /**
  703. * @}
  704. */
  705. /**
  706. * @}
  707. */
  708. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/