1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_hal_dcmi_ex.c
4   * @author  MCD Application Team
5   * @brief   DCMI Extension HAL module driver
6   *          This file provides firmware functions to manage the following
7   *          functionalities of DCMI extension peripheral:
8   *           + Extension features functions
9   *
10   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2017 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file in
17   * the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   ******************************************************************************
20   @verbatim
21   ==============================================================================
22                ##### DCMI peripheral extension features  #####
23   ==============================================================================
24 
25   [..] Comparing to other previous devices, the DCMI interface for STM32F446xx
26        devices contains the following additional features :
27 
28        (+) Support of Black and White cameras
29 
30                      ##### How to use this driver #####
31   ==============================================================================
32   [..] This driver provides functions to manage the Black and White feature
33 
34   @endverbatim
35   ******************************************************************************
36   */
37 
38 /* Includes ------------------------------------------------------------------*/
39 #include "stm32f4xx_hal.h"
40 
41 /** @addtogroup STM32F4xx_HAL_Driver
42   * @{
43   */
44 /** @defgroup DCMIEx DCMIEx
45   * @brief DCMI Extended HAL module driver
46   * @{
47   */
48 
49 #ifdef HAL_DCMI_MODULE_ENABLED
50 
51 #if defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) ||\
52     defined(STM32F439xx) || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
53 /* Private typedef -----------------------------------------------------------*/
54 /* Private define ------------------------------------------------------------*/
55 /* Private macro -------------------------------------------------------------*/
56 /* Private variables ---------------------------------------------------------*/
57 /* Private function prototypes -----------------------------------------------*/
58 /* Exported functions --------------------------------------------------------*/
59 
60 /** @defgroup DCMIEx_Exported_Functions DCMI Extended Exported Functions
61   * @{
62   */
63 
64 /**
65   * @}
66   */
67 
68 /** @addtogroup DCMI_Exported_Functions_Group1 Initialization and Configuration functions
69   * @{
70   */
71 
72 /**
73   * @brief  Initializes the DCMI according to the specified
74   *         parameters in the DCMI_InitTypeDef and create the associated handle.
75   * @param  hdcmi pointer to a DCMI_HandleTypeDef structure that contains
76   *                the configuration information for DCMI.
77   * @retval HAL status
78   */
HAL_DCMI_Init(DCMI_HandleTypeDef * hdcmi)79 HAL_StatusTypeDef HAL_DCMI_Init(DCMI_HandleTypeDef *hdcmi)
80 {
81   /* Check the DCMI peripheral state */
82   if(hdcmi == NULL)
83   {
84      return HAL_ERROR;
85   }
86 
87   /* Check function parameters */
88   assert_param(IS_DCMI_ALL_INSTANCE(hdcmi->Instance));
89   assert_param(IS_DCMI_PCKPOLARITY(hdcmi->Init.PCKPolarity));
90   assert_param(IS_DCMI_VSPOLARITY(hdcmi->Init.VSPolarity));
91   assert_param(IS_DCMI_HSPOLARITY(hdcmi->Init.HSPolarity));
92   assert_param(IS_DCMI_SYNCHRO(hdcmi->Init.SynchroMode));
93   assert_param(IS_DCMI_CAPTURE_RATE(hdcmi->Init.CaptureRate));
94   assert_param(IS_DCMI_EXTENDED_DATA(hdcmi->Init.ExtendedDataMode));
95   assert_param(IS_DCMI_MODE_JPEG(hdcmi->Init.JPEGMode));
96 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
97   assert_param(IS_DCMI_BYTE_SELECT_MODE(hdcmi->Init.ByteSelectMode));
98   assert_param(IS_DCMI_BYTE_SELECT_START(hdcmi->Init.ByteSelectStart));
99   assert_param(IS_DCMI_LINE_SELECT_MODE(hdcmi->Init.LineSelectMode));
100   assert_param(IS_DCMI_LINE_SELECT_START(hdcmi->Init.LineSelectStart));
101 #endif /* STM32F446xx || STM32F469xx || STM32F479xx */
102   if(hdcmi->State == HAL_DCMI_STATE_RESET)
103   {
104     /* Allocate lock resource and initialize it */
105     hdcmi->Lock = HAL_UNLOCKED;
106     /* Init the low level hardware */
107   /* Init the DCMI Callback settings */
108 #if (USE_HAL_DCMI_REGISTER_CALLBACKS == 1)
109     hdcmi->FrameEventCallback = HAL_DCMI_FrameEventCallback; /* Legacy weak FrameEventCallback  */
110     hdcmi->VsyncEventCallback = HAL_DCMI_VsyncEventCallback; /* Legacy weak VsyncEventCallback  */
111     hdcmi->LineEventCallback  = HAL_DCMI_LineEventCallback;  /* Legacy weak LineEventCallback   */
112     hdcmi->ErrorCallback      = HAL_DCMI_ErrorCallback;      /* Legacy weak ErrorCallback       */
113 
114     if(hdcmi->MspInitCallback == NULL)
115     {
116       /* Legacy weak MspInit Callback        */
117       hdcmi->MspInitCallback = HAL_DCMI_MspInit;
118     }
119     /* Initialize the low level hardware (MSP) */
120     hdcmi->MspInitCallback(hdcmi);
121 #else
122     /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
123     HAL_DCMI_MspInit(hdcmi);
124 #endif /* (USE_HAL_DCMI_REGISTER_CALLBACKS) */
125     HAL_DCMI_MspInit(hdcmi);
126   }
127 
128   /* Change the DCMI state */
129   hdcmi->State = HAL_DCMI_STATE_BUSY;
130                           /* Configures the HS, VS, DE and PC polarity */
131   hdcmi->Instance->CR &= ~(DCMI_CR_PCKPOL | DCMI_CR_HSPOL  | DCMI_CR_VSPOL  | DCMI_CR_EDM_0 |\
132                            DCMI_CR_EDM_1  | DCMI_CR_FCRC_0 | DCMI_CR_FCRC_1 | DCMI_CR_JPEG  |\
133                            DCMI_CR_ESS
134 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
135                            | DCMI_CR_BSM_0 | DCMI_CR_BSM_1 | DCMI_CR_OEBS |\
136                            DCMI_CR_LSM | DCMI_CR_OELS
137 #endif /* STM32F446xx || STM32F469xx || STM32F479xx */
138                            );
139   hdcmi->Instance->CR |=  (uint32_t)(hdcmi->Init.SynchroMode | hdcmi->Init.CaptureRate |\
140                                      hdcmi->Init.VSPolarity  | hdcmi->Init.HSPolarity  |\
141                                      hdcmi->Init.PCKPolarity | hdcmi->Init.ExtendedDataMode |\
142                                      hdcmi->Init.JPEGMode
143 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
144                                      | hdcmi->Init.ByteSelectMode |\
145                                      hdcmi->Init.ByteSelectStart | hdcmi->Init.LineSelectMode |\
146                                      hdcmi->Init.LineSelectStart
147 #endif /* STM32F446xx || STM32F469xx || STM32F479xx */
148                                      );
149   if(hdcmi->Init.SynchroMode == DCMI_SYNCHRO_EMBEDDED)
150   {
151     hdcmi->Instance->ESCR = (((uint32_t)hdcmi->Init.SyncroCode.FrameStartCode)    |
152                              ((uint32_t)hdcmi->Init.SyncroCode.LineStartCode << DCMI_POSITION_ESCR_LSC)|
153                              ((uint32_t)hdcmi->Init.SyncroCode.LineEndCode << DCMI_POSITION_ESCR_LEC) |
154                              ((uint32_t)hdcmi->Init.SyncroCode.FrameEndCode << DCMI_POSITION_ESCR_FEC));
155 
156   }
157 
158   /* Enable the Line, Vsync, Error and Overrun interrupts */
159   __HAL_DCMI_ENABLE_IT(hdcmi, DCMI_IT_LINE | DCMI_IT_VSYNC | DCMI_IT_ERR | DCMI_IT_OVR);
160 
161   /* Update error code */
162   hdcmi->ErrorCode = HAL_DCMI_ERROR_NONE;
163 
164   /* Initialize the DCMI state*/
165   hdcmi->State  = HAL_DCMI_STATE_READY;
166 
167   return HAL_OK;
168 }
169 
170 /**
171   * @}
172   */
173 #endif /* STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx ||\
174           STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */
175 #endif /* HAL_DCMI_MODULE_ENABLED */
176 /**
177   * @}
178   */
179 
180 /**
181   * @}
182   */
183