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