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