1 /*!
2  * @file        apm32f4xx_rng.c
3  *
4  * @brief       This file provides all the RNG firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-06-23
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be usefull and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 #include "apm32f4xx_rng.h"
27 #include "apm32f4xx_rcm.h"
28 
29 /** @addtogroup APM32F4xx_StdPeriphDriver
30   @{
31 */
32 
33 /** @defgroup RNG_Driver
34   * @brief RNG driver modules
35   @{
36 */
37 
38 /** @defgroup RNG_Functions
39   @{
40 */
41 
42 /*!
43  * @brief     Reset RNG peripheral registers to their default reset values.
44  *
45  * @param     None
46  *
47  * @retval    None
48  */
RNG_Reset(void)49 void RNG_Reset(void)
50 {
51     RCM_EnableAHB2PeriphReset(RCM_AHB2_PERIPH_RNG);
52     RCM_DisableAHB2PeriphReset(RCM_AHB2_PERIPH_RNG);
53 }
54 
55 /*!
56  * @brief     Enable the RNG peripheral.
57  *
58  * @param     None
59  *
60  * @retval    None
61  */
RNG_Enable(void)62 void RNG_Enable(void)
63 {
64     RNG->CTRL_B.RNGEN = BIT_SET;
65 }
66 
67 /*!
68  * @brief     Disable the RNG peripheral.
69  *
70  * @param     None
71  *
72  * @retval    None
73  */
RNG_Disable(void)74 void RNG_Disable(void)
75 {
76     RNG->CTRL_B.RNGEN = BIT_RESET;
77 }
78 
79 /*!
80  * @brief     Read the 32-bit random number.
81  *
82  * @param     None
83  *
84  * @retval    a 32-bit random number.
85  */
RNG_ReadRandomNumber(void)86 uint32_t RNG_ReadRandomNumber(void)
87 {
88     return RNG->DATA;
89 }
90 
91 /*!
92  * @brief     Enable the RNG interrupt.
93  *
94  * @param     None
95  *
96  * @retval    None
97  */
EnableInterrupt(void)98 void EnableInterrupt(void)
99 {
100     RNG->CTRL_B.INTEN = BIT_SET;
101 }
102 
103 /*!
104  * @brief     Disable the RNG interrupt.
105  *
106  * @param     None
107  *
108  * @retval    None
109  */
DisableInterrupt(void)110 void DisableInterrupt(void)
111 {
112     RNG->CTRL_B.INTEN = BIT_RESET;
113 }
114 
115 /*!
116  * @brief     Read the specified RNG flag.
117  *
118  * @param     flag: specifies the RNG flag to check.
119  *                  This parameter can be one of the following values:
120  *                  @arg RNG_FLAG_DATARDY  : Data Ready flag.
121  *                  @arg RNG_FLAG_CLKERCSTS: RNG clock error flag.
122  *                  @arg RNG_FLAG_FSCSTS   : Faulty sequence flag.
123  *
124  * @retval    SET or RESET
125  */
RNG_ReadStatusFlag(RNG_FLAG_T flag)126 uint8_t RNG_ReadStatusFlag(RNG_FLAG_T flag)
127 {
128     if ((RNG->STS & flag) != RESET)
129     {
130         return SET;
131     }
132     else
133     {
134         return RESET;
135     }
136 }
137 
138 /*!
139  * @brief     Clears the RNG flags.
140  *
141  * @param     flag: specifies the flag to clear.
142  *                  This parameter can be any combination of the following values:
143  *                  @arg RNG_FLAG_CLKERCSTS: RNG clock error flag.
144  *                  @arg RNG_FLAG_FSCSTS   : Faulty sequence flag.
145  *
146  * @note      RNG_FLAG_DATARDY can not be cleared only by reading the Random data
147  *            (using RNG_ReadRandomNumber() function).
148  *
149  * @retval    None
150  */
RNG_ClearStatusFlag(uint8_t flag)151 void RNG_ClearStatusFlag(uint8_t flag)
152 {
153     RNG->STS = ~(uint32_t)(((uint32_t)flag) << 4);
154 }
155 
156 /*!
157  * @brief     Read the specified RNG interrupt flag.
158  *
159  * @param     flag: check status of specifies the RNG interrupt source.
160  *                  This parameter can be one of the following values:
161  *                  @arg RNG_INT_FLAG_CLKERINT: RNGCLK Error Interrupt.
162  *                  @arg RNG_INT_FLAG_FSINT   : Faulty Sequence Interrupt.
163  *
164  * @retval    SET or RESET
165  */
RNG_ReadIntFlag(RNG_INT_FLAG_T flag)166 uint8_t RNG_ReadIntFlag(RNG_INT_FLAG_T flag)
167 {
168     if ((RNG->STS & flag) != RESET)
169     {
170         return SET;
171     }
172     else
173     {
174         return RESET;
175     }
176 }
177 
178 /*!
179  * @brief     Clears the RNG interrupt flags.
180  *
181  * @param     flag: clear flag specifies the RNG interrupt flag.
182  *                  This parameter can be any combination of the following values:
183  *                  @arg RNG_INT_FLAG_CLKERINT: RNGCLK Error Interrupt flag.
184  *                  @arg RNG_INT_FLAG_FSINT   : Faulty Sequence Interrupt flag.
185  *
186  * @retval    None
187  */
RNG_ClearIntFlag(uint8_t flag)188 void RNG_ClearIntFlag(uint8_t flag)
189 {
190     RNG->STS = (uint8_t)~flag;
191 }
192 
193 /**@} end of group RNG_Functions */
194 /**@} end of group RNG_Driver */
195 /**@} end of group APM32F4xx_StdPeriphDriver */
196