gd32f4xx_dci.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*!
  2. \file gd32f4xx_dci.c
  3. \brief DCI driver
  4. \version 2016-08-15, V1.0.0, firmware for GD32F4xx
  5. \version 2018-12-12, V2.0.0, firmware for GD32F4xx
  6. \version 2020-09-30, V2.1.0, firmware for GD32F4xx
  7. */
  8. /*
  9. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice, this
  13. list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. 3. Neither the name of the copyright holder nor the names of its contributors
  18. may be used to endorse or promote products derived from this software without
  19. specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. OF SUCH DAMAGE.
  30. */
  31. #include "gd32f4xx_dci.h"
  32. /*!
  33. \brief DCI deinit
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void dci_deinit(void)
  39. {
  40. rcu_periph_reset_enable(RCU_DCIRST);
  41. rcu_periph_reset_disable(RCU_DCIRST);
  42. }
  43. /*!
  44. \brief initialize DCI registers
  45. \param[in] dci_struct: DCI parameter initialization structure
  46. members of the structure and the member values are shown as below:
  47. capture_mode : DCI_CAPTURE_MODE_CONTINUOUS, DCI_CAPTURE_MODE_SNAPSHOT
  48. colck_polarity : DCI_CK_POLARITY_FALLING, DCI_CK_POLARITY_RISING
  49. hsync_polarity : DCI_HSYNC_POLARITY_LOW, DCI_HSYNC_POLARITY_HIGH
  50. vsync_polarity : DCI_VSYNC_POLARITY_LOW, DCI_VSYNC_POLARITY_HIGH
  51. frame_rate : DCI_FRAME_RATE_ALL, DCI_FRAME_RATE_1_2, DCI_FRAME_RATE_1_4
  52. interface_format: DCI_INTERFACE_FORMAT_8BITS, DCI_INTERFACE_FORMAT_10BITS,
  53. DCI_INTERFACE_FORMAT_12BITS, DCI_INTERFACE_FORMAT_14BITS
  54. \param[out] none
  55. \retval none
  56. */
  57. void dci_init(dci_parameter_struct* dci_struct)
  58. {
  59. uint32_t reg = 0U;
  60. /* disable capture function and DCI */
  61. DCI_CTL &= ~(DCI_CTL_CAP | DCI_CTL_DCIEN);
  62. /* configure DCI parameter */
  63. reg |= dci_struct->capture_mode;
  64. reg |= dci_struct->clock_polarity;
  65. reg |= dci_struct->hsync_polarity;
  66. reg |= dci_struct->vsync_polarity;
  67. reg |= dci_struct->frame_rate;
  68. reg |= dci_struct->interface_format;
  69. DCI_CTL = reg;
  70. }
  71. /*!
  72. \brief enable DCI function
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. void dci_enable(void)
  78. {
  79. DCI_CTL |= DCI_CTL_DCIEN;
  80. }
  81. /*!
  82. \brief disable DCI function
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void dci_disable(void)
  88. {
  89. DCI_CTL &= ~DCI_CTL_DCIEN;
  90. }
  91. /*!
  92. \brief enable DCI capture
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. void dci_capture_enable(void)
  98. {
  99. DCI_CTL |= DCI_CTL_CAP;
  100. }
  101. /*!
  102. \brief disable DCI capture
  103. \param[in] none
  104. \param[out] none
  105. \retval none
  106. */
  107. void dci_capture_disable(void)
  108. {
  109. DCI_CTL &= ~DCI_CTL_CAP;
  110. }
  111. /*!
  112. \brief enable DCI jpeg mode
  113. \param[in] none
  114. \param[out] none
  115. \retval none
  116. */
  117. void dci_jpeg_enable(void)
  118. {
  119. DCI_CTL |= DCI_CTL_JM;
  120. }
  121. /*!
  122. \brief disable DCI jpeg mode
  123. \param[in] none
  124. \param[out] none
  125. \retval none
  126. */
  127. void dci_jpeg_disable(void)
  128. {
  129. DCI_CTL &= ~DCI_CTL_JM;
  130. }
  131. /*!
  132. \brief enable cropping window function
  133. \param[in] none
  134. \param[out] none
  135. \retval none
  136. */
  137. void dci_crop_window_enable(void)
  138. {
  139. DCI_CTL |= DCI_CTL_WDEN;
  140. }
  141. /*!
  142. \brief disable cropping window function
  143. \param[in] none
  144. \param[out] none
  145. \retval none
  146. */
  147. void dci_crop_window_disable(void)
  148. {
  149. DCI_CTL &= ~DCI_CTL_WDEN;
  150. }
  151. /*!
  152. \brief configure DCI cropping window
  153. \param[in] start_x: window horizontal start position
  154. \param[in] start_y: window vertical start position
  155. \param[in] size_width: window horizontal size
  156. \param[in] size_height: window vertical size
  157. \param[out] none
  158. \retval none
  159. */
  160. void dci_crop_window_config(uint16_t start_x, uint16_t start_y, uint16_t size_width, uint16_t size_height)
  161. {
  162. DCI_CWSPOS = ((uint32_t)start_x | ((uint32_t)start_y<<16));
  163. DCI_CWSZ = ((uint32_t)size_width | ((uint32_t)size_height<<16));
  164. }
  165. /*!
  166. \brief enable embedded synchronous mode
  167. \param[in] none
  168. \param[out] none
  169. \retval none
  170. */
  171. void dci_embedded_sync_enable(void)
  172. {
  173. DCI_CTL |= DCI_CTL_ESM;
  174. }
  175. /*!
  176. \brief disble embedded synchronous mode
  177. \param[in] none
  178. \param[out] none
  179. \retval none
  180. */
  181. void dci_embedded_sync_disable(void)
  182. {
  183. DCI_CTL &= ~DCI_CTL_ESM;
  184. }
  185. /*!
  186. \brief config synchronous codes in embedded synchronous mode
  187. \param[in] frame_start: frame start code in embedded synchronous mode
  188. \param[in] line_start: line start code in embedded synchronous mode
  189. \param[in] line_end: line end code in embedded synchronous mode
  190. \param[in] frame_end: frame end code in embedded synchronous mode
  191. \param[out] none
  192. \retval none
  193. */
  194. void dci_sync_codes_config(uint8_t frame_start, uint8_t line_start, uint8_t line_end, uint8_t frame_end)
  195. {
  196. DCI_SC = ((uint32_t)frame_start | ((uint32_t)line_start<<8) | ((uint32_t)line_end<<16) | ((uint32_t)frame_end<<24));
  197. }
  198. /*!
  199. \brief config synchronous codes unmask in embedded synchronous mode
  200. \param[in] frame_start: frame start code unmask bits in embedded synchronous mode
  201. \param[in] line_start: line start code unmask bits in embedded synchronous mode
  202. \param[in] line_end: line end code unmask bits in embedded synchronous mode
  203. \param[in] frame_end: frame end code unmask bits in embedded synchronous mode
  204. \param[out] none
  205. \retval none
  206. */
  207. void dci_sync_codes_unmask_config(uint8_t frame_start, uint8_t line_start, uint8_t line_end, uint8_t frame_end)
  208. {
  209. DCI_SCUMSK = ((uint32_t)frame_start | ((uint32_t)line_start<<8) | ((uint32_t)line_end<<16) | ((uint32_t)frame_end<<24));
  210. }
  211. /*!
  212. \brief read DCI data register
  213. \param[in] none
  214. \param[out] none
  215. \retval data
  216. */
  217. uint32_t dci_data_read(void)
  218. {
  219. return DCI_DATA;
  220. }
  221. /*!
  222. \brief get specified flag
  223. \param[in] flag:
  224. \arg DCI_FLAG_HS: HS line status
  225. \arg DCI_FLAG_VS: VS line status
  226. \arg DCI_FLAG_FV:FIFO valid
  227. \arg DCI_FLAG_EF: end of frame flag
  228. \arg DCI_FLAG_OVR: FIFO overrun flag
  229. \arg DCI_FLAG_ESE: embedded synchronous error flag
  230. \arg DCI_FLAG_VSYNC: vsync flag
  231. \arg DCI_FLAG_EL: end of line flag
  232. \param[out] none
  233. \retval FlagStatus: SET or RESET
  234. */
  235. FlagStatus dci_flag_get(uint32_t flag)
  236. {
  237. uint32_t stat = 0U;
  238. if(flag >> 31){
  239. /* get flag status from DCI_STAT1 register */
  240. stat = DCI_STAT1;
  241. }else{
  242. /* get flag status from DCI_STAT0 register */
  243. stat = DCI_STAT0;
  244. }
  245. if(flag & stat){
  246. return SET;
  247. }else{
  248. return RESET;
  249. }
  250. }
  251. /*!
  252. \brief enable specified DCI interrupt
  253. \param[in] interrupt:
  254. \arg DCI_INT_EF: end of frame interrupt
  255. \arg DCI_INT_OVR: FIFO overrun interrupt
  256. \arg DCI_INT_ESE: embedded synchronous error interrupt
  257. \arg DCI_INT_VSYNC: vsync interrupt
  258. \arg DCI_INT_EL: end of line interrupt
  259. \param[out] none
  260. \retval none
  261. */
  262. void dci_interrupt_enable(uint32_t interrupt)
  263. {
  264. DCI_INTEN |= interrupt;
  265. }
  266. /*!
  267. \brief disable specified DCI interrupt
  268. \param[in] interrupt:
  269. \arg DCI_INT_EF: end of frame interrupt
  270. \arg DCI_INT_OVR: FIFO overrun interrupt
  271. \arg DCI_INT_ESE: embedded synchronous error interrupt
  272. \arg DCI_INT_VSYNC: vsync interrupt
  273. \arg DCI_INT_EL: end of line interrupt
  274. \param[out] none
  275. \retval none
  276. */
  277. void dci_interrupt_disable(uint32_t interrupt)
  278. {
  279. DCI_INTEN &= ~interrupt;
  280. }
  281. /*!
  282. \brief clear specified interrupt flag
  283. \param[in] int_flag:
  284. \arg DCI_INT_EF: end of frame interrupt
  285. \arg DCI_INT_OVR: FIFO overrun interrupt
  286. \arg DCI_INT_ESE: embedded synchronous error interrupt
  287. \arg DCI_INT_VSYNC: vsync interrupt
  288. \arg DCI_INT_EL: end of line interrupt
  289. \param[out] none
  290. \retval none
  291. */
  292. void dci_interrupt_flag_clear(uint32_t int_flag)
  293. {
  294. DCI_INTC |= int_flag;
  295. }
  296. /*!
  297. \brief get specified interrupt flag
  298. \param[in] int_flag:
  299. \arg DCI_INT_FLAG_EF: end of frame interrupt flag
  300. \arg DCI_INT_FLAG_OVR: FIFO overrun interrupt flag
  301. \arg DCI_INT_FLAG_ESE: embedded synchronous error interrupt flag
  302. \arg DCI_INT_FLAG_VSYNC: vsync interrupt flag
  303. \arg DCI_INT_FLAG_EL: end of line interrupt flag
  304. \param[out] none
  305. \retval FlagStatus: SET or RESET
  306. */
  307. FlagStatus dci_interrupt_flag_get(uint32_t int_flag)
  308. {
  309. if(RESET == (DCI_INTF & int_flag)){
  310. return RESET;
  311. }else{
  312. return SET;
  313. }
  314. }