训练营PLSR题目
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.
 
 
 
 
 
 

1162 line
36 KiB

  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. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file in
  20. * the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The sequence below describes how to use this driver to capture image
  29. from a camera module connected to the DCMI Interface.
  30. This sequence does not take into account the configuration of the
  31. camera module, which should be made before to configure and enable
  32. the DCMI to capture images.
  33. (#) Program the required configuration through following parameters:
  34. horizontal and vertical polarity, pixel clock polarity, Capture Rate,
  35. Synchronization Mode, code of the frame delimiter and data width
  36. using HAL_DCMI_Init() function.
  37. (#) Configure the DMA2_Stream1 channel1 to transfer Data from DCMI DR
  38. register to the destination memory buffer.
  39. (#) Program the required configuration through following parameters:
  40. DCMI mode, destination memory Buffer address and the data length
  41. and enable capture using HAL_DCMI_Start_DMA() function.
  42. (#) Optionally, configure and Enable the CROP feature to select a rectangular
  43. window from the received image using HAL_DCMI_ConfigCrop()
  44. and HAL_DCMI_EnableCROP() functions
  45. (#) The capture can be stopped using HAL_DCMI_Stop() function.
  46. (#) To control DCMI state you can use the function HAL_DCMI_GetState().
  47. *** DCMI HAL driver macros list ***
  48. =============================================
  49. [..]
  50. Below the list of most used macros in DCMI HAL driver.
  51. (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
  52. (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
  53. (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
  54. (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
  55. (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
  56. (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
  57. (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred or not.
  58. [..]
  59. (@) You can refer to the DCMI HAL driver header file for more useful macros
  60. *** Callback registration ***
  61. =============================
  62. The compilation define USE_HAL_DCMI_REGISTER_CALLBACKS when set to 1
  63. allows the user to configure dynamically the driver callbacks.
  64. Use functions HAL_DCMI_RegisterCallback() to register a user callback.
  65. Function HAL_DCMI_RegisterCallback() allows to register following callbacks:
  66. (+) FrameEventCallback : DCMI Frame Event.
  67. (+) VsyncEventCallback : DCMI Vsync Event.
  68. (+) LineEventCallback : DCMI Line Event.
  69. (+) ErrorCallback : DCMI error.
  70. (+) MspInitCallback : DCMI MspInit.
  71. (+) MspDeInitCallback : DCMI MspDeInit.
  72. This function takes as parameters the HAL peripheral handle, the callback ID
  73. and a pointer to the user callback function.
  74. Use function HAL_DCMI_UnRegisterCallback() to reset a callback to the default
  75. weak (surcharged) function.
  76. HAL_DCMI_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  77. and the callback ID.
  78. This function allows to reset following callbacks:
  79. (+) FrameEventCallback : DCMI Frame Event.
  80. (+) VsyncEventCallback : DCMI Vsync Event.
  81. (+) LineEventCallback : DCMI Line Event.
  82. (+) ErrorCallback : DCMI error.
  83. (+) MspInitCallback : DCMI MspInit.
  84. (+) MspDeInitCallback : DCMI MspDeInit.
  85. By default, after the HAL_DCMI_Init and if the state is HAL_DCMI_STATE_RESET
  86. all callbacks are reset to the corresponding legacy weak (surcharged) functions:
  87. examples FrameEventCallback(), HAL_DCMI_ErrorCallback().
  88. Exception done for MspInit and MspDeInit callbacks that are respectively
  89. reset to the legacy weak (surcharged) functions in the HAL_DCMI_Init
  90. and HAL_DCMI_DeInit only when these callbacks are null (not registered beforehand).
  91. If not, MspInit or MspDeInit are not null, the HAL_DCMI_Init and HAL_DCMI_DeInit
  92. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  93. Callbacks can be registered/unregistered in READY state only.
  94. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  95. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  96. during the Init/DeInit.
  97. In that case first register the MspInit/MspDeInit user callbacks
  98. using HAL_DCMI_RegisterCallback before calling HAL_DCMI_DeInit
  99. or HAL_DCMI_Init function.
  100. When the compilation define USE_HAL_DCMI_REGISTER_CALLBACKS is set to 0 or
  101. not defined, the callback registering feature is not available
  102. and weak (surcharged) callbacks are used.
  103. @endverbatim
  104. ******************************************************************************
  105. */
  106. /* Includes ------------------------------------------------------------------*/
  107. #include "stm32f4xx_hal.h"
  108. /** @addtogroup STM32F4xx_HAL_Driver
  109. * @{
  110. */
  111. /** @defgroup DCMI DCMI
  112. * @brief DCMI HAL module driver
  113. * @{
  114. */
  115. #ifdef HAL_DCMI_MODULE_ENABLED
  116. #if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) ||\
  117. defined(STM32F429xx) || defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) ||\
  118. defined(STM32F479xx)
  119. /* Private typedef -----------------------------------------------------------*/
  120. /* Private define ------------------------------------------------------------*/
  121. #define HAL_TIMEOUT_DCMI_STOP 14U /* Set timeout to 1s */
  122. /* Private macro -------------------------------------------------------------*/
  123. /* Private variables ---------------------------------------------------------*/
  124. /* Private function prototypes -----------------------------------------------*/
  125. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma);
  126. static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
  127. /* Exported functions --------------------------------------------------------*/
  128. /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
  129. * @{
  130. */
  131. /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
  132. * @brief Initialization and Configuration functions
  133. *
  134. @verbatim
  135. ===============================================================================
  136. ##### Initialization and Configuration functions #####
  137. ===============================================================================
  138. [..] This section provides functions allowing to:
  139. (+) Initialize and configure the DCMI
  140. (+) De-initialize the DCMI
  141. @endverbatim
  142. * @{
  143. */
  144. /**
  145. * @brief Initializes the DCMI according to the specified
  146. * parameters in the DCMI_InitTypeDef and create the associated handle.
  147. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  148. * the configuration information for DCMI.
  149. * @retval HAL status
  150. */
  151. __weak HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
  152. {
  153. /* Check the DCMI peripheral state */
  154. if(hdcmi == NULL)
  155. {
  156. return HAL_ERROR;
  157. }
  158. /* Check function parameters */
  159. assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
  160. assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
  161. assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
  162. assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
  163. assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
  164. assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
  165. assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
  166. assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
  167. if(hdcmi->State == HAL_DCMI_STATE_RESET)
  168. {
  169. /* Allocate lock resource and initialize it */
  170. hdcmi->Lock = HAL_UNLOCKED;
  171. /* Init the low level hardware */
  172. /* Init the DCMI Callback settings */
  173. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  174. hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
  175. hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
  176. hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
  177. hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
  178. if(hdcmi->MspInitCallback == NULL)
  179. {
  180. /* Legacy weak MspInit Callback */
  181. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  182. }
  183. /* Initialize the low level hardware (MSP) */
  184. hdcmi->MspInitCallback(hdcmi);
  185. #else
  186. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  187. HAL_DCMI_MspInit(hdcmi);
  188. #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
  189. HAL_DCMI_MspInit(hdcmi);
  190. }
  191. /* Change the DCMI state */
  192. hdcmi->State = HAL_DCMI_STATE_BUSY;
  193. /* Set DCMI parameters */
  194. /* Configures the HS, VS, DE and PC polarity */
  195. hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |
  196. DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |
  197. DCMI_CR_ESS);
  198. hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate | \
  199. hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity | \
  200. hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode | \
  201. hdcmi->Init.JPEGMode);
  202. if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
  203. {
  204. hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode) |
  205. ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << DCMI_POSITION_ESCR_LSC)|
  206. ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << DCMI_POSITION_ESCR_LEC) |
  207. ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << DCMI_POSITION_ESCR_FEC));
  208. }
  209. /* Enable the Line, Vsync, Error and Overrun interrupts */
  210. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  211. /* Update error code */
  212. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  213. /* Initialize the DCMI state*/
  214. hdcmi->State = HAL_DCMI_STATE_READY;
  215. return HAL_OK;
  216. }
  217. /**
  218. * @brief Deinitializes the DCMI peripheral registers to their default reset
  219. * values.
  220. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  221. * the configuration information for DCMI.
  222. * @retval HAL status
  223. */
  224. HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
  225. {
  226. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  227. if(hdcmi->MspDeInitCallback == NULL)
  228. {
  229. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  230. }
  231. /* De-Initialize the low level hardware (MSP) */
  232. hdcmi->MspDeInitCallback(hdcmi);
  233. #else
  234. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  235. HAL_DCMI_MspDeInit(hdcmi);
  236. #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
  237. /* Update error code */
  238. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  239. /* Initialize the DCMI state*/
  240. hdcmi->State = HAL_DCMI_STATE_RESET;
  241. /* Release Lock */
  242. __HAL_UNLOCK(hdcmi);
  243. return HAL_OK;
  244. }
  245. /**
  246. * @brief Initializes the DCMI MSP.
  247. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  248. * the configuration information for DCMI.
  249. * @retval None
  250. */
  251. __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
  252. {
  253. /* Prevent unused argument(s) compilation warning */
  254. UNUSED(hdcmi);
  255. /* NOTE : This function Should not be modified, when the callback is needed,
  256. the HAL_DCMI_MspInit could be implemented in the user file
  257. */
  258. }
  259. /**
  260. * @brief DeInitializes the DCMI MSP.
  261. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  262. * the configuration information for DCMI.
  263. * @retval None
  264. */
  265. __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
  266. {
  267. /* Prevent unused argument(s) compilation warning */
  268. UNUSED(hdcmi);
  269. /* NOTE : This function Should not be modified, when the callback is needed,
  270. the HAL_DCMI_MspDeInit could be implemented in the user file
  271. */
  272. }
  273. /**
  274. * @}
  275. */
  276. /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
  277. * @brief IO operation functions
  278. *
  279. @verbatim
  280. ===============================================================================
  281. ##### IO operation functions #####
  282. ===============================================================================
  283. [..] This section provides functions allowing to:
  284. (+) Configure destination address and data length and
  285. Enables DCMI DMA request and enables DCMI capture
  286. (+) Stop the DCMI capture.
  287. (+) Handles DCMI interrupt request.
  288. @endverbatim
  289. * @{
  290. */
  291. /**
  292. * @brief Enables DCMI DMA request and enables DCMI capture
  293. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  294. * the configuration information for DCMI.
  295. * @param DCMI_Mode DCMI capture mode snapshot or continuous grab.
  296. * @param pData The destination memory Buffer address (LCD Frame buffer).
  297. * @param Length The length of capture to be transferred.
  298. * @retval HAL status
  299. */
  300. HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
  301. {
  302. /* Initialize the second memory address */
  303. uint32_t SecondMemAddress = 0U;
  304. /* Check function parameters */
  305. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  306. /* Process Locked */
  307. __HAL_LOCK(hdcmi);
  308. /* Lock the DCMI peripheral state */
  309. hdcmi->State = HAL_DCMI_STATE_BUSY;
  310. /* Enable DCMI by setting DCMIEN bit */
  311. __HAL_DCMI_ENABLE(hdcmi);
  312. /* Configure the DCMI Mode */
  313. hdcmi->Instance->CR &= ~(DCMI_CR_CM);
  314. hdcmi->Instance->CR |= (uint32_t)(DCMI_Mode);
  315. /* Set the DMA memory0 conversion complete callback */
  316. hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
  317. /* Set the DMA error callback */
  318. hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
  319. /* Set the dma abort callback */
  320. hdcmi->DMA_Handle->XferAbortCallback = NULL;
  321. /* Reset transfer counters value */
  322. hdcmi->XferCount = 0U;
  323. hdcmi->XferTransferNumber = 0U;
  324. if(Length <= 0xFFFFU)
  325. {
  326. /* Enable the DMA Stream */
  327. HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length);
  328. }
  329. else /* DCMI_DOUBLE_BUFFER Mode */
  330. {
  331. /* Set the DMA memory1 conversion complete callback */
  332. hdcmi->DMA_Handle->XferM1CpltCallback = DCMI_DMAXferCplt;
  333. /* Initialize transfer parameters */
  334. hdcmi->XferCount = 1U;
  335. hdcmi->XferSize = Length;
  336. hdcmi->pBuffPtr = pData;
  337. /* Get the number of buffer */
  338. while(hdcmi->XferSize > 0xFFFFU)
  339. {
  340. hdcmi->XferSize = (hdcmi->XferSize/2U);
  341. hdcmi->XferCount = hdcmi->XferCount*2U;
  342. }
  343. /* Update DCMI counter and transfer number*/
  344. hdcmi->XferCount = (hdcmi->XferCount - 2U);
  345. hdcmi->XferTransferNumber = hdcmi->XferCount;
  346. /* Update second memory address */
  347. SecondMemAddress = (uint32_t)(pData + (4U*hdcmi->XferSize));
  348. /* Start DMA multi buffer transfer */
  349. HAL_DMAEx_MultiBufferStart_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, SecondMemAddress, hdcmi->XferSize);
  350. }
  351. /* Enable Capture */
  352. hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
  353. /* Release Lock */
  354. __HAL_UNLOCK(hdcmi);
  355. /* Return function status */
  356. return HAL_OK;
  357. }
  358. /**
  359. * @brief Disable DCMI DMA request and Disable DCMI capture
  360. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  361. * the configuration information for DCMI.
  362. * @retval HAL status
  363. */
  364. HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
  365. {
  366. __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
  367. HAL_StatusTypeDef status = HAL_OK;
  368. /* Process locked */
  369. __HAL_LOCK(hdcmi);
  370. /* Lock the DCMI peripheral state */
  371. hdcmi->State = HAL_DCMI_STATE_BUSY;
  372. /* Disable Capture */
  373. hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
  374. /* Check if the DCMI capture effectively disabled */
  375. do
  376. {
  377. if (count-- == 0U)
  378. {
  379. /* Update error code */
  380. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  381. status = HAL_TIMEOUT;
  382. break;
  383. }
  384. }
  385. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0U);
  386. /* Disable the DCMI */
  387. __HAL_DCMI_DISABLE(hdcmi);
  388. /* Disable the DMA */
  389. HAL_DMA_Abort(hdcmi->DMA_Handle);
  390. /* Update error code */
  391. hdcmi->ErrorCode |= HAL_DCMI_ERROR_NONE;
  392. /* Change DCMI state */
  393. hdcmi->State = HAL_DCMI_STATE_READY;
  394. /* Process Unlocked */
  395. __HAL_UNLOCK(hdcmi);
  396. /* Return function status */
  397. return status;
  398. }
  399. /**
  400. * @brief Suspend DCMI capture
  401. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  402. * the configuration information for DCMI.
  403. * @retval HAL status
  404. */
  405. HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
  406. {
  407. __IO uint32_t count = SystemCoreClock / HAL_TIMEOUT_DCMI_STOP;
  408. HAL_StatusTypeDef status = HAL_OK;
  409. /* Process locked */
  410. __HAL_LOCK(hdcmi);
  411. if(hdcmi->State == HAL_DCMI_STATE_BUSY)
  412. {
  413. /* Change DCMI state */
  414. hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
  415. /* Disable Capture */
  416. hdcmi->Instance->CR &= ~(DCMI_CR_CAPTURE);
  417. /* Check if the DCMI capture effectively disabled */
  418. do
  419. {
  420. if (count-- == 0U)
  421. {
  422. /* Update error code */
  423. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  424. /* Change DCMI state */
  425. hdcmi->State = HAL_DCMI_STATE_READY;
  426. status = HAL_TIMEOUT;
  427. break;
  428. }
  429. }
  430. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0);
  431. }
  432. /* Process Unlocked */
  433. __HAL_UNLOCK(hdcmi);
  434. /* Return function status */
  435. return status;
  436. }
  437. /**
  438. * @brief Resume DCMI capture
  439. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  440. * the configuration information for DCMI.
  441. * @retval HAL status
  442. */
  443. HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
  444. {
  445. /* Process locked */
  446. __HAL_LOCK(hdcmi);
  447. if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
  448. {
  449. /* Change DCMI state */
  450. hdcmi->State = HAL_DCMI_STATE_BUSY;
  451. /* Disable Capture */
  452. hdcmi->Instance->CR |= DCMI_CR_CAPTURE;
  453. }
  454. /* Process Unlocked */
  455. __HAL_UNLOCK(hdcmi);
  456. /* Return function status */
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Handles DCMI interrupt request.
  461. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  462. * the configuration information for the DCMI.
  463. * @retval None
  464. */
  465. void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
  466. {
  467. uint32_t isr_value = READ_REG(hdcmi->Instance->MISR);
  468. /* Synchronization error interrupt management *******************************/
  469. if((isr_value & DCMI_FLAG_ERRRI) == DCMI_FLAG_ERRRI)
  470. {
  471. /* Clear the Synchronization error flag */
  472. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
  473. /* Update error code */
  474. hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
  475. /* Change DCMI state */
  476. hdcmi->State = HAL_DCMI_STATE_ERROR;
  477. /* Set the synchronization error callback */
  478. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  479. /* Abort the DMA Transfer */
  480. HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
  481. }
  482. /* Overflow interrupt management ********************************************/
  483. if((isr_value & DCMI_FLAG_OVRRI) == DCMI_FLAG_OVRRI)
  484. {
  485. /* Clear the Overflow flag */
  486. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
  487. /* Update error code */
  488. hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
  489. /* Change DCMI state */
  490. hdcmi->State = HAL_DCMI_STATE_ERROR;
  491. /* Set the overflow callback */
  492. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  493. /* Abort the DMA Transfer */
  494. HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
  495. }
  496. /* Line Interrupt management ************************************************/
  497. if((isr_value & DCMI_FLAG_LINERI) == DCMI_FLAG_LINERI)
  498. {
  499. /* Clear the Line interrupt flag */
  500. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
  501. /* Line interrupt Callback */
  502. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  503. /*Call registered DCMI line event callback*/
  504. hdcmi->LineEventCallback(hdcmi);
  505. #else
  506. HAL_DCMI_LineEventCallback(hdcmi);
  507. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  508. }
  509. /* VSYNC interrupt management ***********************************************/
  510. if((isr_value & DCMI_FLAG_VSYNCRI) == DCMI_FLAG_VSYNCRI)
  511. {
  512. /* Clear the VSYNC flag */
  513. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
  514. /* VSYNC Callback */
  515. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  516. /*Call registered DCMI vsync event callback*/
  517. hdcmi->VsyncEventCallback(hdcmi);
  518. #else
  519. HAL_DCMI_VsyncEventCallback(hdcmi);
  520. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  521. }
  522. /* FRAME interrupt management ***********************************************/
  523. if((isr_value & DCMI_FLAG_FRAMERI) == DCMI_FLAG_FRAMERI)
  524. {
  525. /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
  526. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  527. {
  528. /* Disable the Line, Vsync, Error and Overrun interrupts */
  529. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  530. }
  531. /* Disable the Frame interrupt */
  532. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_FRAME);
  533. /* Frame Callback */
  534. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  535. /*Call registered DCMI frame event callback*/
  536. hdcmi->FrameEventCallback(hdcmi);
  537. #else
  538. HAL_DCMI_FrameEventCallback(hdcmi);
  539. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  540. }
  541. }
  542. /**
  543. * @brief Error DCMI callback.
  544. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  545. * the configuration information for DCMI.
  546. * @retval None
  547. */
  548. __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  549. {
  550. /* Prevent unused argument(s) compilation warning */
  551. UNUSED(hdcmi);
  552. /* NOTE : This function Should not be modified, when the callback is needed,
  553. the HAL_DCMI_ErrorCallback could be implemented in the user file
  554. */
  555. }
  556. /**
  557. * @brief Line Event callback.
  558. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  559. * the configuration information for DCMI.
  560. * @retval None
  561. */
  562. __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  563. {
  564. /* Prevent unused argument(s) compilation warning */
  565. UNUSED(hdcmi);
  566. /* NOTE : This function Should not be modified, when the callback is needed,
  567. the HAL_DCMI_LineEventCallback could be implemented in the user file
  568. */
  569. }
  570. /**
  571. * @brief VSYNC Event callback.
  572. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  573. * the configuration information for DCMI.
  574. * @retval None
  575. */
  576. __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  577. {
  578. /* Prevent unused argument(s) compilation warning */
  579. UNUSED(hdcmi);
  580. /* NOTE : This function Should not be modified, when the callback is needed,
  581. the HAL_DCMI_VsyncEventCallback could be implemented in the user file
  582. */
  583. }
  584. /**
  585. * @brief Frame Event callback.
  586. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  587. * the configuration information for DCMI.
  588. * @retval None
  589. */
  590. __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  591. {
  592. /* Prevent unused argument(s) compilation warning */
  593. UNUSED(hdcmi);
  594. /* NOTE : This function Should not be modified, when the callback is needed,
  595. the HAL_DCMI_FrameEventCallback could be implemented in the user file
  596. */
  597. }
  598. /**
  599. * @}
  600. */
  601. /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
  602. * @brief Peripheral Control functions
  603. *
  604. @verbatim
  605. ===============================================================================
  606. ##### Peripheral Control functions #####
  607. ===============================================================================
  608. [..] This section provides functions allowing to:
  609. (+) Configure the CROP feature.
  610. (+) Enable/Disable the CROP feature.
  611. @endverbatim
  612. * @{
  613. */
  614. /**
  615. * @brief Configure the DCMI CROP coordinate.
  616. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  617. * the configuration information for DCMI.
  618. * @param X0 DCMI window X offset
  619. * @param Y0 DCMI window Y offset
  620. * @param XSize DCMI Pixel per line
  621. * @param YSize DCMI Line number
  622. * @retval HAL status
  623. */
  624. HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
  625. {
  626. /* Process Locked */
  627. __HAL_LOCK(hdcmi);
  628. /* Lock the DCMI peripheral state */
  629. hdcmi->State = HAL_DCMI_STATE_BUSY;
  630. /* Check the parameters */
  631. assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
  632. assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
  633. assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
  634. assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
  635. /* Configure CROP */
  636. hdcmi->Instance->CWSIZER = (XSize | (YSize << DCMI_POSITION_CWSIZE_VLINE));
  637. hdcmi->Instance->CWSTRTR = (X0 | (Y0 << DCMI_POSITION_CWSTRT_VST));
  638. /* Initialize the DCMI state*/
  639. hdcmi->State = HAL_DCMI_STATE_READY;
  640. /* Process Unlocked */
  641. __HAL_UNLOCK(hdcmi);
  642. return HAL_OK;
  643. }
  644. /**
  645. * @brief Disable the Crop feature.
  646. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  647. * the configuration information for DCMI.
  648. * @retval HAL status
  649. */
  650. HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
  651. {
  652. /* Process Locked */
  653. __HAL_LOCK(hdcmi);
  654. /* Lock the DCMI peripheral state */
  655. hdcmi->State = HAL_DCMI_STATE_BUSY;
  656. /* Disable DCMI Crop feature */
  657. hdcmi->Instance->CR &= ~(uint32_t)DCMI_CR_CROP;
  658. /* Change the DCMI state*/
  659. hdcmi->State = HAL_DCMI_STATE_READY;
  660. /* Process Unlocked */
  661. __HAL_UNLOCK(hdcmi);
  662. return HAL_OK;
  663. }
  664. /**
  665. * @brief Enable the Crop feature.
  666. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  667. * the configuration information for DCMI.
  668. * @retval HAL status
  669. */
  670. HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
  671. {
  672. /* Process Locked */
  673. __HAL_LOCK(hdcmi);
  674. /* Lock the DCMI peripheral state */
  675. hdcmi->State = HAL_DCMI_STATE_BUSY;
  676. /* Enable DCMI Crop feature */
  677. hdcmi->Instance->CR |= (uint32_t)DCMI_CR_CROP;
  678. /* Change the DCMI state*/
  679. hdcmi->State = HAL_DCMI_STATE_READY;
  680. /* Process Unlocked */
  681. __HAL_UNLOCK(hdcmi);
  682. return HAL_OK;
  683. }
  684. /**
  685. * @brief Set embedded synchronization delimiters unmasks.
  686. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  687. * the configuration information for DCMI.
  688. * @param SyncUnmask pointer to a DCMI_SyncUnmaskTypeDef structure that contains
  689. * the embedded synchronization delimiters unmasks.
  690. * @retval HAL status
  691. */
  692. HAL_StatusTypeDef HAL_DCMI_ConfigSyncUnmask(DCMI_HandleTypeDef *hdcmi, DCMI_SyncUnmaskTypeDef *SyncUnmask)
  693. {
  694. /* Process Locked */
  695. __HAL_LOCK(hdcmi);
  696. /* Lock the DCMI peripheral state */
  697. hdcmi->State = HAL_DCMI_STATE_BUSY;
  698. /* Write DCMI embedded synchronization unmask register */
  699. hdcmi->Instance->ESUR = (((uint32_t)SyncUnmask->FrameStartUnmask) |\
  700. ((uint32_t)SyncUnmask->LineStartUnmask << DCMI_ESUR_LSU_Pos)|\
  701. ((uint32_t)SyncUnmask->LineEndUnmask << DCMI_ESUR_LEU_Pos)|\
  702. ((uint32_t)SyncUnmask->FrameEndUnmask << DCMI_ESUR_FEU_Pos));
  703. /* Change the DCMI state*/
  704. hdcmi->State = HAL_DCMI_STATE_READY;
  705. /* Process Unlocked */
  706. __HAL_UNLOCK(hdcmi);
  707. return HAL_OK;
  708. }
  709. /**
  710. * @}
  711. */
  712. /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
  713. * @brief Peripheral State functions
  714. *
  715. @verbatim
  716. ===============================================================================
  717. ##### Peripheral State and Errors functions #####
  718. ===============================================================================
  719. [..]
  720. This subsection provides functions allowing to
  721. (+) Check the DCMI state.
  722. (+) Get the specific DCMI error flag.
  723. @endverbatim
  724. * @{
  725. */
  726. /**
  727. * @brief Return the DCMI state
  728. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  729. * the configuration information for DCMI.
  730. * @retval HAL state
  731. */
  732. HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
  733. {
  734. return hdcmi->State;
  735. }
  736. /**
  737. * @brief Return the DCMI error code
  738. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  739. * the configuration information for DCMI.
  740. * @retval DCMI Error Code
  741. */
  742. uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
  743. {
  744. return hdcmi->ErrorCode;
  745. }
  746. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  747. /**
  748. * @brief DCMI Callback registering
  749. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  750. * the configuration information for DCMI.
  751. * @param CallbackID dcmi Callback ID
  752. * @param pCallback pointer to DCMI_CallbackTypeDef structure
  753. * @retval status
  754. */
  755. HAL_StatusTypeDef HAL_DCMI_RegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID, pDCMI_CallbackTypeDef pCallback)
  756. {
  757. HAL_StatusTypeDef status = HAL_OK;
  758. if(pCallback == NULL)
  759. {
  760. /* update the error code */
  761. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  762. /* update return status */
  763. status = HAL_ERROR;
  764. }
  765. else
  766. {
  767. if(hdcmi->State == HAL_DCMI_STATE_READY)
  768. {
  769. switch (CallbackID)
  770. {
  771. case HAL_DCMI_FRAME_EVENT_CB_ID :
  772. hdcmi->FrameEventCallback = pCallback;
  773. break;
  774. case HAL_DCMI_VSYNC_EVENT_CB_ID :
  775. hdcmi->VsyncEventCallback = pCallback;
  776. break;
  777. case HAL_DCMI_LINE_EVENT_CB_ID :
  778. hdcmi->LineEventCallback = pCallback;
  779. break;
  780. case HAL_DCMI_ERROR_CB_ID :
  781. hdcmi->ErrorCallback = pCallback;
  782. break;
  783. case HAL_DCMI_MSPINIT_CB_ID :
  784. hdcmi->MspInitCallback = pCallback;
  785. break;
  786. case HAL_DCMI_MSPDEINIT_CB_ID :
  787. hdcmi->MspDeInitCallback = pCallback;
  788. break;
  789. default :
  790. /* Return error status */
  791. status = HAL_ERROR;
  792. break;
  793. }
  794. }
  795. else if(hdcmi->State == HAL_DCMI_STATE_RESET)
  796. {
  797. switch (CallbackID)
  798. {
  799. case HAL_DCMI_MSPINIT_CB_ID :
  800. hdcmi->MspInitCallback = pCallback;
  801. break;
  802. case HAL_DCMI_MSPDEINIT_CB_ID :
  803. hdcmi->MspDeInitCallback = pCallback;
  804. break;
  805. default :
  806. /* update the error code */
  807. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  808. /* update return status */
  809. status = HAL_ERROR;
  810. break;
  811. }
  812. }
  813. else
  814. {
  815. /* update the error code */
  816. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  817. /* update return status */
  818. status = HAL_ERROR;
  819. }
  820. }
  821. return status;
  822. }
  823. /**
  824. * @brief DCMI Callback Unregistering
  825. * @param hdcmi dcmi handle
  826. * @param CallbackID dcmi Callback ID
  827. * @retval status
  828. */
  829. HAL_StatusTypeDef HAL_DCMI_UnRegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID)
  830. {
  831. HAL_StatusTypeDef status = HAL_OK;
  832. if(hdcmi->State == HAL_DCMI_STATE_READY)
  833. {
  834. switch (CallbackID)
  835. {
  836. case HAL_DCMI_FRAME_EVENT_CB_ID :
  837. hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
  838. break;
  839. case HAL_DCMI_VSYNC_EVENT_CB_ID :
  840. hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
  841. break;
  842. case HAL_DCMI_LINE_EVENT_CB_ID :
  843. hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
  844. break;
  845. case HAL_DCMI_ERROR_CB_ID :
  846. hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
  847. break;
  848. case HAL_DCMI_MSPINIT_CB_ID :
  849. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  850. break;
  851. case HAL_DCMI_MSPDEINIT_CB_ID :
  852. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  853. break;
  854. default :
  855. /* update the error code */
  856. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  857. /* update return status */
  858. status = HAL_ERROR;
  859. break;
  860. }
  861. }
  862. else if(hdcmi->State == HAL_DCMI_STATE_RESET)
  863. {
  864. switch (CallbackID)
  865. {
  866. case HAL_DCMI_MSPINIT_CB_ID :
  867. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  868. break;
  869. case HAL_DCMI_MSPDEINIT_CB_ID :
  870. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  871. break;
  872. default :
  873. /* update the error code */
  874. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  875. /* update return status */
  876. status = HAL_ERROR;
  877. break;
  878. }
  879. }
  880. else
  881. {
  882. /* update the error code */
  883. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  884. /* update return status */
  885. status = HAL_ERROR;
  886. }
  887. return status;
  888. }
  889. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  890. /**
  891. * @}
  892. */
  893. /* Private functions ---------------------------------------------------------*/
  894. /** @defgroup DCMI_Private_Functions DCMI Private Functions
  895. * @{
  896. */
  897. /**
  898. * @brief DMA conversion complete callback.
  899. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  900. * the configuration information for the specified DMA module.
  901. * @retval None
  902. */
  903. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
  904. {
  905. uint32_t tmp = 0U;
  906. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  907. if(hdcmi->XferCount != 0U)
  908. {
  909. /* Update memory 0 address location */
  910. tmp = ((hdcmi->DMA_Handle->Instance->CR) & DMA_SxCR_CT);
  911. if(((hdcmi->XferCount % 2U) == 0U) && (tmp != 0U))
  912. {
  913. tmp = hdcmi->DMA_Handle->Instance->M0AR;
  914. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY0);
  915. hdcmi->XferCount--;
  916. }
  917. /* Update memory 1 address location */
  918. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
  919. {
  920. tmp = hdcmi->DMA_Handle->Instance->M1AR;
  921. HAL_DMAEx_ChangeMemory(hdcmi->DMA_Handle, (tmp + (8U*hdcmi->XferSize)), MEMORY1);
  922. hdcmi->XferCount--;
  923. }
  924. }
  925. /* Update memory 0 address location */
  926. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) != 0U)
  927. {
  928. hdcmi->DMA_Handle->Instance->M0AR = hdcmi->pBuffPtr;
  929. }
  930. /* Update memory 1 address location */
  931. else if((hdcmi->DMA_Handle->Instance->CR & DMA_SxCR_CT) == 0U)
  932. {
  933. tmp = hdcmi->pBuffPtr;
  934. hdcmi->DMA_Handle->Instance->M1AR = (tmp + (4U*hdcmi->XferSize));
  935. hdcmi->XferCount = hdcmi->XferTransferNumber;
  936. }
  937. /* Check if the frame is transferred */
  938. if(hdcmi->XferCount == hdcmi->XferTransferNumber)
  939. {
  940. /* Enable the Frame interrupt */
  941. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME);
  942. /* When snapshot mode, set dcmi state to ready */
  943. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  944. {
  945. hdcmi->State= HAL_DCMI_STATE_READY;
  946. }
  947. }
  948. }
  949. /**
  950. * @brief DMA error callback
  951. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  952. * the configuration information for the specified DMA module.
  953. * @retval None
  954. */
  955. static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
  956. {
  957. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  958. if(hdcmi->DMA_Handle->ErrorCode != HAL_DMA_ERROR_FE)
  959. {
  960. /* Initialize the DCMI state*/
  961. hdcmi->State = HAL_DCMI_STATE_READY;
  962. }
  963. /* DCMI error Callback */
  964. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  965. /*Call registered DCMI error callback*/
  966. hdcmi->ErrorCallback(hdcmi);
  967. #else
  968. HAL_DCMI_ErrorCallback(hdcmi);
  969. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  970. }
  971. /**
  972. * @}
  973. */
  974. /**
  975. * @}
  976. */
  977. #endif /* STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  978. STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\
  979. STM32F479xx */
  980. #endif /* HAL_DCMI_MODULE_ENABLED */
  981. /**
  982. * @}
  983. */
  984. /**
  985. * @}
  986. */