gd32f3x0_cec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*!
  2. \file gd32f3x0_cec.c
  3. \brief CEC driver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #ifdef GD32F350
  10. #include "gd32f3x0_cec.h"
  11. #include "gd32f3x0_rcu.h"
  12. /*!
  13. \brief reset HDMI-CEC controller
  14. \param[in] none
  15. \param[out] none
  16. \retval none
  17. */
  18. void cec_deinit(void)
  19. {
  20. rcu_periph_reset_enable(RCU_CECRST);
  21. rcu_periph_reset_disable(RCU_CECRST);
  22. }
  23. /*!
  24. \brief configure signal free time,the signal free time counter start option,own address
  25. \param[in] sftmopt: signal free time counter start option
  26. \arg CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
  27. \arg CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
  28. \param[in] sft: signal free time
  29. \arg CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
  30. \arg CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
  31. \arg CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
  32. \arg CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
  33. \arg CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
  34. \arg CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
  35. \arg CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
  36. \arg CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
  37. \param[in] address: own address
  38. \arg CEC_OWN_ADDRESS_CLEAR: own address is cleared
  39. \arg CEC_OWN_ADDRESSx(x=0..14): own address is x
  40. \param[out] none
  41. \retval none
  42. */
  43. void cec_init(uint32_t sftmopt, uint32_t sft, uint32_t address)
  44. {
  45. uint32_t cfg;
  46. cfg = CEC_CFG;
  47. /* clear SFTMOPT bit,SFT[2:0] */
  48. cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
  49. /* assign SFTMOPT bit,SFT[2:0] */
  50. cfg |= (sftmopt | sft);
  51. CEC_CFG = cfg;
  52. if(CEC_OWN_ADDRESS_CLEAR == address){
  53. CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
  54. }else{
  55. CEC_CFG |= address;
  56. }
  57. }
  58. /*!
  59. \brief configure generate Error-bit when detected some abnormal situation or not,
  60. whether stop receive message when detected bit rising error
  61. \param[in] broadcast:
  62. \arg CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
  63. \arg CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
  64. \param[in] singlecast_lbpe:
  65. \arg CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
  66. \arg CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
  67. \param[in] singlecast_bre:
  68. \arg CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
  69. \arg CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
  70. \param[in] rxbrestp:
  71. \arg CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
  72. \arg CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
  73. \param[out] none
  74. \retval none
  75. */
  76. void cec_error_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre, uint32_t rxbrestp)
  77. {
  78. uint32_t cfg;
  79. cfg = CEC_CFG;
  80. /* clear BCNG bit, BPLEG bit, BREG bit */
  81. cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
  82. /* assign BCNG bit, BPLEG bit, BREG bit */
  83. cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
  84. CEC_CFG = cfg;
  85. if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
  86. CEC_CFG |= CEC_CFG_BRES;
  87. }else{
  88. CEC_CFG &= ~CEC_CFG_BRES;
  89. }
  90. }
  91. /*!
  92. \brief enable HDMI-CEC controller
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. void cec_enable(void)
  98. {
  99. CEC_CTL |= CEC_CTL_CECEN;
  100. }
  101. /*!
  102. \brief disable HDMI-CEC controller
  103. \param[in] none
  104. \param[out] none
  105. \retval none
  106. */
  107. void cec_disable(void)
  108. {
  109. CEC_CTL &= ~CEC_CTL_CECEN;
  110. }
  111. /*!
  112. \brief start CEC message transmission
  113. \param[in] none
  114. \param[out] none
  115. \retval none
  116. */
  117. void cec_transmission_start(void)
  118. {
  119. CEC_CTL |= CEC_CTL_STAOM;
  120. }
  121. /*!
  122. \brief end CEC message transmission
  123. \param[in] none
  124. \param[out] none
  125. \retval none
  126. */
  127. void cec_transmission_end(void)
  128. {
  129. CEC_CTL |= CEC_CTL_ENDOM;
  130. }
  131. /*!
  132. \brief enable CEC listen mode.
  133. \param[in] none
  134. \param[out] none
  135. \retval none
  136. */
  137. void cec_listen_mode_enable(void)
  138. {
  139. CEC_CFG |= CEC_CFG_LMEN;
  140. }
  141. /*!
  142. \brief disable CEC listen mode.
  143. \param[in] none
  144. \param[out] none
  145. \retval none
  146. */
  147. void cec_listen_mode_disable(void)
  148. {
  149. CEC_CFG &= ~CEC_CFG_LMEN;
  150. }
  151. /*!
  152. \brief configure and clear own address.the controller can be configured to multiple own address
  153. \param[in] address: own address
  154. \arg CEC_OWN_ADDRESS_CLEAR: own address is cleared
  155. \arg CEC_OWN_ADDRESSx(x=0..14): own address is x
  156. \param[out] none
  157. \retval none
  158. */
  159. void cec_own_address_config(uint32_t address)
  160. {
  161. if(CEC_OWN_ADDRESS_CLEAR == address){
  162. CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
  163. } else {
  164. CEC_CFG |= address;
  165. }
  166. }
  167. /*!
  168. \brief configure signal free time and the signal free time counter start option
  169. \param[in] sftmopt: signal free time counter start option
  170. \arg CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
  171. \arg CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
  172. \param[in] sft: signal free time
  173. \arg CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
  174. \arg CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
  175. \arg CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
  176. \arg CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
  177. \arg CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
  178. \arg CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
  179. \arg CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
  180. \arg CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
  181. \param[out] none
  182. \retval none
  183. */
  184. void cec_sft_config(uint32_t sftmopt, uint32_t sft)
  185. {
  186. uint32_t cfg;
  187. cfg = CEC_CFG;
  188. /* clear SFTMOPT bit,SFT[2:0] */
  189. cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
  190. /* assign SFTMOPT bit,SFT[2:0] */
  191. cfg |= (sftmopt | sft);
  192. CEC_CFG = cfg;
  193. }
  194. /*!
  195. \brief configure generate Error-bit when detected some abnormal situation or not
  196. \param[in] broadcast:
  197. \arg CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
  198. \arg CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
  199. \param[in] singlecast_lbpe:
  200. \arg CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
  201. \arg CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
  202. \param[in] singlecast_bre:
  203. \arg CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
  204. \arg CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
  205. \param[out] none
  206. \retval none
  207. */
  208. void cec_generate_errorbit_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre)
  209. {
  210. uint32_t cfg;
  211. cfg = CEC_CFG;
  212. /* clear BCNG bit, RLBPEGEN bit, RBREGEN bit */
  213. cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
  214. /* assign BCNG bit, RLBPEGEN bit, RBREGEN bit */
  215. cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
  216. CEC_CFG = cfg;
  217. }
  218. /*!
  219. \brief whether stop receive message when detected bit rising error
  220. \param[in] rxbrestp:
  221. \arg CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
  222. \arg CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
  223. \param[out] none
  224. \retval none
  225. */
  226. void cec_stop_receive_bre_config(uint32_t rxbrestp)
  227. {
  228. if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
  229. CEC_CFG |= CEC_CFG_BRES;
  230. } else {
  231. CEC_CFG &= ~CEC_CFG_BRES;
  232. }
  233. }
  234. /*!
  235. \brief enable reception bit timing tolerance
  236. \param[in] none
  237. \param[out] none
  238. \retval none
  239. */
  240. void cec_reception_tolerance_enable(void)
  241. {
  242. CEC_CFG |= CEC_CFG_RTOL;
  243. }
  244. /*!
  245. \brief disable reception bit timing tolerance
  246. \param[in] none
  247. \param[out] none
  248. \retval none
  249. */
  250. void cec_reception_tolerance_disable(void)
  251. {
  252. CEC_CFG &= ~CEC_CFG_RTOL;
  253. }
  254. /*!
  255. \brief send a data by the CEC peripheral
  256. \param[in] data: the data to transmit
  257. \param[out] none
  258. \retval none
  259. */
  260. void cec_data_send(uint8_t data)
  261. {
  262. CEC_TDATA = (uint32_t)data;
  263. }
  264. /*!
  265. \brief receive a data by the CEC peripheral
  266. \param[in] data: the data to receive
  267. \param[out] none
  268. \retval none
  269. */
  270. uint8_t cec_data_receive(void)
  271. {
  272. return (uint8_t)CEC_RDATA;
  273. }
  274. /*!
  275. \brief get CEC int flag
  276. \param[in] flag: specify which flag
  277. \arg CEC_INT_FLAG_BR: Rx-byte data received
  278. \arg CEC_INT_FLAG_REND: end of reception
  279. \arg CEC_INT_FLAG_RO: RX overrun
  280. \arg CEC_INT_FLAG_BRE: bit rising error
  281. \arg CEC_INT_FLAG_BPSE: short bit period error
  282. \arg CEC_INT_FLAG_BPLE: long bit period error
  283. \arg CEC_INT_FLAG_RAE: Rx ACK error
  284. \arg CEC_INT_FLAG_ARBF: arbitration lost
  285. \arg CEC_INT_FLAG_TBR: Tx-byte data request
  286. \arg CEC_INT_FLAG_TEND: transmission successfully end
  287. \arg CEC_INT_FLAG_TU: Tx data buffer underrun
  288. \arg CEC_INT_FLAG_TERR: Tx-error
  289. \arg CEC_INT_FLAG_TAERR: Tx ACK error flag
  290. \param[out] none
  291. \retval FlagStatus: SET or RESET
  292. */
  293. FlagStatus cec_interrupt_flag_get(uint32_t flag)
  294. {
  295. uint32_t interrupt_enable = 0U,interrupt_flag = 0U;
  296. interrupt_flag = (CEC_INTF & flag);
  297. interrupt_enable = (CEC_INTEN & flag);
  298. if(interrupt_flag && interrupt_enable){
  299. return SET;
  300. }else{
  301. return RESET;
  302. }
  303. }
  304. /*!
  305. \brief clear CEC int flag and status
  306. \param[in] flag: specify which flag
  307. \arg CEC_INT_FLAG_BR: Rx-byte data received
  308. \arg CEC_INT_FLAG_REND: end of reception
  309. \arg CEC_INT_FLAG_RO: RX overrun
  310. \arg CEC_INT_FLAG_BRE: bit rising error
  311. \arg CEC_INT_FLAG_BPSE: short bit period error
  312. \arg CEC_INT_FLAG_BPLE: long bit period error
  313. \arg CEC_INT_FLAG_RAE: Rx ACK error
  314. \arg CEC_INT_FLAG_ARBF: arbitration lost
  315. \arg CEC_INT_FLAG_TBR: Tx-byte data request
  316. \arg CEC_INT_FLAG_TEND: transmission successfully end
  317. \arg CEC_INT_FLAG_TU: Tx data buffer underrun
  318. \arg CEC_INT_FLAG_TERR: Tx-error
  319. \arg CEC_INT_FLAG_TAERR: Tx ACK error flag
  320. \param[out] none
  321. \retval none
  322. */
  323. void cec_interrupt_flag_clear(uint32_t flag)
  324. {
  325. CEC_INTF = flag;
  326. }
  327. /*!
  328. \brief enable interrupt
  329. \param[in] flag: specify which flag
  330. \arg CEC_INT_BR: enable Rx-byte data received interrupt
  331. \arg CEC_INT_REND: enable end of reception interrupt
  332. \arg CEC_INT_RO: enable RX overrun interrupt
  333. \arg CEC_INT_BRE: enable bit rising error interrupt
  334. \arg CEC_INT_BPSE: enable short bit period error interrupt
  335. \arg CEC_INT_BPLE: enable long bit period error interrupt
  336. \arg CEC_INT_RAE: enable Rx ACK error interrupt
  337. \arg CEC_INT_ARBF: enable arbitration lost interrupt
  338. \arg CEC_INT_TBR: enable Tx-byte data request interrupt
  339. \arg CEC_INT_TEND: enable transmission successfully end interrupt
  340. \arg CEC_INT_TU: enable Tx data buffer underrun interrupt
  341. \arg CEC_INT_TERR: enable Tx-error interrupt
  342. \arg CEC_INT_TAERR: enable Tx ACK error interrupt
  343. \param[out] none
  344. \retval none
  345. */
  346. void cec_interrupt_enable(uint32_t flag)
  347. {
  348. CEC_INTEN |= flag;
  349. }
  350. /*!
  351. \brief disable interrupt
  352. \param[in] flag: specify which flag
  353. \arg CEC_INT_BR: disable Rx-byte data received interrupt
  354. \arg CEC_INT_REND: disable end of reception interrupt
  355. \arg CEC_INT_RO: disable RX overrun interrupt
  356. \arg CEC_INT_BRE: disable bit rising error interrupt
  357. \arg CEC_INT_BPSE: disable short bit period error interrupt
  358. \arg CEC_INT_BPLE: disable long bit period error interrupt
  359. \arg CEC_INT_RAE: disable Rx ACK error interrupt
  360. \arg CEC_INT_ARBF: disable arbitration lost interrupt
  361. \arg CEC_INT_TBR: disable Tx-byte data request interrupt
  362. \arg CEC_INT_TEND: disable transmission successfully end interrupt
  363. \arg CEC_INT_TU: disable Tx data buffer underrun interrupt
  364. \arg CEC_INT_TERR: disable Tx-error interrupt
  365. \arg CEC_INT_TAERR: disable Tx ACK error interrupt
  366. \param[out] none
  367. \retval none
  368. */
  369. void cec_interrupt_disable(uint32_t flag)
  370. {
  371. CEC_INTEN &= ~flag;
  372. }
  373. /*!
  374. \brief get CEC status
  375. \param[in] flag: specify which flag
  376. \arg CEC_FLAG_BR: Rx-byte data received
  377. \arg CEC_FLAG_REND: end of reception
  378. \arg CEC_FLAG_RO: RX overrun
  379. \arg CEC_FLAG_BRE: bit rising error
  380. \arg CEC_FLAG_BPSE: short bit period error
  381. \arg CEC_FLAG_BPLE: long bit period error
  382. \arg CEC_FLAG_RAE: Rx ACK error
  383. \arg CEC_FLAG_ARBF: arbitration lost
  384. \arg CEC_FLAG_TBR: Tx-byte data request
  385. \arg CEC_FLAG_TEND: transmission successfully end
  386. \arg CEC_FLAG_TU: Tx data buffer underrun
  387. \arg CEC_FLAG_TERR: Tx-error
  388. \arg CEC_FLAG_TAERR Tx ACK error flag
  389. \param[out] none
  390. \retval FlagStatus: SET or RESET
  391. */
  392. FlagStatus cec_flag_get(uint32_t flag)
  393. {
  394. if(CEC_INTF & flag){
  395. return SET;
  396. }else{
  397. return RESET;
  398. }
  399. }
  400. /*!
  401. \brief clear CEC status
  402. \param[in] flag: specify which flag
  403. \arg CEC_FLAG_BR: Rx-byte data received
  404. \arg CEC_FLAG_REND: end of reception
  405. \arg CEC_FLAG_RO: RX overrun
  406. \arg CEC_FLAG_BRE: bit rising error
  407. \arg CEC_FLAG_BPSE: short bit period error
  408. \arg CEC_FLAG_BPLE: long bit period error
  409. \arg CEC_FLAG_RAE: Rx ACK error
  410. \arg CEC_FLAG_ARBF: arbitration lost
  411. \arg CEC_FLAG_TBR: Tx-byte data request
  412. \arg CEC_FLAG_TEND: transmission successfully end
  413. \arg CEC_FLAG_TU: Tx data buffer underrun
  414. \arg CEC_FLAG_TERR: Tx-error
  415. \arg CEC_FLAG_TAERR Tx ACK error flag
  416. \param[out] none
  417. \retval FlagStatus: SET or RESET
  418. */
  419. void cec_flag_clear(uint32_t flag)
  420. {
  421. CEC_INTF |= flag;
  422. }
  423. #endif