]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/AtmelFiles/libchip_sama5d4x/source/trng.c
Core kernel files:
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / AtmelFiles / libchip_sama5d4x / source / trng.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2013, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /** \addtogroup rtng_module Working with RTNG\r
31  * \ingroup peripherals_module\r
32  * The TRNG driver provides the interface to configure and use the TRNG peripheral.\r
33  * \n\r
34  *\r
35  * The True Random Number Generator (TRNG) passes the American NIST Special Publication\r
36  * 800-22 and Diehard Random Tests Suites. As soon as the TRNG is enabled (TRNG_Enable()), \r
37  * the generator provides one 32-bit value every 84 clock cycles. \r
38  * Interrupt trng_int can be enabled through TRNG_EnableIt()(respectively disabled in TRNG_IDR).\r
39  * This interrupt is set when a new random value is available and is cleared when the status \r
40  * register is read (TRNG_SR register). The flag DATRDY of the status register (TRNG_ISR) is set\r
41  * when the random data is ready to be read out on the 32-bit output data through TRNG_GetRandData().\r
42  *\r
43  * For more accurate information, please look at the SHA section of the\r
44  * Datasheet.\r
45  *\r
46  * Related files :\n\r
47  * \ref trng.c\n\r
48  * \ref trng.h\n\r
49  */\r
50 /*@{*/\r
51 /*@}*/\r
52 \r
53 /**\r
54  * \file\r
55  *\r
56  * Implementation of True Random Number Generator (TRNG)\r
57  *\r
58  */\r
59 \r
60 \r
61 /*----------------------------------------------------------------------------\r
62  *        Headers\r
63  *----------------------------------------------------------------------------*/\r
64 \r
65 #include "chip.h"\r
66 \r
67 /*----------------------------------------------------------------------------\r
68  *        Exported functions\r
69  *----------------------------------------------------------------------------*/\r
70 \r
71 /**\r
72  * \brief Enables the TRNG to provide Random Values.\r
73  * \param key  This key is to be written when the ENABLE bit is set.\r
74  */\r
75 void TRNG_Enable(uint32_t key)\r
76 {\r
77     TRNG->TRNG_CR = TRNG_CR_ENABLE | TRNG_CR_KEY(key);\r
78 }\r
79 \r
80 /**\r
81  * \brief Disables the TRNG to provide Random Values.\r
82  * \param key  This key is to be written when the DISABLE bit is set.\r
83  */\r
84 void TRNG_Disable(uint32_t key)\r
85 {\r
86     TRNG->TRNG_CR = TRNG_CR_KEY(key);\r
87 }\r
88 \r
89 /**\r
90  * \brief Data Ready Interrupt enable.\r
91  */\r
92 void TRNG_EnableIt(void)\r
93 {\r
94     TRNG->TRNG_IER = TRNG_IER_DATRDY;\r
95 }\r
96 \r
97 /**\r
98  * \brief Data Ready Interrupt Disable.\r
99  */\r
100 void TRNG_DisableIt(void)\r
101 {\r
102     TRNG->TRNG_IDR = TRNG_IDR_DATRDY;\r
103 }\r
104 \r
105 /**\r
106  * \brief Get the current status register of the given TRNG peripheral.\r
107  * \return  TRNG status register.\r
108  */\r
109 uint32_t TRNG_GetStatus(void)\r
110 {\r
111     return TRNG->TRNG_ISR;\r
112 }\r
113 \r
114 /**\r
115  * \brief Get the  32-bit Output Data from TRNG peripheral.\r
116  * \return  TRNG output data.\r
117  */\r
118 uint32_t TRNG_GetRandData(void)\r
119 {\r
120     return TRNG->TRNG_ODATA;\r
121 }\r