stm322xg_eval_camera.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. ******************************************************************************
  3. * @file stm322xg_eval_camera.c
  4. * @author MCD Application Team
  5. * @brief This file includes the driver for Camera module mounted on
  6. * STM322xG-EVAL evaluation board(MB786).
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* File Info: ------------------------------------------------------------------
  37. User NOTES
  38. 1. How to use this driver:
  39. --------------------------
  40. - This driver is used to drive the Camera.
  41. - The OV2640 component driver MUST be included with this driver.
  42. 2. Driver description:
  43. ---------------------
  44. + Initialization steps:
  45. o Initialize the Camera using the BSP_CAMERA_Init() function.
  46. o Start the Camera capture or snapshot using CAMERA_Start() function.
  47. o Suspend, resume or stop the Camera capture using the following functions:
  48. - BSP_CAMERA_Suspend()
  49. - BSP_CAMERA_Resume()
  50. - BSP_CAMERA_Stop()
  51. + Options
  52. o Increase or decrease on the fly the brightness and/or contrast
  53. using the following function:
  54. - BSP_CAMERA_ContrastBrightnessConfig
  55. o Add a special effect on the fly using the following functions:
  56. - BSP_CAMERA_BlackWhiteConfig()
  57. - BSP_CAMERA_ColorEffectConfig()
  58. ------------------------------------------------------------------------------*/
  59. /* Includes ------------------------------------------------------------------*/
  60. #include "stm322xg_eval_camera.h"
  61. /** @addtogroup BSP
  62. * @{
  63. */
  64. /** @addtogroup STM322xG_EVAL
  65. * @{
  66. */
  67. /** @defgroup STM322xG_EVAL_CAMERA STM322xG EVAL CAMERA
  68. * @{
  69. */
  70. /** @defgroup STM322xG_EVAL_CAMERA_Private_Variables STM322xG EVAL CAMERA Private Variables
  71. * @{
  72. */
  73. DCMI_HandleTypeDef hdcmi_eval;
  74. CAMERA_DrvTypeDef *camera_drv;
  75. uint32_t current_resolution;
  76. /**
  77. * @}
  78. */
  79. /** @defgroup STM322xG_EVAL_CAMERA_Private_FunctionPrototypes STM322xG EVAL CAMERA Private FunctionPrototypes
  80. * @{
  81. */
  82. static uint32_t GetSize(uint32_t resolution);
  83. /**
  84. * @}
  85. */
  86. /** @defgroup STM322xG_EVAL_CAMERA_Private_Functions STM322xG EVAL CAMERA Private Functions
  87. * @{
  88. */
  89. /**
  90. * @brief Initializes the Camera.
  91. * @param Resolution: Camera resolution
  92. * @retval Camera status
  93. */
  94. uint8_t BSP_CAMERA_Init(uint32_t Resolution)
  95. {
  96. DCMI_HandleTypeDef *phdcmi;
  97. uint8_t ret = CAMERA_ERROR;
  98. /* Get the DCMI handle structure */
  99. phdcmi = &hdcmi_eval;
  100. /*** Configures the DCMI to interface with the Camera module ***/
  101. /* DCMI configuration */
  102. phdcmi->Init.CaptureRate = DCMI_CR_ALL_FRAME;
  103. phdcmi->Init.HSPolarity = DCMI_HSPOLARITY_LOW;
  104. phdcmi->Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
  105. phdcmi->Init.VSPolarity = DCMI_VSPOLARITY_LOW;
  106. phdcmi->Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;
  107. phdcmi->Init.PCKPolarity = DCMI_PCKPOLARITY_RISING;
  108. phdcmi->Instance = DCMI;
  109. /* DCMI Initialization */
  110. BSP_CAMERA_MspInit();
  111. HAL_DCMI_Init(phdcmi);
  112. if(ov2640_drv.ReadID(CAMERA_I2C_ADDRESS) == OV2640_ID)
  113. {
  114. /* Initialize the Camera driver structure */
  115. camera_drv = &ov2640_drv;
  116. /* Camera Init */
  117. camera_drv->Init(CAMERA_I2C_ADDRESS, Resolution);
  118. /* Return CAMERA_OK status */
  119. ret = CAMERA_OK;
  120. }
  121. current_resolution = Resolution;
  122. return ret;
  123. }
  124. /**
  125. * @brief Starts the Camera capture in continuous mode.
  126. * @param buff: pointer to the Camera output buffer
  127. */
  128. void BSP_CAMERA_ContinuousStart(uint8_t *buff)
  129. {
  130. /* Start the Camera capture */
  131. HAL_DCMI_Start_DMA(&hdcmi_eval, DCMI_MODE_CONTINUOUS, (uint32_t)buff, GetSize(current_resolution));
  132. }
  133. /**
  134. * @brief Starts the Camera capture in snapshot mode.
  135. * @param buff: pointer to the Camera output buffer
  136. */
  137. void BSP_CAMERA_SnapshotStart(uint8_t *buff)
  138. {
  139. /* Start the Camera capture */
  140. HAL_DCMI_Start_DMA(&hdcmi_eval, DCMI_MODE_SNAPSHOT, (uint32_t)buff, GetSize(current_resolution));
  141. }
  142. /**
  143. * @brief Suspends the Camera capture.
  144. */
  145. void BSP_CAMERA_Suspend(void)
  146. {
  147. /* Suspend the Camera Capture */
  148. HAL_DCMI_Suspend(&hdcmi_eval);
  149. }
  150. /**
  151. * @brief Resumes the Camera capture.
  152. */
  153. void BSP_CAMERA_Resume(void)
  154. {
  155. /* Start the Camera Capture */
  156. HAL_DCMI_Resume(&hdcmi_eval);
  157. }
  158. /**
  159. * @brief Stops the Camera capture.
  160. * @retval Camera status
  161. */
  162. uint8_t BSP_CAMERA_Stop(void)
  163. {
  164. DCMI_HandleTypeDef *phdcmi;
  165. uint8_t ret = CAMERA_ERROR;
  166. /* Get the DCMI handle structure */
  167. phdcmi = &hdcmi_eval;
  168. if(HAL_DCMI_Stop(phdcmi) == HAL_OK)
  169. {
  170. ret = CAMERA_OK;
  171. }
  172. return ret;
  173. }
  174. /**
  175. * @brief Configures the Camera contrast and brightness.
  176. * @param contrast_level: Contrast level
  177. * This parameter can be one of the following values:
  178. * @arg CAMERA_CONTRAST_LEVEL4: for contrast +2
  179. * @arg CAMERA_CONTRAST_LEVEL3: for contrast +1
  180. * @arg CAMERA_CONTRAST_LEVEL2: for contrast 0
  181. * @arg CAMERA_CONTRAST_LEVEL1: for contrast -1
  182. * @arg CAMERA_CONTRAST_LEVEL0: for contrast -2
  183. * @param brightness_level: Brightness level
  184. * This parameter can be one of the following values:
  185. * @arg CAMERA_BRIGHTNESS_LEVEL4: for brightness +2
  186. * @arg CAMERA_BRIGHTNESS_LEVEL3: for brightness +1
  187. * @arg CAMERA_BRIGHTNESS_LEVEL2: for brightness 0
  188. * @arg CAMERA_BRIGHTNESS_LEVEL1: for brightness -1
  189. * @arg CAMERA_BRIGHTNESS_LEVEL0: for brightness -2
  190. */
  191. void BSP_CAMERA_ContrastBrightnessConfig(uint32_t contrast_level, uint32_t brightness_level)
  192. {
  193. if(camera_drv->Config != NULL)
  194. {
  195. camera_drv->Config(CAMERA_I2C_ADDRESS, CAMERA_CONTRAST_BRIGHTNESS, contrast_level, brightness_level);
  196. }
  197. }
  198. /**
  199. * @brief Configures the Camera white balance.
  200. * @param Mode: black_white mode
  201. * This parameter can be one of the following values:
  202. * @arg CAMERA_BLACK_WHITE_BW
  203. * @arg CAMERA_BLACK_WHITE_NEGATIVE
  204. * @arg CAMERA_BLACK_WHITE_BW_NEGATIVE
  205. * @arg CAMERA_BLACK_WHITE_NORMAL
  206. */
  207. void BSP_CAMERA_BlackWhiteConfig(uint32_t Mode)
  208. {
  209. if(camera_drv->Config != NULL)
  210. {
  211. camera_drv->Config(CAMERA_I2C_ADDRESS, CAMERA_BLACK_WHITE, Mode, 0);
  212. }
  213. }
  214. /**
  215. * @brief Configures the Camera color effect.
  216. * @param Effect: Color effect
  217. * This parameter can be one of the following values:
  218. * @arg CAMERA_COLOR_EFFECT_ANTIQUE
  219. * @arg CAMERA_COLOR_EFFECT_BLUE
  220. * @arg CAMERA_COLOR_EFFECT_GREEN
  221. * @arg CAMERA_COLOR_EFFECT_RED
  222. */
  223. void BSP_CAMERA_ColorEffectConfig(uint32_t Effect)
  224. {
  225. if(camera_drv->Config != NULL)
  226. {
  227. camera_drv->Config(CAMERA_I2C_ADDRESS, CAMERA_COLOR_EFFECT, Effect, 0);
  228. }
  229. }
  230. /**
  231. * @brief Handles DCMI interrupt request.
  232. */
  233. void BSP_CAMERA_IRQHandler(void)
  234. {
  235. HAL_DCMI_IRQHandler(&hdcmi_eval);
  236. }
  237. /**
  238. * @brief Handles DMA interrupt request.
  239. */
  240. void BSP_CAMERA_DMA_IRQHandler(void)
  241. {
  242. HAL_DMA_IRQHandler(hdcmi_eval.DMA_Handle);
  243. }
  244. /**
  245. * @brief Get the capture size.
  246. * @param resolution: the current resolution.
  247. * @retval cpature size
  248. */
  249. static uint32_t GetSize(uint32_t resolution)
  250. {
  251. uint32_t size = 0;
  252. /* Get capture size */
  253. switch (resolution)
  254. {
  255. case CAMERA_R160x120:
  256. {
  257. size = 0x2580;
  258. }
  259. break;
  260. case CAMERA_R320x240:
  261. {
  262. size = 0x9600;
  263. }
  264. break;
  265. default:
  266. {
  267. break;
  268. }
  269. }
  270. return size;
  271. }
  272. /**
  273. * @brief Initializes the DCMI MSP.
  274. */
  275. __weak void BSP_CAMERA_MspInit(void)
  276. {
  277. static DMA_HandleTypeDef hdma;
  278. GPIO_InitTypeDef GPIO_Init_Structure;
  279. DCMI_HandleTypeDef *hdcmi = &hdcmi_eval;
  280. /*** Enable peripherals and GPIO clocks ***/
  281. /* Enable DCMI clock */
  282. __HAL_RCC_DCMI_CLK_ENABLE();
  283. /* Enable DMA2 clock */
  284. __HAL_RCC_DMA2_CLK_ENABLE();
  285. /* Enable GPIO clocks */
  286. __HAL_RCC_GPIOA_CLK_ENABLE();
  287. __HAL_RCC_GPIOH_CLK_ENABLE();
  288. __HAL_RCC_GPIOI_CLK_ENABLE();
  289. /*** Configure the GPIO ***/
  290. /* Configure DCMI GPIO as alternate function */
  291. GPIO_Init_Structure.Pin = GPIO_PIN_6;
  292. GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
  293. GPIO_Init_Structure.Pull = GPIO_PULLUP;
  294. GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
  295. GPIO_Init_Structure.Alternate = GPIO_AF13_DCMI;
  296. HAL_GPIO_Init(GPIOA, &GPIO_Init_Structure);
  297. GPIO_Init_Structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 |\
  298. GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_14;
  299. GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
  300. GPIO_Init_Structure.Pull = GPIO_PULLUP;
  301. GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
  302. GPIO_Init_Structure.Alternate = GPIO_AF13_DCMI;
  303. HAL_GPIO_Init(GPIOH, &GPIO_Init_Structure);
  304. GPIO_Init_Structure.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 |\
  305. GPIO_PIN_7;
  306. GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
  307. GPIO_Init_Structure.Pull = GPIO_PULLUP;
  308. GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
  309. GPIO_Init_Structure.Alternate = GPIO_AF13_DCMI;
  310. HAL_GPIO_Init(GPIOI, &GPIO_Init_Structure);
  311. /*** Configure the DMA streams ***/
  312. /* Configure the DMA handler for Transmission process */
  313. hdma.Init.Channel = DMA_CHANNEL_1;
  314. hdma.Init.Direction = DMA_PERIPH_TO_MEMORY;
  315. hdma.Init.PeriphInc = DMA_PINC_DISABLE;
  316. hdma.Init.MemInc = DMA_MINC_ENABLE;
  317. hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  318. hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  319. hdma.Init.Mode = DMA_CIRCULAR;
  320. hdma.Init.Priority = DMA_PRIORITY_HIGH;
  321. hdma.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  322. hdma.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  323. hdma.Init.MemBurst = DMA_MBURST_SINGLE;
  324. hdma.Init.PeriphBurst = DMA_PBURST_SINGLE;
  325. hdma.Instance = DMA2_Stream1;
  326. /* Associate the initialized DMA handle to the DCMI handle */
  327. __HAL_LINKDMA(hdcmi, DMA_Handle, hdma);
  328. /*** Configure the NVIC for DCMI and DMA ***/
  329. /* NVIC configuration for DCMI transfer complete interrupt */
  330. HAL_NVIC_SetPriority(DCMI_IRQn, 0x0F, 0);
  331. HAL_NVIC_EnableIRQ(DCMI_IRQn);
  332. /* NVIC configuration for DMA2 transfer complete interrupt */
  333. HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 0x0F, 0);
  334. HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
  335. /* Configure the DMA stream */
  336. HAL_DMA_Init(hdcmi->DMA_Handle);
  337. }
  338. /**
  339. * @brief Line event callback
  340. * @param hdcmi: pointer to the DCMI handle
  341. */
  342. void HAL_DCMI_LineEventCallback(DCMI_HandleTypeDef *hdcmi)
  343. {
  344. BSP_CAMERA_LineEventCallback();
  345. }
  346. /**
  347. * @brief Line Event callback.
  348. */
  349. __weak void BSP_CAMERA_LineEventCallback(void)
  350. {
  351. /* NOTE : This function Should not be modified, when the callback is needed,
  352. the HAL_DCMI_LineEventCallback could be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @brief VSYNC event callback
  357. * @param hdcmi: pointer to the DCMI handle
  358. */
  359. void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi)
  360. {
  361. BSP_CAMERA_VsyncEventCallback();
  362. }
  363. /**
  364. * @brief VSYNC Event callback.
  365. */
  366. __weak void BSP_CAMERA_VsyncEventCallback(void)
  367. {
  368. /* NOTE : This function Should not be modified, when the callback is needed,
  369. the HAL_DCMI_VsyncEventCallback could be implemented in the user file
  370. */
  371. }
  372. /**
  373. * @brief Frame event callback
  374. * @param hdcmi: pointer to the DCMI handle
  375. */
  376. void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi)
  377. {
  378. BSP_CAMERA_FrameEventCallback();
  379. }
  380. /**
  381. * @brief Frame Event callback.
  382. */
  383. __weak void BSP_CAMERA_FrameEventCallback(void)
  384. {
  385. /* NOTE : This function Should not be modified, when the callback is needed,
  386. the HAL_DCMI_FrameEventCallback could be implemented in the user file
  387. */
  388. }
  389. /**
  390. * @brief Error callback
  391. * @param hdcmi: pointer to the DCMI handle
  392. */
  393. void HAL_DCMI_ErrorCallback(DCMI_HandleTypeDef *hdcmi)
  394. {
  395. BSP_CAMERA_ErrorCallback();
  396. }
  397. /**
  398. * @brief Error callback.
  399. */
  400. __weak void BSP_CAMERA_ErrorCallback(void)
  401. {
  402. /* NOTE : This function Should not be modified, when the callback is needed,
  403. the HAL_DCMI_ErrorCallback could be implemented in the user file
  404. */
  405. }
  406. /**
  407. * @}
  408. */
  409. /**
  410. * @}
  411. */
  412. /**
  413. * @}
  414. */
  415. /**
  416. * @}
  417. */
  418. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/