summaryrefslogtreecommitdiff
path: root/bsp/apm32/libraries/APM32F4xx_Library/APM32F4xx_StdPeriphDriver/src/apm32f4xx_rng.c
blob: 370365b372a77fb40fc25d876dd3aba8a4290b76 (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
/*!
 * @file        apm32f4xx_rng.c
 *
 * @brief       This file provides all the RNG firmware functions
 *
 * @version     V1.0.2
 *
 * @date        2022-06-23
 *
 * @attention
 *
 *  Copyright (C) 2021-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be usefull and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

#include "apm32f4xx_rng.h"
#include "apm32f4xx_rcm.h"

/** @addtogroup APM32F4xx_StdPeriphDriver
  @{
*/

/** @defgroup RNG_Driver
  * @brief RNG driver modules
  @{
*/

/** @defgroup RNG_Functions
  @{
*/

/*!
 * @brief     Reset RNG peripheral registers to their default reset values.
 *
 * @param     None
 *
 * @retval    None
 */
void RNG_Reset(void)
{
    RCM_EnableAHB2PeriphReset(RCM_AHB2_PERIPH_RNG);
    RCM_DisableAHB2PeriphReset(RCM_AHB2_PERIPH_RNG);
}

/*!
 * @brief     Enable the RNG peripheral.
 *
 * @param     None
 *
 * @retval    None
 */
void RNG_Enable(void)
{
    RNG->CTRL_B.RNGEN = BIT_SET;
}

/*!
 * @brief     Disable the RNG peripheral.
 *
 * @param     None
 *
 * @retval    None
 */
void RNG_Disable(void)
{
    RNG->CTRL_B.RNGEN = BIT_RESET;
}

/*!
 * @brief     Read the 32-bit random number.
 *
 * @param     None
 *
 * @retval    a 32-bit random number.
 */
uint32_t RNG_ReadRandomNumber(void)
{
    return RNG->DATA;
}

/*!
 * @brief     Enable the RNG interrupt.
 *
 * @param     None
 *
 * @retval    None
 */
void EnableInterrupt(void)
{
    RNG->CTRL_B.INTEN = BIT_SET;
}

/*!
 * @brief     Disable the RNG interrupt.
 *
 * @param     None
 *
 * @retval    None
 */
void DisableInterrupt(void)
{
    RNG->CTRL_B.INTEN = BIT_RESET;
}

/*!
 * @brief     Read the specified RNG flag.
 *
 * @param     flag: specifies the RNG flag to check.
 *                  This parameter can be one of the following values:
 *                  @arg RNG_FLAG_DATARDY  : Data Ready flag.
 *                  @arg RNG_FLAG_CLKERCSTS: RNG clock error flag.
 *                  @arg RNG_FLAG_FSCSTS   : Faulty sequence flag.
 *
 * @retval    SET or RESET
 */
uint8_t RNG_ReadStatusFlag(RNG_FLAG_T flag)
{
    if ((RNG->STS & flag) != RESET)
    {
        return SET;
    }
    else
    {
        return RESET;
    }
}

/*!
 * @brief     Clears the RNG flags.
 *
 * @param     flag: specifies the flag to clear.
 *                  This parameter can be any combination of the following values:
 *                  @arg RNG_FLAG_CLKERCSTS: RNG clock error flag.
 *                  @arg RNG_FLAG_FSCSTS   : Faulty sequence flag.
 *
 * @note      RNG_FLAG_DATARDY can not be cleared only by reading the Random data
 *            (using RNG_ReadRandomNumber() function).
 *
 * @retval    None
 */
void RNG_ClearStatusFlag(uint8_t flag)
{
    RNG->STS = ~(uint32_t)(((uint32_t)flag) << 4);
}

/*!
 * @brief     Read the specified RNG interrupt flag.
 *
 * @param     flag: check status of specifies the RNG interrupt source.
 *                  This parameter can be one of the following values:
 *                  @arg RNG_INT_FLAG_CLKERINT: RNGCLK Error Interrupt.
 *                  @arg RNG_INT_FLAG_FSINT   : Faulty Sequence Interrupt.
 *
 * @retval    SET or RESET
 */
uint8_t RNG_ReadIntFlag(RNG_INT_FLAG_T flag)
{
    if ((RNG->STS & flag) != RESET)
    {
        return SET;
    }
    else
    {
        return RESET;
    }
}

/*!
 * @brief     Clears the RNG interrupt flags.
 *
 * @param     flag: clear flag specifies the RNG interrupt flag.
 *                  This parameter can be any combination of the following values:
 *                  @arg RNG_INT_FLAG_CLKERINT: RNGCLK Error Interrupt flag.
 *                  @arg RNG_INT_FLAG_FSINT   : Faulty Sequence Interrupt flag.
 *
 * @retval    None
 */
void RNG_ClearIntFlag(uint8_t flag)
{
    RNG->STS = (uint8_t)~flag;
}

/**@} end of group RNG_Functions */
/**@} end of group RNG_Driver */
/**@} end of group APM32F4xx_StdPeriphDriver */