stm32l4xx_hal_dcmi.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2017 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The sequence below describes how to use this driver to capture images
  30. from a camera module connected to the DCMI Interface.
  31. This sequence does not take into account the configuration of the
  32. camera module, which should be made before configuring and enabling
  33. the DCMI to capture images.
  34. (#) Program the required configuration through the following parameters:
  35. horizontal and vertical polarity, pixel clock polarity, capture rate,
  36. synchronization mode, frame delimiter codes, data width, byte and line
  37. selection using HAL_DCMI_Init() function.
  38. (#) Optionally select JPEG mode; in that case, only the polarity
  39. and the capture mode parameters need to be set.
  40. (#) Capture mode can be either snapshot or continuous mode.
  41. (#) Configure the DMA_Handle to transfer data from DCMI DR
  42. register to the destination memory buffer.
  43. -@- In snapshot mode, the interface transfers a single frame through DMA. In
  44. continuous mode, the DMA must be set in circular mode to ensure a continuous
  45. flow of images data samples.
  46. (#) Program the transfer configuration through the following parameters:
  47. DCMI mode, destination memory buffer address and data length then
  48. enable capture using HAL_DCMI_Start_DMA() function.
  49. (#) Whether in continuous or snapshot mode, data length parameter must be
  50. equal to the frame size.
  51. (#) When the frame size is unknown beforehand (e.g. JPEG case), data length must
  52. be large enough to ensure the capture of a frame.
  53. (#) If the frame size is larger than the maximum DMA transfer length (i.e. 65535),
  54. (++) the DMA must be configured in circular mode, either for snapshot or continuous
  55. capture mode,
  56. (++) during capture, the driver copies the image data samples from DCMI DR register
  57. at the end of the final destination buffer used as a work buffer,
  58. (++) at each DMA half (respectively complete) transfer interrupt, the first
  59. (resp. second) half of the work buffer is copied to the final destination through
  60. a second DMA channel.
  61. (++) Parameters of this second DMA channel are contained in the memory to memory DMA
  62. handle "DMAM2M_Handle", itself field of the DCMI handle structure.
  63. (++) This memory to memory transfer has length half that of the work buffer and is
  64. carried out in normal mode (not in circular mode).
  65. (#) Optionally, configure and enable the CROP feature to select a
  66. rectangular window from the received image using HAL_DCMI_ConfigCrop()
  67. and HAL_DCMI_EnableCrop() functions. Use HAL_DCMI_DisableCrop() to
  68. disable this feature.
  69. (#) The capture can be stopped with HAL_DCMI_Stop() function.
  70. (#) To control the DCMI state, use the function HAL_DCMI_GetState().
  71. (#) To read the DCMI error code, use the function HAL_DCMI_GetError().
  72. [..]
  73. (@) When the frame size is less than the maximum DMA transfer length (i.e. 65535)
  74. and when in snapshot mode, user must make sure the FRAME interrupt is disabled.
  75. This allows to avoid corner cases where the FRAME interrupt might be triggered
  76. before the DMA transfer completion interrupt. In this specific configuration,
  77. the driver checks the FRAME capture flag after the DMA transfer end and calls
  78. HAL_DCMI_FrameEventCallback() if the flag is set.
  79. *** DCMI HAL driver macros list ***
  80. =============================================
  81. [..]
  82. Below the list of most used macros in DCMI HAL driver.
  83. (+) __HAL_DCMI_ENABLE: Enable the DCMI peripheral.
  84. (+) __HAL_DCMI_DISABLE: Disable the DCMI peripheral.
  85. (+) __HAL_DCMI_GET_FLAG: Get the DCMI pending flags.
  86. (+) __HAL_DCMI_CLEAR_FLAG: Clear the DCMI pending flags.
  87. (+) __HAL_DCMI_ENABLE_IT: Enable the specified DCMI interrupts.
  88. (+) __HAL_DCMI_DISABLE_IT: Disable the specified DCMI interrupts.
  89. (+) __HAL_DCMI_GET_IT_SOURCE: Check whether the specified DCMI interrupt has occurred or not.
  90. *** Callback registration ***
  91. =============================
  92. The compilation define USE_HAL_DCMI_REGISTER_CALLBACKS when set to 1
  93. allows the user to configure dynamically the driver callbacks.
  94. Use functions @ref HAL_DCMI_RegisterCallback() to register a user callback.
  95. Function @ref HAL_DCMI_RegisterCallback() allows to register following callbacks:
  96. (+) FrameEventCallback : DCMI Frame Event.
  97. (+) VsyncEventCallback : DCMI Vsync Event.
  98. (+) LineEventCallback : DCMI Line Event.
  99. (+) ErrorCallback : DCMI error.
  100. (+) MspInitCallback : DCMI MspInit.
  101. (+) MspDeInitCallback : DCMI MspDeInit.
  102. This function takes as parameters the HAL peripheral handle, the callback ID
  103. and a pointer to the user callback function.
  104. Use function @ref HAL_DCMI_UnRegisterCallback() to reset a callback to the default
  105. weak (surcharged) function.
  106. @ref HAL_DCMI_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  107. and the callback ID.
  108. This function allows to reset following callbacks:
  109. (+) FrameEventCallback : DCMI Frame Event.
  110. (+) VsyncEventCallback : DCMI Vsync Event.
  111. (+) LineEventCallback : DCMI Line Event.
  112. (+) ErrorCallback : DCMI error.
  113. (+) MspInitCallback : DCMI MspInit.
  114. (+) MspDeInitCallback : DCMI MspDeInit.
  115. By default, after the @ref HAL_DCMI_Init and if the state is HAL_DCMI_STATE_RESET
  116. all callbacks are reset to the corresponding legacy weak (surcharged) functions:
  117. examples @ref FrameEventCallback(), @ref HAL_DCMI_ErrorCallback().
  118. Exception done for MspInit and MspDeInit callbacks that are respectively
  119. reset to the legacy weak (surcharged) functions in the @ref HAL_DCMI_Init
  120. and @ref HAL_DCMI_DeInit only when these callbacks are null (not registered beforehand).
  121. If not, MspInit or MspDeInit are not null, the @ref HAL_DCMI_Init and @ref HAL_DCMI_DeInit
  122. keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  123. Callbacks can be registered/unregistered in READY state only.
  124. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  125. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  126. during the Init/DeInit.
  127. In that case first register the MspInit/MspDeInit user callbacks
  128. using @ref HAL_DCMI_RegisterCallback before calling @ref HAL_DCMI_DeInit
  129. or @ref HAL_DCMI_Init function.
  130. When the compilation define USE_HAL_DCMI_REGISTER_CALLBACKS is set to 0 or
  131. not defined, the callback registering feature is not available
  132. and weak (surcharged) callbacks are used.
  133. @endverbatim
  134. ******************************************************************************
  135. */
  136. /* Includes ------------------------------------------------------------------*/
  137. #include "stm32l4xx_hal.h"
  138. #ifdef HAL_DCMI_MODULE_ENABLED
  139. #if defined (DCMI)
  140. /** @addtogroup STM32L4xx_HAL_Driver
  141. * @{
  142. */
  143. /** @defgroup DCMI DCMI
  144. * @brief DCMI HAL module driver
  145. * @{
  146. */
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /** @defgroup DCMI_Private_Constants DCMI Private Constants
  150. * @{
  151. */
  152. /** @defgroup DCMI_Stop_TimeOut DCMI Stop TimeOut
  153. * @{
  154. */
  155. #define DCMI_TIMEOUT_STOP ((uint32_t)1000U) /*!< 1s */
  156. /**
  157. * @}
  158. */
  159. #define NPRIME 16U
  160. /**
  161. * @}
  162. */
  163. /* Private macro -------------------------------------------------------------*/
  164. /* Private variables ---------------------------------------------------------*/
  165. /* Private function prototypes -----------------------------------------------*/
  166. /** @addtogroup DCMI_Private_Functions DCMI Private Functions
  167. * @{
  168. */
  169. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma);
  170. static void DCMI_DMAHalfXferCplt(DMA_HandleTypeDef *hdma);
  171. static void DCMI_DMAError(DMA_HandleTypeDef *hdma);
  172. static uint32_t DCMI_TransferSize(uint32_t InputSize);
  173. /**
  174. * @}
  175. */
  176. /* Exported functions --------------------------------------------------------*/
  177. /** @defgroup DCMI_Exported_Functions DCMI Exported Functions
  178. * @{
  179. */
  180. /** @defgroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
  181. * @brief Initialization and Configuration functions
  182. *
  183. @verbatim
  184. ===============================================================================
  185. ##### Initialization and Configuration functions #####
  186. ===============================================================================
  187. [..] This section provides functions allowing to:
  188. (+) Initialize and configure the DCMI
  189. (+) De-initialize the DCMI
  190. @endverbatim
  191. * @{
  192. */
  193. /**
  194. * @brief Initialize the DCMI according to the specified
  195. * parameters in the DCMI_InitTypeDef and create the associated handle.
  196. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  197. * the configuration information for DCMI.
  198. * @note By default, all interruptions are enabled (line end, frame end, overrun,
  199. * VSYNC and embedded synchronization error interrupts).
  200. * @retval HAL status
  201. */
  202. HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
  203. {
  204. /* Check the DCMI peripheral state */
  205. if(hdcmi == NULL)
  206. {
  207. return HAL_ERROR;
  208. }
  209. /* Check function parameters */
  210. assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
  211. assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
  212. assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
  213. assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
  214. assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
  215. assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
  216. assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
  217. assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
  218. assert_param(IS_DCMI_BYTE_SELECT_MODE(hdcmi->Init.ByteSelectMode));
  219. assert_param(IS_DCMI_BYTE_SELECT_START(hdcmi->Init.ByteSelectStart));
  220. assert_param(IS_DCMI_LINE_SELECT_MODE(hdcmi->Init.LineSelectMode));
  221. assert_param(IS_DCMI_LINE_SELECT_START(hdcmi->Init.LineSelectStart));
  222. if(hdcmi->State == HAL_DCMI_STATE_RESET)
  223. {
  224. /* Allocate lock resource and initialize it */
  225. hdcmi->Lock = HAL_UNLOCKED;
  226. /* Init the DCMI Callback settings */
  227. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  228. hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
  229. hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
  230. hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
  231. hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
  232. if(hdcmi->MspInitCallback == NULL)
  233. {
  234. /* Legacy weak MspInit Callback */
  235. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  236. }
  237. /* Initialize the low level hardware (MSP) */
  238. hdcmi->MspInitCallback(hdcmi);
  239. #else
  240. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  241. HAL_DCMI_MspInit(hdcmi);
  242. #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
  243. }
  244. /* Change the DCMI state */
  245. hdcmi->State = HAL_DCMI_STATE_BUSY;
  246. /* Disable DCMI IP before setting the configuration register */
  247. __HAL_DCMI_DISABLE(hdcmi);
  248. if (hdcmi->Init.ExtendedDataMode != DCMI_EXTEND_DATA_8B)
  249. {
  250. /* Byte select mode must be programmed to the reset value if the extended mode
  251. is not set to 8-bit data capture on every pixel clock */
  252. hdcmi->Init.ByteSelectMode = DCMI_BSM_ALL;
  253. }
  254. /* Set DCMI parameters */
  255. hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL | DCMI_CR_VSPOL | DCMI_CR_EDM_0 |\
  256. DCMI_CR_EDM_1 | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG |\
  257. DCMI_CR_ESS | DCMI_CR_BSM_0 | DCMI_CR_BSM_1 | DCMI_CR_OEBS |\
  258. DCMI_CR_LSM | DCMI_CR_OELS);
  259. hdcmi->Instance->CR |= (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate |\
  260. hdcmi->Init.VSPolarity | hdcmi->Init.HSPolarity |\
  261. hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode |\
  262. hdcmi->Init.JPEGMode | hdcmi->Init.ByteSelectMode |\
  263. hdcmi->Init.ByteSelectStart | hdcmi->Init.LineSelectMode |\
  264. hdcmi->Init.LineSelectStart);
  265. if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
  266. {
  267. hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SynchroCode.FrameStartCode) |\
  268. ((uint32_t)hdcmi->Init.SynchroCode.LineStartCode << DCMI_ESCR_LSC_Pos)|\
  269. ((uint32_t)hdcmi->Init.SynchroCode.LineEndCode << DCMI_ESCR_LEC_Pos) |\
  270. ((uint32_t)hdcmi->Init.SynchroCode.FrameEndCode << DCMI_ESCR_FEC_Pos));
  271. }
  272. /* By default, enable all interrupts. The user may disable the unwanted ones
  273. in resorting to __HAL_DCMI_DISABLE_IT() macro before invoking HAL_DCMI_Start_DMA().
  274. Enabled interruptions are
  275. - end of line
  276. - end of frame
  277. - data reception overrun
  278. - frame synchronization signal VSYNC
  279. - synchronization error */
  280. __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_FRAME|DCMI_IT_OVR|DCMI_IT_ERR|DCMI_IT_VSYNC|DCMI_IT_LINE);
  281. /* Update error code */
  282. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  283. /* Initialize the DCMI state*/
  284. hdcmi->State = HAL_DCMI_STATE_READY;
  285. return HAL_OK;
  286. }
  287. /**
  288. * @brief De-initialize the DCMI peripheral, reset control registers to
  289. * their default values.
  290. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  291. * the configuration information for DCMI.
  292. * @retval HAL status
  293. */
  294. HAL_StatusTypeDef HAL_DCMI_DeInit(DCMI_HandleTypeDef *hdcmi)
  295. {
  296. /* Before aborting any DCMI transfer, check
  297. first whether or not DCMI clock is enabled */
  298. if (__HAL_RCC_DCMI_IS_CLK_ENABLED())
  299. {
  300. if (HAL_DCMI_Stop(hdcmi) != HAL_OK)
  301. {
  302. /* Issue when stopping DCMI IP */
  303. return HAL_ERROR;
  304. }
  305. }
  306. /* Reset DCMI control register */
  307. hdcmi->Instance->CR = 0;
  308. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  309. if(hdcmi->MspDeInitCallback == NULL)
  310. {
  311. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  312. }
  313. /* De-Initialize the low level hardware (MSP) */
  314. hdcmi->MspDeInitCallback(hdcmi);
  315. #else
  316. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  317. HAL_DCMI_MspDeInit(hdcmi);
  318. #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
  319. /* Update error code */
  320. hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
  321. /* Initialize the DCMI state*/
  322. hdcmi->State = HAL_DCMI_STATE_RESET;
  323. /* Release Lock */
  324. __HAL_UNLOCK(hdcmi);
  325. return HAL_OK;
  326. }
  327. /**
  328. * @brief Initialize the DCMI MSP.
  329. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  330. * the configuration information for DCMI.
  331. * @retval None
  332. */
  333. __weak void HAL_DCMI_MspInit(DCMI_HandleTypeDef* hdcmi)
  334. {
  335. /* Prevent unused argument(s) compilation warning */
  336. UNUSED(hdcmi);
  337. /* NOTE : This function should not be modified; when the callback is needed,
  338. the HAL_DCMI_MspInit() callback can be implemented in the user file
  339. */
  340. }
  341. /**
  342. * @brief De-initialize the DCMI MSP.
  343. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  344. * the configuration information for DCMI.
  345. * @retval None
  346. */
  347. __weak void HAL_DCMI_MspDeInit(DCMI_HandleTypeDef* hdcmi)
  348. {
  349. /* Prevent unused argument(s) compilation warning */
  350. UNUSED(hdcmi);
  351. /* NOTE : This function should not be modified; when the callback is needed,
  352. the HAL_DCMI_MspDeInit() callback can be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @}
  357. */
  358. /** @defgroup DCMI_Exported_Functions_Group2 IO operation functions
  359. * @brief IO operation functions
  360. *
  361. @verbatim
  362. ===============================================================================
  363. ##### IO operation functions #####
  364. ===============================================================================
  365. [..] This section provides functions allowing to:
  366. (+) Configure destination address and data length,
  367. enable DCMI DMA request and DCMI capture.
  368. (+) Stop DCMI capture.
  369. (+) Handle DCMI interrupt request.
  370. [..] A set of callbacks is provided:
  371. (+) HAL_DCMI_ErrorCallback()
  372. (+) HAL_DCMI_LineEventCallback()
  373. (+) HAL_DCMI_VsyncEventCallback()
  374. (+) HAL_DCMI_FrameEventCallback()
  375. @endverbatim
  376. * @{
  377. */
  378. /**
  379. * @brief Enable DCMI capture in DMA mode.
  380. * @param hdcmi Pointer to a DCMI_HandleTypeDef structure that contains
  381. * the configuration information for DCMI.
  382. * @param DCMI_Mode DCMI capture mode snapshot or continuous grab.
  383. * @param pData The destination memory buffer address.
  384. * @param Length The length of capture to be transferred (in 32-bit words).
  385. * @note In case of length larger than 65535 (0xFFFF is the DMA maximum transfer length),
  386. * the API uses the end of the destination buffer as a work area: HAL_DCMI_Start_DMA()
  387. * initiates a circular DMA transfer from DCMI DR to the ad-hoc work buffer and each
  388. * half and complete transfer interrupt triggers a copy from the work buffer to
  389. * the final destination pData through a second DMA channel.
  390. * @note Following HAL_DCMI_Init() call, all interruptions are enabled (line end,
  391. * frame end, overrun, VSYNC and embedded synchronization error interrupts).
  392. * User can disable unwanted interrupts through __HAL_DCMI_DISABLE_IT() macro
  393. * before invoking HAL_DCMI_Start_DMA().
  394. * @note For length less than 0xFFFF (DMA maximum transfer length) and in snapshot mode,
  395. * frame interrupt is disabled before DMA transfer. FRAME capture flag is checked
  396. * in DCMI_DMAXferCplt callback at the end of the DMA transfer. If flag is set,
  397. * HAL_DCMI_FrameEventCallback() API is called.
  398. * @retval HAL status
  399. */
  400. HAL_StatusTypeDef HAL_DCMI_Start_DMA(DCMI_HandleTypeDef* hdcmi, uint32_t DCMI_Mode, uint32_t pData, uint32_t Length)
  401. {
  402. uint32_t circular_copy_length;
  403. /* Check capture parameter */
  404. assert_param(IS_DCMI_CAPTURE_MODE(DCMI_Mode));
  405. /* Process Locked */
  406. __HAL_LOCK(hdcmi);
  407. /* Lock the DCMI peripheral state */
  408. hdcmi->State = HAL_DCMI_STATE_BUSY;
  409. /* Configure the DCMI Mode and enable the DCMI IP at the same time */
  410. MODIFY_REG(hdcmi->Instance->CR, (DCMI_CR_CM|DCMI_CR_ENABLE), (DCMI_Mode|DCMI_CR_ENABLE));
  411. /* Set the DMA conversion complete callback */
  412. hdcmi->DMA_Handle->XferCpltCallback = DCMI_DMAXferCplt;
  413. /* Set the DMA error callback */
  414. hdcmi->DMA_Handle->XferErrorCallback = DCMI_DMAError;
  415. /* Set the dma abort callback */
  416. hdcmi->DMA_Handle->XferAbortCallback = NULL;
  417. if(Length <= 0xFFFFU)
  418. {
  419. hdcmi->XferCount = 0; /* Mark as direct transfer from DCMI_DR register to final destination buffer */
  420. /* Enable the DMA channel */
  421. if (HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)pData, Length) != HAL_OK)
  422. {
  423. /* Update error code */
  424. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  425. /* Set state back to Ready */
  426. hdcmi->State = HAL_DCMI_STATE_READY;
  427. /* Process Unlocked */
  428. __HAL_UNLOCK(hdcmi);
  429. return HAL_ERROR;
  430. }
  431. }
  432. else /* Capture length is longer than DMA maximum transfer size */
  433. {
  434. /* Set DMA in circular mode */
  435. hdcmi->DMA_Handle->Init.Mode = DMA_CIRCULAR;
  436. /* Set the DMA half transfer complete callback */
  437. hdcmi->DMA_Handle->XferHalfCpltCallback = DCMI_DMAHalfXferCplt;
  438. /* Initialize transfer parameters */
  439. hdcmi->XferSize = Length; /* Store the complete transfer length in DCMI handle */
  440. hdcmi->pBuffPtr = pData; /* Final destination buffer pointer */
  441. circular_copy_length = DCMI_TransferSize(Length);
  442. /* Check if issue in intermediate length computation */
  443. if (circular_copy_length == 0U)
  444. {
  445. /* Set state back to Ready */
  446. hdcmi->State = HAL_DCMI_STATE_READY;
  447. /* Process Unlocked */
  448. __HAL_UNLOCK(hdcmi);
  449. return HAL_ERROR;
  450. }
  451. /* Store the number of half - intermediate buffer copies needed */
  452. hdcmi->XferCount = 2U * ((Length / circular_copy_length) - 1U);
  453. /* Store the half-buffer copy length */
  454. hdcmi->HalfCopyLength = circular_copy_length / 2U;
  455. /* Save initial values for continuous mode case */
  456. hdcmi->XferCount_0 = hdcmi->XferCount;
  457. hdcmi->XferSize_0 = hdcmi->XferSize;
  458. hdcmi->pBuffPtr_0 = hdcmi->pBuffPtr;
  459. /* DCMI DR samples in circular mode will be copied
  460. at the end of the final buffer.
  461. Now compute the circular buffer start address. */
  462. /* Start by pointing at the final buffer */
  463. hdcmi->pCircularBuffer = pData;
  464. /* Update pCircularBuffer in "moving" at the end of the final
  465. buffer, don't forger to convert in bytes to compute exact address */
  466. hdcmi->pCircularBuffer += 4U * (((Length / circular_copy_length) - 1U) * circular_copy_length);
  467. /* Initiate the circular DMA transfer from DCMI IP to final buffer end */
  468. if ( HAL_DMA_Start_IT(hdcmi->DMA_Handle, (uint32_t)&hdcmi->Instance->DR, (uint32_t)hdcmi->pCircularBuffer, circular_copy_length) != HAL_OK)
  469. {
  470. /* Update error code */
  471. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  472. /* Set state back to Ready */
  473. hdcmi->State = HAL_DCMI_STATE_READY;
  474. /* Process Unlocked */
  475. __HAL_UNLOCK(hdcmi);
  476. return HAL_ERROR;
  477. }
  478. }
  479. /* Enable Capture */
  480. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  481. /* Release Lock */
  482. __HAL_UNLOCK(hdcmi);
  483. /* Return function status */
  484. return HAL_OK;
  485. }
  486. /**
  487. * @brief Disable DCMI capture in DMA mode.
  488. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  489. * the configuration information for DCMI.
  490. * @retval HAL status
  491. */
  492. HAL_StatusTypeDef HAL_DCMI_Stop(DCMI_HandleTypeDef* hdcmi)
  493. {
  494. uint32_t tickstart;
  495. HAL_StatusTypeDef status = HAL_OK;
  496. /* Process locked */
  497. __HAL_LOCK(hdcmi);
  498. /* Lock the DCMI peripheral state */
  499. hdcmi->State = HAL_DCMI_STATE_BUSY;
  500. /* Disable Capture */
  501. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  502. /* Get tick */
  503. tickstart = HAL_GetTick();
  504. /* Check if the DCMI capture is effectively disabled */
  505. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0U)
  506. {
  507. if((HAL_GetTick() - tickstart ) > DCMI_TIMEOUT_STOP)
  508. {
  509. /* Update error code */
  510. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  511. status = HAL_TIMEOUT;
  512. break;
  513. }
  514. }
  515. /* Disable the DMA */
  516. if (HAL_DMA_Abort(hdcmi->DMA_Handle) != HAL_OK)
  517. {
  518. DCMI_DMAError(hdcmi->DMA_Handle);
  519. }
  520. /* Disable DCMI IP */
  521. __HAL_DCMI_DISABLE(hdcmi);
  522. /* Change DCMI state */
  523. hdcmi->State = HAL_DCMI_STATE_READY;
  524. /* Process Unlocked */
  525. __HAL_UNLOCK(hdcmi);
  526. /* Return function status */
  527. return status;
  528. }
  529. /**
  530. * @brief Suspend DCMI capture.
  531. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  532. * the configuration information for DCMI.
  533. * @retval HAL status
  534. */
  535. HAL_StatusTypeDef HAL_DCMI_Suspend(DCMI_HandleTypeDef* hdcmi)
  536. {
  537. uint32_t tickstart;
  538. /* Process locked */
  539. __HAL_LOCK(hdcmi);
  540. if(hdcmi->State == HAL_DCMI_STATE_BUSY)
  541. {
  542. /* Change DCMI state */
  543. hdcmi->State = HAL_DCMI_STATE_SUSPENDED;
  544. /* Disable Capture */
  545. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  546. /* Get tick */
  547. tickstart = HAL_GetTick();
  548. /* Check if the DCMI capture is effectively disabled */
  549. while((hdcmi->Instance->CR & DCMI_CR_CAPTURE) != 0U)
  550. {
  551. if((HAL_GetTick() - tickstart ) > DCMI_TIMEOUT_STOP)
  552. {
  553. /* Update error code */
  554. hdcmi->ErrorCode |= HAL_DCMI_ERROR_TIMEOUT;
  555. /* Change DCMI state */
  556. hdcmi->State = HAL_DCMI_STATE_READY;
  557. /* Process Unlocked */
  558. __HAL_UNLOCK(hdcmi);
  559. return HAL_TIMEOUT;
  560. }
  561. }
  562. }
  563. /* Process Unlocked */
  564. __HAL_UNLOCK(hdcmi);
  565. /* Return function status */
  566. return HAL_OK;
  567. }
  568. /**
  569. * @brief Resume DCMI capture.
  570. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  571. * the configuration information for DCMI.
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef HAL_DCMI_Resume(DCMI_HandleTypeDef* hdcmi)
  575. {
  576. /* Process locked */
  577. __HAL_LOCK(hdcmi);
  578. if(hdcmi->State == HAL_DCMI_STATE_SUSPENDED)
  579. {
  580. /* Change DCMI state */
  581. hdcmi->State = HAL_DCMI_STATE_BUSY;
  582. /* Enable Capture */
  583. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CAPTURE);
  584. }
  585. /* Process Unlocked */
  586. __HAL_UNLOCK(hdcmi);
  587. return HAL_OK;
  588. }
  589. /**
  590. * @brief Handle DCMI interrupt request.
  591. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  592. * the configuration information for the DCMI.
  593. * @retval None
  594. */
  595. void HAL_DCMI_IRQHandler(DCMI_HandleTypeDef *hdcmi)
  596. {
  597. uint32_t misflags = READ_REG(hdcmi->Instance->MISR);
  598. /* Synchronization error interrupt management *******************************/
  599. if ((misflags & DCMI_MIS_ERR_MIS) != 0x0U)
  600. {
  601. /* Clear the Synchronization error flag */
  602. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_ERRRI);
  603. /* Update error code */
  604. hdcmi->ErrorCode |= HAL_DCMI_ERROR_SYNC;
  605. }
  606. /* Overflow interrupt management ********************************************/
  607. if ((misflags & DCMI_MIS_OVR_MIS) != 0x0U)
  608. {
  609. /* Clear the Overflow flag */
  610. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_OVRRI);
  611. /* Update error code */
  612. hdcmi->ErrorCode |= HAL_DCMI_ERROR_OVR;
  613. }
  614. if (hdcmi->ErrorCode != HAL_DCMI_ERROR_NONE)
  615. {
  616. /* Change DCMI state */
  617. hdcmi->State = HAL_DCMI_STATE_READY;
  618. /* Set the overflow callback */
  619. hdcmi->DMA_Handle->XferAbortCallback = DCMI_DMAError;
  620. /* Abort the DMA Transfer */
  621. if (HAL_DMA_Abort_IT(hdcmi->DMA_Handle) != HAL_OK)
  622. {
  623. DCMI_DMAError(hdcmi->DMA_Handle);
  624. }
  625. }
  626. /* Line Interrupt management ************************************************/
  627. if ((misflags & DCMI_MIS_LINE_MIS) != 0x0U)
  628. {
  629. /* Clear the Line interrupt flag */
  630. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_LINERI);
  631. /* Line interrupt Event Callback */
  632. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  633. /*Call registered DCMI line event callback*/
  634. hdcmi->LineEventCallback(hdcmi);
  635. #else
  636. HAL_DCMI_LineEventCallback(hdcmi);
  637. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  638. }
  639. /* VSYNC interrupt management ***********************************************/
  640. if ((misflags & DCMI_MIS_VSYNC_MIS) != 0x0U)
  641. {
  642. /* Clear the VSYNC flag */
  643. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_VSYNCRI);
  644. /* VSYNC Event Callback */
  645. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  646. /*Call registered DCMI vsync event callback*/
  647. hdcmi->VsyncEventCallback(hdcmi);
  648. #else
  649. HAL_DCMI_VsyncEventCallback(hdcmi);
  650. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  651. }
  652. /* End of Frame interrupt management ****************************************/
  653. if ((misflags & DCMI_MIS_FRAME_MIS) != 0x0U)
  654. {
  655. /* Disable the Line interrupt when using snapshot mode */
  656. if ((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  657. {
  658. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE|DCMI_IT_VSYNC|DCMI_IT_ERR|DCMI_IT_OVR);
  659. /* Change the DCMI state */
  660. hdcmi->State = HAL_DCMI_STATE_READY;
  661. }
  662. /* Clear the End of Frame flag */
  663. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
  664. /* Frame Event Callback */
  665. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  666. /*Call registered DCMI frame event callback*/
  667. hdcmi->FrameEventCallback(hdcmi);
  668. #else
  669. HAL_DCMI_FrameEventCallback(hdcmi);
  670. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  671. }
  672. }
  673. /**
  674. * @brief Error DCMI callback.
  675. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  676. * the configuration information for DCMI.
  677. * @retval None
  678. */
  679. __weak void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  680. {
  681. /* Prevent unused argument(s) compilation warning */
  682. UNUSED(hdcmi);
  683. /* NOTE : This function should not be modified; when the callback is needed,
  684. the HAL_DCMI_ErrorCallback() callback can be implemented in the user file.
  685. */
  686. }
  687. /**
  688. * @brief Line Event callback.
  689. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  690. * the configuration information for DCMI.
  691. * @retval None
  692. */
  693. __weak void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  694. {
  695. /* Prevent unused argument(s) compilation warning */
  696. UNUSED(hdcmi);
  697. /* NOTE : This function should not be modified; when the callback is needed,
  698. the HAL_DCMI_LineEventCallback() callback can be implemented in the user file.
  699. */
  700. }
  701. /**
  702. * @brief VSYNC Event callback.
  703. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  704. * the configuration information for DCMI.
  705. * @retval None
  706. */
  707. __weak void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  708. {
  709. /* Prevent unused argument(s) compilation warning */
  710. UNUSED(hdcmi);
  711. /* NOTE : This function should not be modified; when the callback is needed,
  712. the HAL_DCMI_VsyncEventCallback() callback can be implemented in the user file.
  713. */
  714. }
  715. /**
  716. * @brief Frame Event callback.
  717. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  718. * the configuration information for DCMI.
  719. * @retval None
  720. */
  721. __weak void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  722. {
  723. /* Prevent unused argument(s) compilation warning */
  724. UNUSED(hdcmi);
  725. /* NOTE : This function should not be modified; when the callback is needed,
  726. the HAL_DCMI_FrameEventCallback() callback can be implemented in the user file.
  727. */
  728. }
  729. /**
  730. * @}
  731. */
  732. /** @defgroup DCMI_Exported_Functions_Group3 Peripheral Control functions
  733. * @brief Peripheral Control functions
  734. *
  735. @verbatim
  736. ===============================================================================
  737. ##### Peripheral Control functions #####
  738. ===============================================================================
  739. [..] This section provides functions allowing to:
  740. (+) Configure the crop feature.
  741. (+) Enable/Disable the crop feature.
  742. (+) Configure the synchronization delimiters unmasks.
  743. (+) Enable/Disable user-specified DCMI interrupts.
  744. @endverbatim
  745. * @{
  746. */
  747. /**
  748. * @brief Configure the DCMI crop window coordinates.
  749. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  750. * the configuration information for DCMI.
  751. * @param X0 DCMI window crop window X offset (number of pixels clocks to count before the capture).
  752. * @param Y0 DCMI window crop window Y offset (image capture starts with this line number, previous
  753. * line data are ignored).
  754. * @param XSize DCMI crop window horizontal size (in number of pixels per line).
  755. * @param YSize DCMI crop window vertical size (in lines count).
  756. * @note For all the parameters, the actual value is the input data + 1 (e.g. YSize = 0x0 means 1 line,
  757. * YSize = 0x1 means 2 lines, ...)
  758. * @retval HAL status
  759. */
  760. HAL_StatusTypeDef HAL_DCMI_ConfigCrop(DCMI_HandleTypeDef *hdcmi, uint32_t X0, uint32_t Y0, uint32_t XSize, uint32_t YSize)
  761. {
  762. /* Check the parameters */
  763. assert_param(IS_DCMI_WINDOW_COORDINATE(X0));
  764. assert_param(IS_DCMI_WINDOW_HEIGHT(Y0));
  765. assert_param(IS_DCMI_WINDOW_COORDINATE(XSize));
  766. assert_param(IS_DCMI_WINDOW_COORDINATE(YSize));
  767. /* Process Locked */
  768. __HAL_LOCK(hdcmi);
  769. /* Lock the DCMI peripheral state */
  770. hdcmi->State = HAL_DCMI_STATE_BUSY;
  771. /* Configure CROP */
  772. MODIFY_REG(hdcmi->Instance->CWSIZER, (DCMI_CWSIZE_VLINE|DCMI_CWSIZE_CAPCNT), (XSize | (YSize << DCMI_CWSIZE_VLINE_Pos)));
  773. MODIFY_REG(hdcmi->Instance->CWSTRTR, (DCMI_CWSTRT_VST|DCMI_CWSTRT_HOFFCNT), (X0 | (Y0 << DCMI_CWSTRT_VST_Pos)));
  774. /* Initialize the DCMI state*/
  775. hdcmi->State = HAL_DCMI_STATE_READY;
  776. /* Process Unlocked */
  777. __HAL_UNLOCK(hdcmi);
  778. return HAL_OK;
  779. }
  780. /**
  781. * @brief Disable the crop feature.
  782. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  783. * the configuration information for DCMI.
  784. * @retval HAL status
  785. */
  786. HAL_StatusTypeDef HAL_DCMI_DisableCrop(DCMI_HandleTypeDef *hdcmi)
  787. {
  788. /* Process Locked */
  789. __HAL_LOCK(hdcmi);
  790. /* Lock the DCMI peripheral state */
  791. hdcmi->State = HAL_DCMI_STATE_BUSY;
  792. /* Disable DCMI Crop feature */
  793. CLEAR_BIT(hdcmi->Instance->CR, DCMI_CR_CROP);
  794. /* Change the DCMI state*/
  795. hdcmi->State = HAL_DCMI_STATE_READY;
  796. /* Process Unlocked */
  797. __HAL_UNLOCK(hdcmi);
  798. return HAL_OK;
  799. }
  800. /**
  801. * @brief Enable the crop feature.
  802. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  803. * the configuration information for DCMI.
  804. * @retval HAL status
  805. */
  806. HAL_StatusTypeDef HAL_DCMI_EnableCrop(DCMI_HandleTypeDef *hdcmi)
  807. {
  808. /* Process Locked */
  809. __HAL_LOCK(hdcmi);
  810. /* Lock the DCMI peripheral state */
  811. hdcmi->State = HAL_DCMI_STATE_BUSY;
  812. /* Enable DCMI Crop feature */
  813. SET_BIT(hdcmi->Instance->CR, DCMI_CR_CROP);
  814. /* Change the DCMI state*/
  815. hdcmi->State = HAL_DCMI_STATE_READY;
  816. /* Process Unlocked */
  817. __HAL_UNLOCK(hdcmi);
  818. return HAL_OK;
  819. }
  820. /**
  821. * @brief Set embedded synchronization delimiters unmasks.
  822. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  823. * the configuration information for DCMI.
  824. * @param SyncUnmask pointer to a DCMI_SyncUnmaskTypeDef structure that contains
  825. * the embedded synchronization delimiters unmasks.
  826. * @retval HAL status
  827. */
  828. HAL_StatusTypeDef HAL_DCMI_ConfigSyncUnmask(DCMI_HandleTypeDef *hdcmi, DCMI_SyncUnmaskTypeDef *SyncUnmask)
  829. {
  830. /* Process Locked */
  831. __HAL_LOCK(hdcmi);
  832. /* Lock the DCMI peripheral state */
  833. hdcmi->State = HAL_DCMI_STATE_BUSY;
  834. /* Write DCMI embedded synchronization unmask register */
  835. hdcmi->Instance->ESUR = (((uint32_t)SyncUnmask->FrameStartUnmask) |\
  836. ((uint32_t)SyncUnmask->LineStartUnmask << DCMI_ESUR_LSU_Pos)|\
  837. ((uint32_t)SyncUnmask->LineEndUnmask << DCMI_ESUR_LEU_Pos)|\
  838. ((uint32_t)SyncUnmask->FrameEndUnmask << DCMI_ESUR_FEU_Pos));
  839. /* Change the DCMI state*/
  840. hdcmi->State = HAL_DCMI_STATE_READY;
  841. /* Process Unlocked */
  842. __HAL_UNLOCK(hdcmi);
  843. return HAL_OK;
  844. }
  845. /**
  846. * @}
  847. */
  848. /** @defgroup DCMI_Exported_Functions_Group4 Peripheral State functions
  849. * @brief Peripheral State functions
  850. *
  851. @verbatim
  852. ===============================================================================
  853. ##### Peripheral State and Errors functions #####
  854. ===============================================================================
  855. [..]
  856. This subsection provides functions allowing to
  857. (+) Check the DCMI state.
  858. (+) Get the specific DCMI error flag.
  859. @endverbatim
  860. * @{
  861. */
  862. /**
  863. * @brief Return the DCMI state.
  864. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  865. * the configuration information for DCMI.
  866. * @retval HAL state
  867. */
  868. HAL_DCMI_StateTypeDef HAL_DCMI_GetState(DCMI_HandleTypeDef *hdcmi)
  869. {
  870. return hdcmi->State;
  871. }
  872. /**
  873. * @brief Return the DCMI error code.
  874. * @param hdcmi pointer to a DCMI_HandleTypeDef structure that contains
  875. * the configuration information for DCMI.
  876. * @retval DCMI Error Code
  877. */
  878. uint32_t HAL_DCMI_GetError(DCMI_HandleTypeDef *hdcmi)
  879. {
  880. return hdcmi->ErrorCode;
  881. }
  882. /**
  883. * @}
  884. */
  885. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  886. /**
  887. * @brief DCMI Callback registering
  888. * @param hdcmi dcmi handle
  889. * @param CallbackID dcmi Callback ID
  890. * @param pCallback pointer to dcmi Callback function
  891. * @retval status
  892. */
  893. HAL_StatusTypeDef HAL_DCMI_RegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID, pDCMI_CallbackTypeDef pCallback)
  894. {
  895. HAL_StatusTypeDef status = HAL_OK;
  896. if(pCallback == NULL)
  897. {
  898. /* update the error code */
  899. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  900. /* update return status */
  901. status = HAL_ERROR;
  902. }
  903. else
  904. {
  905. if(hdcmi->State == HAL_DCMI_STATE_READY)
  906. {
  907. switch (CallbackID)
  908. {
  909. case HAL_DCMI_FRAME_EVENT_CB_ID :
  910. hdcmi->FrameEventCallback = pCallback;
  911. break;
  912. case HAL_DCMI_VSYNC_EVENT_CB_ID :
  913. hdcmi->VsyncEventCallback = pCallback;
  914. break;
  915. case HAL_DCMI_LINE_EVENT_CB_ID :
  916. hdcmi->LineEventCallback = pCallback;
  917. break;
  918. case HAL_DCMI_ERROR_CB_ID :
  919. hdcmi->ErrorCallback = pCallback;
  920. break;
  921. case HAL_DCMI_MSPINIT_CB_ID :
  922. hdcmi->MspInitCallback = pCallback;
  923. break;
  924. case HAL_DCMI_MSPDEINIT_CB_ID :
  925. hdcmi->MspDeInitCallback = pCallback;
  926. break;
  927. default :
  928. /* Return error status */
  929. status = HAL_ERROR;
  930. break;
  931. }
  932. }
  933. else if(hdcmi->State == HAL_DCMI_STATE_RESET)
  934. {
  935. switch (CallbackID)
  936. {
  937. case HAL_DCMI_MSPINIT_CB_ID :
  938. hdcmi->MspInitCallback = pCallback;
  939. break;
  940. case HAL_DCMI_MSPDEINIT_CB_ID :
  941. hdcmi->MspDeInitCallback = pCallback;
  942. break;
  943. default :
  944. /* update the error code */
  945. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  946. /* update return status */
  947. status = HAL_ERROR;
  948. break;
  949. }
  950. }
  951. else
  952. {
  953. /* update the error code */
  954. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  955. /* update return status */
  956. status = HAL_ERROR;
  957. }
  958. }
  959. return status;
  960. }
  961. /**
  962. * @brief DCMI Callback Unregistering
  963. * @param hdcmi dcmi handle
  964. * @param CallbackID dcmi Callback ID
  965. * @retval status
  966. */
  967. HAL_StatusTypeDef HAL_DCMI_UnRegisterCallback(DCMI_HandleTypeDef *hdcmi, HAL_DCMI_CallbackIDTypeDef CallbackID)
  968. {
  969. HAL_StatusTypeDef status = HAL_OK;
  970. if(hdcmi->State == HAL_DCMI_STATE_READY)
  971. {
  972. switch (CallbackID)
  973. {
  974. case HAL_DCMI_FRAME_EVENT_CB_ID :
  975. hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback */
  976. break;
  977. case HAL_DCMI_VSYNC_EVENT_CB_ID :
  978. hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback */
  979. break;
  980. case HAL_DCMI_LINE_EVENT_CB_ID :
  981. hdcmi->LineEventCallback = HAL_DCMI_LineEventCallback; /* Legacy weak LineEventCallback */
  982. break;
  983. case HAL_DCMI_ERROR_CB_ID :
  984. hdcmi->ErrorCallback = HAL_DCMI_ErrorCallback; /* Legacy weak ErrorCallback */
  985. break;
  986. case HAL_DCMI_MSPINIT_CB_ID :
  987. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  988. break;
  989. case HAL_DCMI_MSPDEINIT_CB_ID :
  990. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  991. break;
  992. default :
  993. /* update the error code */
  994. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  995. /* update return status */
  996. status = HAL_ERROR;
  997. break;
  998. }
  999. }
  1000. else if(hdcmi->State == HAL_DCMI_STATE_RESET)
  1001. {
  1002. switch (CallbackID)
  1003. {
  1004. case HAL_DCMI_MSPINIT_CB_ID :
  1005. hdcmi->MspInitCallback = HAL_DCMI_MspInit;
  1006. break;
  1007. case HAL_DCMI_MSPDEINIT_CB_ID :
  1008. hdcmi->MspDeInitCallback = HAL_DCMI_MspDeInit;
  1009. break;
  1010. default :
  1011. /* update the error code */
  1012. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  1013. /* update return status */
  1014. status = HAL_ERROR;
  1015. break;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. /* update the error code */
  1021. hdcmi->ErrorCode |= HAL_DCMI_ERROR_INVALID_CALLBACK;
  1022. /* update return status */
  1023. status = HAL_ERROR;
  1024. }
  1025. return status;
  1026. }
  1027. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  1028. /**
  1029. * @}
  1030. */
  1031. /* Private functions ---------------------------------------------------------*/
  1032. /** @defgroup DCMI_Private_Functions DCMI Private Functions
  1033. * @{
  1034. */
  1035. /**
  1036. * @brief DMA conversion complete callback.
  1037. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1038. * the configuration information for the specified DMA module.
  1039. * @note When the size of the frame being captured by the DCMI peripheral is
  1040. * larger than 0xFFFF (DMA maximum transfer length), this API initiates
  1041. * another DMA transfer to copy the second half of the work buffer
  1042. * associated to the DCMI handle to the final destination buffer.
  1043. * @retval None
  1044. */
  1045. static void DCMI_DMAXferCplt(DMA_HandleTypeDef *hdma)
  1046. {
  1047. uint32_t loop_length; /* transfer length */
  1048. uint32_t * tmpBuffer_Dest;
  1049. uint32_t * tmpBuffer_Orig;
  1050. uint32_t temp;
  1051. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1052. if(hdcmi->XferCount != 0U)
  1053. {
  1054. if (hdcmi->XferCount == 0xBEBE)
  1055. {
  1056. hdcmi->XferCount = hdcmi->XferCount_0;
  1057. hdcmi->XferSize = hdcmi->XferSize_0;
  1058. hdcmi->pBuffPtr = hdcmi->pBuffPtr_0;
  1059. }
  1060. else
  1061. {
  1062. /* Manage second half buffer copy in case of big transfer */
  1063. /* Decrement half-copies counter */
  1064. hdcmi->XferCount--;
  1065. /* Point at DCMI final destination */
  1066. tmpBuffer_Dest = (uint32_t *)hdcmi->pBuffPtr;
  1067. /* Point at DCMI circular buffer mid-location */
  1068. tmpBuffer_Orig = (uint32_t *)hdcmi->pCircularBuffer;
  1069. temp = (uint32_t) (tmpBuffer_Orig);
  1070. temp += hdcmi->HalfCopyLength * 4U;
  1071. tmpBuffer_Orig = (uint32_t *) temp;
  1072. /* copy half the buffer size */
  1073. loop_length = hdcmi->HalfCopyLength;
  1074. /* Save next entry to write at next half DMA transfer interruption */
  1075. hdcmi->pBuffPtr += (uint32_t) loop_length*4U;
  1076. hdcmi->XferSize -= hdcmi->HalfCopyLength;
  1077. if (hdcmi->XferCount == 0)
  1078. {
  1079. hdcmi->XferCount = 0xBEBE;
  1080. }
  1081. /* Data copy from work buffer to final destination buffer */
  1082. /* Enable the DMA Channel */
  1083. if (HAL_DMA_Start_IT(hdcmi->DMAM2M_Handle, (uint32_t) tmpBuffer_Orig, (uint32_t) tmpBuffer_Dest, loop_length) != HAL_OK)
  1084. {
  1085. /* Update error code */
  1086. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  1087. /* Change DCMI state */
  1088. hdcmi->State = HAL_DCMI_STATE_READY;
  1089. /* Process Unlocked */
  1090. __HAL_UNLOCK(hdcmi);
  1091. /* DCMI error Callback */
  1092. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  1093. /*Call registered DCMI error callback*/
  1094. hdcmi->ErrorCallback(hdcmi);
  1095. #else
  1096. HAL_DCMI_ErrorCallback(hdcmi);
  1097. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  1098. }
  1099. }
  1100. }
  1101. else
  1102. {
  1103. /* if End of frame IT is disabled */
  1104. if((hdcmi->Instance->IER & DCMI_IT_FRAME) == 0x0U)
  1105. {
  1106. /* If End of Frame flag is set */
  1107. if(__HAL_DCMI_GET_FLAG(hdcmi, (uint32_t)DCMI_FLAG_FRAMERI) != 0x0UL)
  1108. {
  1109. /* Clear the End of Frame flag */
  1110. __HAL_DCMI_CLEAR_FLAG(hdcmi, DCMI_FLAG_FRAMERI);
  1111. /* When snapshot mode, disable Vsync, Error and Overrun interrupts */
  1112. if((hdcmi->Instance->CR & DCMI_CR_CM) == DCMI_MODE_SNAPSHOT)
  1113. {
  1114. /* Disable the Vsync, Error and Overrun interrupts */
  1115. __HAL_DCMI_DISABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
  1116. hdcmi->State = HAL_DCMI_STATE_READY;
  1117. /* Process Unlocked */
  1118. __HAL_UNLOCK(hdcmi);
  1119. }
  1120. /* Frame Event Callback */
  1121. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  1122. /*Call registered DCMI frame event callback*/
  1123. hdcmi->FrameEventCallback(hdcmi);
  1124. #else
  1125. HAL_DCMI_FrameEventCallback(hdcmi);
  1126. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  1127. }
  1128. }
  1129. }
  1130. }
  1131. /**
  1132. * @brief DMA Half Transfer complete callback.
  1133. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1134. * the configuration information for the specified DMA module.
  1135. * @note When the size of the frame being captured by the DCMI peripheral is
  1136. * larger than 0xFFFF (DMA maximum transfer length), this API initiates
  1137. * another DMA transfer to copy the first half of the work buffer
  1138. * associated to the DCMI handle to the final destination buffer.
  1139. * @retval None
  1140. */
  1141. static void DCMI_DMAHalfXferCplt(DMA_HandleTypeDef *hdma)
  1142. {
  1143. uint32_t loop_length; /* transfer length */
  1144. uint32_t * tmpBuffer_Dest;
  1145. uint32_t * tmpBuffer_Orig;
  1146. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1147. if(hdcmi->XferCount != 0U)
  1148. {
  1149. if (hdcmi->XferCount != 0xBEBE)
  1150. {
  1151. /* Manage first half buffer copy in case of big transfer */
  1152. /* Decrement half-copies counter */
  1153. hdcmi->XferCount--;
  1154. /* Point at DCMI final destination */
  1155. tmpBuffer_Dest = (uint32_t *)hdcmi->pBuffPtr;
  1156. /* Point at DCMI circular buffer start */
  1157. tmpBuffer_Orig = (uint32_t *)hdcmi->pCircularBuffer;
  1158. /* copy half the buffer size */
  1159. loop_length = hdcmi->HalfCopyLength;
  1160. /* Save next entry to write at next DMA transfer interruption */
  1161. hdcmi->pBuffPtr += (uint32_t) loop_length*4U;
  1162. hdcmi->XferSize -= hdcmi->HalfCopyLength;
  1163. /* Data copy from work buffer to final destination buffer */
  1164. /* Enable the DMA Channel */
  1165. if (HAL_DMA_Start_IT(hdcmi->DMAM2M_Handle, (uint32_t) tmpBuffer_Orig, (uint32_t) tmpBuffer_Dest, loop_length) != HAL_OK)
  1166. {
  1167. /* Update error code */
  1168. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  1169. /* Change DCMI state */
  1170. hdcmi->State = HAL_DCMI_STATE_READY;
  1171. /* Process Unlocked */
  1172. __HAL_UNLOCK(hdcmi);
  1173. /* DCMI error Callback */
  1174. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  1175. /*Call registered DCMI error callback*/
  1176. hdcmi->ErrorCallback(hdcmi);
  1177. #else
  1178. HAL_DCMI_ErrorCallback(hdcmi);
  1179. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  1180. }
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. * @brief DMA error callback
  1186. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1187. * the configuration information for the specified DMA module.
  1188. * @retval None
  1189. */
  1190. static void DCMI_DMAError(DMA_HandleTypeDef *hdma)
  1191. {
  1192. DCMI_HandleTypeDef* hdcmi = ( DCMI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1193. /* Update error code */
  1194. hdcmi->ErrorCode |= HAL_DCMI_ERROR_DMA;
  1195. /* Change DCMI state */
  1196. hdcmi->State = HAL_DCMI_STATE_READY;
  1197. /* DCMI error Callback */
  1198. #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
  1199. /*Call registered DCMI error callback*/
  1200. hdcmi->ErrorCallback(hdcmi);
  1201. #else
  1202. HAL_DCMI_ErrorCallback(hdcmi);
  1203. #endif /* USE_HAL_DCMI_REGISTER_CALLBACKS */
  1204. }
  1205. /**
  1206. * @brief Sub-buffers transfer size computation.
  1207. * @note In the case of a frame size larger than the maximum DMA transfer length (0xFFFF),
  1208. * the transfer from DCMI DR register to the final output buffer is carried out by a sequence
  1209. * of intermediate sub-copies to temporary buffers of size less than 0xFFFF.
  1210. * To optimize the number of DMA transfers, the API computes the temporary buffer
  1211. * size so that the latter is an even number less than 0xFFFF, that divides the final
  1212. * buffer size and is as high as possible. The API implements a sub-optimum solution for
  1213. * complexity's sake.
  1214. * @note InputSize MUST be even.
  1215. * @param InputSize full buffer size (in 32-bit words)
  1216. * @retval Transfer size (in 32-bit words)
  1217. */
  1218. static uint32_t DCMI_TransferSize(uint32_t InputSize)
  1219. {
  1220. uint32_t j = 1;
  1221. uint32_t temp = InputSize;
  1222. uint32_t aPrime[NPRIME] = {0};
  1223. uint32_t output = 2; /* Want a result which is an even number */
  1224. static const uint32_t PrimeArray[NPRIME] = { 1UL, 2UL, 3UL, 5UL,
  1225. 7UL, 11UL, 13UL, 17UL,
  1226. 19UL, 23UL, 29UL, 31UL,
  1227. 37UL, 41UL, 43UL, 47UL};
  1228. /* Develop InputSize in product of prime numbers */
  1229. while (j < NPRIME)
  1230. {
  1231. if (temp < PrimeArray[j])
  1232. {
  1233. break;
  1234. }
  1235. while ((temp % PrimeArray[j]) == 0U)
  1236. {
  1237. aPrime[j]++;
  1238. temp /= PrimeArray[j];
  1239. }
  1240. j++;
  1241. }
  1242. /* Search for the biggest even divisor less or equal to 0xFFFE = 65534 */
  1243. aPrime[1] -= 1U; /* output is initialized to 2, so don't count divider 2 twice */
  1244. /* The algorithm below yields a sub-optimal solution
  1245. but in an acceptable time. */
  1246. j = NPRIME-1U;
  1247. while ((j > 0U) && (output <= 0xFFFEU))
  1248. {
  1249. while (aPrime[j] > 0U)
  1250. {
  1251. if ((output * PrimeArray[j]) > 0xFFFEU)
  1252. {
  1253. break;
  1254. }
  1255. else
  1256. {
  1257. output *= PrimeArray[j];
  1258. aPrime[j]--;
  1259. }
  1260. }
  1261. j--;
  1262. }
  1263. return output;
  1264. }
  1265. /**
  1266. * @}
  1267. */
  1268. /**
  1269. * @}
  1270. */
  1271. /**
  1272. * @}
  1273. */
  1274. #endif /* DCMI */
  1275. #endif /* HAL_DCMI_MODULE_ENABLED */