1 /*!
2     \file    gd32f3x0_cec.h
3     \brief   definitions for the CEC
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9 
10 /*
11     Copyright (c) 2020, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #ifdef GD32F350
38 
39 #ifndef GD32F3X0_CEC_H
40 #define GD32F3X0_CEC_H
41 
42 #include "gd32f3x0.h"
43 
44 /* CEC definitions */
45 #define CEC                                CEC_BASE                   /*!< CEC base address */
46 
47 /* registers definitions */
48 #define CEC_CTL                            REG32(CEC + 0x00000000U)   /*!< CEC control register */
49 #define CEC_CFG                            REG32(CEC + 0x00000004U)   /*!< CEC configuration register */
50 #define CEC_TDATA                          REG32(CEC + 0x00000008U)   /*!< CEC transmit data register */
51 #define CEC_RDATA                          REG32(CEC + 0x0000000CU)   /*!< CEC receive data register */
52 #define CEC_INTF                           REG32(CEC + 0x00000010U)   /*!< CEC interrupt flag Register */
53 #define CEC_INTEN                          REG32(CEC + 0x00000014U)   /*!< CEC interrupt enable register */
54 
55 /* bits definitions */
56 /* CEC_CTL */
57 #define CEC_CTL_CECEN                      BIT(0)                     /*!< enable or disable HDMI-CEC controller bit */
58 #define CEC_CTL_STAOM                      BIT(1)                     /*!< start of sending a message. */
59 #define CEC_CTL_ENDOM                      BIT(2)                     /*!< ENDOM bit value in the next frame in Tx mode */
60 
61 /* CEC_CFG */
62 #define CEC_CFG_SFT                        BITS(0,2)                  /*!< signal free time */
63 #define CEC_CFG_RTOL                       BIT(3)                     /*!< reception bit timing tolerance */
64 #define CEC_CFG_BRES                       BIT(4)                     /*!< whether stop receive message when detected BRE */
65 #define CEC_CFG_BREG                       BIT(5)                     /*!< generate Error-bit when detected BRE in singlecast */
66 #define CEC_CFG_BPLEG                      BIT(6)                     /*!< generate Error-bit when detected BPLE in singlecast */
67 #define CEC_CFG_BCNG                       BIT(7)                     /*!< do not generate Error-bit in broadcast message */
68 #define CEC_CFG_SFTOPT                     BIT(8)                     /*!< the SFT start option bit */
69 #define CEC_CFG_OAD                        BITS(16,30)                /*!< own address */
70 #define CEC_CFG_LMEN                       BIT(31)                    /*!< listen mode enable bit */
71 
72 /* CEC_TDATA */
73 #define CEC_TDATA_TDATA                    BITS(0,7)                  /*!< Tx data register */
74 
75 /* CEC_RDATA */
76 #define CEC_RDATA_RDATA                    BITS(0,7)                  /*!< Rx data register */
77 
78 /* CEC_INTF */
79 #define CEC_INTF_BR                        BIT(0)                     /*!< Rx-byte data received */
80 #define CEC_INTF_REND                      BIT(1)                     /*!< end of reception */
81 #define CEC_INTF_RO                        BIT(2)                     /*!< Rx overrun */
82 #define CEC_INTF_BRE                       BIT(3)                     /*!< bit rising error */
83 #define CEC_INTF_BPSE                      BIT(4)                     /*!< short bit period error */
84 #define CEC_INTF_BPLE                      BIT(5)                     /*!< long bit period error */
85 #define CEC_INTF_RAE                       BIT(6)                     /*!< Rx ACK error */
86 #define CEC_INTF_ARBF                      BIT(7)                     /*!< arbitration fail */
87 #define CEC_INTF_TBR                       BIT(8)                     /*!< Tx-byte data request */
88 #define CEC_INTF_TEND                      BIT(9)                     /*!< transmission successfully end */
89 #define CEC_INTF_TU                        BIT(10)                    /*!< Tx data buffer underrun */
90 #define CEC_INTF_TERR                      BIT(11)                    /*!< Tx-error */
91 #define CEC_INTF_TAERR                     BIT(12)                    /*!< Tx ACK error flag */
92 
93 /* CEC_INTEN */
94 #define CEC_INTEN_BRIE                     BIT(0)                     /*!< BR interrupt enable */
95 #define CEC_INTEN_RENDIE                   BIT(1)                     /*!< REND interrupt enable */
96 #define CEC_INTEN_ROIE                     BIT(2)                     /*!< RO interrupt enable */
97 #define CEC_INTEN_BREIE                    BIT(3)                     /*!< BRE interrupt enable. */
98 #define CEC_INTEN_BPSEIE                   BIT(4)                     /*!< BPSE interrupt enable */
99 #define CEC_INTEN_BPLEIE                   BIT(5)                     /*!< BPLE interrupt enable. */
100 #define CEC_INTEN_RAEIE                    BIT(6)                     /*!< RAE interrupt enable */
101 #define CEC_INTEN_ARBFIE                   BIT(7)                     /*!< ARBF interrupt enable */
102 #define CEC_INTEN_TBRIE                    BIT(8)                     /*!< TBR interrupt enable */
103 #define CEC_INTEN_TENDIE                   BIT(9)                     /*!< TEND interrupt enable */
104 #define CEC_INTEN_TUIE                     BIT(10)                    /*!< TU interrupt enable */
105 #define CEC_INTEN_TERRIE                   BIT(11)                    /*!< TE interrupt enable */
106 #define CEC_INTEN_TAERRIE                  BIT(12)                    /*!< TAE interrupt enable */
107 
108 /* constants definitions */
109 /* signal free time */
110 #define CFG_SFT(regval)                    (BITS(0, 2) & ((regval) << 0U))
111 #define CEC_SFT_PROTOCOL_PERIOD            CFG_SFT(0)                 /*!< the signal free time will perform as HDMI-CEC protocol description */
112 #define CEC_SFT_1POINT5_PERIOD             CFG_SFT(1)                 /*!< 1.5 nominal data bit periods */
113 #define CEC_SFT_2POINT5_PERIOD             CFG_SFT(2)                 /*!< 2.5 nominal data bit periods */
114 #define CEC_SFT_3POINT5_PERIOD             CFG_SFT(3)                 /*!< 3.5 nominal data bit periods */
115 #define CEC_SFT_4POINT5_PERIOD             CFG_SFT(4)                 /*!< 4.5 nominal data bit periods */
116 #define CEC_SFT_5POINT5_PERIOD             CFG_SFT(5)                 /*!< 5.5 nominal data bit periods */
117 #define CEC_SFT_6POINT5_PERIOD             CFG_SFT(6)                 /*!< 6.5 nominal data bit periods */
118 #define CEC_SFT_7POINT5_PERIOD             CFG_SFT(7)                 /*!< 7.5 nominal data bit periods */
119 
120 /* signal free time start option */
121 #define CEC_SFT_START_STAOM                ((uint32_t)0x00000000U)    /*!< signal free time counter starts counting when STAOM is asserted */
122 #define CEC_SFT_START_LAST                 CEC_CFG_SFTOPT             /*!< signal free time counter starts automatically after transmission/reception end */
123 
124 /* own address */
125 #define CEC_OWN_ADDRESS_CLEAR              ((uint32_t)0x00000000U)    /*!< own address is cleared */
126 #define CEC_OWN_ADDRESS0                   BIT(16)                    /*!< own address is 0 */
127 #define CEC_OWN_ADDRESS1                   BIT(17)                    /*!< own address is 1 */
128 #define CEC_OWN_ADDRESS2                   BIT(18)                    /*!< own address is 2 */
129 #define CEC_OWN_ADDRESS3                   BIT(19)                    /*!< own address is 3 */
130 #define CEC_OWN_ADDRESS4                   BIT(20)                    /*!< own address is 4 */
131 #define CEC_OWN_ADDRESS5                   BIT(21)                    /*!< own address is 5 */
132 #define CEC_OWN_ADDRESS6                   BIT(22)                    /*!< own address is 6 */
133 #define CEC_OWN_ADDRESS7                   BIT(23)                    /*!< own address is 7 */
134 #define CEC_OWN_ADDRESS8                   BIT(24)                    /*!< own address is 8 */
135 #define CEC_OWN_ADDRESS9                   BIT(25)                    /*!< own address is 9 */
136 #define CEC_OWN_ADDRESS10                  BIT(26)                    /*!< own address is 10 */
137 #define CEC_OWN_ADDRESS11                  BIT(27)                    /*!< own address is 11 */
138 #define CEC_OWN_ADDRESS12                  BIT(28)                    /*!< own address is 12 */
139 #define CEC_OWN_ADDRESS13                  BIT(29)                    /*!< own address is 13 */
140 #define CEC_OWN_ADDRESS14                  BIT(30)                    /*!< own address is 14 */
141 
142 /* error-bit generate */
143 #define CEC_BROADCAST_ERROR_BIT_ON         ((uint32_t)0x00000000U)    /*!< generate Error-bit in broadcast */
144 #define CEC_BROADCAST_ERROR_BIT_OFF        CEC_CFG_BCNG               /*!< do not generate Error-bit in broadcast */
145 #define CEC_LONG_PERIOD_ERROR_BIT_OFF      ((uint32_t)0x00000000U)    /*!< generate Error-bit on long bit period error */
146 #define CEC_LONG_PERIOD_ERROR_BIT_ON       CEC_CFG_BPLEG              /*!< do not generate Error-bit on long bit period error */
147 #define CEC_RISING_PERIOD_ERROR_BIT_OFF    ((uint32_t)0x00000000U)    /*!< generate Error-bit on bit rising error */
148 #define CEC_RISING_PERIOD_ERROR_BIT_ON     CEC_CFG_BREG               /*!< do not generate Error-bit on bit rising error */
149 
150 /* whether stop receive message when detected bit rising error */
151 #define CEC_STOP_RISING_ERROR_BIT_ON       ((uint32_t)0x00000000U)    /*!< stop reception when detected bit rising error */
152 #define CEC_STOP_RISING_ERROR_BIT_OFF      ((uint32_t)0x00000001U)    /*!< do not stop reception when detected bit rising error */
153 
154 /* flag bits */
155 #define CEC_FLAG_BR                        CEC_INTF_BR                /*!< RX-byte data received */
156 #define CEC_FLAG_REND                      CEC_INTF_REND              /*!< end of reception */
157 #define CEC_FLAG_RO                        CEC_INTF_RO                /*!< RX overrun */
158 #define CEC_FLAG_BRE                       CEC_INTF_BRE               /*!< bit rising error */
159 #define CEC_FLAG_BPSE                      CEC_INTF_BPSE              /*!< short bit period error */
160 #define CEC_FLAG_BPLE                      CEC_INTF_BPLE              /*!< long bit period error */
161 #define CEC_FLAG_RAE                       CEC_INTF_RAE               /*!< RX ACK error */
162 #define CEC_FLAG_ARBF                      CEC_INTF_ARBF              /*!< arbitration lost */
163 #define CEC_FLAG_TBR                       CEC_INTF_TBR               /*!< TX-byte data request */
164 #define CEC_FLAG_TEND                      CEC_INTF_TEND              /*!< transmission successfully end */
165 #define CEC_FLAG_TU                        CEC_INTF_TU                /*!< TX data buffer underrun */
166 #define CEC_FLAG_TERR                      CEC_INTF_TERR              /*!< TX-error */
167 #define CEC_FLAG_TAERR                     CEC_INTF_TAERR             /*!< TX ACK error flag */
168 
169 /* interrupt flag bits */
170 #define CEC_INT_FLAG_BR                    CEC_INTF_BR                /*!< RX-byte data received */
171 #define CEC_INT_FLAG_REND                  CEC_INTF_REND              /*!< end of reception */
172 #define CEC_INT_FLAG_RO                    CEC_INTF_RO                /*!< RX overrun */
173 #define CEC_INT_FLAG_BRE                   CEC_INTF_BRE               /*!< bit rising error */
174 #define CEC_INT_FLAG_BPSE                  CEC_INTF_BPSE              /*!< short bit period error */
175 #define CEC_INT_FLAG_BPLE                  CEC_INTF_BPLE              /*!< long bit period error */
176 #define CEC_INT_FLAG_RAE                   CEC_INTF_RAE               /*!< RX ACK error */
177 #define CEC_INT_FLAG_ARBF                  CEC_INTF_ARBF              /*!< arbitration lost */
178 #define CEC_INT_FLAG_TBR                   CEC_INTF_TBR               /*!< TX-byte data request */
179 #define CEC_INT_FLAG_TEND                  CEC_INTF_TEND              /*!< transmission successfully end */
180 #define CEC_INT_FLAG_TU                    CEC_INTF_TU                /*!< TX data buffer underrun */
181 #define CEC_INT_FLAG_TERR                  CEC_INTF_TERR              /*!< TX-error */
182 #define CEC_INT_FLAG_TAERR                 CEC_INTF_TAERR             /*!< TX ACK error flag */
183 
184 /* interrupt enable bits */
185 #define CEC_INT_BR                         CEC_INTEN_BRIE             /*!< RBR interrupt enable */
186 #define CEC_INT_REND                       CEC_INTEN_RENDIE           /*!< REND interrupt enable */
187 #define CEC_INT_RO                         CEC_INTEN_ROIE             /*!< RO interrupt enable */
188 #define CEC_INT_BRE                        CEC_INTEN_BREIE            /*!< RBRE interrupt enable. */
189 #define CEC_INT_BPSE                       CEC_INTEN_BPSEIE           /*!< RSBPE interrupt enable */
190 #define CEC_INT_BPLE                       CEC_INTEN_BPLEIE           /*!< RLBPE interrupt enable. */
191 #define CEC_INT_RAE                        CEC_INTEN_RAEIE            /*!< RAE interrupt enable */
192 #define CEC_INT_ARBF                       CEC_INTEN_ARBFIE           /*!< ALRLST interrupt enable */
193 #define CEC_INT_TBR                        CEC_INTEN_TBRIE            /*!< TBR interrupt enable */
194 #define CEC_INT_TEND                       CEC_INTEN_TENDIE           /*!< TEND interrupt enable */
195 #define CEC_INT_TU                         CEC_INTEN_TUIE             /*!< TU interrupt enable */
196 #define CEC_INT_TERR                       CEC_INTEN_TERRIE           /*!< TE interrupt enable */
197 #define CEC_INT_TAERR                      CEC_INTEN_TAERRIE          /*!< TAE interrupt enable */
198 
199 /* function declarations */
200 /* reset HDMI-CEC controller */
201 void cec_deinit(void);
202 /* configure signal free time,the signal free time counter start option,own address */
203 void cec_init(uint32_t sftmopt, uint32_t sft, uint32_t address);
204 /* configure generate Error-bit, whether stop receive message when detected bit rising error */
205 void cec_error_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre, uint32_t rxbrestp);
206 /* enable HDMI-CEC controller */
207 void cec_enable(void);
208 /* disable HDMI-CEC controller */
209 void cec_disable(void);
210 
211 /* start CEC message transmission */
212 void cec_transmission_start(void);
213 /* end CEC message transmission */
214 void cec_transmission_end(void);
215 /* enable CEC listen mode */
216 void cec_listen_mode_enable(void);
217 /* disable CEC listen mode */
218 void cec_listen_mode_disable(void);
219 /* configure and clear own address */
220 void cec_own_address_config(uint32_t address);
221 /* configure signal free time and the signal free time counter start option */
222 void cec_sft_config(uint32_t sftmopt,uint32_t sft);
223 /* configure generate Error-bit when detected some abnormal situation or not */
224 void cec_generate_errorbit_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre);
225 /* whether stop receive message when detected bit rising error */
226 void cec_stop_receive_bre_config(uint32_t rxbrestp);
227 /* enable reception bit timing tolerance */
228 void cec_reception_tolerance_enable(void);
229 /* disable reception bit timing tolerance */
230 void cec_reception_tolerance_disable(void);
231 /* send a data by the CEC peripheral */
232 void cec_data_send(uint8_t data);
233 /* receive a data by the CEC peripheral */
234 uint8_t cec_data_receive(void);
235 
236 /* enable interrupt */
237 void cec_interrupt_enable(uint32_t flag);
238 /* disable interrupt */
239 void cec_interrupt_disable(uint32_t flag);
240 /* get CEC status */
241 FlagStatus cec_flag_get(uint32_t flag);
242 /* clear CEC status */
243 void cec_flag_clear(uint32_t flag);
244 /* get CEC int flag and status */
245 FlagStatus cec_interrupt_flag_get(uint32_t flag);
246 /* clear CEC int flag and status */
247 void cec_interrupt_flag_clear(uint32_t flag);
248 
249 #endif /* GD32F3X0_CEC_H */
250 
251 #endif /* GD32F350 */
252