1 /*
2  * Copyright (c) 2022 OpenLuat & AirM2M
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 
22 
23 #ifndef __AIR105_DCMI_H
24 #define __AIR105_DCMI_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* Includes ------------------------------------------------------------------*/
31 #include "air105.h"
32 
33 
34 /* Exported types ------------------------------------------------------------*/
35 /**
36   * @brief   DCMI Init structure definition
37   */
38 typedef struct
39 {
40     uint16_t DCMI_CaptureMode;      /*!< Specifies the Capture Mode: Continuous or Snapshot.
41                                        This parameter can be a value of @ref DCMI_Capture_Mode */
42 
43     uint16_t DCMI_SynchroMode;      /*!< Specifies the Synchronization Mode: Hardware or Embedded.
44                                        This parameter can be a value of @ref DCMI_Synchronization_Mode */
45 
46     uint16_t DCMI_PCKPolarity;      /*!< Specifies the Pixel clock polarity: Falling or Rising.
47                                        This parameter can be a value of @ref DCMI_PIXCK_Polarity */
48 
49     uint16_t DCMI_VSPolarity;       /*!< Specifies the Vertical synchronization polarity: High or Low.
50                                        This parameter can be a value of @ref DCMI_VSYNC_Polarity */
51 
52     uint16_t DCMI_HSPolarity;       /*!< Specifies the Horizontal synchronization polarity: High or Low.
53                                        This parameter can be a value of @ref DCMI_HSYNC_Polarity */
54 
55     uint16_t DCMI_CaptureRate;      /*!< Specifies the frequency of frame capture: All, 1/2 or 1/4.
56                                        This parameter can be a value of @ref DCMI_Capture_Rate */
57 
58     uint16_t DCMI_ExtendedDataMode; /*!< Specifies the data width: 8-bit, 10-bit, 12-bit or 14-bit.
59                                        This parameter can be a value of @ref DCMI_Extended_Data_Mode */
60     uint32_t DCMI_ByteSelectMode;   /*!< Specifies the Capture Select Byte Mode: Select All Bytes, 1 of 2, 1 of 4 or 2 of 4
61                                        This parameter can be a value of @ref DCMI_ByteSelect_Mode */
62     uint32_t DCMI_LineSelectMode;   /*!< Specifies the Capture Select Line Mode: Select All Lines, Odd Line or Even Line
63                                        This parameter can be a value of @ref DCMI_ByteLine_Mode */
64     uint32_t DCMI_ClockDiv;     /*!< Specifies the DCMI Slave Clock Div: 2~16 from HCLK
65                                        This parameter can be a value of @ref DCMI_Clock_Div */
66 } DCMI_InitTypeDef;
67 
68 /**
69   * @brief   DCMI CROP Init structure definition
70   */
71 typedef struct
72 {
73     uint16_t DCMI_VerticalStartLine;      /*!< Specifies the Vertical start line count from which the image capture
74                                              will start. This parameter can be a value between 0x00 and 0x1FFF */
75 
76     uint16_t DCMI_HorizontalOffsetCount;  /*!< Specifies the number of pixel clocks to count before starting a capture.
77                                              This parameter can be a value between 0x00 and 0x3FFF */
78 
79     uint16_t DCMI_VerticalLineCount;      /*!< Specifies the number of lines to be captured from the starting point.
80                                              This parameter can be a value between 0x00 and 0x3FFF */
81 
82     uint16_t DCMI_CaptureCount;           /*!< Specifies the number of pixel clocks to be captured from the starting
83                                              point on the same line.
84                                              This parameter can be a value between 0x00 and 0x3FFF */
85 } DCMI_CROPInitTypeDef;
86 
87 /**
88   * @brief   DCMI Embedded Synchronisation CODE Init structure definition
89   */
90 typedef struct
91 {
92     uint8_t DCMI_FrameStartCode; /*!< Specifies the code of the frame start delimiter. */
93     uint8_t DCMI_LineStartCode;  /*!< Specifies the code of the line start delimiter. */
94     uint8_t DCMI_LineEndCode;    /*!< Specifies the code of the line end delimiter. */
95     uint8_t DCMI_FrameEndCode;   /*!< Specifies the code of the frame end delimiter. */
96 } DCMI_CodesInitTypeDef;
97 
98 /* Exported constants --------------------------------------------------------*/
99 
100 /** @defgroup DCMI_Exported_Constants
101   * @{
102   */
103 
104 /** @defgroup DCMI_Capture_Mode
105   * @{
106   */
107 #define DCMI_CaptureMode_Continuous    ((uint16_t)0x0000) /*!< The received data are transferred continuously
108                                                                into the destination memory through the DMA */
109 #define DCMI_CaptureMode_SnapShot      ((uint16_t)0x0002) /*!< Once activated, the interface waits for the start of
110                                                                frame and then transfers a single frame through the DMA */
111 #define IS_DCMI_CAPTURE_MODE(MODE)(((MODE) == DCMI_CaptureMode_Continuous) || \
112                                    ((MODE) == DCMI_CaptureMode_SnapShot))
113 /**
114   * @}
115   */
116 
117 
118 /** @defgroup DCMI_Synchronization_Mode
119   * @{
120   */
121 #define DCMI_SynchroMode_Hardware    ((uint16_t)0x0000) /*!< Hardware synchronization data capture (frame/line start/stop)
122                                                              is synchronized with the HSYNC/VSYNC signals */
123 #define DCMI_SynchroMode_Embedded    ((uint16_t)0x0010) /*!< Embedded synchronization data capture is synchronized with
124                                                              synchronization codes embedded in the data flow */
125 #define IS_DCMI_SYNCHRO(MODE)(((MODE) == DCMI_SynchroMode_Hardware) || \
126                               ((MODE) == DCMI_SynchroMode_Embedded))
127 /**
128   * @}
129   */
130 
131 
132 /** @defgroup DCMI_PIXCK_Polarity
133   * @{
134   */
135 #define DCMI_PCKPolarity_Falling    ((uint16_t)0x0000) /*!< Pixel clock active on Falling edge */
136 #define DCMI_PCKPolarity_Rising     ((uint16_t)0x0020) /*!< Pixel clock active on Rising edge */
137 #define IS_DCMI_PCKPOLARITY(POLARITY)(((POLARITY) == DCMI_PCKPolarity_Falling) || \
138                                       ((POLARITY) == DCMI_PCKPolarity_Rising))
139 /**
140   * @}
141   */
142 
143 
144 /** @defgroup DCMI_VSYNC_Polarity
145   * @{
146   */
147 #define DCMI_VSPolarity_Low     ((uint16_t)0x0000) /*!< Vertical synchronization active Low */
148 #define DCMI_VSPolarity_High    ((uint16_t)0x0080) /*!< Vertical synchronization active High */
149 #define IS_DCMI_VSPOLARITY(POLARITY)(((POLARITY) == DCMI_VSPolarity_Low) || \
150                                      ((POLARITY) == DCMI_VSPolarity_High))
151 /**
152   * @}
153   */
154 
155 
156 /** @defgroup DCMI_HSYNC_Polarity
157   * @{
158   */
159 #define DCMI_HSPolarity_Low     ((uint16_t)0x0000) /*!< Horizontal synchronization active Low */
160 #define DCMI_HSPolarity_High    ((uint16_t)0x0040) /*!< Horizontal synchronization active High */
161 #define IS_DCMI_HSPOLARITY(POLARITY)(((POLARITY) == DCMI_HSPolarity_Low) || \
162                                      ((POLARITY) == DCMI_HSPolarity_High))
163 /**
164   * @}
165   */
166 
167 
168 /** @defgroup DCMI_Capture_Rate
169   * @{
170   */
171 #define DCMI_CaptureRate_All_Frame     ((uint16_t)0x0000) /*!< All frames are captured */
172 #define DCMI_CaptureRate_1of2_Frame    ((uint16_t)0x0100) /*!< Every alternate frame captured */
173 #define DCMI_CaptureRate_1of4_Frame    ((uint16_t)0x0200) /*!< One frame in 4 frames captured */
174 #define IS_DCMI_CAPTURE_RATE(RATE) (((RATE) == DCMI_CaptureRate_All_Frame) || \
175                                     ((RATE) == DCMI_CaptureRate_1of2_Frame) ||\
176                                     ((RATE) == DCMI_CaptureRate_1of4_Frame))
177 
178 /** @defgroup DCMI_Clock_Div
179   * @{
180   */
181 #define DCMI_Clock_Div2     ((uint32_t)0x00000000)
182 #define DCMI_Clock_Div4     ((uint32_t)0x20000000)
183 #define DCMI_Clock_Div6     ((uint32_t)0x40000000)
184 #define DCMI_Clock_Div8     ((uint32_t)0x60000000)
185 #define DCMI_Clock_Div10        ((uint32_t)0x80000000)
186 #define DCMI_Clock_Div12        ((uint32_t)0xA0000000)
187 #define DCMI_Clock_Div14        ((uint32_t)0xC0000000)
188 #define DCMI_Clock_Div16        ((uint32_t)0xE0000000)
189 #define IS_DCMI_CLOCK_DIV(DIV)  (((DIV) == DCMI_Clock_Div2) ||\
190                                  ((DIV) == DCMI_Clock_Div4) ||\
191                                  ((DIV) == DCMI_Clock_Div6) ||\
192                                  ((DIV) == DCMI_Clock_Div8) ||\
193                                  ((DIV) == DCMI_Clock_Div10) ||\
194                                  ((DIV) == DCMI_Clock_Div12) ||\
195                                  ((DIV) == DCMI_Clock_Div14) ||\
196                                  ((DIV) == DCMI_Clock_Div16))
197 #define DCMI_Clock_Div_MSK      ((uint32_t)0xE0000000)
198 
199 /**
200   * @}
201   */
202 
203 
204 /** @defgroup DCMI_Extended_Data_Mode
205   * @{
206   */
207 #define DCMI_ExtendedDataMode_8b     ((uint16_t)0x0000) /*!< Interface captures 8-bit data on every pixel clock */
208 #define DCMI_ExtendedDataMode_10b    ((uint16_t)0x0400) /*!< Interface captures 10-bit data on every pixel clock */
209 #define DCMI_ExtendedDataMode_12b    ((uint16_t)0x0800) /*!< Interface captures 12-bit data on every pixel clock */
210 #define DCMI_ExtendedDataMode_14b    ((uint16_t)0x0C00) /*!< Interface captures 14-bit data on every pixel clock */
211 #define IS_DCMI_EXTENDED_DATA(DATA) (((DATA) == DCMI_ExtendedDataMode_8b) || \
212                                      ((DATA) == DCMI_ExtendedDataMode_10b) ||\
213                                      ((DATA) == DCMI_ExtendedDataMode_12b) ||\
214                                      ((DATA) == DCMI_ExtendedDataMode_14b))
215 
216 /** @defgroup DCMI_ByteSelectMode
217   * @{
218   */
219 #define DCMI_ByteSelect_Mode_AllByte        ((uint32_t)0x00000000)
220 #define DCMI_ByteSelect_Mode_1of2_OddStart  ((uint32_t)0x00010000)
221 #define DCMI_ByteSelect_Mode_1of4_OddStart  ((uint32_t)0x00020000)
222 #define DCMI_ByteSelect_Mode_2of4_OddStart  ((uint32_t)0x00030000)
223 #define DCMI_ByteSelect_Mode_1of2_EvenStart ((uint32_t)0x00050000)
224 #define DCMI_ByteSelect_Mode_1of4_EvenStart ((uint32_t)0x00060000)
225 #define DCMI_ByteSelect_Mode_2of4_EvenStart ((uint32_t)0x00070000)
226 #define IS_DCMI_BYTESELECT_MODE(MODE)      (((MODE) == DCMI_ByteSelect_Mode_AllByte) ||\
227                                             ((MODE) == DCMI_ByteSelect_Mode_1of2_OddStart) ||\
228                                             ((MODE) == DCMI_ByteSelect_Mode_1of4_OddStart) ||\
229                                             ((MODE) == DCMI_ByteSelect_Mode_2of4_OddStart) ||\
230                                             ((MODE) == DCMI_ByteSelect_Mode_1of2_EvenStart) ||\
231                                             ((MODE) == DCMI_ByteSelect_Mode_1of4_EvenStart) ||\
232                                             ((MODE) == DCMI_ByteSelect_Mode_2of4_EvenStart))
233 
234 /** @defgroup DCMI_ByteSelectMode
235   * @{
236   */
237 #define DCMI_LineSelect_Mode_AllLine        ((uint32_t)0x00000000)
238 #define DCMI_LineSelect_Mode_OddLine        ((uint32_t)0x00080000)
239 #define DCMI_LineSelect_Mode_EvenLine       ((uint32_t)0x00180000)
240 #define IS_DCMI_LINESELECT_MODE(MODE)      (((MODE) == DCMI_LineSelect_Mode_AllLine) ||\
241                                             ((MODE) == DCMI_LineSelect_Mode_OddLine) ||\
242                                             ((MODE) == DCMI_LineSelect_Mode_EvenLine))
243 
244 /**
245   * @}
246   */
247 
248 
249 /** @defgroup DCMI_DMA_Request_FIFO_Size
250   * @{
251   */
252 #define DCMI_DMARequestFIFOSize_1Word     ((uint32_t)0x20000000) /*!< DCMI FIFO DMARequest*/
253 #define DCMI_DMARequestFIFOSize_2Word     ((uint32_t)0x40000000) /*!< DCMI FIFO DMARequest*/
254 #define DCMI_DMARequestFIFOSize_4Word     ((uint32_t)0x80000000) /*!< DCMI FIFO DMARequest*/
255 #define DCMI_DMARequestFIFOSize_MSK       ((uint32_t)0xE0000000)
256 #define IS_DCMI_DMAREQUESTFIFO_SIZE(SIZE) (((SIZE) == DCMI_DMARequestFIFOSize_1Word) ||\
257                                            ((SIZE) == DCMI_DMARequestFIFOSize_2Word) ||\
258                                            ((SIZE) == DCMI_DMARequestFIFOSize_4Word))
259 /**
260   * @}
261   */
262 
263 
264 /** @defgroup DCMI_interrupt_sources
265   * @{
266   */
267 #define DCMI_IT_FRAME    ((uint16_t)0x0001)
268 #define DCMI_IT_OVF      ((uint16_t)0x0002)
269 #define DCMI_IT_ERR      ((uint16_t)0x0004)
270 #define DCMI_IT_VSYNC    ((uint16_t)0x0008)
271 #define DCMI_IT_LINE     ((uint16_t)0x0010)
272 #define IS_DCMI_CONFIG_IT(IT) ((((IT) & (uint16_t)0xFFE0) == 0x0000) && ((IT) != 0x0000))
273 #define IS_DCMI_GET_IT(IT) (((IT) == DCMI_IT_FRAME) || \
274                             ((IT) == DCMI_IT_OVF) || \
275                             ((IT) == DCMI_IT_ERR) || \
276                             ((IT) == DCMI_IT_VSYNC) || \
277                             ((IT) == DCMI_IT_LINE))
278 /**
279   * @}
280   */
281 
282 
283 /** @defgroup DCMI_Flags
284   * @{
285   */
286 /**
287   * @brief   DCMI SR register
288   */
289 #define DCMI_FLAG_HSYNC     ((uint16_t)0x2001)
290 #define DCMI_FLAG_VSYNC     ((uint16_t)0x2002)
291 #define DCMI_FLAG_FNE       ((uint16_t)0x2004)
292 /**
293   * @brief   DCMI RISR register
294   */
295 #define DCMI_FLAG_FRAMERI    ((uint16_t)0x0001)
296 #define DCMI_FLAG_OVFRI      ((uint16_t)0x0002)
297 #define DCMI_FLAG_ERRRI      ((uint16_t)0x0004)
298 #define DCMI_FLAG_VSYNCRI    ((uint16_t)0x0008)
299 #define DCMI_FLAG_LINERI     ((uint16_t)0x0010)
300 /**
301   * @brief   DCMI MISR register
302   */
303 #define DCMI_FLAG_FRAMEMI    ((uint16_t)0x1001)
304 #define DCMI_FLAG_OVFMI      ((uint16_t)0x1002)
305 #define DCMI_FLAG_ERRMI      ((uint16_t)0x1004)
306 #define DCMI_FLAG_VSYNCMI    ((uint16_t)0x1008)
307 #define DCMI_FLAG_LINEMI     ((uint16_t)0x1010)
308 #define IS_DCMI_GET_FLAG(FLAG) (((FLAG) == DCMI_FLAG_HSYNC) || \
309                                 ((FLAG) == DCMI_FLAG_VSYNC) || \
310                                 ((FLAG) == DCMI_FLAG_FNE) || \
311                                 ((FLAG) == DCMI_FLAG_FRAMERI) || \
312                                 ((FLAG) == DCMI_FLAG_OVFRI) || \
313                                 ((FLAG) == DCMI_FLAG_ERRRI) || \
314                                 ((FLAG) == DCMI_FLAG_VSYNCRI) || \
315                                 ((FLAG) == DCMI_FLAG_LINERI) || \
316                                 ((FLAG) == DCMI_FLAG_FRAMEMI) || \
317                                 ((FLAG) == DCMI_FLAG_OVFMI) || \
318                                 ((FLAG) == DCMI_FLAG_ERRMI) || \
319                                 ((FLAG) == DCMI_FLAG_VSYNCMI) || \
320                                 ((FLAG) == DCMI_FLAG_LINEMI))
321 
322 #define IS_DCMI_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFFE0) == 0x0000) && ((FLAG) != 0x0000))
323 
324 /**
325   * @brief  set dcmi input from out signal
326   */
327 #define DCMI_FROM_OUT   (0x1<<13)
328 
329 /*  Function used to set the DCMI configuration to the default reset state ****/
330 void DCMI_DeInit(void);
331 
332 /* Initialization and Configuration functions *********************************/
333 void DCMI_Init(DCMI_InitTypeDef* DCMI_InitStruct);
334 void DCMI_StructInit(DCMI_InitTypeDef* DCMI_InitStruct);
335 void DCMI_CROPConfig(DCMI_CROPInitTypeDef* DCMI_CROPInitStruct);
336 void DCMI_CROPCmd(FunctionalState NewState);
337 void DCMI_JPEGCmd(FunctionalState NewState);
338 void DCMI_SetDMARequsetFIFOSize(uint32_t NewFIFOSize);
339 void DCMI_SetExtInput(void);
340 
341 /* Image capture functions ****************************************************/
342 void DCMI_Cmd(FunctionalState NewState);
343 void DCMI_CaptureCmd(FunctionalState NewState);
344 uint32_t DCMI_ReadData(void);
345 uint32_t DCMI_GetFIFODataWordNum(void);
346 
347 /* Interrupts and flags management functions **********************************/
348 void DCMI_ITConfig(uint16_t DCMI_IT, FunctionalState NewState);
349 FlagStatus DCMI_GetFlagStatus(uint16_t DCMI_FLAG);
350 void DCMI_ClearFlag(uint16_t DCMI_FLAG);
351 ITStatus DCMI_GetITStatus(uint16_t DCMI_IT);
352 void DCMI_ClearITPendingBit(uint16_t DCMI_IT);
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif /*__AIR105_DCMI_H */
359 
360 
361