1 /**************************************************************************//**
2  * @file     ccap.c
3  * @version  V1.00
4  * @brief    M480 Series CCAP Driver Source File
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 
10 #include "NuMicro.h"
11 /** @addtogroup Standard_Driver Standard Driver
12   @{
13 */
14 
15 /** @addtogroup CCAP_Driver CCAP Driver
16   @{
17 */
18 
19 
20 /** @addtogroup CCAP_EXPORTED_FUNCTIONS CCAP Exported Functions
21   @{
22 */
23 
24 /**
25  * @brief      Open engine clock and sensor clock
26  *
27  * @param[in]  u32InFormat  The bits corresponding VSP, HSP, PCLK, INFMT, SNRTYPE, OUTFMT, PDORD and PNFMT configurations.
28  *             - VSP should be ether \ref CCAP_PAR_VSP_LOW or \ref CCAP_PAR_VSP_HIGH
29  *             - HSP should be ether \ref CCAP_PAR_HSP_LOW or \ref CCAP_PAR_HSP_HIGH
30  *             - PCLK should be ether \ref CCAP_PAR_PCLKP_LOW or \ref CCAP_PAR_PCLKP_HIGH
31  *             - INFMT should be ether \ref CCAP_PAR_INFMT_YUV422 or \ref CCAP_PAR_INFMT_RGB565
32  *             - SNRTYPE should be ether \ref CCAP_PAR_SENTYPE_CCIR601 or \ref CCAP_PAR_SENTYPE_CCIR656
33  *             - OUTFMT should be one of the following setting
34  *                      - \ref CCAP_PAR_OUTFMT_YUV422
35  *                      - \ref CCAP_PAR_OUTFMT_ONLY_Y
36  *                      - \ref CCAP_PAR_OUTFMT_RGB555
37  *                      - \ref CCAP_PAR_OUTFMT_RGB565
38  *             - PDORD should be one of the following setting
39  *                      - \ref CCAP_PAR_INDATORD_YUYV
40  *                      - \ref CCAP_PAR_INDATORD_YVYU
41  *                      - \ref CCAP_PAR_INDATORD_UYVY
42  *                      - \ref CCAP_PAR_INDATORD_VYUY
43  *                      - \ref CCAP_PAR_INDATORD_RGGB
44  *                      - \ref CCAP_PAR_INDATORD_BGGR
45  *                      - \ref CCAP_PAR_INDATORD_GBRG
46  *                      - \ref CCAP_PAR_INDATORD_GRBG
47  *             - PNFMT should be one of the following setting
48  *                      - \ref CCAP_PAR_PLNFMT_YUV422
49  *                      - \ref CCAP_PAR_PLNFMT_YUV420
50  *
51  * @param[in]  u32OutFormet Capture output format, should be one of following setting
52  *                      - \ref CCAP_CTL_PKTEN
53  *
54  * @return     None
55  *
56  * @details    Initialize the Image Capture Interface. Register a call back for driver internal using
57  */
CCAP_Open(uint32_t u32InFormat,uint32_t u32OutFormet)58 void CCAP_Open(uint32_t u32InFormat, uint32_t u32OutFormet)
59 {
60     CCAP->PAR = (CCAP->PAR & ~(0x000007BFUL)) | u32InFormat;
61     CCAP->CTL = (CCAP->CTL & ~(0x00000040UL)) | u32OutFormet;
62 }
63 
64 /**
65  * @brief Set Cropping Window Starting Address and Size
66  *
67  * @param[in] u32VStart: Cropping Window Vertical Starting Address. It should be 0 ~ 0x7FF.
68  *
69  * @param[in] u32HStart: Cropping Window Horizontal Starting Address. It should be 0 ~ 0x7FF.
70  *
71  * @param[in] u32Height: Cropping Window Height . It should be 0 ~ 0x7FF.
72  *
73  * @param[in] u32Width: Cropping Window Width. It should be 0 ~ 0x7FF.
74  *
75  * @return    None
76  *
77  * @details   Set Cropping Window Starting Address Register
78  */
CCAP_SetCroppingWindow(uint32_t u32VStart,uint32_t u32HStart,uint32_t u32Height,uint32_t u32Width)79 void CCAP_SetCroppingWindow(uint32_t u32VStart,uint32_t u32HStart, uint32_t u32Height, uint32_t u32Width)
80 {
81     CCAP->CWSP = (CCAP->CWSP & ~(CCAP_CWSP_CWSADDRV_Msk | CCAP_CWSP_CWSADDRH_Msk))
82                  | (((u32VStart << 16) | u32HStart));
83 
84     CCAP->CWS = (CCAP->CWS & ~(CCAP_CWS_CWH_Msk | CCAP_CWS_CWW_Msk))
85                 | ((u32Height << 16)| u32Width);
86 }
87 
88 
89 /**
90  * @brief     Set System Memory Packet Base Address0 Register
91  *
92  * @param[in]  u32Address : set PKTBA0 register, It should be 0x0 ~ 0xFFFFFFFF
93  *
94  * @return    None
95  *
96  * @details   Set System Memory Packet Base Address Register
97  */
CCAP_SetPacketBuf(uint32_t u32Address)98 void CCAP_SetPacketBuf(uint32_t  u32Address )
99 {
100     CCAP->PKTBA0 = u32Address;
101     CCAP->CTL |= CCAP_CTL_UPDATE_Msk;
102 }
103 
104 /**
105  * @brief     Close Camera Capture Interface
106  *
107  * @return    None
108  */
CCAP_Close(void)109 void CCAP_Close(void)
110 {
111     CCAP->CTL &= ~CCAP_CTL_CCAPEN;
112 }
113 
114 
115 /**
116  * @brief      Set CCAP Interrupt
117  *
118  * @param[in]  u32IntMask   Interrupt settings. It could be
119  *                           - \ref CCAP_INT_VIEN_Msk
120  *                           - \ref CCAP_INT_MEIEN_Msk
121  *                           - \ref CCAP_INT_ADDRMIEN_Msk
122  * @return     None
123  *
124  * @details    Set Video Frame End Interrupt Enable,
125  *                  System Memory Error Interrupt Enable,
126  *                  Address Match Interrupt Enable,
127  *                  Motion Detection Output Finish Interrupt Enable.
128  */
CCAP_EnableInt(uint32_t u32IntMask)129 void CCAP_EnableInt(uint32_t u32IntMask)
130 {
131     CCAP->INT = (CCAP->INT & ~(CCAP_INT_VIEN_Msk | CCAP_INT_MEIEN_Msk | CCAP_INT_ADDRMIEN_Msk ) )
132                 | u32IntMask;
133 }
134 
135 /**
136  * @brief      Disable CCAP Interrupt
137  *
138  * @param[in]  u32IntMask   Interrupt settings. It could be
139  *                           - \ref CCAP_INT_VINTF_Msk
140  *                           - \ref CCAP_INT_MEINTF_Msk
141  *                           - \ref CCAP_INT_ADDRMINTF_Msk
142 
143  * @return     None
144  *
145  * @details    Disable Video Frame End Interrupt ,
146  *                  System Memory Error Interrupt ,
147  *                  Address Match Interrupt and
148  *                  Motion Detection Output Finish Interrupt .
149  */
CCAP_DisableInt(uint32_t u32IntMask)150 void CCAP_DisableInt(uint32_t u32IntMask)
151 {
152     CCAP->INT = (CCAP->INT & ~(u32IntMask) ) ;
153 }
154 
155 
156 /**
157  * @brief      Enable Monochrome CMOS Sensor
158  *
159  * @param[in]  u32Interface   I/O interface settings. It could be
160  *                           - \ref CCAP_CTL_MY8_MY4
161  *                           - \ref CCAP_CTL_MY8_MY8
162  * @return     None
163  *
164  */
CCAP_EnableMono(uint32_t u32Interface)165 void CCAP_EnableMono(uint32_t u32Interface)
166 {
167     CCAP->CTL = (CCAP->CTL & ~CCAP_CTL_MY8_MY4) | CCAP_CTL_MONO_Msk |u32Interface;
168 }
169 
170 /**
171  * @brief      Disable Monochrome CMOS Sensor
172  *
173  * @return     None
174  *
175  */
CCAP_DisableMono(void)176 void CCAP_DisableMono(void)
177 {
178     CCAP->CTL |= CCAP_CTL_MONO_Msk;
179 }
180 
181 /**
182  * @brief      Enable Luminance 8-bit Y to 1-bit Y Conversion
183  *
184  * @param[in]  u32th   Luminance Y8 to Y1 Threshold Value, It should be 0 ~ 255.
185  *
186  * @return     None
187  *
188  */
CCAP_EnableLumaYOne(uint32_t u32th)189 void CCAP_EnableLumaYOne(uint32_t u32th)
190 {
191     CCAP->CTL |= CCAP_CTL_Luma_Y_One_Msk;
192     CCAP->LUMA_Y1_THD = u32th & 0xff;
193 }
194 
195 /**
196  * @brief      Disable Luminance 8-bit Y to 1-bit Y Conversion
197  *
198  * @return     None
199  *
200  */
CCAP_DisableLumaYOne(void)201 void CCAP_DisableLumaYOne(void)
202 {
203     CCAP->CTL &= ~CCAP_CTL_Luma_Y_One_Msk;
204 }
205 
206 /**
207  * @brief     Start Camera Capture Interface
208  *
209  * @return    None
210  */
CCAP_Start(void)211 void CCAP_Start(void)
212 {
213     CCAP->CTL |= CCAP_CTL_CCAPEN;
214 }
215 
216 /**
217  * @brief     Stop Camera Capture Interface
218  *
219  * @param[in]  u32FrameComplete :
220  *             TRUE:  Capture module automatically disable the CCAP module after a frame had been captured
221  *             FALSE: Stop Capture module now
222  * @return    None
223  *
224  * @details   if u32FrameComplete is set to TRUE then get a new frame and disable CCAP module
225  */
CCAP_Stop(uint32_t u32FrameComplete)226 void CCAP_Stop(uint32_t u32FrameComplete)
227 {
228     if(u32FrameComplete==FALSE)
229         CCAP->CTL &= ~CCAP_CTL_CCAPEN;
230     else
231     {
232         CCAP->CTL |= CCAP_CTL_SHUTTER_Msk;
233         while(CCAP_IS_STOPPED());
234     }
235 }
236 
237 /**
238  * @brief     Set Packet Scaling Vertical and Horizontal Factor Register
239  *
240  * @param[in] u32VNumerator: Packet Scaling Vertical Factor N. It should be 0 ~ FFFF.
241  *
242  * @param[in] u32VDenominator: Packet Scaling Vertical Factor M. It should be 0 ~ FFFF.
243  *
244  * @param[in] u32HNumerator: Packet Scaling Vertical Factor N. It should be 0 ~ FFFF.
245  *
246  * @param[in] u32HDenominator: Packet Scaling Vertical Factor M. It should be 0 ~ FFFF.
247  *
248  * @return    None
249  *
250  */
CCAP_SetPacketScaling(uint32_t u32VNumerator,uint32_t u32VDenominator,uint32_t u32HNumerator,uint32_t u32HDenominator)251 void CCAP_SetPacketScaling(uint32_t u32VNumerator, uint32_t u32VDenominator, uint32_t u32HNumerator, uint32_t u32HDenominator)
252 {
253     uint32_t u32NumeratorL, u32NumeratorH;
254     uint32_t u32DenominatorL, u32DenominatorH;
255 
256     u32NumeratorL = u32VNumerator&0xFF;
257     u32NumeratorH=u32VNumerator>>8;
258     u32DenominatorL = u32VDenominator&0xFF;
259     u32DenominatorH = u32VDenominator>>8;
260     CCAP->PKTSL = (CCAP->PKTSL & ~(CCAP_PKTSL_PKTSVNL_Msk | CCAP_PKTSL_PKTSVML_Msk))
261                   | ((u32NumeratorL << 24)| (u32DenominatorL << 16));
262     CCAP->PKTSM = (CCAP->PKTSM & ~(CCAP_PKTSM_PKTSVNH_Msk | CCAP_PKTSM_PKTSVMH_Msk))
263                   | ((u32NumeratorH << 24) | (u32DenominatorH << 16));
264 
265     u32NumeratorL = u32HNumerator&0xFF;
266     u32NumeratorH=u32HNumerator>>8;
267     u32DenominatorL = u32HDenominator&0xFF;
268     u32DenominatorH = u32HDenominator>>8;
269     CCAP->PKTSL = (CCAP->PKTSL & ~(CCAP_PKTSL_PKTSHNL_Msk | CCAP_PKTSL_PKTSHML_Msk))
270                   | ((u32NumeratorL << 8)| u32DenominatorL);
271     CCAP->PKTSM = (CCAP->PKTSM & ~(CCAP_PKTSM_PKTSHNH_Msk | CCAP_PKTSM_PKTSHMH_Msk))
272                   | ((u32NumeratorH << 8) | u32DenominatorH);
273 }
274 
275 /**
276  * @brief     Set Packet Frame Output Pixel Stride Width.
277  *
278  * @param[in] u32Stride : set PKTSTRIDE register, It should be 0x0 ~ 0x3FFF
279  *
280  * @return    None
281  *
282  * @details   Set Packet Frame Output Pixel Stride Width
283  */
CCAP_SetPacketStride(uint32_t u32Stride)284 void CCAP_SetPacketStride(uint32_t u32Stride )
285 {
286     CCAP->STRIDE = (CCAP->STRIDE & ~CCAP_STRIDE_PKTSTRIDE_Msk) | u32Stride;
287 }
288 
289 
290 /*@}*/ /* end of group CCAP_EXPORTED_FUNCTIONS */
291 
292 /*@}*/ /* end of group CCAP_Driver */
293 
294 /*@}*/ /* end of group Standard_Driver */
295 
296 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
297