01. RNG概述. N+ ^% @7 {- H+ `4 S
RNG 处理器是一个以连续模拟噪声为基础的随机数发生器,在主机读数时提供一个 32 位的随机数。! V5 n$ z& v; ` {( _% ~
3 ~& P# A* ?% e6 M" ~0 X. F0 C- U. b
RNG 已通过 FIPS PUB 140-2(2001 年 10 月 10 日)测试,成功率达 99%。
$ w! M2 Q$ h% P8 e/ Q m
7 F+ ?; K2 d4 k& {! C& ~& F/ } w( i随机数发生器支持库函数stm32f4xx_rng.c 和对应的头文件 stm32f4xx_rng.h。' ` B; i0 C( V& P* M
) Z9 v D$ e7 T7 g' }2 ^+ |
02. 相关类型# o" W# q$ }8 l/ m" B+ b4 R
- /** @defgroup RNG_flags_definition
+ q- G8 ?" N9 ] - * @{
9 C# ^+ J3 \. r# T - */
( c Z/ u) a2 Y' n - #define RNG_FLAG_DRDY ((uint8_t)0x0001) /*!< Data ready */
3 M* D/ u, `, V B h, z - #define RNG_FLAG_CECS ((uint8_t)0x0002) /*!< Clock error current status */1 W i/ K5 R6 A! x. a+ s8 J
- #define RNG_FLAG_SECS ((uint8_t)0x0004) /*!< Seed error current status */' D% ^2 ? J- W1 E4 L1 ?3 O- f
" f2 O9 J9 v8 K/ u- #define IS_RNG_GET_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_DRDY) || \
, [) f8 I3 F1 B# j+ J2 C' l - ((RNG_FLAG) == RNG_FLAG_CECS) || \
2 a6 N- |9 d, W7 T* x/ s6 B - ((RNG_FLAG) == RNG_FLAG_SECS))
9 U$ I e6 f A, s5 } - #define IS_RNG_CLEAR_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_CECS) || \
2 {( L' j9 A9 t - ((RNG_FLAG) == RNG_FLAG_SECS))' v) X/ z. k/ r* S
- /**. i+ p1 z- x U1 X3 O1 S7 `/ K' B
- * @}
& Y: _% p5 E2 m& T- h - */ * ?5 s. F7 V. R
- % c8 q, P7 U% i% z" v x6 H
- /** @defgroup RNG_interrupts_definition # O2 E+ f& s" P, h/ k; g7 I1 W3 g
- * @{2 K* ^! `. R4 b" `
- */
9 J4 B0 \2 m. _6 i" { - #define RNG_IT_CEI ((uint8_t)0x20) /*!< Clock error interrupt */
, t, p0 u. ^1 H - #define RNG_IT_SEI ((uint8_t)0x40) /*!< Seed error interrupt */
8 t0 U7 P9 T2 V6 J2 v! T8 F - ( f+ R( `. M' K7 H8 v. Z
- #define IS_RNG_IT(IT) ((((IT) & (uint8_t)0x9F) == 0x00) && ((IT) != 0x00))
$ r5 _- A& K7 @& [4 Y6 h; z6 P& a. ` - #define IS_RNG_GET_IT(RNG_IT) (((RNG_IT) == RNG_IT_CEI) || ((RNG_IT) == RNG_IT_SEI)). {* K' U; S" b& D
复制代码 ) k; ~2 h8 Q5 }+ C; Q
03. 相关函数
- C6 w, O6 g; E$ [. _) X- /* Function used to set the RNG configuration to the default reset state *****/
K! m/ E4 J; X* U. b+ ?2 H - void RNG_DeInit(void);
/ q% ~' m9 i1 |+ E5 `
1 ], L! z0 {: k( M3 r7 y. Z# H! A- /* Configuration function *****************************************************/
0 w: ]6 T3 z2 }1 q: u3 \' l - void RNG_Cmd(FunctionalState NewState);
6 C* `% ?5 z1 O/ T6 a - ; i Q6 t6 I9 J3 \$ y
- /* Get 32 bit Random number function ******************************************/* ?. A6 @ Q' k6 |" M4 P; r
- uint32_t RNG_GetRandomNumber(void);
( L. l+ U c5 [
2 U& G- h& h6 u$ R0 a9 N6 t; Z- F- /* Interrupts and flags management functions **********************************/ E3 r1 Q! e, I8 j/ V! p
- void RNG_ITConfig(FunctionalState NewState);$ F9 m( K$ b/ |
- FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG);1 W3 w# w( X. n- k4 a
- void RNG_ClearFlag(uint8_t RNG_FLAG);" k) Q- `- h& D2 N+ d& x* R. r5 [
- ITStatus RNG_GetITStatus(uint8_t RNG_IT);' ~* t1 x2 {: `! c. o. a
- void RNG_ClearITPendingBit(uint8_t RNG_IT);5 q# B/ ]# a/ x: F
复制代码 ' Y% l* z4 y! {% i2 o
04. 结构体封装
2 m7 N# O3 i$ a, [3 P# Q- /** ( N. m4 a) p( L( E
- * @brief RNG
& b4 b# E* a5 o5 V( ~+ E8 f - */7 h" v( e z: x$ q$ T* ~ o3 A/ G$ e
6 I5 {2 w4 ~# \, ~* g- typedef struct 4 J, ?; s7 c! @+ {; b' }
- {
; Y' z; a$ U4 T2 \( h/ a4 h+ m' `1 f - __IO uint32_t CR; /*!< RNG control register, Address offset: 0x00 */
; K- Z0 l2 I, W. A, u; c0 \ - __IO uint32_t SR; /*!< RNG status register, Address offset: 0x04 */( z' }! k' ?& }2 `* V0 u( C( ~ n
- __IO uint32_t DR; /*!< RNG data register, Address offset: 0x08 */1 c' R$ m+ M! r0 B$ B, z% Y9 h
- } RNG_TypeDef;
复制代码
; K& ~ e( Y0 f6 A, |& s3 s9 W8 `" ^: P2 |* {- i; [
: Z* r1 F. h6 V3 e# ^
|