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.
 
 
 

1272 lines
42 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_dcmi.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  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 an 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 configuring and enabling
  24. the DCMI.
  25. (#) Program the required configuration through the following parameters:
  26. horizontal and vertical polarity, pixel clock polarity, capture rate,
  27. synchronization mode, frame delimiter codes, data width, byte and line
  28. selection using HAL_DCMI_Init() function.
  29. (#) Optionally select JPEG mode; in that case, only the polarity
  30. and the capture mode parameters need to be set.
  31. (#) Capture mode can be either snapshot or continuous mode.
  32. (#) Configure the DMA_Handle to transfer data from DCMI DR
  33. register to the destination memory buffer.
  34. -@- In snapshot mode, the interface transfers a single frame through DMA. In
  35. continuous mode, the DMA must be set in circular mode to ensure a continuous
  36. flow of images data samples.
  37. (#) Program the transfer configuration through the following parameters:
  38. DCMI mode, destination memory buffer address and data length then
  39. enable capture using HAL_DCMI_Start_DMA() function.
  40. (#) Whether in continuous or snapshot mode, data length parameter must be
  41. equal to the frame size.
  42. (#) When the frame size is unknown beforehand (e.g. JPEG case), data length must
  43. be large enough to ensure the capture of a frame.
  44. (#) If the frame size is larger than the maximum DMA transfer length (i.e. 65535),
  45. (++) resort to a user-defined work buffer and associate it to the DCMI handle with
  46. the function HAL_DCMIEx_WorkBuffer_Associate(),
  47. (++) the DMA must be configured in circular mode, either for snapshot or continuous
  48. capture mode,
  49. (++) during capture, the driver copies the image data samples from DCMI DR register
  50. to the work buffer,
  51. (++) at each DMA half (respectively complete) transfer interrupt, the first
  52. (resp. second) half of the work buffer is copied to the final destination thru
  53. a second DMA channel.
  54. (++) Parameters of this second DMA channel are contained in the memory to memory DMA
  55. handle "DMAM2M_Handle", itself field of the DCMI handle structure.
  56. (++) This memory to memory transfer has length half that of the work buffer and is
  57. carried out in normal mode (not in circular mode).
  58. (#) Optionally, configure and enable the CROP feature to select a
  59. rectangular window from the received image using HAL_DCMI_ConfigCrop()
  60. and HAL_DCMI_EnableCrop() functions. Use HAL_DCMI_DisableCrop() to
  61. disable this feature.
  62. (#) The capture can be stopped with HAL_DCMI_Stop() function.
  63. (#) To control the DCMI state, use the function HAL_DCMI_GetState().
  64. (#) To read the DCMI error code, use the function HAL_DCMI_GetError().
  65. [..]
  66. (@) When the frame size is less than the maximum DMA transfer length (i.e. 65535)
  67. and when in snapshot mode, user must make sure the FRAME interrupt is disabled.
  68. This allows to avoid corner cases where the FRAME interrupt might be triggered
  69. before the DMA transfer completion interrupt. In this specific configuration,
  70. the driver checks the FRAME capture flag after the DMA transfer end and calls
  71. HAL_DCMI_FrameEventCallback() if the flag is set.
  72. *** DCMI HAL driver macros list ***
  73. =============================================
  74. [..]
  75. Below the list of most used macros in DCMI HAL driver.
  76. (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
  77. (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
  78. (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
  79. (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
  80. (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
  81. (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
  82. (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred
  83. and that the interruption is enabled at the same time.
  84. @endverbatim
  85. ******************************************************************************
  86. * @attention
  87. *
  88. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  89. *
  90. * Redistribution and use in source and binary forms, with or without modification,
  91. * are permitted provided that the following conditions are met:
  92. * 1. Redistributions of source code must retain the above copyright notice,
  93. * this list of conditions and the following disclaimer.
  94. * 2. Redistributions in binary form must reproduce the above copyright notice,
  95. * this list of conditions and the following disclaimer in the documentation
  96. * and/or other materials provided with the distribution.
  97. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  98. * may be used to endorse or promote products derived from this software
  99. * without specific prior written permission.
  100. *
  101. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  102. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  103. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  104. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  105. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  106. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  107. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  108. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  109. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  110. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  111. *
  112. ******************************************************************************
  113. */
  114. /* Includes ------------------------------------------------------------------*/
  115. #include "stm32l4xx_hal.h"
  116. #ifdef HAL_DCMI_MODULE_ENABLED
  117. #if defined(STM32L496xx) || defined(STM32L4A6xx)
  118. /** @addtogroup STM32L4xx_HAL_Driver
  119. * @{
  120. */
  121. /** @defgroup DCMI DCMI
  122. * @brief DCMI HAL module driver
  123. * @{
  124. */
  125. /* Private typedef -----------------------------------------------------------*/
  126. /* Private define ------------------------------------------------------------*/
  127. /** @defgroup DCMI_Private_Constants DCMI Private Constants
  128. * @{
  129. */
  130. /** @defgroup DCMI_Stop_TimeOut DCMI Stop TimeOut
  131. * @{
  132. */
  133. #define DCMI_TIMEOUT_STOP ((uint32_t)1000) /*!< 1s */
  134. /**
  135. * @}
  136. */
  137. /** @defgroup DCMI_Shifts DCMI Shifts
  138. * @{
  139. */
  140. #define DCMI_POSITION_CWSIZE_VLINE (uint32_t)POSITION_VAL(DCMI_CWSIZE_VLINE) /*!< Required left shift to set crop window vertical line count */
  141. #define DCMI_POSITION_CWSTRT_VST (uint32_t)POSITION_VAL(DCMI_CWSTRT_VST) /*!< Required left shift to set crop window vertical start line count */
  142. #define DCMI_POSITION_ESCR_LSC (uint32_t)POSITION_VAL(DCMI_ESCR_LSC) /*!< Required left shift to set line start delimiter */
  143. #define DCMI_POSITION_ESCR_LEC (uint32_t)POSITION_VAL(DCMI_ESCR_LEC) /*!< Required left shift to set line end delimiter */
  144. #define DCMI_POSITION_ESCR_FEC (uint32_t)POSITION_VAL(DCMI_ESCR_FEC) /*!< Required left shift to set frame end delimiter */
  145. #define DCMI_POSITION_ESUR_LSU (uint32_t)POSITION_VAL(DCMI_ESUR_LSU) /*!< Required left shift to set line start delimiter unmask */
  146. #define DCMI_POSITION_ESUR_LEU (uint32_t)POSITION_VAL(DCMI_ESUR_LEU) /*!< Required left shift to set line end delimiter unmask */
  147. #define DCMI_POSITION_ESUR_FEU (uint32_t)POSITION_VAL(DCMI_ESUR_FEU) /*!< Required left shift to set frame end delimiter unmask */
  148. /**
  149. * @}
  150. */
  151. #define NPRIME 16
  152. /**
  153. * @}
  154. */
  155. /* Private macro -------------------------------------------------------------*/
  156. /* Private variables ---------------------------------------------------------*/
  157. uint32_t PrimeArray[NPRIME] = { 1, 2, 3, 5,
  158. 7, 11, 13, 17,
  159. 19, 23, 29, 31,
  160. 37, 41, 43, 47};
  161. /* Private function prototypes -----------------------------------------------*/
  162. /** @addtogroup DCMI_Private_Functions DCMI Private Functions
  163. * @{
  164. */
  165. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma);
  166. static void DCMI_DMAHalfXferCplt(DMA_HandleTypeDef *hdma);
  167. static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
  168. static uint32_t DCMI_TransferSize(uint32_t InputSize);
  169. /**
  170. * @}
  171. */
  172. /* Exported functions --------------------------------------------------------*/
  173. /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
  174. * @{
  175. */
  176. /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
  177. * @brief Initialization and Configuration functions
  178. *
  179. @verbatim
  180. ===============================================================================
  181. ##### Initialization and Configuration functions #####
  182. ===============================================================================
  183. [..] This section provides functions allowing to:
  184. (+) Initialize and configure the DCMI
  185. (+) De-initialize the DCMI
  186. @endverbatim
  187. * @{
  188. */
  189. /**
  190. * @brief Initialize the DCMI according to the specified
  191. * parameters in the DCMI_InitTypeDef and create the associated handle.
  192. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  193. * the configuration information for DCMI.
  194. * @note By default, all interruptions are enabled (line end, frame end, overrun,
  195. * VSYNC and embedded synchronization error interrupts).
  196. * @retval HAL status
  197. */
  198. HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
  199. {
  200. /* Check the DCMI peripheral state */
  201. if(hdcmi == NULL)
  202. {
  203. return HAL_ERROR;
  204. }
  205. /* Check function parameters */
  206. assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
  207. assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
  208. assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
  209. assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
  210. assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
  211. assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
  212. assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
  213. assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
  214. assert_param(IS_DCMI_BYTE_SELECT_MODE(hdcmi->Init.ByteSelectMode));
  215. assert_param(IS_DCMI_BYTE_SELECT_START(hdcmi->Init.ByteSelectStart));
  216. assert_param(IS_DCMI_LINE_SELECT_MODE(hdcmi->Init.LineSelectMode));
  217. assert_param(IS_DCMI_LINE_SELECT_START(hdcmi->Init.LineSelectStart));
  218. if(hdcmi->State == HAL_DCMI_STATE_RESET)
  219. {
  220. /* Allocate lock resource and initialize it */
  221. hdcmi->Lock = HAL_UNLOCKED;
  222. /* Init the low level hardware */
  223. HAL_DCMI_MspInit(hdcmi);
  224. }
  225. /* Change the DCMI state */
  226. hdcmi->State = HAL_DCMI_STATE_BUSY;
  227. /* Disable DCMI IP before setting the configuration register */
  228. __HAL_DCMI_DISABLE(hdcmi);
  229. if (hdcmi->Init.ExtendedDataMode != DCMI_EXTEND_DATA_8B)
  230. {
  231. /* Byte select mode must be programmed to the reset value if the extended mode
  232. is not set to 8-bit data capture on every pixel clock */
  233. hdcmi->Init.ByteSelectMode = DCMI_BSM_ALL;
  234. }
  235. /* Set DCMI parameters */
  236. hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |\
  237. DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |\
  238. DCMI_CR_ESS | DCMI_CR_BSM_0 | DCMI_CR_BSM_1 | DCMI_CR_OEBS |\
  239. DCMI_CR_LSM | DCMI_CR_OELS);
  240. hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate |\
  241. hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity |\
  242. hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode |\
  243. hdcmi->Init.JPEGMode | hdcmi->Init.ByteSelectMode |\
  244. hdcmi->Init.ByteSelectStart | hdcmi->Init.LineSelectMode |\
  245. hdcmi->Init.LineSelectStart);
  246. if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
  247. {
  248. hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SynchroCode.FrameStartCode) |\
  249. ((uint32_t)hdcmi->Init.SynchroCode.LineStartCode << DCMI_POSITION_ESCR_LSC)|\
  250. ((uint32_t)hdcmi->Init.SynchroCode.LineEndCode << DCMI_POSITION_ESCR_LEC) |\
  251. ((uint32_t)hdcmi->Init.SynchroCode.FrameEndCode << DCMI_POSITION_ESCR_FEC));
  252. }
  253. /* By default, enable all interrupts. The user may disable the unwanted ones
  254. in resorting to __HAL_DCMI_DISABLE_IT() macro before invoking HAL_DCMI_Start_DMA().
  255. Enabled interruptions are
  256. - end of line
  257. - end of frame
  258. - data reception overrun
  259. - frame synchronization signal VSYNC
  260. - synchronization error */
  261. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME|DCMI_IT_OVR|DCMI_IT_ERR|DCMI_IT_VSYNC|DCMI_IT_LINE);
  262. /* Update error code */
  263. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  264. /* Initialize the DCMI state*/
  265. hdcmi->State = HAL_DCMI_STATE_READY;
  266. return HAL_OK;
  267. }
  268. /**
  269. * @brief De-initialize the DCMI peripheral, reset control registers to
  270. * their default values.
  271. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  272. * the configuration information for DCMI.
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
  276. {
  277. /* Before aborting any DCMI transfer, check
  278. first whether or not DCMI clock is enabled */
  279. if (__HAL_RCC_DCMI_IS_CLK_ENABLED())
  280. {
  281. if (HAL_DCMI_Stop(hdcmi) != HAL_OK)
  282. {
  283. /* Issue when stopping DCMI IP */
  284. return HAL_ERROR;
  285. }
  286. }
  287. /* DeInit the DCMI low level hardware */
  288. HAL_DCMI_MspDeInit(hdcmi);
  289. /* Reset DCMI control register */
  290. hdcmi->Instance->CR = 0;
  291. /* Update error code */
  292. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  293. /* Initialize the DCMI state*/
  294. hdcmi->State = HAL_DCMI_STATE_RESET;
  295. /* Release Lock */
  296. __HAL_UNLOCK(hdcmi);
  297. return HAL_OK;
  298. }
  299. /**
  300. * @brief Initialize the DCMI MSP.
  301. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  302. * the configuration information for DCMI.
  303. * @retval None
  304. */
  305. __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
  306. {
  307. /* Prevent unused argument(s) compilation warning */
  308. UNUSED(hdcmi);
  309. /* NOTE : This function should not be modified; when the callback is needed,
  310. the HAL_DCMI_MspInit() callback can be implemented in the user file
  311. */
  312. }
  313. /**
  314. * @brief De-initialize the DCMI MSP.
  315. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  316. * the configuration information for DCMI.
  317. * @retval None
  318. */
  319. __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
  320. {
  321. /* Prevent unused argument(s) compilation warning */
  322. UNUSED(hdcmi);
  323. /* NOTE : This function should not be modified; when the callback is needed,
  324. the HAL_DCMI_MspDeInit() callback can be implemented in the user file
  325. */
  326. }
  327. /**
  328. * @}
  329. */
  330. /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
  331. * @brief IO operation functions
  332. *
  333. @verbatim
  334. ===============================================================================
  335. ##### IO operation functions #####
  336. ===============================================================================
  337. [..] This section provides functions allowing to:
  338. (+) Configure destination address and data length,
  339. enable DCMI DMA request and DCMI capture.
  340. (+) Stop DCMI capture.
  341. (+) Handle DCMI interrupt request.
  342. [..] A set of callbacks is provided:
  343. (+) HAL_DCMI_ErrorCallback()
  344. (+) HAL_DCMI_LineEventCallback()
  345. (+) HAL_DCMI_VsyncEventCallback()
  346. (+) HAL_DCMI_FrameEventCallback()
  347. @endverbatim
  348. * @{
  349. */
  350. /**
  351. * @brief Enable DCMI capture in DMA mode.
  352. * @param hdcmi: Pointer to a DCMI_HandleTypeDef structure that contains
  353. * the configuration information for DCMI.
  354. * @param DCMI_Mode: DCMI capture mode snapshot or continuous grab.
  355. * @param pData: The destination memory buffer address.
  356. * @param Length: The length of capture to be transferred (in 32-bit words).
  357. * @note In case of length larger than 65535 (0xFFFF is the DMA maximum transfer length),
  358. * user must beforehand associate a work buffer to the DCMI handle thru the API
  359. * HAL_DCMIEx_WorkBuffer_Associate(). Then, HAL_DCMI_Start_DMA()
  360. * initiates a circular DMA transfer from DCMI DR to the work buffer and each
  361. * half and complete transfer interrupt triggers a copy from the work buffer to
  362. * the final destination pData thru a second DMA channel.
  363. * @note Following HAL_DCMI_Init() call, all interruptions are enabled (line end,
  364. * frame end, overrun, VSYNC and embedded synchronization error interrupts).
  365. * User can disable unwanted interrupts thru __HAL_DCMI_DISABLE_IT() macro
  366. * before invoking HAL_DCMI_Start_DMA().
  367. * @note For length less than 0xFFFF (DMA maximum transfer length) and in snapshot mode,
  368. * frame interrupt is disabled before DMA transfer. FRAME capture flag is checked
  369. * in DCMI_DMAXferCplt callback at the end of the DMA transfer. If flag is set,
  370. * HAL_DCMI_FrameEventCallback() API is called.
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
  374. {
  375. uint32_t circular_copy_length = 0;
  376. /* Check capture parameter */
  377. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  378. /* Process Locked */
  379. __HAL_LOCK(hdcmi);
  380. /* Lock the DCMI peripheral state */
  381. hdcmi->State = HAL_DCMI_STATE_BUSY;
  382. /* Configure the DCMI Mode and enable the DCMI IP at the same time */
  383. MODIFY_REG(hdcmi->Instance->CR, (DCMI_CR_CM|DCMI_CR_ENABLE), (DCMI_Mode|DCMI_CR_ENABLE));
  384. /* Set the DMA conversion complete callback */
  385. hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
  386. /* Set the DMA error callback */
  387. hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
  388. if(Length <= 0xFFFF)
  389. {
  390. hdcmi->XferCount = 0; /* Mark as direct transfer from DCMI_DR register to final destination buffer */
  391. /* Enable the DMA channel */
  392. if (HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length) != HAL_OK)
  393. {
  394. /* Update error code */
  395. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  396. /* Set state back to Ready */
  397. hdcmi->State = HAL_DCMI_STATE_READY;
  398. /* Process Unlocked */
  399. __HAL_UNLOCK(hdcmi);
  400. return HAL_ERROR;
  401. }
  402. }
  403. else /* Capture length is longer than DMA maximum transfer size */
  404. {
  405. /* Set DMA in circular mode */
  406. hdcmi->DMA_Handle->Init.Mode = DMA_CIRCULAR;
  407. /* Set the DMA half transfer complete callback */
  408. hdcmi->DMA_Handle->XferHalfCpltCallback = DCMI_DMAHalfXferCplt;
  409. /* Initialize transfer parameters */
  410. hdcmi->XferSize = Length; /* Store the complete transfer length in DCMI handle */
  411. hdcmi->pBuffPtr = pData; /* Final destination buffer pointer */
  412. circular_copy_length = DCMI_TransferSize(Length);
  413. /* Check if issue in intermediate length computation */
  414. if (circular_copy_length == 0)
  415. {
  416. /* Set state back to Ready */
  417. hdcmi->State = HAL_DCMI_STATE_READY;
  418. /* Process Unlocked */
  419. __HAL_UNLOCK(hdcmi);
  420. return HAL_ERROR;
  421. }
  422. /* Store the number of half - intermediate buffer copies needed */
  423. hdcmi->XferCount = 2 * ((Length / circular_copy_length) - 1);
  424. /* Store the half-buffer copy length */
  425. hdcmi->HalfCopyLength = circular_copy_length / 2;
  426. /* DCMI DR samples in circular mode will be copied
  427. at the end of the final buffer.
  428. Now compute the circular buffer start address. */
  429. /* Start by pointing at the final buffer */
  430. hdcmi->pCircularBuffer = pData;
  431. /* Update pCircularBuffer in "moving" at the end of the final
  432. buffer, don't forger to convert in bytes to compute exact address */
  433. hdcmi->pCircularBuffer += 4 * (((Length / circular_copy_length) - 1) * circular_copy_length);
  434. /* Initiate the circular DMA transfer from DCMI IP to final buffer end */
  435. if ( HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)hdcmi->pCircularBuffer, circular_copy_length) != HAL_OK)
  436. {
  437. /* Update error code */
  438. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  439. /* Set state back to Ready */
  440. hdcmi->State = HAL_DCMI_STATE_READY;
  441. /* Process Unlocked */
  442. __HAL_UNLOCK(hdcmi);
  443. return HAL_ERROR;
  444. }
  445. }
  446. /* Enable Capture */
  447. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  448. /* Release Lock */
  449. __HAL_UNLOCK(hdcmi);
  450. /* Return function status */
  451. return HAL_OK;
  452. }
  453. /**
  454. * @brief Disable DCMI capture in DMA mode.
  455. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  456. * the configuration information for DCMI.
  457. * @retval HAL status
  458. */
  459. HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
  460. {
  461. uint32_t tickstart = 0;
  462. /* Process locked */
  463. __HAL_LOCK(hdcmi);
  464. /* Lock the DCMI peripheral state */
  465. hdcmi->State = HAL_DCMI_STATE_BUSY;
  466. /* Disable Capture */
  467. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  468. /* Get tick */
  469. tickstart = HAL_GetTick();
  470. /* Check if the DCMI capture is effectively disabled */
  471. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0)
  472. {
  473. if((HAL_GetTick() - tickstart ) > DCMI_TIMEOUT_STOP)
  474. {
  475. /* Update error code */
  476. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  477. /* Change DCMI state */
  478. hdcmi->State = HAL_DCMI_STATE_READY;
  479. /* Process Unlocked */
  480. __HAL_UNLOCK(hdcmi);
  481. return HAL_TIMEOUT;
  482. }
  483. }
  484. /* Disable the DMA */
  485. HAL_DMA_Abort(hdcmi->DMA_Handle);
  486. /* Disable DCMI IP */
  487. __HAL_DCMI_DISABLE(hdcmi);
  488. /* Change DCMI state */
  489. hdcmi->State = HAL_DCMI_STATE_READY;
  490. /* Process Unlocked */
  491. __HAL_UNLOCK(hdcmi);
  492. /* Return function status */
  493. return HAL_OK;
  494. }
  495. /**
  496. * @brief Suspend DCMI capture.
  497. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  498. * the configuration information for DCMI.
  499. * @retval HAL status
  500. */
  501. HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
  502. {
  503. uint32_t tickstart = 0;
  504. /* Process locked */
  505. __HAL_LOCK(hdcmi);
  506. if(hdcmi->State == HAL_DCMI_STATE_BUSY)
  507. {
  508. /* Change DCMI state */
  509. hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
  510. /* Disable Capture */
  511. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  512. /* Get tick */
  513. tickstart = HAL_GetTick();
  514. /* Check if the DCMI capture is effectively disabled */
  515. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0)
  516. {
  517. if((HAL_GetTick() - tickstart ) > DCMI_TIMEOUT_STOP)
  518. {
  519. /* Update error code */
  520. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  521. /* Change DCMI state */
  522. hdcmi->State = HAL_DCMI_STATE_READY;
  523. /* Process Unlocked */
  524. __HAL_UNLOCK(hdcmi);
  525. return HAL_TIMEOUT;
  526. }
  527. }
  528. }
  529. /* Process Unlocked */
  530. __HAL_UNLOCK(hdcmi);
  531. /* Return function status */
  532. return HAL_OK;
  533. }
  534. /**
  535. * @brief Resume DCMI capture.
  536. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  537. * the configuration information for DCMI.
  538. * @retval HAL status
  539. */
  540. HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
  541. {
  542. /* Process locked */
  543. __HAL_LOCK(hdcmi);
  544. if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
  545. {
  546. /* Change DCMI state */
  547. hdcmi->State = HAL_DCMI_STATE_BUSY;
  548. /* Enable Capture */
  549. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  550. }
  551. /* Process Unlocked */
  552. __HAL_UNLOCK(hdcmi);
  553. return HAL_OK;
  554. }
  555. /**
  556. * @brief Handle DCMI interrupt request.
  557. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  558. * the configuration information for the DCMI.
  559. * @retval None
  560. */
  561. void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
  562. {
  563. uint32_t misflags = READ_REG(hdcmi->Instance->MISR);
  564. /* Synchronization error interrupt management *******************************/
  565. if ((misflags & DCMI_MIS_ERR_MIS) != RESET)
  566. {
  567. /* Clear the Synchronization error flag */
  568. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
  569. /* Update error code */
  570. hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
  571. }
  572. /* Overflow interrupt management ********************************************/
  573. if ((misflags & DCMI_MIS_OVR_MIS) != RESET)
  574. {
  575. /* Clear the Overflow flag */
  576. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
  577. /* Update error code */
  578. hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
  579. }
  580. if (hdcmi->ErrorCode != HAL_DCMI_ERROR_NONE)
  581. {
  582. /* Change DCMI state */
  583. hdcmi->State = HAL_DCMI_STATE_READY;
  584. /* Set the overflow callback */
  585. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  586. /* Abort the DMA Transfer */
  587. HAL_DMA_Abort_IT(hdcmi->DMA_Handle);
  588. /* Error Callback */
  589. HAL_DCMI_ErrorCallback(hdcmi);
  590. }
  591. /* Line Interrupt management ************************************************/
  592. if ((misflags & DCMI_MIS_LINE_MIS) != RESET)
  593. {
  594. /* Clear the Line interrupt flag */
  595. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
  596. /* Line interrupt Callback */
  597. HAL_DCMI_LineEventCallback(hdcmi);
  598. }
  599. /* VSYNC interrupt management ***********************************************/
  600. if ((misflags & DCMI_MIS_VSYNC_MIS) != RESET)
  601. {
  602. /* Clear the VSYNC flag */
  603. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
  604. /* VSYNC Callback */
  605. HAL_DCMI_VsyncEventCallback(hdcmi);
  606. }
  607. /* End of Frame interrupt management ****************************************/
  608. if ((misflags & DCMI_MIS_FRAME_MIS) != RESET)
  609. {
  610. /* Disable the Line interrupt when using snapshot mode */
  611. if ((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  612. {
  613. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE|DCMI_IT_VSYNC|DCMI_IT_ERR|DCMI_IT_OVR);
  614. /* Change the DCMI state */
  615. hdcmi->State = HAL_DCMI_STATE_READY;
  616. }
  617. /* Clear the End of Frame flag */
  618. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
  619. /* End of Frame Callback */
  620. HAL_DCMI_FrameEventCallback(hdcmi);
  621. }
  622. }
  623. /**
  624. * @brief Error DCMI callback.
  625. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  626. * the configuration information for DCMI.
  627. * @retval None
  628. */
  629. __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  630. {
  631. /* Prevent unused argument(s) compilation warning */
  632. UNUSED(hdcmi);
  633. /* NOTE : This function should not be modified; when the callback is needed,
  634. the HAL_DCMI_ErrorCallback() callback can be implemented in the user file.
  635. */
  636. }
  637. /**
  638. * @brief Line Event callback.
  639. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  640. * the configuration information for DCMI.
  641. * @retval None
  642. */
  643. __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  644. {
  645. /* Prevent unused argument(s) compilation warning */
  646. UNUSED(hdcmi);
  647. /* NOTE : This function should not be modified; when the callback is needed,
  648. the HAL_DCMI_LineEventCallback() callback can be implemented in the user file.
  649. */
  650. }
  651. /**
  652. * @brief VSYNC Event callback.
  653. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  654. * the configuration information for DCMI.
  655. * @retval None
  656. */
  657. __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  658. {
  659. /* Prevent unused argument(s) compilation warning */
  660. UNUSED(hdcmi);
  661. /* NOTE : This function should not be modified; when the callback is needed,
  662. the HAL_DCMI_VsyncEventCallback() callback can be implemented in the user file.
  663. */
  664. }
  665. /**
  666. * @brief Frame Event callback.
  667. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  668. * the configuration information for DCMI.
  669. * @retval None
  670. */
  671. __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  672. {
  673. /* Prevent unused argument(s) compilation warning */
  674. UNUSED(hdcmi);
  675. /* NOTE : This function should not be modified; when the callback is needed,
  676. the HAL_DCMI_FrameEventCallback() callback can be implemented in the user file.
  677. */
  678. }
  679. /**
  680. * @}
  681. */
  682. /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
  683. * @brief Peripheral Control functions
  684. *
  685. @verbatim
  686. ===============================================================================
  687. ##### Peripheral Control functions #####
  688. ===============================================================================
  689. [..] This section provides functions allowing to:
  690. (+) Configure the crop feature.
  691. (+) Enable/Disable the crop feature.
  692. (+) Configure the synchronization delimiters unmasks.
  693. (+) Enable/Disable user-specified DCMI interrupts.
  694. @endverbatim
  695. * @{
  696. */
  697. /**
  698. * @brief Configure the DCMI crop window coordinates.
  699. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  700. * the configuration information for DCMI.
  701. * @param X0: DCMI window crop window X offset (number of pixels clocks to count before the capture).
  702. * @param Y0: DCMI window crop window Y offset (image capture starts with this line number, previous
  703. * line data are ignored).
  704. * @param XSize: DCMI crop window horizontal size (in number of pixels per line).
  705. * @param YSize: DCMI crop window vertical size (in lines count).
  706. * @note For all the parameters, the actual value is the input data + 1 (e.g. YSize = 0x0 means 1 line,
  707. * YSize = 0x1 means 2 lines, ...)
  708. * @retval HAL status
  709. */
  710. HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
  711. {
  712. /* Check the parameters */
  713. assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
  714. assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
  715. assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
  716. assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
  717. /* Process Locked */
  718. __HAL_LOCK(hdcmi);
  719. /* Lock the DCMI peripheral state */
  720. hdcmi->State = HAL_DCMI_STATE_BUSY;
  721. /* Configure CROP */
  722. MODIFY_REG(hdcmi->Instance->CWSIZER, (DCMI_CWSIZE_VLINE|DCMI_CWSIZE_CAPCNT), (XSize | (YSize << DCMI_POSITION_CWSIZE_VLINE)));
  723. MODIFY_REG(hdcmi->Instance->CWSTRTR, (DCMI_CWSTRT_VST|DCMI_CWSTRT_HOFFCNT), (X0 | (Y0 << DCMI_POSITION_CWSTRT_VST)));
  724. /* Initialize the DCMI state*/
  725. hdcmi->State = HAL_DCMI_STATE_READY;
  726. /* Process Unlocked */
  727. __HAL_UNLOCK(hdcmi);
  728. return HAL_OK;
  729. }
  730. /**
  731. * @brief Disable the crop feature.
  732. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  733. * the configuration information for DCMI.
  734. * @retval HAL status
  735. */
  736. HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
  737. {
  738. /* Process Locked */
  739. __HAL_LOCK(hdcmi);
  740. /* Lock the DCMI peripheral state */
  741. hdcmi->State = HAL_DCMI_STATE_BUSY;
  742. /* Disable DCMI Crop feature */
  743. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CROP);
  744. /* Change the DCMI state*/
  745. hdcmi->State = HAL_DCMI_STATE_READY;
  746. /* Process Unlocked */
  747. __HAL_UNLOCK(hdcmi);
  748. return HAL_OK;
  749. }
  750. /**
  751. * @brief Enable the crop feature.
  752. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  753. * the configuration information for DCMI.
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
  757. {
  758. /* Process Locked */
  759. __HAL_LOCK(hdcmi);
  760. /* Lock the DCMI peripheral state */
  761. hdcmi->State = HAL_DCMI_STATE_BUSY;
  762. /* Enable DCMI Crop feature */
  763. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CROP);
  764. /* Change the DCMI state*/
  765. hdcmi->State = HAL_DCMI_STATE_READY;
  766. /* Process Unlocked */
  767. __HAL_UNLOCK(hdcmi);
  768. return HAL_OK;
  769. }
  770. /**
  771. * @brief Set embedded synchronization delimiters unmasks.
  772. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  773. * the configuration information for DCMI.
  774. * @param SyncUnmask: pointer to a DCMI_SyncUnmaskTypeDef structure that contains
  775. * the embedded synchronization delimiters unmasks.
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_DCMI_ConfigSyncUnmask(DCMI_HandleTypeDef *hdcmi, DCMI_SyncUnmaskTypeDef *SyncUnmask)
  779. {
  780. /* Process Locked */
  781. __HAL_LOCK(hdcmi);
  782. /* Lock the DCMI peripheral state */
  783. hdcmi->State = HAL_DCMI_STATE_BUSY;
  784. /* Write DCMI embedded synchronization unmask register */
  785. hdcmi->Instance->ESUR = (((uint32_t)SyncUnmask->FrameStartUnmask) |\
  786. ((uint32_t)SyncUnmask->LineStartUnmask << DCMI_POSITION_ESUR_LSU)|\
  787. ((uint32_t)SyncUnmask->LineEndUnmask << DCMI_POSITION_ESUR_LEU)|\
  788. ((uint32_t)SyncUnmask->FrameEndUnmask << DCMI_POSITION_ESUR_FEU));
  789. /* Change the DCMI state*/
  790. hdcmi->State = HAL_DCMI_STATE_READY;
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hdcmi);
  793. return HAL_OK;
  794. }
  795. /**
  796. * @}
  797. */
  798. /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
  799. * @brief Peripheral State functions
  800. *
  801. @verbatim
  802. ===============================================================================
  803. ##### Peripheral State and Errors functions #####
  804. ===============================================================================
  805. [..]
  806. This subsection provides functions allowing to
  807. (+) Check the DCMI state.
  808. (+) Get the specific DCMI error flag.
  809. @endverbatim
  810. * @{
  811. */
  812. /**
  813. * @brief Return the DCMI state.
  814. * @param hdcmi: pointer to a DCMI_HandleTypeDef structure that contains
  815. * the configuration information for DCMI.
  816. * @retval HAL state
  817. */
  818. HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
  819. {
  820. return hdcmi->State;
  821. }
  822. /**
  823. * @brief Return the DCMI error code.
  824. * @param hdcmi : pointer to a DCMI_HandleTypeDef structure that contains
  825. * the configuration information for DCMI.
  826. * @retval DCMI Error Code
  827. */
  828. uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
  829. {
  830. return hdcmi->ErrorCode;
  831. }
  832. /**
  833. * @}
  834. */
  835. /**
  836. * @}
  837. */
  838. /* Private functions ---------------------------------------------------------*/
  839. /** @defgroup DCMI_Private_Functions DCMI Private Functions
  840. * @{
  841. */
  842. /**
  843. * @brief DMA conversion complete callback.
  844. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  845. * the configuration information for the specified DMA module.
  846. * @note When the size of the frame being captured by the DCMI peripheral is
  847. * larger than 0xFFFF (DMA maximum transfer length), this API initiates
  848. * another DMA transfer to copy the second half of the work buffer
  849. * associated to the DCMI handle to the final destination buffer.
  850. * @retval None
  851. */
  852. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
  853. {
  854. uint32_t loop_length = 0; /* transfer length */
  855. uint32_t * tmpBuffer_Dest = NULL;
  856. uint32_t * tmpBuffer_Orig = NULL;
  857. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  858. if(hdcmi->XferCount != 0)
  859. {
  860. /* Manage second half buffer copy in case of big transfer */
  861. /* Decrement half-copies counter */
  862. hdcmi->XferCount--;
  863. /* Point at DCMI final destination */
  864. tmpBuffer_Dest = (uint32_t *)hdcmi->pBuffPtr;
  865. /* Point at DCMI circular buffer mid-location */
  866. tmpBuffer_Orig = (uint32_t *)hdcmi->pCircularBuffer;
  867. tmpBuffer_Orig += hdcmi->HalfCopyLength;
  868. /* copy half the buffer size */
  869. loop_length = hdcmi->HalfCopyLength;
  870. /* Save next entry to write at next half DMA transfer interruption */
  871. hdcmi->pBuffPtr += (uint32_t) loop_length*4;
  872. hdcmi->XferSize -= hdcmi->HalfCopyLength;
  873. /* Data copy from work buffer to final destination buffer */
  874. /* Enable the DMA Channel */
  875. if (HAL_DMA_Start_IT(hdcmi->DMAM2M_Handle, (uint32_t) tmpBuffer_Orig, (uint32_t) tmpBuffer_Dest, loop_length) != HAL_OK)
  876. {
  877. /* Update error code */
  878. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  879. /* Change DCMI state */
  880. hdcmi->State = HAL_DCMI_STATE_READY;
  881. /* Process Unlocked */
  882. __HAL_UNLOCK(hdcmi);
  883. /* Error Callback */
  884. HAL_DCMI_ErrorCallback(hdcmi);
  885. }
  886. }
  887. else
  888. {
  889. /* if End of frame IT is disabled */
  890. if((hdcmi->Instance->IER & DCMI_IT_FRAME) == RESET)
  891. {
  892. /* If End of Frame flag is set */
  893. if(__HAL_DCMI_GET_FLAG(hdcmi, DCMI_FLAG_FRAMERI) != RESET)
  894. {
  895. /* Clear the End of Frame flag */
  896. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
  897. /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
  898. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  899. {
  900. /* Disable the Vsync, Error and Overrun interrupts */
  901. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  902. hdcmi->State = HAL_DCMI_STATE_READY;
  903. /* Process Unlocked */
  904. __HAL_UNLOCK(hdcmi);
  905. }
  906. /* FRAME Callback */
  907. HAL_DCMI_FrameEventCallback(hdcmi);
  908. }
  909. }
  910. }
  911. }
  912. /**
  913. * @brief DMA Half Transfer complete callback.
  914. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  915. * the configuration information for the specified DMA module.
  916. * @note When the size of the frame being captured by the DCMI peripheral is
  917. * larger than 0xFFFF (DMA maximum transfer length), this API initiates
  918. * another DMA transfer to copy the first half of the work buffer
  919. * associated to the DCMI handle to the final destination buffer.
  920. * @retval None
  921. */
  922. static void DCMI_DMAHalfXferCplt(DMA_HandleTypeDef *hdma)
  923. {
  924. uint32_t loop_length = 0; /* transfer length */
  925. uint32_t * tmpBuffer_Dest = NULL;
  926. uint32_t * tmpBuffer_Orig = NULL;
  927. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  928. if(hdcmi->XferCount != 0)
  929. {
  930. /* Manage first half buffer copy in case of big transfer */
  931. /* Decrement half-copies counter */
  932. hdcmi->XferCount--;
  933. /* Point at DCMI final destination */
  934. tmpBuffer_Dest = (uint32_t *)hdcmi->pBuffPtr;
  935. /* Point at DCMI circular buffer start */
  936. tmpBuffer_Orig = (uint32_t *)hdcmi->pCircularBuffer;
  937. /* copy half the buffer size */
  938. loop_length = hdcmi->HalfCopyLength;
  939. /* Save next entry to write at next DMA transfer interruption */
  940. hdcmi->pBuffPtr += (uint32_t) loop_length*4;
  941. hdcmi->XferSize -= hdcmi->HalfCopyLength;
  942. /* Data copy from work buffer to final destination buffer */
  943. /* Enable the DMA Channel */
  944. if (HAL_DMA_Start_IT(hdcmi->DMAM2M_Handle, (uint32_t) tmpBuffer_Orig, (uint32_t) tmpBuffer_Dest, loop_length) != HAL_OK)
  945. {
  946. /* Update error code */
  947. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  948. /* Change DCMI state */
  949. hdcmi->State = HAL_DCMI_STATE_READY;
  950. /* Process Unlocked */
  951. __HAL_UNLOCK(hdcmi);
  952. /* Error Callback */
  953. HAL_DCMI_ErrorCallback(hdcmi);
  954. }
  955. }
  956. }
  957. /**
  958. * @brief DMA error callback
  959. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  960. * the configuration information for the specified DMA module.
  961. * @retval None
  962. */
  963. static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
  964. {
  965. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  966. /* Update error code */
  967. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  968. /* Change DCMI state */
  969. hdcmi->State = HAL_DCMI_STATE_READY;
  970. /* Error Callback */
  971. HAL_DCMI_ErrorCallback(hdcmi);
  972. }
  973. /**
  974. * @brief Sub-buffers transfer size computation.
  975. * @note In the case of a frame size larger than the maximum DMA transfer length (0xFFFF),
  976. * the tranfer from DCMI DR register to the final output buffer is carried out by a sequence
  977. * of intermediate sub-copies to temporary buffers of size less than 0xFFFF.
  978. * To optimize the number of DMA transfers, the API computes the temporary buffer
  979. * size so that the latter is an even number less than 0xFFFF, that divides the final
  980. * buffer size and is as high as possible. The API implements a sub-optimum solution for
  981. * complexity's sake.
  982. * @note InputSize MUST be even.
  983. * @param InputSize: full buffer size (in 32-bit words)
  984. * @retval Transfer size (in 32-bit words)
  985. */
  986. static uint32_t DCMI_TransferSize(uint32_t InputSize)
  987. {
  988. uint32_t j = 1;
  989. uint32_t temp = InputSize;
  990. uint32_t aPrime[NPRIME] = {0};
  991. uint32_t output = 2; /* Want a result which is an even number */
  992. /* Develop InputSize in product of prime numbers */
  993. while (j < NPRIME)
  994. {
  995. if (temp < PrimeArray[j])
  996. {
  997. break;
  998. }
  999. while ((temp % PrimeArray[j]) == 0)
  1000. {
  1001. aPrime[j]++;
  1002. temp /= PrimeArray[j];
  1003. }
  1004. j++;
  1005. }
  1006. /* Search for the biggest even divisor less or equal to 0xFFFE = 65534 */
  1007. aPrime[1] -= 1; /* output is initialized to 2, so don't count dividor 2 twice */
  1008. /* The algorithm below yields a sub-optimal solution
  1009. but in an acceptable time. */
  1010. j = NPRIME-1;
  1011. while ((j > 0) && (output <= 0xFFFE))
  1012. {
  1013. while (aPrime[j] >0)
  1014. {
  1015. if (output * PrimeArray[j] > 0xFFFE)
  1016. {
  1017. break;
  1018. }
  1019. else
  1020. {
  1021. output *= PrimeArray[j];
  1022. aPrime[j]--;
  1023. }
  1024. }
  1025. j--;
  1026. }
  1027. return output;
  1028. }
  1029. /**
  1030. * @}
  1031. */
  1032. /**
  1033. * @}
  1034. */
  1035. /**
  1036. * @}
  1037. */
  1038. #endif /* STM32L496xx || STM32L4A6xx */
  1039. #endif /* HAL_DCMI_MODULE_ENABLED */
  1040. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/