1 /********************************** (C) COPYRIGHT *******************************
2 * File Name : ch32f20x_rng.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2021/08/08
6 * Description : This file provides all the RNG firmware functions.
7 ********************************************************************************/
8 #include "ch32f20x_rng.h"
9 #include "ch32f20x_rcc.h"
10
11 /*******************************************************************************
12 * Function Name : RNG_Cmd
13 * Description : Enables or disables the RNG peripheral.
14 * Input : NewState: ENABLE or DISABLE.
15 * Return : None
16 *******************************************************************************/
RNG_Cmd(FunctionalState NewState)17 void RNG_Cmd(FunctionalState NewState)
18 {
19 if (NewState != DISABLE)
20 {
21 RNG->CR |= RNG_CR_RNGEN;
22 }
23 else
24 {
25 RNG->CR &= ~RNG_CR_RNGEN;
26 }
27 }
28
29 /*******************************************************************************
30 * Function Name : RNG_GetRandomNumber
31 * Description : Returns a 32-bit random number.
32 * Input : None
33 * Return : 32-bit random number.
34 *******************************************************************************/
RNG_GetRandomNumber(void)35 uint32_t RNG_GetRandomNumber(void)
36 {
37 return RNG->DR;
38 }
39
40 /*******************************************************************************
41 * Function Name : RNG_ITConfig
42 * Description : Enables or disables the RNG interrupt.
43 * Input : NewState: ENABLE or DISABLE.
44 * Return : None
45 *******************************************************************************/
RNG_ITConfig(FunctionalState NewState)46 void RNG_ITConfig(FunctionalState NewState)
47 {
48 if (NewState != DISABLE)
49 {
50 RNG->CR |= RNG_CR_IE;
51 }
52 else
53 {
54 RNG->CR &= ~RNG_CR_IE;
55 }
56 }
57
58 /*******************************************************************************
59 * Function Name : RNG_GetFlagStatus
60 * Description : Checks whether the specified RNG flag is set or not.
61 * Input : RNG_FLAG: specifies the RNG flag to check.
62 * RNG_FLAG_DRDY: Data Ready flag.
63 * RNG_FLAG_CECS: Clock Error Current flag.
64 * RNG_FLAG_SECS: Seed Error Current flag.
65 * Return : bitstatus��SET or RESET.
66 *******************************************************************************/
RNG_GetFlagStatus(uint8_t RNG_FLAG)67 FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG)
68 {
69 FlagStatus bitstatus = RESET;
70
71 if ((RNG->SR & RNG_FLAG) != (uint8_t)RESET)
72 {
73 bitstatus = SET;
74 }
75 else
76 {
77 bitstatus = RESET;
78 }
79
80 return bitstatus;
81 }
82
83 /*******************************************************************************
84 * Function Name : RNG_ClearFlag
85 * Description : Clears the RNG flags.
86 * Input : RNG_FLAG: specifies the flag to clear.
87 * RNG_FLAG_CECS: Clock Error Current flag.
88 * RNG_FLAG_SECS: Seed Error Current flag.
89 * Return : None
90 *******************************************************************************/
RNG_ClearFlag(uint8_t RNG_FLAG)91 void RNG_ClearFlag(uint8_t RNG_FLAG)
92 {
93 RNG->SR = ~(uint32_t)(((uint32_t)RNG_FLAG) << 4);
94 }
95
96 /*******************************************************************************
97 * Function Name : RNG_GetFlagStatus
98 * Description : Checks whether the specified RNG interrupt has occurred or not.
99 * Input : RNG_IT: specifies the RNG interrupt source to check.
100 * RNG_IT_CEI: Clock Error Interrupt.
101 * RNG_IT_SEI: Seed Error Interrupt.
102 * Return : bitstatus��SET or RESET.
103 *******************************************************************************/
RNG_GetITStatus(uint8_t RNG_IT)104 ITStatus RNG_GetITStatus(uint8_t RNG_IT)
105 {
106 ITStatus bitstatus = RESET;
107
108 if ((RNG->SR & RNG_IT) != (uint8_t)RESET)
109 {
110 bitstatus = SET;
111 }
112 else
113 {
114 bitstatus = RESET;
115 }
116
117 return bitstatus;
118 }
119
120 /*******************************************************************************
121 * Function Name : RNG_ClearITPendingBit
122 * Description : Clears the RNG interrupt pending bit(s).
123 * Input : RNG_IT: specifies the RNG interrupt pending bit(s) to clear.
124 * RNG_IT_CEI: Clock Error Interrupt.
125 * RNG_IT_SEI: Seed Error Interrupt.
126 * Return : None
127 *******************************************************************************/
RNG_ClearITPendingBit(uint8_t RNG_IT)128 void RNG_ClearITPendingBit(uint8_t RNG_IT)
129 {
130 RNG->SR = (uint8_t)~RNG_IT;
131 }
132
133