stm32h7xx_hal_dcmi.c 29 KB

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