ESOS32
ESOSOn32-bitProcessors
esos_stm32l4_utils.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
28 // Documentation for this file. If the \file tag isn't present,
29 // this file won't be documented.
35 // Include any HW-specific header files to pick up the HW register
36 // definitions, macros, etc.
37 #include "esos_stm32l4.h"
38 #include "esos_stm32l4_utils.h"
39 
40 // VARIABLES
41 #ifdef STM32L452xx
42 RNG_HandleTypeDef hrng;
43 #endif
44 
45 /***************************************************
46  *
47  * HARDWARE-specific ESOS utility functions
48  *
49  * *************************************************/
50 
51 /* ******************************************************
52 * \brief HW pseudo-random number generation...
53 *
54 * \note All hardware must implement this function.
55 *
56 * If the underlying hardware has PRNG on-board, it can
57 * implement it here and return the uint32 "random" number.
58 *
59 * If the underlying HW does not have PRNG on-board, it
60 * can call the hidden ESOS function for software RNG
61 * uint32 __esos_get_PRNG_RandomUint32(void)
62 * and return that value.
63 *
64 ********************************************************/
65 uint32_t __esos_hw_PRNG_u32(void) {
66 #ifdef STM32L452xx
67  uint32_t u32_randomNum;
68 
69  #if 1
70  HAL_RNG_GenerateRandomNumber( &hrng, &u32_randomNum);
71  #else
72  // not sure why this doesn't work...... should be
73  // faster than above as we will read last RNG and then
74  // kick off the RNG hardware to make a new RNG before
75  // our next read. Need to study HAL more to figure this out.
76  u32_randomNum = HAL_RNG_ReadLastRandomNumber( &hrng );
77  HAL_RNG_GenerateRandomNumber_IT( &hrng );
78  #endif
79  return u32_randomNum;
80 #else
81  return ( __esos_get_PRNG_RandomUint32() );
82 #endif
83 } // end __esos_hw_PRNG_u32(void)
84 
85 
86 /* *******************************************************
87 * \brief user-provided function to config the HW RNG
88 *
89 * \note All hardware must implement this function.
90 *
91 * If the underlying hardware has PRNG on-board, HW-specific code
92 * for intializing the PRNG hardware goes here.
93 *
94 * If the underlying HW does not have PRNG on-board, this
95 * function should just return.
96 *
97 ******************************************************* */
98 void __esos_hw_config_PRNG(void) {
99 #ifdef STM32L452xx
100  hrng.Instance = RNG;
101  if (HAL_RNG_Init(&hrng) != HAL_OK)
102  {
103  Error_Handler();
104  }
105 #endif
106 } // end __esos_hw_config_PRNG(void)
107 
108 /* *******************************************************
109 * \brief seed the HW pseudo-random number generator...
110 *
111 * \note All hardware must implement this function.
112 *
113 * If the underlying hardware has PRNG on-board, it can
114 * do what it needs to "seed" the PRNG.
115 *
116 * If the underlying HW does not have PRNG on-board, it
117 * can call the hidden ESOS function for seeding the
118 * ESOS software RNG function
119 * void __esos_setSWRNGU32Seed(uint32)
120 *
121 * \sa __esos_get_PRNG_RandomUint32
122 *
123 ********************************************************** */
124 void __esos_hw_set_PRNG_Seed(uint32_t u32_seed) {
125 #ifdef STM32L452xx
126  // RNG hardware doesn't let us set the seed
127 #else
128  __esos_set_PRNG_U32Seed(u32_seed);
129 #endif
130 
131 } // end __esos_hw_setPRNG_Seed(uint32)
__esos_set_PRNG_U32Seed
void __esos_set_PRNG_U32Seed(uint32_t u32_seed)
Definition: esos_utils.c:70
__esos_get_PRNG_RandomUint32
uint32_t __esos_get_PRNG_RandomUint32(void)
Definition: esos_utils.c:80
Error_Handler
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition: esos_stm32l4_tick.c:170