1 /*!
2     \file    gd32f4xx_trng.h
3     \brief   definitions for the TRNG
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10 
11 /*
12     Copyright (c) 2022, GigaDevice Semiconductor Inc.
13 
14     Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16 
17     1. Redistributions of source code must retain the above copyright notice, this
18        list of conditions and the following disclaimer.
19     2. Redistributions in binary form must reproduce the above copyright notice,
20        this list of conditions and the following disclaimer in the documentation
21        and/or other materials provided with the distribution.
22     3. Neither the name of the copyright holder nor the names of its contributors
23        may be used to endorse or promote products derived from this software without
24        specific prior written permission.
25 
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37 
38 #ifndef GD32F4XX_TRNG_H
39 #define GD32F4XX_TRNG_H
40 
41 #include "gd32f4xx.h"
42 
43 /* TRNG definitions */
44 #define TRNG                        TRNG_BASE
45 
46 /* registers definitions */
47 #define TRNG_CTL                    REG32(TRNG + 0x00U)        /*!< control register */
48 #define TRNG_STAT                   REG32(TRNG + 0x04U)        /*!< status register */
49 #define TRNG_DATA                   REG32(TRNG + 0x08U)        /*!< data register */
50 
51 /* bits definitions */
52 /* TRNG_CTL */
53 #define TRNG_CTL_TRNGEN             BIT(2)                     /*!< TRNG enable bit */
54 #define TRNG_CTL_IE                 BIT(3)                     /*!< interrupt enable bit */
55 
56 /* TRNG_STAT */
57 #define TRNG_STAT_DRDY              BIT(0)                     /*!< random data ready status bit */
58 #define TRNG_STAT_CECS              BIT(1)                     /*!< clock error current status */
59 #define TRNG_STAT_SECS              BIT(2)                     /*!< seed error current status */
60 #define TRNG_STAT_CEIF              BIT(5)                     /*!< clock error interrupt flag */
61 #define TRNG_STAT_SEIF              BIT(6)                     /*!< seed error interrupt flag */
62 
63 /* TRNG_DATA */
64 #define TRNG_DATA_TRNDATA           BITS(0,31)                 /*!< 32-Bit Random data */
65 
66 /* constants definitions */
67 /* trng status flag */
68 typedef enum
69 {
70     TRNG_FLAG_DRDY = TRNG_STAT_DRDY,                           /*!< random Data ready status */
71     TRNG_FLAG_CECS = TRNG_STAT_CECS,                           /*!< clock error current status */
72     TRNG_FLAG_SECS = TRNG_STAT_SECS                            /*!< seed error current status */
73 }trng_flag_enum;
74 
75 /* trng inerrupt flag */
76 typedef enum
77 {
78     TRNG_INT_FLAG_CEIF = TRNG_STAT_CEIF,                       /*!< clock error interrupt flag */
79     TRNG_INT_FLAG_SEIF = TRNG_STAT_SEIF                        /*!< seed error interrupt flag */
80 }trng_int_flag_enum;
81 
82 /* function declarations */
83 /* initialization functions */
84 /* deinitialize the TRNG */
85 void trng_deinit(void);
86 /* enable the TRNG interface */
87 void trng_enable(void);
88 /* disable the TRNG interface */
89 void trng_disable(void);
90 /* get the true random data */
91 uint32_t trng_get_true_random_data(void);
92 
93 /* flag & interrupt functions */
94 /* trng interrupt enable */
95 void trng_interrupt_enable(void);
96 /* trng interrupt disable */
97 void trng_interrupt_disable(void);
98 /* get the trng status flags */
99 FlagStatus trng_flag_get(trng_flag_enum flag);
100 /* get the trng interrupt flags */
101 FlagStatus trng_interrupt_flag_get(trng_int_flag_enum int_flag);
102 /* clear the trng interrupt flags */
103 void trng_interrupt_flag_clear(trng_int_flag_enum int_flag);
104 
105 #endif /* GD32F4XX_TRNG_H */
106