• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #include "trng.h"
19 #include "compiler.h"
20 /**********************************************************************************************************************
21  *                                			  local constants                                                       *
22  *********************************************************************************************************************/
23 
24 /**********************************************************************************************************************
25  *                                           	local macro                                                        *
26  *********************************************************************************************************************/
27 
28 /**********************************************************************************************************************
29  *                                             local data type                                                     *
30  *********************************************************************************************************************/
31 
32 /**********************************************************************************************************************
33  *                                              global variable                                                       *
34  *********************************************************************************************************************/
35 
36 _attribute_data_retention_sec_ unsigned int g_rnd_m_w = 0;
37 _attribute_data_retention_sec_ unsigned int g_rnd_m_z = 0;
38 
39 /**********************************************************************************************************************
40  *                                              local variable                                                     *
41  *********************************************************************************************************************/
42 /**********************************************************************************************************************
43  *                                          local function prototype                                               *
44  *********************************************************************************************************************/
45 /**********************************************************************************************************************
46  *                                         global function implementation                                             *
47  *********************************************************************************************************************/
48 /**
49  * @brief     This function performs to get one random number.If chip in suspend TRNG module should be close.
50  *            else its current will be larger.
51  * @return    the value of one random number.
52  */
trng_init(void)53 void trng_init(void)
54 {
55     // TRNG module Reset clear
56     reg_rst2 |= FLD_RST2_TRNG;
57     // turn on TRNG clock
58     reg_clk_en2 |= FLD_CLK2_TRNG_EN;
59 
60     reg_trng_cr0 &= ~(FLD_TRNG_CR0_RBGEN);  // disable
61     reg_trng_rtcr = 0x00;                   // TCR_MSEL
62     reg_trng_cr0 |= (FLD_TRNG_CR0_RBGEN);   // enable
63 
64     while (!(reg_rbg_sr & FLD_RBG_SR_DRDY)) {
65     }
66 
67     g_rnd_m_w = reg_rbg_dr;  // get the random number
68     while (!(reg_rbg_sr & FLD_RBG_SR_DRDY)) {
69     }
70 
71     g_rnd_m_z = reg_rbg_dr;
72 
73     // Reset TRNG module
74     reg_rst2 &= (~FLD_RST2_TRNG);
75     // turn off TRNG module clock
76     reg_clk_en2 &= ~(FLD_CLK2_TRNG_EN);
77 
78     reg_trng_cr0 &=
79         ~(FLD_TRNG_CR0_RBGEN | FLD_TRNG_CR0_ROSEN0 | FLD_TRNG_CR0_ROSEN1 | FLD_TRNG_CR0_ROSEN2 | FLD_TRNG_CR0_ROSEN3);
80 }
81 
82 /**
83  * @brief     This function performs to get one random number.
84  * @return    the value of one random number.
85  */
trng_rand(void)86 _attribute_ram_code_sec_noinline_ unsigned int trng_rand(void)  // 16M clock, code in flash 23us, code in sram 4us
87 {
88     g_rnd_m_w = 18000 * (g_rnd_m_w & 0xffff) + (g_rnd_m_w >> 16);
89     g_rnd_m_z = 36969 * (g_rnd_m_z & 0xffff) + (g_rnd_m_z >> 16);
90     unsigned int result = (g_rnd_m_z << 16) + g_rnd_m_w;
91 
92     return (unsigned int)(result ^ stimer_get_tick());
93 }
94 /**********************************************************************************************************************
95   *                    						local function implementation                                             *
96   *********************************************************************************************************************/
97