01. RNG概述
: G3 K6 F+ N N c% WRNG 处理器是一个以连续模拟噪声为基础的随机数发生器,在主机读数时提供一个 32 位的随机数。3 ^# v. v$ Q) t$ U
6 n+ p) ?' }' t& |$ O% I, P
RNG 已通过 FIPS PUB 140-2(2001 年 10 月 10 日)测试,成功率达 99%。
/ Z9 m: y! Q3 I. L# \) P! }
/ o3 ^1 f1 F3 i L( u j6 P随机数发生器支持库函数stm32f4xx_rng.c 和对应的头文件 stm32f4xx_rng.h。" [$ T7 d# L7 ]$ A' J
5 y; [& [6 T( ~. _7 v1 f! ?# }
02. 相关类型/ y; ~; K/ l5 W2 E2 E
- /** @defgroup RNG_flags_definition 1 P4 V3 B# _; ^- \0 s8 m' }
- * @{, ?2 X6 N$ G7 ~0 g7 A
- */
) e- N! R4 e9 T- h% V; q - #define RNG_FLAG_DRDY ((uint8_t)0x0001) /*!< Data ready */ A! P; y, U; v: J
- #define RNG_FLAG_CECS ((uint8_t)0x0002) /*!< Clock error current status */2 D9 j* g4 J+ I+ `3 c$ B! F
- #define RNG_FLAG_SECS ((uint8_t)0x0004) /*!< Seed error current status */; A* O7 V( O7 H# T* h( Z
- / K- x" ~3 ?5 {' ]2 ?
- #define IS_RNG_GET_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_DRDY) || \
, _& @# T) q6 d$ M - ((RNG_FLAG) == RNG_FLAG_CECS) || \; ~& u' a* S. x6 i
- ((RNG_FLAG) == RNG_FLAG_SECS)), v8 z7 ~# J! y0 F' c7 ]6 Z3 {' C
- #define IS_RNG_CLEAR_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_CECS) || \8 _+ X9 d6 O8 a& E W- j
- ((RNG_FLAG) == RNG_FLAG_SECS))8 k7 w: o( R | u- h6 ]9 Q! B
- /**
8 \& l g4 c3 |9 g9 C - * @} @* r' V" C1 W
- */ / d5 I+ ~# s3 I5 m
' p% F) Z* Q: i/ ]' G7 S* X1 y5 f- /** @defgroup RNG_interrupts_definition
# ~" F- D4 @1 v6 H - * @{
6 c; d% M( \/ Y9 i3 [ - */
: I2 J2 E8 Y; A' o - #define RNG_IT_CEI ((uint8_t)0x20) /*!< Clock error interrupt */6 w' z8 W. r, v* O3 F6 {
- #define RNG_IT_SEI ((uint8_t)0x40) /*!< Seed error interrupt */5 v( [- r' k2 ~& L/ k
- $ W1 _. B% k3 ^* U5 Y8 J; u( G2 w
- #define IS_RNG_IT(IT) ((((IT) & (uint8_t)0x9F) == 0x00) && ((IT) != 0x00))! P3 u( O" @9 X, v g6 ]
- #define IS_RNG_GET_IT(RNG_IT) (((RNG_IT) == RNG_IT_CEI) || ((RNG_IT) == RNG_IT_SEI))
复制代码 & A. d$ \* b& Y# H
03. 相关函数& u+ r! E. R$ E5 h, W
- /* Function used to set the RNG configuration to the default reset state *****/ 2 y/ p% @, c0 l
- void RNG_DeInit(void);, t0 v8 n" Y+ }/ b U; F# f
. m" r! C1 i* u- /* Configuration function *****************************************************/
# g+ f; ?$ S; F4 |0 A4 p - void RNG_Cmd(FunctionalState NewState);" j# k5 v$ ^7 {
4 y" s3 V: H1 f# z! `6 f- R- /* Get 32 bit Random number function ******************************************/ A& j& ~+ @ n% ~9 O
- uint32_t RNG_GetRandomNumber(void);3 a8 c$ I3 F# @2 _& u2 o
- `# x* Q" i2 x; l" G, s- /* Interrupts and flags management functions **********************************/, ?; }& W' n/ f* n- u
- void RNG_ITConfig(FunctionalState NewState);
2 y6 T! h2 L7 P5 ^. }4 F - FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG);! ^+ D$ Q1 l+ u4 F( x
- void RNG_ClearFlag(uint8_t RNG_FLAG);4 d; U5 b+ ^3 G+ l0 V
- ITStatus RNG_GetITStatus(uint8_t RNG_IT);
0 ~& q- L- Q9 y+ Z" o& w& E: T( G - void RNG_ClearITPendingBit(uint8_t RNG_IT);
复制代码 b; j% q/ u* u5 C: z' _1 ?
04. 结构体封装# l) ~ G: l7 [" S6 C) {
- /**
# r7 v; ]2 S) f) r - * @brief RNG
# q# O* \7 g6 s% G$ o - */1 M" \+ T5 t4 x, y2 G9 h/ _
- & g$ w+ ~2 p. y8 X
- typedef struct
1 D* k+ X" e& B# ]2 I - {
- C( `) n! B3 y4 z3 w - __IO uint32_t CR; /*!< RNG control register, Address offset: 0x00 */2 b7 ^8 U! `# h# r. _/ e
- __IO uint32_t SR; /*!< RNG status register, Address offset: 0x04 */- R8 H+ h" g1 o2 A5 W! S A" f
- __IO uint32_t DR; /*!< RNG data register, Address offset: 0x08 */
4 f0 ^. Q; k. S" _ - } RNG_TypeDef;
复制代码
% J1 h, S1 X }
7 v& u, A5 v9 o/ \6 }
t- n- R. s0 L9 e) e8 u |