gd32f3x0_cec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*!
  2. \file gd32f3x0_cec.c
  3. \brief CEC driver
  4. \version 2017-06-06, V1.0.0, firmware for GD32F3x0
  5. \version 2019-06-01, V2.0.0, firmware for GD32F3x0
  6. */
  7. /*
  8. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #ifdef GD32F350
  31. #include "gd32f3x0_cec.h"
  32. /*!
  33. \brief reset HDMI-CEC controller
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void cec_deinit(void)
  39. {
  40. rcu_periph_reset_enable(RCU_CECRST);
  41. rcu_periph_reset_disable(RCU_CECRST);
  42. }
  43. /*!
  44. \brief configure signal free time,the signal free time counter start option,own address
  45. \param[in] sftmopt: signal free time counter start option
  46. only one parameter can be selected which is shown as below:
  47. \arg CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
  48. \arg CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
  49. \param[in] sft: signal free time
  50. only one parameter can be selected which is shown as below:
  51. \arg CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
  52. \arg CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
  53. \arg CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
  54. \arg CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
  55. \arg CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
  56. \arg CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
  57. \arg CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
  58. \arg CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
  59. \param[in] address: own address
  60. only one parameter can be selected which is shown as below:
  61. \arg CEC_OWN_ADDRESS_CLEAR: own address is cleared
  62. \arg CEC_OWN_ADDRESSx(x=0..14): own address is x
  63. \param[out] none
  64. \retval none
  65. */
  66. void cec_init(uint32_t sftmopt, uint32_t sft, uint32_t address)
  67. {
  68. uint32_t cfg;
  69. cfg = CEC_CFG;
  70. /* clear SFTMOPT bit,SFT[2:0] */
  71. cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
  72. /* assign SFTMOPT bit,SFT[2:0] */
  73. cfg |= (sftmopt | sft);
  74. CEC_CFG = cfg;
  75. if(CEC_OWN_ADDRESS_CLEAR == address){
  76. CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
  77. }else{
  78. CEC_CFG |= address;
  79. }
  80. }
  81. /*!
  82. \brief configure generate Error-bit when detected some abnormal situation or not,
  83. whether stop receive message when detected bit rising error
  84. \param[in] broadcast:
  85. only one parameter can be selected which is shown as below:
  86. \arg CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
  87. \arg CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
  88. \param[in] singlecast_lbpe:
  89. only one parameter can be selected which is shown as below:
  90. \arg CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
  91. \arg CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
  92. \param[in] singlecast_bre:
  93. only one parameter can be selected which is shown as below:
  94. \arg CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
  95. \arg CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
  96. \param[in] rxbrestp:
  97. only one parameter can be selected which is shown as below:
  98. \arg CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
  99. \arg CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
  100. \param[out] none
  101. \retval none
  102. */
  103. void cec_error_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre, uint32_t rxbrestp)
  104. {
  105. uint32_t cfg;
  106. cfg = CEC_CFG;
  107. /* clear BCNG bit, BPLEG bit, BREG bit */
  108. cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
  109. /* assign BCNG bit, BPLEG bit, BREG bit */
  110. cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
  111. CEC_CFG = cfg;
  112. if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
  113. CEC_CFG |= CEC_CFG_BRES;
  114. }else{
  115. CEC_CFG &= ~CEC_CFG_BRES;
  116. }
  117. }
  118. /*!
  119. \brief enable HDMI-CEC controller
  120. \param[in] none
  121. \param[out] none
  122. \retval none
  123. */
  124. void cec_enable(void)
  125. {
  126. CEC_CTL |= CEC_CTL_CECEN;
  127. }
  128. /*!
  129. \brief disable HDMI-CEC controller
  130. \param[in] none
  131. \param[out] none
  132. \retval none
  133. */
  134. void cec_disable(void)
  135. {
  136. CEC_CTL &= ~CEC_CTL_CECEN;
  137. }
  138. /*!
  139. \brief start CEC message transmission
  140. \param[in] none
  141. \param[out] none
  142. \retval none
  143. */
  144. void cec_transmission_start(void)
  145. {
  146. CEC_CTL |= CEC_CTL_STAOM;
  147. }
  148. /*!
  149. \brief end CEC message transmission
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. void cec_transmission_end(void)
  155. {
  156. CEC_CTL |= CEC_CTL_ENDOM;
  157. }
  158. /*!
  159. \brief enable CEC listen mode.
  160. \param[in] none
  161. \param[out] none
  162. \retval none
  163. */
  164. void cec_listen_mode_enable(void)
  165. {
  166. CEC_CFG |= CEC_CFG_LMEN;
  167. }
  168. /*!
  169. \brief disable CEC listen mode.
  170. \param[in] none
  171. \param[out] none
  172. \retval none
  173. */
  174. void cec_listen_mode_disable(void)
  175. {
  176. CEC_CFG &= ~CEC_CFG_LMEN;
  177. }
  178. /*!
  179. \brief configure and clear own address.the controller can be configured to multiple own address
  180. \param[in] address: own address
  181. one or more parameters can be selected which are shown as below:
  182. \arg CEC_OWN_ADDRESS_CLEAR: own address is cleared
  183. \arg CEC_OWN_ADDRESSx(x=0..14): own address is x
  184. \param[out] none
  185. \retval none
  186. */
  187. void cec_own_address_config(uint32_t address)
  188. {
  189. if(CEC_OWN_ADDRESS_CLEAR == address){
  190. CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
  191. } else {
  192. CEC_CFG |= address;
  193. }
  194. }
  195. /*!
  196. \brief configure signal free time and the signal free time counter start option
  197. \param[in] sftmopt: signal free time counter start option
  198. only one parameter can be selected which is shown as below:
  199. \arg CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
  200. \arg CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
  201. \param[in] sft: signal free time
  202. only one parameter can be selected which is shown as below:
  203. \arg CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
  204. \arg CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
  205. \arg CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
  206. \arg CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
  207. \arg CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
  208. \arg CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
  209. \arg CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
  210. \arg CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
  211. \param[out] none
  212. \retval none
  213. */
  214. void cec_sft_config(uint32_t sftmopt, uint32_t sft)
  215. {
  216. uint32_t cfg;
  217. cfg = CEC_CFG;
  218. /* clear SFTMOPT bit,SFT[2:0] */
  219. cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
  220. /* assign SFTMOPT bit,SFT[2:0] */
  221. cfg |= (sftmopt | sft);
  222. CEC_CFG = cfg;
  223. }
  224. /*!
  225. \brief configure generate Error-bit when detected some abnormal situation or not
  226. \param[in] broadcast:
  227. only one parameter can be selected which is shown as below:
  228. \arg CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
  229. \arg CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
  230. \param[in] singlecast_lbpe:
  231. only one parameter can be selected which is shown as below:
  232. \arg CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
  233. \arg CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
  234. \param[in] singlecast_bre:
  235. only one parameter can be selected which is shown as below:
  236. \arg CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
  237. \arg CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
  238. \param[out] none
  239. \retval none
  240. */
  241. void cec_generate_errorbit_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre)
  242. {
  243. uint32_t cfg;
  244. cfg = CEC_CFG;
  245. /* clear BCNG bit, RLBPEGEN bit, RBREGEN bit */
  246. cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
  247. /* assign BCNG bit, RLBPEGEN bit, RBREGEN bit */
  248. cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
  249. CEC_CFG = cfg;
  250. }
  251. /*!
  252. \brief whether stop receive message when detected bit rising error
  253. \param[in] rxbrestp:
  254. only one parameter can be selected which is shown as below:
  255. \arg CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
  256. \arg CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
  257. \param[out] none
  258. \retval none
  259. */
  260. void cec_stop_receive_bre_config(uint32_t rxbrestp)
  261. {
  262. if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
  263. CEC_CFG |= CEC_CFG_BRES;
  264. } else {
  265. CEC_CFG &= ~CEC_CFG_BRES;
  266. }
  267. }
  268. /*!
  269. \brief enable reception bit timing tolerance
  270. \param[in] none
  271. \param[out] none
  272. \retval none
  273. */
  274. void cec_reception_tolerance_enable(void)
  275. {
  276. CEC_CFG |= CEC_CFG_RTOL;
  277. }
  278. /*!
  279. \brief disable reception bit timing tolerance
  280. \param[in] none
  281. \param[out] none
  282. \retval none
  283. */
  284. void cec_reception_tolerance_disable(void)
  285. {
  286. CEC_CFG &= ~CEC_CFG_RTOL;
  287. }
  288. /*!
  289. \brief send a data by the CEC peripheral
  290. \param[in] data: the data to transmit
  291. \param[out] none
  292. \retval none
  293. */
  294. void cec_data_send(uint8_t data)
  295. {
  296. CEC_TDATA = (uint32_t)data;
  297. }
  298. /*!
  299. \brief receive a data by the CEC peripheral
  300. \param[in] data: the data to receive
  301. \param[out] none
  302. \retval none
  303. */
  304. uint8_t cec_data_receive(void)
  305. {
  306. return (uint8_t)CEC_RDATA;
  307. }
  308. /*!
  309. \brief enable interrupt
  310. \param[in] flag: specify which flag
  311. one or more parameters can be selected which are shown as below:
  312. \arg CEC_INT_BR: enable Rx-byte data received interrupt
  313. \arg CEC_INT_REND: enable end of reception interrupt
  314. \arg CEC_INT_RO: enable RX overrun interrupt
  315. \arg CEC_INT_BRE: enable bit rising error interrupt
  316. \arg CEC_INT_BPSE: enable short bit period error interrupt
  317. \arg CEC_INT_BPLE: enable long bit period error interrupt
  318. \arg CEC_INT_RAE: enable Rx ACK error interrupt
  319. \arg CEC_INT_ARBF: enable arbitration lost interrupt
  320. \arg CEC_INT_TBR: enable Tx-byte data request interrupt
  321. \arg CEC_INT_TEND: enable transmission successfully end interrupt
  322. \arg CEC_INT_TU: enable Tx data buffer underrun interrupt
  323. \arg CEC_INT_TERR: enable Tx-error interrupt
  324. \arg CEC_INT_TAERR: enable Tx ACK error interrupt
  325. \param[out] none
  326. \retval none
  327. */
  328. void cec_interrupt_enable(uint32_t flag)
  329. {
  330. CEC_INTEN |= flag;
  331. }
  332. /*!
  333. \brief disable interrupt
  334. \param[in] flag: specify which flag
  335. one or more parameters can be selected which are shown as below:
  336. \arg CEC_INT_BR: disable Rx-byte data received interrupt
  337. \arg CEC_INT_REND: disable end of reception interrupt
  338. \arg CEC_INT_RO: disable RX overrun interrupt
  339. \arg CEC_INT_BRE: disable bit rising error interrupt
  340. \arg CEC_INT_BPSE: disable short bit period error interrupt
  341. \arg CEC_INT_BPLE: disable long bit period error interrupt
  342. \arg CEC_INT_RAE: disable Rx ACK error interrupt
  343. \arg CEC_INT_ARBF: disable arbitration lost interrupt
  344. \arg CEC_INT_TBR: disable Tx-byte data request interrupt
  345. \arg CEC_INT_TEND: disable transmission successfully end interrupt
  346. \arg CEC_INT_TU: disable Tx data buffer underrun interrupt
  347. \arg CEC_INT_TERR: disable Tx-error interrupt
  348. \arg CEC_INT_TAERR: disable Tx ACK error interrupt
  349. \param[out] none
  350. \retval none
  351. */
  352. void cec_interrupt_disable(uint32_t flag)
  353. {
  354. CEC_INTEN &= ~flag;
  355. }
  356. /*!
  357. \brief get CEC status
  358. \param[in] flag: specify which flag
  359. one or more parameters can be selected which are shown as below:
  360. \arg CEC_FLAG_BR: Rx-byte data received
  361. \arg CEC_FLAG_REND: end of reception
  362. \arg CEC_FLAG_RO: RX overrun
  363. \arg CEC_FLAG_BRE: bit rising error
  364. \arg CEC_FLAG_BPSE: short bit period error
  365. \arg CEC_FLAG_BPLE: long bit period error
  366. \arg CEC_FLAG_RAE: Rx ACK error
  367. \arg CEC_FLAG_ARBF: arbitration lost
  368. \arg CEC_FLAG_TBR: Tx-byte data request
  369. \arg CEC_FLAG_TEND: transmission successfully end
  370. \arg CEC_FLAG_TU: Tx data buffer underrun
  371. \arg CEC_FLAG_TERR: Tx-error
  372. \arg CEC_FLAG_TAERR Tx ACK error flag
  373. \param[out] none
  374. \retval FlagStatus: SET or RESET
  375. */
  376. FlagStatus cec_flag_get(uint32_t flag)
  377. {
  378. if(CEC_INTF & flag){
  379. return SET;
  380. }else{
  381. return RESET;
  382. }
  383. }
  384. /*!
  385. \brief clear CEC status
  386. \param[in] flag: specify which flag
  387. one or more parameters can be selected which are shown as below:
  388. \arg CEC_FLAG_BR: Rx-byte data received
  389. \arg CEC_FLAG_REND: end of reception
  390. \arg CEC_FLAG_RO: RX overrun
  391. \arg CEC_FLAG_BRE: bit rising error
  392. \arg CEC_FLAG_BPSE: short bit period error
  393. \arg CEC_FLAG_BPLE: long bit period error
  394. \arg CEC_FLAG_RAE: Rx ACK error
  395. \arg CEC_FLAG_ARBF: arbitration lost
  396. \arg CEC_FLAG_TBR: Tx-byte data request
  397. \arg CEC_FLAG_TEND: transmission successfully end
  398. \arg CEC_FLAG_TU: Tx data buffer underrun
  399. \arg CEC_FLAG_TERR: Tx-error
  400. \arg CEC_FLAG_TAERR: Tx ACK error flag
  401. \param[out] none
  402. \retval FlagStatus: SET or RESET
  403. */
  404. void cec_flag_clear(uint32_t flag)
  405. {
  406. CEC_INTF |= flag;
  407. }
  408. /*!
  409. \brief get CEC int flag and status
  410. \param[in] flag: specify which flag
  411. one or more parameters can be selected which are shown as below:
  412. \arg CEC_INT_FLAG_BR: Rx-byte data received
  413. \arg CEC_INT_FLAG_REND: end of reception
  414. \arg CEC_INT_FLAG_RO: RX overrun
  415. \arg CEC_INT_FLAG_BRE: bit rising error
  416. \arg CEC_INT_FLAG_BPSE: short bit period error
  417. \arg CEC_INT_FLAG_BPLE: long bit period error
  418. \arg CEC_INT_FLAG_RAE: Rx ACK error
  419. \arg CEC_INT_FLAG_ARBF: arbitration lost
  420. \arg CEC_INT_FLAG_TBR: Tx-byte data request
  421. \arg CEC_INT_FLAG_TEND: transmission successfully end
  422. \arg CEC_INT_FLAG_TU: Tx data buffer underrun
  423. \arg CEC_INT_FLAG_TERR: Tx-error
  424. \arg CEC_INT_FLAG_TAERR: Tx ACK error flag
  425. \param[out] none
  426. \retval FlagStatus: SET or RESET
  427. */
  428. FlagStatus cec_interrupt_flag_get(uint32_t flag)
  429. {
  430. uint32_t interrupt_enable = 0U,interrupt_flag = 0U;
  431. interrupt_flag = (CEC_INTF & flag);
  432. interrupt_enable = (CEC_INTEN & flag);
  433. if(interrupt_flag && interrupt_enable){
  434. return SET;
  435. }else{
  436. return RESET;
  437. }
  438. }
  439. /*!
  440. \brief clear CEC int flag and status
  441. \param[in] flag: specify which flag
  442. one or more parameters can be selected which are shown as below:
  443. \arg CEC_INT_FLAG_BR: Rx-byte data received
  444. \arg CEC_INT_FLAG_REND: end of reception
  445. \arg CEC_INT_FLAG_RO: RX overrun
  446. \arg CEC_INT_FLAG_BRE: bit rising error
  447. \arg CEC_INT_FLAG_BPSE: short bit period error
  448. \arg CEC_INT_FLAG_BPLE: long bit period error
  449. \arg CEC_INT_FLAG_RAE: Rx ACK error
  450. \arg CEC_INT_FLAG_ARBF: arbitration lost
  451. \arg CEC_INT_FLAG_TBR: Tx-byte data request
  452. \arg CEC_INT_FLAG_TEND: transmission successfully end
  453. \arg CEC_INT_FLAG_TU: Tx data buffer underrun
  454. \arg CEC_INT_FLAG_TERR: Tx-error
  455. \arg CEC_INT_FLAG_TAERR: Tx ACK error flag
  456. \param[out] none
  457. \retval none
  458. */
  459. void cec_interrupt_flag_clear(uint32_t flag)
  460. {
  461. CEC_INTF = flag;
  462. }
  463. #endif