summaryrefslogtreecommitdiff
path: root/bsp/gd32350r-eval/Libraries/GD32F3x0_standard_peripheral/Source/gd32f3x0_cec.c
blob: 42b0d502536a543cad170b76bd27e2336948cb10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*!
    \file  gd32f3x0_cec.c
    \brief CEC driver
    
    \version 2017-06-06, V1.0.0, firmware for GD32F3x0
    \version 2019-06-01, V2.0.0, firmware for GD32F3x0
*/

/*
    Copyright (c) 2019, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#ifdef GD32F350

#include "gd32f3x0_cec.h"

/*!
    \brief      reset HDMI-CEC controller
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_deinit(void)
{
    rcu_periph_reset_enable(RCU_CECRST);
    rcu_periph_reset_disable(RCU_CECRST);
}

/*!
    \brief      configure signal free time,the signal free time counter start option,own address 
    \param[in]  sftmopt: signal free time counter start option
                only one parameter can be selected which is shown as below:
      \arg        CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
      \arg        CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
    \param[in]  sft: signal free time
                only one parameter can be selected which is shown as below:
      \arg        CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
      \arg        CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
      \arg        CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
      \arg        CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
      \arg        CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
      \arg        CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
      \arg        CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
      \arg        CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
    \param[in]  address: own address
                only one parameter can be selected which is shown as below:
      \arg        CEC_OWN_ADDRESS_CLEAR: own address is cleared
      \arg        CEC_OWN_ADDRESSx(x=0..14): own address is x
    \param[out] none
    \retval     none
*/
void cec_init(uint32_t sftmopt, uint32_t sft, uint32_t address)
{
    uint32_t cfg;
    cfg = CEC_CFG;
    /* clear SFTMOPT bit,SFT[2:0] */
    cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
    /* assign SFTMOPT bit,SFT[2:0] */
    cfg |= (sftmopt | sft);
    CEC_CFG = cfg;
    if(CEC_OWN_ADDRESS_CLEAR == address){
        CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
    }else{
        CEC_CFG |= address;
    }
}

/*!
    \brief      configure generate Error-bit when detected some abnormal situation or not,
                whether stop receive message when detected bit rising error
    \param[in]  broadcast:
                only one parameter can be selected which is shown as below:
      \arg        CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
      \arg        CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
    \param[in]  singlecast_lbpe:
                only one parameter can be selected which is shown as below:
      \arg        CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
      \arg        CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
    \param[in]  singlecast_bre:
                only one parameter can be selected which is shown as below:
      \arg        CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
      \arg        CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
    \param[in]  rxbrestp:
                only one parameter can be selected which is shown as below:
      \arg        CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
      \arg        CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
    \param[out] none
    \retval     none
*/
void cec_error_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre, uint32_t rxbrestp)
{
    uint32_t cfg;
    cfg = CEC_CFG;
    /* clear BCNG bit, BPLEG bit, BREG bit */
    cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
    /* assign BCNG bit, BPLEG bit, BREG bit */
    cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
    CEC_CFG = cfg;
    if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
        CEC_CFG |= CEC_CFG_BRES;
    }else{
        CEC_CFG &= ~CEC_CFG_BRES;
    }
}

/*!
    \brief      enable HDMI-CEC controller
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_enable(void)
{
    CEC_CTL |= CEC_CTL_CECEN;
}

/*!
    \brief      disable HDMI-CEC controller
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_disable(void)
{
    CEC_CTL &= ~CEC_CTL_CECEN;
}

/*!
    \brief      start CEC message transmission 
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_transmission_start(void)
{
    CEC_CTL |= CEC_CTL_STAOM;
}

/*!
    \brief      end CEC message transmission
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_transmission_end(void)
{
    CEC_CTL |= CEC_CTL_ENDOM;
}

/*!
    \brief      enable CEC listen mode.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_listen_mode_enable(void)
{
    CEC_CFG |= CEC_CFG_LMEN;
}

/*!
    \brief      disable CEC listen mode.
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_listen_mode_disable(void)
{
    CEC_CFG &= ~CEC_CFG_LMEN;
}

/*!
    \brief      configure and clear own address.the controller can be configured to multiple own address 
    \param[in]  address: own address
                one or more parameters can be selected which are shown as below:
      \arg        CEC_OWN_ADDRESS_CLEAR: own address is cleared
      \arg        CEC_OWN_ADDRESSx(x=0..14): own address is x
    \param[out] none
    \retval     none
*/
void cec_own_address_config(uint32_t address)
{
    if(CEC_OWN_ADDRESS_CLEAR == address){
        CEC_CFG &= ~CEC_CFG_OWN_ADDRESS;
    } else {
        CEC_CFG |= address;
    }
}

/*!
    \brief      configure signal free time and the signal free time counter start option 
    \param[in]  sftmopt: signal free time counter start option
                only one parameter can be selected which is shown as below:
      \arg        CEC_SFT_START_STAOM: signal free time counter starts counting when STAOM is asserted
      \arg        CEC_SFT_START_LAST: signal free time counter starts automatically after transmission/reception end
    \param[in]  sft: signal free time
                only one parameter can be selected which is shown as below:
      \arg        CEC_SFT_PROTOCOL_PERIOD: the signal free time will perform as HDMI-CEC protocol description
      \arg        CEC_SFT_1POINT5_PERIOD: 1.5 nominal data bit periods
      \arg        CEC_SFT_2POINT5_PERIOD: 2.5 nominal data bit periods
      \arg        CEC_SFT_3POINT5_PERIOD: 3.5 nominal data bit periods
      \arg        CEC_SFT_4POINT5_PERIOD: 4.5 nominal data bit periods
      \arg        CEC_SFT_5POINT5_PERIOD: 5.5 nominal data bit periods
      \arg        CEC_SFT_6POINT5_PERIOD: 6.5 nominal data bit periods
      \arg        CEC_SFT_7POINT5_PERIOD: 7.5 nominal data bit periods
    \param[out] none
    \retval     none
*/
void cec_sft_config(uint32_t sftmopt, uint32_t sft)
{
    uint32_t cfg;
    cfg = CEC_CFG;
    /* clear SFTMOPT bit,SFT[2:0] */
    cfg &= ~(CEC_CFG_SFTOPT | CEC_CFG_SFT);
    /* assign SFTMOPT bit,SFT[2:0] */
    cfg |= (sftmopt | sft);
    CEC_CFG = cfg;
}

/*!
    \brief      configure generate Error-bit when detected some abnormal situation or not
    \param[in]  broadcast:
                only one parameter can be selected which is shown as below:
      \arg        CEC_BROADCAST_ERROR_BIT_ON:generate Error-bit in broadcast
      \arg        CEC_BROADCAST_ERROR_BIT_OFF:do not generate Error-bit in broadcast
    \param[in]  singlecast_lbpe:
                only one parameter can be selected which is shown as below:
      \arg        CEC_LONG_PERIOD_ERROR_BIT_ON:generate Error-bit on long bit period error
      \arg        CEC_LONG_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on long bit period error
    \param[in]  singlecast_bre:
                only one parameter can be selected which is shown as below:
      \arg        CEC_RISING_PERIOD_ERROR_BIT_ON:generate Error-bit on bit rising error
      \arg        CEC_RISING_PERIOD_ERROR_BIT_OFF:do not generate Error-bit on bit rising error
    \param[out] none
    \retval     none
*/
void cec_generate_errorbit_config(uint32_t broadcast, uint32_t singlecast_lbpe, uint32_t singlecast_bre)
{
    uint32_t cfg;
    cfg = CEC_CFG;
    /* clear BCNG bit, RLBPEGEN bit, RBREGEN bit */
    cfg &= ~(CEC_CFG_BCNG | CEC_CFG_BPLEG | CEC_CFG_BREG);
    /* assign BCNG bit, RLBPEGEN bit, RBREGEN bit */
    cfg |= (broadcast | singlecast_lbpe | singlecast_bre);
    CEC_CFG = cfg;
}

/*!
    \brief      whether stop receive message when detected bit rising error
    \param[in]  rxbrestp:
                only one parameter can be selected which is shown as below:
      \arg        CEC_STOP_RISING_ERROR_BIT_ON: stop reception when detected bit rising error
      \arg        CEC_STOP_RISING_ERROR_BIT_OFF: do not stop reception when detected bit rising error
    \param[out] none
    \retval     none
*/
void cec_stop_receive_bre_config(uint32_t rxbrestp)
{
    if(CEC_STOP_RISING_ERROR_BIT_ON == rxbrestp){
        CEC_CFG |= CEC_CFG_BRES;
    } else {
        CEC_CFG &= ~CEC_CFG_BRES;
    }
}

/*!
    \brief      enable reception bit timing tolerance
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_reception_tolerance_enable(void)
{
    CEC_CFG |= CEC_CFG_RTOL;
}

/*!
    \brief      disable reception bit timing tolerance
    \param[in]  none
    \param[out] none
    \retval     none
*/
void cec_reception_tolerance_disable(void)
{
    CEC_CFG &= ~CEC_CFG_RTOL;
}

/*!
    \brief      send a data by the CEC peripheral
    \param[in]  data: the data to transmit
    \param[out] none
    \retval     none
*/
void cec_data_send(uint8_t data)
{
    CEC_TDATA = (uint32_t)data;
}

/*!
    \brief      receive a data by the CEC peripheral
    \param[in]  data: the data to receive
    \param[out] none
    \retval     none
*/
uint8_t cec_data_receive(void)
{
    return (uint8_t)CEC_RDATA;
}


/*!
    \brief      enable interrupt
    \param[in]  flag: specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_INT_BR: enable Rx-byte data received interrupt
      \arg        CEC_INT_REND: enable end of reception interrupt
      \arg        CEC_INT_RO: enable RX overrun interrupt
      \arg        CEC_INT_BRE: enable bit rising error interrupt
      \arg        CEC_INT_BPSE: enable short bit period error interrupt
      \arg        CEC_INT_BPLE: enable long bit period error interrupt
      \arg        CEC_INT_RAE: enable Rx ACK error interrupt
      \arg        CEC_INT_ARBF: enable arbitration lost interrupt
      \arg        CEC_INT_TBR: enable Tx-byte data request interrupt
      \arg        CEC_INT_TEND: enable transmission successfully end interrupt
      \arg        CEC_INT_TU: enable Tx data buffer underrun interrupt
      \arg        CEC_INT_TERR: enable Tx-error interrupt 
      \arg        CEC_INT_TAERR: enable Tx ACK error interrupt
    \param[out] none
    \retval     none
*/
void cec_interrupt_enable(uint32_t flag)
{
    CEC_INTEN |= flag;
}

/*!
    \brief      disable interrupt
    \param[in]  flag: specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_INT_BR: disable Rx-byte data received interrupt
      \arg        CEC_INT_REND: disable end of reception interrupt
      \arg        CEC_INT_RO: disable RX overrun interrupt
      \arg        CEC_INT_BRE: disable bit rising error interrupt
      \arg        CEC_INT_BPSE: disable short bit period error interrupt
      \arg        CEC_INT_BPLE: disable long bit period error interrupt
      \arg        CEC_INT_RAE: disable Rx ACK error interrupt
      \arg        CEC_INT_ARBF: disable arbitration lost interrupt
      \arg        CEC_INT_TBR: disable Tx-byte data request interrupt
      \arg        CEC_INT_TEND: disable transmission successfully end interrupt
      \arg        CEC_INT_TU: disable Tx data buffer underrun interrupt
      \arg        CEC_INT_TERR: disable Tx-error interrupt 
      \arg        CEC_INT_TAERR: disable Tx ACK error  interrupt

    \param[out] none
    \retval     none
*/
void cec_interrupt_disable(uint32_t flag)
{
    CEC_INTEN &= ~flag;
}


/*!
    \brief      get CEC status
    \param[in]  flag:  specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_FLAG_BR: Rx-byte data received
      \arg        CEC_FLAG_REND: end of reception
      \arg        CEC_FLAG_RO: RX overrun
      \arg        CEC_FLAG_BRE: bit rising error
      \arg        CEC_FLAG_BPSE: short bit period error
      \arg        CEC_FLAG_BPLE: long bit period error
      \arg        CEC_FLAG_RAE: Rx ACK error
      \arg        CEC_FLAG_ARBF: arbitration lost
      \arg        CEC_FLAG_TBR: Tx-byte data request
      \arg        CEC_FLAG_TEND: transmission successfully end
      \arg        CEC_FLAG_TU: Tx data buffer underrun
      \arg        CEC_FLAG_TERR: Tx-error
      \arg        CEC_FLAG_TAERR Tx ACK error flag
    \param[out] none
    \retval     FlagStatus: SET or RESET
*/
FlagStatus cec_flag_get(uint32_t flag)
{
    if(CEC_INTF & flag){
        return SET;
    }else{
        return RESET;
    }
}

/*!
    \brief      clear CEC status
    \param[in]  flag:  specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_FLAG_BR: Rx-byte data received
      \arg        CEC_FLAG_REND: end of reception
      \arg        CEC_FLAG_RO: RX overrun
      \arg        CEC_FLAG_BRE: bit rising error
      \arg        CEC_FLAG_BPSE: short bit period error
      \arg        CEC_FLAG_BPLE: long bit period error
      \arg        CEC_FLAG_RAE: Rx ACK error
      \arg        CEC_FLAG_ARBF: arbitration lost
      \arg        CEC_FLAG_TBR: Tx-byte data request
      \arg        CEC_FLAG_TEND: transmission successfully end
      \arg        CEC_FLAG_TU: Tx data buffer underrun
      \arg        CEC_FLAG_TERR: Tx-error
      \arg        CEC_FLAG_TAERR: Tx ACK error flag

    \param[out] none
    \retval     FlagStatus: SET or RESET
*/
void cec_flag_clear(uint32_t flag)
{
    CEC_INTF |= flag;
}

/*!
    \brief      get CEC int flag and status
    \param[in]  flag:  specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_INT_FLAG_BR: Rx-byte data received
      \arg        CEC_INT_FLAG_REND: end of reception
      \arg        CEC_INT_FLAG_RO: RX overrun
      \arg        CEC_INT_FLAG_BRE: bit rising error
      \arg        CEC_INT_FLAG_BPSE: short bit period error
      \arg        CEC_INT_FLAG_BPLE: long bit period error
      \arg        CEC_INT_FLAG_RAE: Rx ACK error
      \arg        CEC_INT_FLAG_ARBF: arbitration lost
      \arg        CEC_INT_FLAG_TBR: Tx-byte data request
      \arg        CEC_INT_FLAG_TEND: transmission successfully end
      \arg        CEC_INT_FLAG_TU: Tx data buffer underrun
      \arg        CEC_INT_FLAG_TERR: Tx-error
      \arg        CEC_INT_FLAG_TAERR: Tx ACK error flag
    \param[out] none
    \retval     FlagStatus: SET or RESET
*/
FlagStatus cec_interrupt_flag_get(uint32_t flag)
{
    uint32_t interrupt_enable = 0U,interrupt_flag = 0U;
    interrupt_flag = (CEC_INTF & flag);
    interrupt_enable = (CEC_INTEN & flag);
    if(interrupt_flag && interrupt_enable){
        return SET;
    }else{
        return RESET;
    }
}

/*!
    \brief      clear CEC int flag and status
    \param[in]  flag:  specify which flag
                one or more parameters can be selected which are shown as below:
      \arg        CEC_INT_FLAG_BR: Rx-byte data received
      \arg        CEC_INT_FLAG_REND: end of reception
      \arg        CEC_INT_FLAG_RO: RX overrun
      \arg        CEC_INT_FLAG_BRE: bit rising error
      \arg        CEC_INT_FLAG_BPSE: short bit period error
      \arg        CEC_INT_FLAG_BPLE: long bit period error
      \arg        CEC_INT_FLAG_RAE: Rx ACK error
      \arg        CEC_INT_FLAG_ARBF: arbitration lost
      \arg        CEC_INT_FLAG_TBR: Tx-byte data request
      \arg        CEC_INT_FLAG_TEND: transmission successfully end
      \arg        CEC_INT_FLAG_TU: Tx data buffer underrun
      \arg        CEC_INT_FLAG_TERR: Tx-error
      \arg        CEC_INT_FLAG_TAERR: Tx ACK error flag
    \param[out] none
    \retval     none
*/
void cec_interrupt_flag_clear(uint32_t flag)
{
    CEC_INTF = flag;
}


#endif