stm32l4xx_hal_dcmi.c 44 KB

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