nu_cap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**************************************************************************//**
  2. * @file cap.c
  3. * @brief CAP driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "nuc980.h"
  9. #include "nu_cap.h"
  10. /** @addtogroup Standard_Driver Standard Driver
  11. @{
  12. */
  13. /** @addtogroup CAP_Driver CAP Driver
  14. @{
  15. */
  16. /** @addtogroup CAP_EXPORTED_FUNCTIONS CAP Exported Functions
  17. @{
  18. */
  19. /**
  20. * @brief Open engine clock and sensor clock
  21. *
  22. * @param[in] VCAP The pointer of the specified CAP module
  23. *
  24. * @param[in] u32InFormat The bits corresponding VSP, HSP, PCLK, INFMT, SNRTYPE, OUTFMT, PDORD and PNFMT configurations.
  25. * - VSP should be ether \ref CAP_PAR_VSP_LOW or \ref CAP_PAR_VSP_HIGH
  26. * - HSP should be ether \ref CAP_PAR_HSP_LOW or \ref CAP_PAR_HSP_HIGH
  27. * - PCLK should be ether \ref CAP_PAR_PCLKP_LOW or \ref CAP_PAR_PCLKP_HIGH
  28. * - INFMT should be ether \ref CAP_PAR_INFMT_YUV422 or \ref CAP_PAR_INFMT_RGB565
  29. * - SNRTYPE should be ether \ref CAP_PAR_SENTYPE_CCIR601 or \ref CAP_PAR_SENTYPE_CCIR656
  30. * - OUTFMT should be one of the following setting
  31. * - \ref CAP_PAR_OUTFMT_YUV422
  32. * - \ref CAP_PAR_OUTFMT_ONLY_Y
  33. * - \ref CAP_PAR_OUTFMT_RGB555
  34. * - \ref CAP_PAR_OUTFMT_RGB565
  35. * - PDORD should be one of the following setting
  36. * - \ref CAP_PAR_INDATORD_YUYV
  37. * - \ref CAP_PAR_INDATORD_YVYU
  38. * - \ref CAP_PAR_INDATORD_UYVY
  39. * - \ref CAP_PAR_INDATORD_VYUY
  40. * - \ref CAP_PAR_INDATORD_RGGB
  41. * - \ref CAP_PAR_INDATORD_BGGR
  42. * - \ref CAP_PAR_INDATORD_GBRG
  43. * - \ref CAP_PAR_INDATORD_GRBG
  44. * - PNFMT should be one of the following setting
  45. * - \ref CAP_PAR_PLNFMT_YUV422
  46. * - \ref CAP_PAR_PLNFMT_YUV420
  47. *
  48. * @param[in] u32OutFormet Capture output format, should be one of following setting
  49. * - \ref CAP_CTL_PKTEN
  50. * - \ref CAP_CTL_PLNEN
  51. *
  52. * @return None
  53. *
  54. * @details Initialize the Image Capture Interface. Register a call back for driver internal using
  55. */
  56. void CAP_Open(CAP_T *VCAP, uint32_t u32InFormat, uint32_t u32OutFormet)
  57. {
  58. VCAP->PAR = (VCAP->PAR & ~0x000007BF) | u32InFormat;
  59. VCAP->CTL = (VCAP->CTL & ~0x00000060) | u32OutFormet;
  60. }
  61. /**
  62. * @brief Set Cropping Window Starting Address and Size
  63. *
  64. * @param[in] VCAP: The pointer of the specified CAP module
  65. *
  66. * @param[in] u32VStart: Cropping Window Vertical Starting Address. It should be 0 ~ 0x7FF.
  67. *
  68. * @param[in] u32HStart: Cropping Window Horizontal Starting Address. It should be 0 ~ 0x7FF.
  69. *
  70. * @param[in] u32Height: Cropping Window Height . It should be 0 ~ 0x7FF.
  71. *
  72. * @param[in] u32Width: Cropping Window Width. It should be 0 ~ 0x7FF.
  73. *
  74. * @return None
  75. *
  76. * @details Set Cropping Window Starting Address Register
  77. */
  78. void CAP_SetCroppingWindow(CAP_T *VCAP, uint32_t u32VStart, uint32_t u32HStart, uint32_t u32Height, uint32_t u32Width)
  79. {
  80. VCAP->CWSP = (VCAP->CWSP & ~(CAP_CWSP_CWSADDRV_Msk | CAP_CWSP_CWSADDRH_Msk))
  81. | (((u32VStart << 16) | u32HStart));
  82. VCAP->CWS = (VCAP->CWS & ~(CAP_CWS_CWH_Msk | CAP_CWS_CWW_Msk))
  83. | ((u32Height << 16) | u32Width);
  84. }
  85. /**
  86. * @brief Set System Memory Packet Base Address0 Register
  87. *
  88. * @param[in] VCAP: The pointer of the specified CAP module
  89. *
  90. * @param[in] u32Address : set PKTBA0 register, It should be 0x0 ~ 0xFFFFFFFF
  91. *
  92. * @return None
  93. *
  94. * @details Set System Memory Packet Base Address Register
  95. */
  96. void CAP_SetPacketBuf(CAP_T *VCAP, uint32_t u32Address)
  97. {
  98. VCAP->PKTBA0 = u32Address;
  99. VCAP->CTL |= CAP_CTL_UPDATE_Msk;
  100. }
  101. /**
  102. * @brief Set System Memory Planar Y, U and V Base Address Registers.
  103. *
  104. * @param[in] VCAP: The pointer of the specified CAP module
  105. *
  106. * @param[in] u32YAddr : set YBA register, It should be 0x0 ~ 0xFFFFFFFF
  107. *
  108. * @param[in] u32UAddr : set UBA register, It should be 0x0 ~ 0xFFFFFFFF
  109. *
  110. * @param[in] u32VAddr : set VBA register, It should be 0x0 ~ 0xFFFFFFFF
  111. *
  112. * @return None
  113. *
  114. * @details Set System Memory Planar Y,U and V Base Address Registers
  115. */
  116. void CAP_SetPlanarBuf(CAP_T *VCAP, uint32_t u32YAddr, uint32_t u32UAddr, uint32_t u32VAddr)
  117. {
  118. VCAP->YBA = u32YAddr;
  119. VCAP->UBA = u32UAddr;
  120. VCAP->VBA = u32VAddr;
  121. VCAP->CTL |= CAP_CTL_UPDATE_Msk;
  122. }
  123. /**
  124. * @brief Close Image Capture Interface
  125. *
  126. * @param[in] VCAP: The pointer of the specified CAP module
  127. *
  128. * @return None
  129. */
  130. void CAP_Close(CAP_T *VCAP)
  131. {
  132. VCAP->CTL &= ~CAP_CTL_CAPEN;
  133. }
  134. /**
  135. * @brief Set CAP Interrupt
  136. *
  137. * @param[in] VCAP: The pointer of the specified CAP module
  138. *
  139. * @param[in] u32IntMask Interrupt settings. It could be
  140. * - \ref CAP_INT_VIEN_Msk
  141. * - \ref CAP_INT_MEIEN_Msk
  142. * - \ref CAP_INT_ADDRMIEN_Msk
  143. * - \ref CAP_INT_MDIEN_Msk
  144. * @return None
  145. *
  146. * @details Set Video Frame End Interrupt Enable,
  147. * System Memory Error Interrupt Enable,
  148. * Address Match Interrupt Enable,
  149. * Motion Detection Output Finish Interrupt Enable.
  150. */
  151. void CAP_EnableInt(CAP_T *VCAP, uint32_t u32IntMask)
  152. {
  153. VCAP->INT = (VCAP->INT & ~(CAP_INT_VIEN_Msk | CAP_INT_MEIEN_Msk | CAP_INT_ADDRMIEN_Msk | CAP_INT_MDIEN_Msk))
  154. | u32IntMask;
  155. }
  156. /**
  157. * @brief Disable CAP Interrupt
  158. *
  159. * @param[in] VCAP: The pointer of the specified CAP module
  160. *
  161. * @param[in] u32IntMask Interrupt settings. It could be
  162. * - \ref CAP_INT_VINTF_Msk
  163. * - \ref CAP_INT_MEINTF_Msk
  164. * - \ref CAP_INT_ADDRMINTF_Msk
  165. * - \ref CAP_INT_MDINTF_Msk
  166. * @return None
  167. *
  168. * @details Disable Video Frame End Interrupt ,
  169. * System Memory Error Interrupt ,
  170. * Address Match Interrupt and
  171. * Motion Detection Output Finish Interrupt .
  172. */
  173. void CAP_DisableInt(CAP_T *VCAP, uint32_t u32IntMask)
  174. {
  175. VCAP->INT = (VCAP->INT & ~(u32IntMask)) ;
  176. }
  177. /**
  178. * @brief Start Image Capture Interface
  179. *
  180. * @param[in] VCAP: The pointer of the specified CAP module
  181. *
  182. * @return None
  183. */
  184. void CAP_Start(CAP_T *VCAP)
  185. {
  186. VCAP->CTL |= CAP_CTL_CAPEN;
  187. }
  188. /**
  189. * @brief Stop Image Capture Interface
  190. *
  191. * @param[in] VCAP: The pointer of the specified CAP module
  192. *
  193. * @param[in] u32FrameComplete :
  194. * TRUE: Capture module automatically disable the CAP module after a frame had been captured
  195. * FALSE: Stop Capture module now
  196. * @return None
  197. *
  198. * @details if u32FrameComplete is set to TRUE then get a new frame and disable CAP module
  199. */
  200. void CAP_Stop(CAP_T *VCAP, uint32_t u32FrameComplete)
  201. {
  202. if (u32FrameComplete == FALSE)
  203. VCAP->CTL &= ~CAP_CTL_CAPEN;
  204. else
  205. {
  206. VCAP->CTL |= CAP_CTL_SHUTTER_Msk;
  207. while (CAP_IS_STOPPED(VCAP));
  208. }
  209. }
  210. /**
  211. * @brief Set Packet Scaling Vertical and Horizontal Factor Register
  212. *
  213. * @param[in] VCAP: The pointer of the specified CAP module
  214. *
  215. * @param[in] u32VNumerator: Packet Scaling Vertical Factor N. It should be 0 ~ FFFF.
  216. *
  217. * @param[in] u32VDenominator: Packet Scaling Vertical Factor M. It should be 0 ~ FFFF.
  218. *
  219. * @param[in] u32HNumerator: Packet Scaling Vertical Factor N. It should be 0 ~ FFFF.
  220. *
  221. * @param[in] u32HDenominator: Packet Scaling Vertical Factor M. It should be 0 ~ FFFF.
  222. *
  223. * @return None
  224. *
  225. */
  226. void CAP_SetPacketScaling(CAP_T *VCAP, uint32_t u32VNumerator, uint32_t u32VDenominator, uint32_t u32HNumerator, uint32_t u32HDenominator)
  227. {
  228. uint32_t u32NumeratorL, u32NumeratorH;
  229. uint32_t u32DenominatorL, u32DenominatorH;
  230. u32NumeratorL = u32VNumerator & 0xFF;
  231. u32NumeratorH = u32VNumerator >> 8;
  232. u32DenominatorL = u32VDenominator & 0xFF;
  233. u32DenominatorH = u32VDenominator >> 8;
  234. VCAP->PKTSL = (VCAP->PKTSL & ~(CAP_PKTSL_PKTSVNL_Msk | CAP_PKTSL_PKTSVML_Msk))
  235. | ((u32NumeratorL << 24) | (u32DenominatorL << 16));
  236. VCAP->PKTSM = (VCAP->PKTSM & ~(CAP_PKTSM_PKTSVNH_Msk | CAP_PKTSM_PKTSVMH_Msk))
  237. | ((u32NumeratorH << 24) | (u32DenominatorH << 16));
  238. u32NumeratorL = u32HNumerator & 0xFF;
  239. u32NumeratorH = u32HNumerator >> 8;
  240. u32DenominatorL = u32HDenominator & 0xFF;
  241. u32DenominatorH = u32HDenominator >> 8;
  242. VCAP->PKTSL = (VCAP->PKTSL & ~(CAP_PKTSL_PKTSHNL_Msk | CAP_PKTSL_PKTSHML_Msk))
  243. | ((u32NumeratorL << 8) | u32DenominatorL);
  244. VCAP->PKTSM = (VCAP->PKTSM & ~(CAP_PKTSM_PKTSHNH_Msk | CAP_PKTSM_PKTSHMH_Msk))
  245. | ((u32NumeratorH << 8) | u32DenominatorH);
  246. }
  247. /**
  248. * @brief Set Planar Scaling Vertical and Horizontal Factor Register
  249. *
  250. * @param[in] VCAP: The pointer of the specified CAP module
  251. *
  252. * @param[in] u32VNumerator: Planar Scaling Vertical Factor N. It should be 0 ~ FFFF.
  253. *
  254. * @param[in] u32VDenominator: Planar Scaling Vertical Factor M. It should be 0 ~ FFFF.
  255. *
  256. * @param[in] u32HNumerator: Planar Scaling Vertical Factor N. It should be 0 ~ FFFF.
  257. *
  258. * @param[in] u32HDenominator: Planar Scaling Vertical Factor M. It should be 0 ~ FFFF.
  259. *
  260. * @return None
  261. *
  262. */
  263. void CAP_SetPlanarScaling(CAP_T *VCAP, uint32_t u32VNumerator, uint32_t u32VDenominator, uint32_t u32HNumerator, uint32_t u32HDenominator)
  264. {
  265. uint32_t u32NumeratorL, u32NumeratorH;
  266. uint32_t u32DenominatorL, u32DenominatorH;
  267. u32NumeratorL = u32VNumerator & 0xFF;
  268. u32NumeratorH = u32VNumerator >> 8;
  269. u32DenominatorL = u32VDenominator & 0xFF;
  270. u32DenominatorH = u32VDenominator >> 8;
  271. VCAP->PLNSL = (VCAP->PLNSL & ~(CAP_PLNSL_PLNSVNL_Msk | CAP_PLNSL_PLNSVML_Msk))
  272. | ((u32NumeratorL << 24) | (u32DenominatorL << 16));
  273. VCAP->PLNSM = (VCAP->PLNSM & ~(CAP_PLNSM_PLNSVNH_Msk | CAP_PLNSM_PLNSVMH_Msk))
  274. | ((u32NumeratorH << 24) | (u32DenominatorH << 16));
  275. u32NumeratorL = u32HNumerator & 0xFF;
  276. u32NumeratorH = u32HNumerator >> 8;
  277. u32DenominatorL = u32HDenominator & 0xFF;
  278. u32DenominatorH = u32HDenominator >> 8;
  279. VCAP->PLNSL = (VCAP->PLNSL & ~(CAP_PLNSL_PLNSHNL_Msk | CAP_PLNSL_PLNSHML_Msk))
  280. | ((u32NumeratorL << 8) | u32DenominatorL);
  281. VCAP->PLNSM = (VCAP->PLNSM & ~(CAP_PLNSM_PLNSHNH_Msk | CAP_PLNSM_PLNSHMH_Msk))
  282. | ((u32NumeratorH << 8) | u32DenominatorH);
  283. }
  284. /**
  285. * @brief Set Packet Frame Output Pixel Stride Width.
  286. *
  287. * @param[in] VCAP: The pointer of the specified CAP module
  288. *
  289. * @param[in] u32Stride : set PKTSTRIDE register, It should be 0x0 ~ 0x3FFF
  290. *
  291. * @return None
  292. *
  293. * @details Set Packet Frame Output Pixel Stride Width
  294. */
  295. void CAP_SetPacketStride(CAP_T *VCAP, uint32_t u32Stride)
  296. {
  297. VCAP->STRIDE = (VCAP->STRIDE & ~CAP_STRIDE_PKTSTRIDE_Msk) | u32Stride;
  298. }
  299. /**
  300. * @brief Set Planar Frame Output Pixel Stride Width.
  301. *
  302. * @param[in] VCAP: The pointer of the specified CAP module
  303. *
  304. * @param[in] u32Stride : set PLNSTRIDE register, It should be 0x0 ~ 0x3FFF
  305. *
  306. * @return None
  307. *
  308. * @details Set Planar Frame Output Pixel Stride Width
  309. */
  310. void CAP_SetPlanarStride(CAP_T *VCAP, uint32_t u32Stride)
  311. {
  312. VCAP->STRIDE = (VCAP->STRIDE & ~CAP_STRIDE_PLNSTRIDE_Msk) | u32Stride << CAP_STRIDE_PLNSTRIDE_Pos;
  313. }
  314. /**
  315. * @brief Enable Motion Detection Function
  316. *
  317. * @param[in] VCAP: The pointer of the specified CAP module
  318. *
  319. * @param[in] u32Freq: Motion Detection Detect Frequency. It should be 0x0 ~ 0x3.
  320. *
  321. * @param[in] u32BlockSize: Motion Detection Block Size
  322. * FALSE : 16x16
  323. * TRUE : 8x8
  324. *
  325. * @param[in] u32Format: Motion Detection Save Mode
  326. * FALSE : 1 bit DIFF + 7 Y Differential
  327. * TRUE : 1 bit DIFF only
  328. *
  329. * @param[in] u32Threshold: Motion Detection Detect Threshold. It should be 0x0 ~ 0x1F.
  330. *
  331. * @param[in] u32YDetAddr : Motion Detection Detect Temp Y Output Address
  332. *
  333. * @param[in] u32DetAddr: Motion Detection Detect Address
  334. *
  335. * @return None
  336. *
  337. * @details Set Planar Frame Output Pixel Stride Width
  338. */
  339. void CAP_EnableMotionDet(CAP_T *VCAP, uint32_t u32Freq, uint32_t u32BlockSize, uint32_t u32Format, uint32_t u32Threshold, uint32_t u32YDetAddr, uint32_t u32DetAddr)
  340. {
  341. VCAP->MD = (VCAP->MD & ~(CAP_MD_MDSM_Msk | CAP_MD_MDBS_Msk | CAP_MD_MDEN_Msk)) |
  342. ((CAP_MD_MDEN_Msk | (u32BlockSize ? CAP_MD_MDBS_Msk : 0)) |
  343. (u32Format ? CAP_MD_MDSM_Msk : 0));
  344. VCAP->MD = (VCAP->MD & ~CAP_MD_MDDF_Msk) | (u32Freq << CAP_MD_MDDF_Pos);
  345. VCAP->MD = (VCAP->MD & ~CAP_MD_MDTHR_Msk) | (u32Threshold << CAP_MD_MDTHR_Pos);
  346. VCAP->MDYADDR = u32YDetAddr;
  347. VCAP->MDADDR = u32DetAddr;
  348. }
  349. /**
  350. * @brief Enable Motion Detection Function
  351. *
  352. * @param[in] VCAP: The pointer of the specified CAP module
  353. *
  354. * @return None
  355. *
  356. * @details Set Planar Frame Output Pixel Stride Width
  357. */
  358. void CAP_DisableMotionDet(CAP_T *VCAP)
  359. {
  360. VCAP->MD &= ~CAP_MD_MDEN_Msk;
  361. }
  362. /*@}*/ /* end of group CAP_EXPORTED_FUNCTIONS */
  363. /*@}*/ /* end of group CAP_Driver */
  364. /*@}*/ /* end of group Standard_Driver */