stm32f4xx_hal_dcmi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dcmi.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 04-November-2016
  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) 2016 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)14U) /* Set timeout to 1s */
  98. /* Private macro -------------------------------------------------------------*/
  99. /* Private variables ---------------------------------------------------------*/
  100. /* Private function prototypes -----------------------------------------------*/
  101. static void DCMI_DMAXferCplt(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. /* Set DCMI parameters */
  153. /* Configures the HS, VS, DE and PC polarity */
  154. hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |
  155. DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |
  156. DCMI_CR_ESS);
  157. hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate | \
  158. hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity | \
  159. hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode | \
  160. hdcmi->Init.JPEGMode);
  161. if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
  162. {
  163. hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode) |
  164. ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << DCMI_POSITION_ESCR_LSC)|
  165. ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << DCMI_POSITION_ESCR_LEC) |
  166. ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << DCMI_POSITION_ESCR_FEC));
  167. }
  168. /* Enable the Line, Vsync, Error and Overrun interrupts */
  169. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  170. /* Update error code */
  171. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  172. /* Initialize the DCMI state*/
  173. hdcmi->State = HAL_DCMI_STATE_READY;
  174. return HAL_OK;
  175. }
  176. /**
  177. * @brief Deinitializes the DCMI peripheral registers to their default reset
  178. * values.
  179. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  180. * the configuration information for DCMI.
  181. * @retval HAL status
  182. */
  183. HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
  184. {
  185. /* DeInit the low level hardware */
  186. HAL_DCMI_MspDeInit(hdcmi);
  187. /* Update error code */
  188. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  189. /* Initialize the DCMI state*/
  190. hdcmi->State = HAL_DCMI_STATE_RESET;
  191. /* Release Lock */
  192. __HAL_UNLOCK(hdcmi);
  193. return HAL_OK;
  194. }
  195. /**
  196. * @brief Initializes the DCMI MSP.
  197. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  198. * the configuration information for DCMI.
  199. * @retval None
  200. */
  201. __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
  202. {
  203. /* Prevent unused argument(s) compilation warning */
  204. UNUSED(hdcmi);
  205. /* NOTE : This function Should not be modified, when the callback is needed,
  206. the HAL_DCMI_MspInit could be implemented in the user file
  207. */
  208. }
  209. /**
  210. * @brief DeInitializes the DCMI MSP.
  211. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  212. * the configuration information for DCMI.
  213. * @retval None
  214. */
  215. __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
  216. {
  217. /* Prevent unused argument(s) compilation warning */
  218. UNUSED(hdcmi);
  219. /* NOTE : This function Should not be modified, when the callback is needed,
  220. the HAL_DCMI_MspDeInit could be implemented in the user file
  221. */
  222. }
  223. /**
  224. * @}
  225. */
  226. /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
  227. * @brief IO operation functions
  228. *
  229. @verbatim
  230. ===============================================================================
  231. ##### IO operation functions #####
  232. ===============================================================================
  233. [..] This section provides functions allowing to:
  234. (+) Configure destination address and data length and
  235. Enables DCMI DMA request and enables DCMI capture
  236. (+) Stop the DCMI capture.
  237. (+) Handles DCMI interrupt request.
  238. @endverbatim
  239. * @{
  240. */
  241. /**
  242. * @brief Enables DCMI DMA request and enables DCMI capture
  243. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  244. * the configuration information for DCMI.
  245. * @param DCMI_Mode: DCMI capture mode snapshot or continuous grab.
  246. * @param pData: The destination memory Buffer address (LCD Frame buffer).
  247. * @param Length: The length of capture to be transferred.
  248. * @retval HAL status
  249. */
  250. HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
  251. {
  252. /* Initialize the second memory address */
  253. uint32_t SecondMemAddress = 0U;
  254. /* Check function parameters */
  255. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  256. /* Process Locked */
  257. __HAL_LOCK(hdcmi);
  258. /* Lock the DCMI peripheral state */
  259. hdcmi->State = HAL_DCMI_STATE_BUSY;
  260. /* Enable DCMI by setting DCMIEN bit */
  261. __HAL_DCMI_ENABLE(hdcmi);
  262. /* Configure the DCMI Mode */
  263. hdcmi->Instance->CR &= ~(DCMI_CR_CM);
  264. hdcmi->Instance->CR |= (uint32_t)(DCMI_Mode);
  265. /* Set the DMA memory0 conversion complete callback */
  266. hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
  267. /* Set the DMA error callback */
  268. hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
  269. /* Set the dma abort callback */
  270. hdcmi->DMA_Handle->XferAbortCallback = NULL;
  271. /* Reset transfer counters value */
  272. hdcmi->XferCount = 0;
  273. hdcmi->XferTransferNumber = 0;
  274. if(Length <= 0xFFFFU)
  275. {
  276. /* Enable the DMA Stream */
  277. HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
  278. }
  279. else /* DCMI_DOUBLE_BUFFER Mode */
  280. {
  281. /* Set the DMA memory1 conversion complete callback */
  282. hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAXferCplt;
  283. /* Initialize transfer parameters */
  284. hdcmi->XferCount = 1U;
  285. hdcmi->XferSize = Length;
  286. hdcmi->pBuffPtr = pData;
  287. /* Get the number of buffer */
  288. while(hdcmi->XferSize > 0xFFFFU)
  289. {
  290. hdcmi->XferSize = (hdcmi->XferSize/2U);
  291. hdcmi->XferCount = hdcmi->XferCount*2U;
  292. }
  293. /* Update DCMI counter and transfer number*/
  294. hdcmi->XferCount = (hdcmi->XferCount - 2U);
  295. hdcmi->XferTransferNumber = hdcmi->XferCount;
  296. /* Update second memory address */
  297. SecondMemAddress = (uint32_t)(pData + (4U*hdcmi->XferSize));
  298. /* Start DMA multi buffer transfer */
  299. HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
  300. }
  301. /* Enable Capture */
  302. hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
  303. /* Release Lock */
  304. __HAL_UNLOCK(hdcmi);
  305. /* Return function status */
  306. return HAL_OK;
  307. }
  308. /**
  309. * @brief Disable DCMI DMA request and Disable DCMI capture
  310. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  311. * the configuration information for DCMI.
  312. * @retval HAL status
  313. */
  314. HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
  315. {
  316. __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
  317. HAL_StatusTypeDef status = HAL_OK;
  318. /* Process locked */
  319. __HAL_LOCK(hdcmi);
  320. /* Lock the DCMI peripheral state */
  321. hdcmi->State = HAL_DCMI_STATE_BUSY;
  322. /* Disable Capture */
  323. hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
  324. /* Check if the DCMI capture effectively disabled */
  325. do
  326. {
  327. if (count-- == 0)
  328. {
  329. /* Update error code */
  330. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  331. status = HAL_TIMEOUT;
  332. }
  333. }
  334. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
  335. /* Disable the DCMI */
  336. __HAL_DCMI_DISABLE(hdcmi);
  337. /* Disable the DMA */
  338. HAL_DMA_Abort(hdcmi->DMA_Handle);
  339. /* Update error code */
  340. hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
  341. /* Change DCMI state */
  342. hdcmi->State = HAL_DCMI_STATE_READY;
  343. /* Process Unlocked */
  344. __HAL_UNLOCK(hdcmi);
  345. /* Return function status */
  346. return status;
  347. }
  348. HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
  349. {
  350. __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
  351. HAL_StatusTypeDef status = HAL_OK;
  352. /* Process locked */
  353. __HAL_LOCK(hdcmi);
  354. if(hdcmi->State == HAL_DCMI_STATE_BUSY)
  355. {
  356. /* Change DCMI state */
  357. hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
  358. /* Disable Capture */
  359. hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
  360. /* Check if the DCMI capture effectively disabled */
  361. do
  362. {
  363. if (count-- == 0)
  364. {
  365. /* Update error code */
  366. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  367. /* Change DCMI state */
  368. hdcmi->State = HAL_DCMI_STATE_READY;
  369. status = HAL_TIMEOUT;
  370. break;
  371. }
  372. }
  373. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
  374. }
  375. /* Process Unlocked */
  376. __HAL_UNLOCK(hdcmi);
  377. /* Return function status */
  378. return status;
  379. }
  380. /**
  381. * @brief Resume DCMI capture
  382. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  383. * the configuration information for DCMI.
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
  387. {
  388. /* Process locked */
  389. __HAL_LOCK(hdcmi);
  390. if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
  391. {
  392. /* Change DCMI state */
  393. hdcmi->State = HAL_DCMI_STATE_BUSY;
  394. /* Disable Capture */
  395. hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
  396. }
  397. /* Process Unlocked */
  398. __HAL_UNLOCK(hdcmi);
  399. /* Return function status */
  400. return HAL_OK;
  401. }
  402. /**
  403. * @brief Handles DCMI interrupt request.
  404. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  405. * the configuration information for the DCMI.
  406. * @retval None
  407. */
  408. void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
  409. {
  410. uint32_t isr_value = READ_REG(hdcmi->Instance->MISR);
  411. /* Synchronization error interrupt management *******************************/
  412. if((isr_value & DCMI_FLAG_ERRRI) == DCMI_FLAG_ERRRI)
  413. {
  414. /* Clear the Synchronization error flag */
  415. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
  416. /* Update error code */
  417. hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
  418. /* Change DCMI state */
  419. hdcmi->State = HAL_DCMI_STATE_ERROR;
  420. /* Set the synchronization error callback */
  421. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  422. /* Abort the DMA Transfer */
  423. HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
  424. }
  425. /* Overflow interrupt management ********************************************/
  426. if((isr_value & DCMI_FLAG_OVRRI) == DCMI_FLAG_OVRRI)
  427. {
  428. /* Clear the Overflow flag */
  429. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
  430. /* Update error code */
  431. hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
  432. /* Change DCMI state */
  433. hdcmi->State = HAL_DCMI_STATE_ERROR;
  434. /* Set the overflow callback */
  435. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  436. /* Abort the DMA Transfer */
  437. HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
  438. }
  439. /* Line Interrupt management ************************************************/
  440. if((isr_value & DCMI_FLAG_LINERI) == DCMI_FLAG_LINERI)
  441. {
  442. /* Clear the Line interrupt flag */
  443. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
  444. /* Line interrupt Callback */
  445. HAL_DCMI_LineEventCallback(hdcmi);
  446. }
  447. /* VSYNC interrupt management ***********************************************/
  448. if((isr_value & DCMI_FLAG_VSYNCRI) == DCMI_FLAG_VSYNCRI)
  449. {
  450. /* Clear the VSYNC flag */
  451. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
  452. /* VSYNC Callback */
  453. HAL_DCMI_VsyncEventCallback(hdcmi);
  454. }
  455. /* FRAME interrupt management ***********************************************/
  456. if((isr_value & DCMI_FLAG_FRAMERI) == DCMI_FLAG_FRAMERI)
  457. {
  458. /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
  459. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  460. {
  461. /* Disable the Line, Vsync, Error and Overrun interrupts */
  462. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  463. }
  464. /* Disable the Frame interrupt */
  465. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
  466. /* Frame Callback */
  467. HAL_DCMI_FrameEventCallback(hdcmi);
  468. }
  469. }
  470. /**
  471. * @brief Error DCMI callback.
  472. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  473. * the configuration information for DCMI.
  474. * @retval None
  475. */
  476. __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  477. {
  478. /* Prevent unused argument(s) compilation warning */
  479. UNUSED(hdcmi);
  480. /* NOTE : This function Should not be modified, when the callback is needed,
  481. the HAL_DCMI_ErrorCallback could be implemented in the user file
  482. */
  483. }
  484. /**
  485. * @brief Line Event callback.
  486. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  487. * the configuration information for DCMI.
  488. * @retval None
  489. */
  490. __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  491. {
  492. /* Prevent unused argument(s) compilation warning */
  493. UNUSED(hdcmi);
  494. /* NOTE : This function Should not be modified, when the callback is needed,
  495. the HAL_DCMI_LineEventCallback could be implemented in the user file
  496. */
  497. }
  498. /**
  499. * @brief VSYNC Event callback.
  500. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  501. * the configuration information for DCMI.
  502. * @retval None
  503. */
  504. __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  505. {
  506. /* Prevent unused argument(s) compilation warning */
  507. UNUSED(hdcmi);
  508. /* NOTE : This function Should not be modified, when the callback is needed,
  509. the HAL_DCMI_VsyncEventCallback could be implemented in the user file
  510. */
  511. }
  512. /**
  513. * @brief Frame Event callback.
  514. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  515. * the configuration information for DCMI.
  516. * @retval None
  517. */
  518. __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  519. {
  520. /* Prevent unused argument(s) compilation warning */
  521. UNUSED(hdcmi);
  522. /* NOTE : This function Should not be modified, when the callback is needed,
  523. the HAL_DCMI_FrameEventCallback could be implemented in the user file
  524. */
  525. }
  526. /**
  527. * @}
  528. */
  529. /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
  530. * @brief Peripheral Control functions
  531. *
  532. @verbatim
  533. ===============================================================================
  534. ##### Peripheral Control functions #####
  535. ===============================================================================
  536. [..] This section provides functions allowing to:
  537. (+) Configure the CROP feature.
  538. (+) Enable/Disable the CROP feature.
  539. @endverbatim
  540. * @{
  541. */
  542. /**
  543. * @brief Configure the DCMI CROP coordinate.
  544. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  545. * the configuration information for DCMI.
  546. * @param X0: DCMI window X offset
  547. * @param Y0: DCMI window Y offset
  548. * @param XSize: DCMI Pixel per line
  549. * @param YSize: DCMI Line number
  550. * @retval HAL status
  551. */
  552. HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
  553. {
  554. /* Process Locked */
  555. __HAL_LOCK(hdcmi);
  556. /* Lock the DCMI peripheral state */
  557. hdcmi->State = HAL_DCMI_STATE_BUSY;
  558. /* Check the parameters */
  559. assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
  560. assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
  561. assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
  562. assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
  563. /* Configure CROP */
  564. hdcmi->Instance->CWSIZER = (XSize | (YSize << DCMI_POSITION_CWSIZE_VLINE));
  565. hdcmi->Instance->CWSTRTR = (X0 | (Y0 << DCMI_POSITION_CWSTRT_VST));
  566. /* Initialize the DCMI state*/
  567. hdcmi->State = HAL_DCMI_STATE_READY;
  568. /* Process Unlocked */
  569. __HAL_UNLOCK(hdcmi);
  570. return HAL_OK;
  571. }
  572. /**
  573. * @brief Disable the Crop feature.
  574. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  575. * the configuration information for DCMI.
  576. * @retval HAL status
  577. */
  578. HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
  579. {
  580. /* Process Locked */
  581. __HAL_LOCK(hdcmi);
  582. /* Lock the DCMI peripheral state */
  583. hdcmi->State = HAL_DCMI_STATE_BUSY;
  584. /* Disable DCMI Crop feature */
  585. hdcmi->Instance->CR &= ~(uint32_t)DCMI_CR_CROP;
  586. /* Change the DCMI state*/
  587. hdcmi->State = HAL_DCMI_STATE_READY;
  588. /* Process Unlocked */
  589. __HAL_UNLOCK(hdcmi);
  590. return HAL_OK;
  591. }
  592. /**
  593. * @brief Enable the Crop feature.
  594. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  595. * the configuration information for DCMI.
  596. * @retval HAL status
  597. */
  598. HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
  599. {
  600. /* Process Locked */
  601. __HAL_LOCK(hdcmi);
  602. /* Lock the DCMI peripheral state */
  603. hdcmi->State = HAL_DCMI_STATE_BUSY;
  604. /* Enable DCMI Crop feature */
  605. hdcmi->Instance->CR |= (uint32_t)DCMI_CR_CROP;
  606. /* Change the DCMI state*/
  607. hdcmi->State = HAL_DCMI_STATE_READY;
  608. /* Process Unlocked */
  609. __HAL_UNLOCK(hdcmi);
  610. return HAL_OK;
  611. }
  612. /**
  613. * @}
  614. */
  615. /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
  616. * @brief Peripheral State functions
  617. *
  618. @verbatim
  619. ===============================================================================
  620. ##### Peripheral State and Errors functions #####
  621. ===============================================================================
  622. [..]
  623. This subsection provides functions allowing to
  624. (+) Check the DCMI state.
  625. (+) Get the specific DCMI error flag.
  626. @endverbatim
  627. * @{
  628. */
  629. /**
  630. * @brief Return the DCMI state
  631. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  632. * the configuration information for DCMI.
  633. * @retval HAL state
  634. */
  635. HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
  636. {
  637. return hdcmi->State;
  638. }
  639. /**
  640. * @brief Return the DCMI error code
  641. * @param hdcmi : pointer to a DCMI_HandleTypeDef structure that contains
  642. * the configuration information for DCMI.
  643. * @retval DCMI Error Code
  644. */
  645. uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
  646. {
  647. return hdcmi->ErrorCode;
  648. }
  649. /**
  650. * @}
  651. */
  652. /* Private functions ---------------------------------------------------------*/
  653. /** @defgroup DCMI_Private_Functions DCMI Private Functions
  654. * @{
  655. */
  656. /**
  657. * @brief DMA conversion complete callback.
  658. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  659. * the configuration information for the specified DMA module.
  660. * @retval None
  661. */
  662. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
  663. {
  664. uint32_t tmp = 0U;
  665. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  666. if(hdcmi->XferCount != 0)
  667. {
  668. /* Update memory 0 address location */
  669. tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
  670. if(((hdcmi->XferCount % 2U) == 0U) && (tmp != 0U))
  671. {
  672. tmp = hdcmi->DMA_Handle->Instance->M0AR;
  673. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY0);
  674. hdcmi->XferCount--;
  675. }
  676. /* Update memory 1 address location */
  677. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
  678. {
  679. tmp = hdcmi->DMA_Handle->Instance->M1AR;
  680. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY1);
  681. hdcmi->XferCount--;
  682. }
  683. }
  684. /* Update memory 0 address location */
  685. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0U)
  686. {
  687. hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
  688. }
  689. /* Update memory 1 address location */
  690. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
  691. {
  692. tmp = hdcmi->pBuffPtr;
  693. hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4U*hdcmi->XferSize));
  694. hdcmi->XferCount = hdcmi->XferTransferNumber;
  695. }
  696. /* Check if the frame is transferred */
  697. if(hdcmi->XferCount == hdcmi->XferTransferNumber)
  698. {
  699. /* Enable the Frame interrupt */
  700. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
  701. /* When snapshot mode, set dcmi state to ready */
  702. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  703. {
  704. hdcmi->State= HAL_DCMI_STATE_READY;
  705. }
  706. }
  707. }
  708. /**
  709. * @brief DMA error callback
  710. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  711. * the configuration information for the specified DMA module.
  712. * @retval None
  713. */
  714. static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
  715. {
  716. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  717. if(hdcmi->DMA_Handle->ErrorCode != HAL_DMA_ERROR_FE)
  718. {
  719. /* Initialize the DCMI state*/
  720. hdcmi->State = HAL_DCMI_STATE_READY;
  721. }
  722. /* DCMI error Callback */
  723. HAL_DCMI_ErrorCallback(hdcmi);
  724. }
  725. /**
  726. * @}
  727. */
  728. /**
  729. * @}
  730. */
  731. #endif /* STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  732. STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
  733. STM32F479xx */
  734. #endif /* HAL_DCMI_MODULE_ENABLED */
  735. /**
  736. * @}
  737. */
  738. /**
  739. * @}
  740. */
  741. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/