You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

907 lines
29 KiB

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