01. RNG概述; L$ E7 E4 X- l8 |. O6 V; C
RNG 处理器是一个以连续模拟噪声为基础的随机数发生器,在主机读数时提供一个 32 位的随机数。& d- h+ e$ @7 l ^
9 |* B/ i" d; a3 p% y* ]/ R; KRNG 已通过 FIPS PUB 140-2(2001 年 10 月 10 日)测试,成功率达 99%。
1 s7 g9 H: x8 h" [
2 j1 L: x) t: Z% o& {随机数发生器支持库函数stm32f4xx_rng.c 和对应的头文件 stm32f4xx_rng.h。
; q' Z. G6 T* k# E# G' h
8 ~, i: e! W1 H# g# f02. 相关类型
7 a& O. S! V8 C# V& S- /** @defgroup RNG_flags_definition # U! [+ F E3 H( ^4 I0 W
- * @{. R( U9 B8 c" C. w( D# p
- */ ; W$ A! `$ Z8 f, [( N$ f4 E+ M8 d
- #define RNG_FLAG_DRDY ((uint8_t)0x0001) /*!< Data ready */
- ^ c: t$ H8 f8 m, s& I! M3 T - #define RNG_FLAG_CECS ((uint8_t)0x0002) /*!< Clock error current status */8 }! E! O6 j4 X) t
- #define RNG_FLAG_SECS ((uint8_t)0x0004) /*!< Seed error current status */
/ C" W# k2 `" z% A0 H4 M - / k4 h J3 C$ Z1 ~
- #define IS_RNG_GET_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_DRDY) || \
. D, }" M1 b, i; Y* @ - ((RNG_FLAG) == RNG_FLAG_CECS) || \
# M/ E4 z1 D9 e# o. T - ((RNG_FLAG) == RNG_FLAG_SECS))
! k" S9 h- X; D! v" S; v - #define IS_RNG_CLEAR_FLAG(RNG_FLAG) (((RNG_FLAG) == RNG_FLAG_CECS) || \! g4 k% G2 z8 Y& H1 [
- ((RNG_FLAG) == RNG_FLAG_SECS))
: {- t2 n& I, |1 ^5 i& U - /**3 i9 }' s. m& E* {
- * @}
; u3 V4 g% T9 Q9 E1 R' f - */ 9 c7 x# e; g: @- ]0 n+ O7 G+ F
. O4 F- n' z, j! J9 D. B- /** @defgroup RNG_interrupts_definition
4 ^# A8 p! l- C; g$ }+ j& i - * @{
9 J8 ?& \/ ^5 b5 K1 C9 e3 _ - */ 0 e0 q" p3 G- a0 b, Q
- #define RNG_IT_CEI ((uint8_t)0x20) /*!< Clock error interrupt */1 }- c9 Z6 g1 K& T
- #define RNG_IT_SEI ((uint8_t)0x40) /*!< Seed error interrupt */
$ k7 j I+ x+ r8 h0 Q0 k - : G: P3 ], v' D9 w4 U t
- #define IS_RNG_IT(IT) ((((IT) & (uint8_t)0x9F) == 0x00) && ((IT) != 0x00))
2 G( Q# J# `5 } g3 B0 ~ - #define IS_RNG_GET_IT(RNG_IT) (((RNG_IT) == RNG_IT_CEI) || ((RNG_IT) == RNG_IT_SEI))
9 |* ^9 T; C: i, _9 d+ R, V
复制代码 Y! C2 e5 i3 P: N# I
03. 相关函数
/ t2 S: G4 ]! |3 D" w3 f' d( M) D- /* Function used to set the RNG configuration to the default reset state *****/ ! F8 W' e! S1 R& B" N
- void RNG_DeInit(void);
" L$ P( A7 {* A1 U' \- g1 m1 Y7 ~) a; R
) ^6 b% i0 T$ U* w- y- /* Configuration function *****************************************************/
1 S* i6 f- p7 k - void RNG_Cmd(FunctionalState NewState);
+ v. |. U/ J/ f; ~3 e1 r; b - # T0 y2 o" W! ]7 a! S/ Y7 P
- /* Get 32 bit Random number function ******************************************/! n* r- o, F. _! `& d; B0 |, C
- uint32_t RNG_GetRandomNumber(void);
# T7 I. }% ?$ G2 `
% z9 X9 s( r* Q$ _3 x- /* Interrupts and flags management functions **********************************/' ~( i9 X! u7 U, W
- void RNG_ITConfig(FunctionalState NewState);; s! _0 v. m* }) {# l# M# O9 R
- FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG);
# {: ]5 c/ @' D% \ S' d% H - void RNG_ClearFlag(uint8_t RNG_FLAG);2 P# u2 Z6 v9 [0 J0 o( ~8 ]
- ITStatus RNG_GetITStatus(uint8_t RNG_IT);* u0 o- c6 B2 ]! Q
- void RNG_ClearITPendingBit(uint8_t RNG_IT);
& X2 k2 k4 f; ]4 g
复制代码 / o3 R7 P& a8 `- g2 F: Y8 v* j$ }
04. 结构体封装
# G4 F) \2 `. u* `- r6 W- /** y4 K4 |& I# c# D% f2 m- ^4 @; H* F
- * @brief RNG7 E0 O) R/ y( i. `
- */
- q2 y4 f+ C$ Q
& p4 t) U& ~0 }& y- typedef struct 6 ?# ^& X# [ I$ V( t0 |
- {' r; L+ w' O r% T" Z
- __IO uint32_t CR; /*!< RNG control register, Address offset: 0x00 */
~+ Z0 n$ V+ V$ R - __IO uint32_t SR; /*!< RNG status register, Address offset: 0x04 */3 f' d* v- J$ Y1 A
- __IO uint32_t DR; /*!< RNG data register, Address offset: 0x08 */
# B; M5 u# q, ]! s3 b& Q$ M - } RNG_TypeDef;
复制代码 / w9 k: [- b9 l! A* }
6 j$ F) ]2 K/ N3 O w9 W, }
/ ?1 L% b& O: d. ^7 c
|