01. RNG概述
& y2 x; j' V8 i2 g6 Q, q/ \RNG 处理器是一个以连续模拟噪声为基础的随机数发生器,在主机读数时提供一个 32 位的随机数。6 p) B% O9 F0 c N" O. d
- w7 J; h) k" D5 c8 ~, [4 l+ e5 B
RNG 已通过 FIPS PUB 140-2(2001 年 10 月 10 日)测试,成功率达 99%。
) C8 C7 e# t6 J* A# }* w% F( s' n6 R/ g4 M& u6 c' H
随机数发生器支持库函数stm32f4xx_rng.c 和对应的头文件 stm32f4xx_rng.h。
0 r% V |# q9 X: {. U( S+ m0 z* y4 p# |. Y( N2 r0 y* E% V
02. 相关类型
. U8 X7 A/ s$ V- M- /** @defgroup RNG_flags_definition & t; R/ Y. E6 O& B- u
- * @{5 \* K; z- V' F* q5 U. @
- */
! | c# L: T0 p0 a. y) F - #define RNG_FLAG_DRDY ((uint8_t)0x0001) /*!< Data ready */
# k: I8 }7 N1 c/ O# w - #define RNG_FLAG_CECS ((uint8_t)0x0002) /*!< Clock error current status */7 W4 M+ S1 M/ j2 {8 ~
- #define RNG_FLAG_SECS ((uint8_t)0x0004) /*!< Seed error current status */
6 U. s* s0 |9 m; F o" T - # Y+ q. I5 b! n: t7 C2 b# F! J+ l% F
- #define IS_RNG_GET_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_DRDY) || \$ R3 \5 A8 l% r+ @' ?
- ((RNG_FLAG) == RNG_FLAG_CECS) || \
; L7 P) g( d# N - ((RNG_FLAG) == RNG_FLAG_SECS))1 H* j. t4 o; x3 [
- #define IS_RNG_CLEAR_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_CECS) || \
- o) ~, o& X" J [- u, ? - ((RNG_FLAG) == RNG_FLAG_SECS))
6 ] @/ l, d; ?# n8 u* G) U - /**
3 a9 h5 f( P8 b& J6 b - * @}7 p- f% w9 B& z" C$ ^5 Q
- */
+ ]1 j! u( [5 a" O+ n* M - " O' x: |) y' ]( d& {
- /** @defgroup RNG_interrupts_definition
# H7 i$ [' W' _/ v4 H. x - * @{+ g4 V: W9 q! r
- */ 4 E$ S1 ~" F0 x' X( i% I+ f
- #define RNG_IT_CEI ((uint8_t)0x20) /*!< Clock error interrupt *// b X: _6 P4 Q9 l
- #define RNG_IT_SEI ((uint8_t)0x40) /*!< Seed error interrupt */
6 c5 K; g+ M h* H6 \7 W2 z: d
2 _9 `/ g1 E7 L- #define IS_RNG_IT(IT) ((((IT) & (uint8_t)0x9F) == 0x00) && ((IT) != 0x00))
+ c+ j- q1 W, l3 z0 D) q8 Q - #define IS_RNG_GET_IT(RNG_IT) (((RNG_IT) == RNG_IT_CEI) || ((RNG_IT) == RNG_IT_SEI))! \# [2 I: V, t, O3 V/ _
复制代码 8 D" j U/ z# V6 S( z- e
03. 相关函数
/ J, d& P5 r- M* ]$ ~3 h8 q- /* Function used to set the RNG configuration to the default reset state *****/
. B9 G/ a5 j5 N+ ] - void RNG_DeInit(void);
& a2 l; R! u6 F/ A8 B8 @+ | - 4 i1 c$ m* {. z* n9 Z7 N* u" {
- /* Configuration function *****************************************************/
% C( y3 K/ l! V% {6 q3 s - void RNG_Cmd(FunctionalState NewState);
8 v4 q3 r) i1 B9 |" ~* B/ m9 w; r - # x9 S' K( C, ~9 P
- /* Get 32 bit Random number function ******************************************/
- Z6 A9 K0 a3 h7 i0 G - uint32_t RNG_GetRandomNumber(void);4 Z. u2 Q$ O2 w% V
- 8 R. f, ~/ Q% e$ }3 `
- /* Interrupts and flags management functions **********************************/
( n6 j; k7 c. n - void RNG_ITConfig(FunctionalState NewState);! t! n& g' I0 m! Q2 y( D- a3 a5 R+ Z
- FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG);
8 e$ h0 Z7 ?* e c/ J$ G& j - void RNG_ClearFlag(uint8_t RNG_FLAG);( B4 s0 L o7 ~+ G+ M/ I; H+ j
- ITStatus RNG_GetITStatus(uint8_t RNG_IT);
/ _1 m i+ ?/ j V9 p - void RNG_ClearITPendingBit(uint8_t RNG_IT);+ f! i; I3 N1 v# L, `1 g
复制代码 ; q/ r& _% B' w! a6 O1 \" G8 b
04. 结构体封装* p2 G/ ]4 m% [* q
- /** . u" l" h; A( f& E) e1 b4 c
- * @brief RNG
1 s9 v) w! e& u5 |) M/ F0 o - */
0 `. B) S* u1 _4 C9 s - 7 V/ s' O+ J- H& L& g9 a# P
- typedef struct 3 U' X+ v0 E h' N: U( q) A
- {3 A% c' @, d8 `+ h" j/ G
- __IO uint32_t CR; /*!< RNG control register, Address offset: 0x00 */
6 s( ]9 G* D3 M0 e - __IO uint32_t SR; /*!< RNG status register, Address offset: 0x04 */5 F2 z2 j: Q0 N3 X
- __IO uint32_t DR; /*!< RNG data register, Address offset: 0x08 */" ]' X( |" r% d; `) s% y2 `
- } RNG_TypeDef;
复制代码
" M% m8 @+ n) D7 o7 G3 C& h5 H+ n4 V$ \. E! j" c/ d' O
k* I$ \9 b% \$ e2 H$ m2 Q0 i7 M
|