1 /*!
2     \file    gd32l23x_trng.c
3     \brief   TRNG driver
4 
5     \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7 
8 /*
9     Copyright (c) 2021, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #include "gd32l23x_trng.h"
36 
37 /*!
38     \brief      deinitialize the TRNG
39     \param[in]  none
40     \param[out] none
41     \retval     none
42 */
trng_deinit(void)43 void trng_deinit(void)
44 {
45     rcu_periph_reset_enable(RCU_TRNGRST);
46     rcu_periph_reset_disable(RCU_TRNGRST);
47 }
48 
49 /*!
50     \brief      enable the TRNG interface
51     \param[in]  none
52     \param[out] none
53     \retval     none
54 */
trng_enable(void)55 void trng_enable(void)
56 {
57     TRNG_CTL |= (uint32_t)TRNG_CTL_TRNGEN;
58 }
59 
60 /*!
61     \brief      disable the TRNG interface
62     \param[in]  none
63     \param[out] none
64     \retval     none
65 */
trng_disable(void)66 void trng_disable(void)
67 {
68     TRNG_CTL &= ~(uint32_t)TRNG_CTL_TRNGEN;
69 }
70 
71 /*!
72     \brief      get the true random data
73     \param[in]  none
74     \param[out] none
75     \retval     the generated random data
76 */
trng_get_true_random_data(void)77 uint32_t trng_get_true_random_data(void)
78 {
79     return (TRNG_DATA);
80 }
81 
82 /*!
83     \brief      get the trng status flags
84     \param[in]  flag: trng status flag, refer to trng_flag_enum
85                 only one parameter can be selected which is shown as below:
86       \arg        TRNG_FLAG_DRDY: Random Data ready status
87       \arg        TRNG_FLAG_CECS: Clock error current status
88       \arg        TRNG_FLAG_SECS: Seed error current status
89     \param[out] none
90     \retval     FlagStatus: SET or RESET
91 */
trng_flag_get(trng_flag_enum flag)92 FlagStatus trng_flag_get(trng_flag_enum flag)
93 {
94     if(RESET != (TRNG_STAT & flag)) {
95         return SET;
96     } else {
97         return RESET;
98     }
99 }
100 
101 /*!
102     \brief      enable the TRNG interrupt
103     \param[in]  none
104     \param[out] none
105     \retval     none
106 */
trng_interrupt_enable(void)107 void trng_interrupt_enable(void)
108 {
109     TRNG_CTL |= (uint32_t)TRNG_CTL_IE;
110 }
111 
112 /*!
113     \brief      disable the TRNG interrupt
114     \param[in]  none
115     \param[out] none
116     \retval     none
117 */
trng_interrupt_disable(void)118 void trng_interrupt_disable(void)
119 {
120     TRNG_CTL &= ~(uint32_t)TRNG_CTL_IE;
121 }
122 
123 /*!
124     \brief      get the trng interrupt flags
125     \param[in]  int_flag: trng interrupt flag, refer to trng_int_flag_enum
126                 only one parameter can be selected which is shown as below:
127       \arg        TRNG_INT_FLAG_CEIF: clock error interrupt flag
128       \arg        TRNG_INT_FLAG_SEIF: Seed error interrupt flag
129     \param[out] none
130     \retval     FlagStatus: SET or RESET
131 */
trng_interrupt_flag_get(trng_int_flag_enum int_flag)132 FlagStatus trng_interrupt_flag_get(trng_int_flag_enum int_flag)
133 {
134     if(RESET != (TRNG_STAT & int_flag)) {
135         return SET;
136     } else {
137         return RESET;
138     }
139 }
140 
141 /*!
142     \brief      clear the trng interrupt flags
143     \param[in]  int_flag: trng interrupt flag, refer to trng_int_flag_enum
144                 only one parameter can be selected which is shown as below:
145       \arg        TRNG_INT_FLAG_CEIF: clock error interrupt flag
146       \arg        TRNG_INT_FLAG_SEIF: Seed error interrupt flag
147     \param[out] none
148     \retval     none
149 */
trng_interrupt_flag_clear(trng_int_flag_enum int_flag)150 void trng_interrupt_flag_clear(trng_int_flag_enum int_flag)
151 {
152     TRNG_STAT &= ~(uint32_t)int_flag;
153 }
154