stm32f4xx_hal_dcmi.c 29 KB

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