1
2 /**
3 * @file xmc_prng.c
4 * @date 2015-06-20
5 *
6 * @cond
7 *********************************************************************************************************************
8 * XMClib v2.1.24 - XMC Peripheral Driver Library
9 *
10 * Copyright (c) 2015-2019, Infineon Technologies AG
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
14 * following conditions are met:
15 *
16 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided with the distribution.
21 *
22 * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
34 * Infineon Technologies AG dave@infineon.com).
35 *********************************************************************************************************************
36 *
37 * Change History
38 * --------------
39 *
40 * 2015-02-20:
41 * - Initial <br>
42 * - Removed GetDriverVersion API <br>
43 *
44 * 2015-06-20
45 * - Removed definition of GetDriverVersion API <br>
46 *
47 * @endcond
48 */
49
50 #include "xmc_prng.h"
51
52 #if defined (PRNG)
53
54 /*********************************************************************************************************************
55 * API IMPLEMENTATION
56 *********************************************************************************************************************/
57
58 /*
59 * Initializes the PRNG peripheral with the settings in the
60 * initialization structure XMC_PRNG_INIT_t
61 */
XMC_PRNG_Init(const XMC_PRNG_INIT_t * prng)62 XMC_PRNG_INIT_STATUS_t XMC_PRNG_Init(const XMC_PRNG_INIT_t *prng)
63 {
64 volatile uint16_t read_warm_up;
65 uint16_t reg_val, iter;
66 XMC_PRNG_INIT_STATUS_t status = XMC_PRNG_INITIALIZED;
67
68 XMC_ASSERT("XMC_PRNG_Init:Null Pointer", (prng != (XMC_PRNG_INIT_t *)NULL));
69
70 /* Configure block size for key loading mode */
71 XMC_PRNG_SetRandomDataBlockSize(XMC_PRNG_RDBS_WORD);
72
73 /* Enable key loading mode */
74 XMC_PRNG_EnableKeyLoadingMode();
75
76 /* Load key words (80 bits) and wait till RDV is set */
77 for (iter = (uint16_t)0UL; iter < (uint16_t)5UL; iter++)
78 {
79 XMC_PRNG_LoadKeyWords(prng->key_words[iter]);
80 while (PRNG_CHK_RDV_Msk != XMC_PRNG_CheckValidStatus());
81 }
82
83 XMC_PRNG_EnableStreamingMode();
84
85 /* Warm up phase: Read and discard 64 bits */
86 read_warm_up = PRNG->WORD;
87 read_warm_up = PRNG->WORD;
88 read_warm_up = PRNG->WORD;
89 reg_val = PRNG->WORD;
90 read_warm_up &= reg_val;
91
92 /* Configure block size either byte (8 bit) or word (16 bit) */
93 XMC_PRNG_SetRandomDataBlockSize(prng->block_size);
94
95 /*
96 * Checks for reset value for "random data block size". If reset,
97 * PRNG is not initialized
98 */
99 if ((uint16_t)XMC_PRNG_RDBS_RESET == (PRNG->CTRL & (uint16_t)PRNG_CTRL_RDBS_Msk))
100 {
101 status = XMC_PRNG_NOT_INITIALIZED;
102 }
103
104 return status;
105 }
106
107 #endif /* #if defined (PRNG) */
108