CC26xx Driver Library
trng.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: trng.h
3 * Revised: 2016-06-30 09:21:03 +0200 (Thu, 30 Jun 2016)
4 * Revision: 46799
5 *
6 * Description: Defines and prototypes for the true random number gen.
7 *
8 * Copyright (c) 2015 - 2016, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * 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
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 //*****************************************************************************
40 //
45 //
46 //*****************************************************************************
47 
48 #ifndef __TRNG_H__
49 #define __TRNG_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include <inc/hw_types.h>
65 #include <inc/hw_trng.h>
66 #include <inc/hw_memmap.h>
67 #include <inc/hw_ints.h>
68 #include <driverlib/debug.h>
69 #include <driverlib/interrupt.h>
70 #include <driverlib/cpu.h>
71 
72 //*****************************************************************************
73 //
74 // Support for DriverLib in ROM:
75 // This section renames all functions that are not "static inline", so that
76 // calling these functions will default to implementation in flash. At the end
77 // of this file a second renaming will change the defaults to implementation in
78 // ROM for available functions.
79 //
80 // To force use of the implementation in flash, e.g. for debugging:
81 // - Globally: Define DRIVERLIB_NOROM at project level
82 // - Per function: Use prefix "NOROM_" when calling the function
83 //
84 //*****************************************************************************
85 #if !defined(DOXYGEN)
86  #define TRNGConfigure NOROM_TRNGConfigure
87  #define TRNGNumberGet NOROM_TRNGNumberGet
88 #endif
89 
90 //*****************************************************************************
91 //
92 //
93 //
94 //*****************************************************************************
95 #define TRNG_NUMBER_READY 0x00000001 //
96 #define TRNG_FRO_SHUTDOWN 0x00000002 //
97 #define TRNG_NEED_CLOCK 0x80000000 //
98 
99 #define TRNG_HI_WORD 0x00000001
100 #define TRNG_LOW_WORD 0x00000002
101 
102 //*****************************************************************************
103 //
104 // API Function and prototypes
105 //
106 //*****************************************************************************
107 
108 //*****************************************************************************
109 //
135 //
136 //*****************************************************************************
137 extern void TRNGConfigure(uint32_t ui32MinSamplesPerCycle,
138  uint32_t ui32MaxSamplesPerCycle,
139  uint32_t ui32ClocksPerSample);
140 
141 //*****************************************************************************
142 //
148 //
149 //*****************************************************************************
150 __STATIC_INLINE void
152 {
153  // Enable the TRNG.
154  HWREGBITW(TRNG_BASE + TRNG_O_CTL, TRNG_CTL_TRNG_EN_BITN) = 1;
155 }
156 
157 //*****************************************************************************
158 //
162 //
163 //*****************************************************************************
164 __STATIC_INLINE void
166 {
167  // Enable the TRNG
168  HWREGBITW(TRNG_BASE + TRNG_O_CTL, TRNG_CTL_TRNG_EN_BITN) = 0;
169 }
170 
171 //*****************************************************************************
172 //
188 //
189 //*****************************************************************************
190 extern uint32_t TRNGNumberGet(uint32_t ui32Word);
191 
192 //*****************************************************************************
193 //
203 //
204 //*****************************************************************************
205 __STATIC_INLINE uint32_t
207 {
208  //
209  // Return the status.
210  //
211  return (HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT));
212 }
213 
214 //*****************************************************************************
215 //
222 //
223 //*****************************************************************************
224 __STATIC_INLINE void
226 {
227  //
228  // Reset the TRNG.
229  //
230  HWREG(TRNG_BASE + TRNG_O_SWRESET) = 1;
231 }
232 
233 //*****************************************************************************
234 //
247 //
248 //*****************************************************************************
249 __STATIC_INLINE void
250 TRNGIntEnable(uint32_t ui32IntFlags)
251 {
252  //
253  // Check the arguments.
254  //
255  ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
256  (ui32IntFlags & TRNG_FRO_SHUTDOWN));
257 
258  //
259  // Enable the specified interrupts.
260  //
261  HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK) |= ui32IntFlags;
262 }
263 
264 //*****************************************************************************
265 //
278 //
279 //*****************************************************************************
280 __STATIC_INLINE void
281 TRNGIntDisable(uint32_t ui32IntFlags)
282 {
283  //
284  // Check the arguments.
285  //
286  ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
287  (ui32IntFlags & TRNG_FRO_SHUTDOWN));
288 
289  //
290  // Disable the specified interrupts.
291  //
292  HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK) &= ~ui32IntFlags;
293 }
294 
295 //*****************************************************************************
296 //
310 //
311 //*****************************************************************************
312 __STATIC_INLINE uint32_t
313 TRNGIntStatus(bool bMasked)
314 {
315  uint32_t ui32Mask;
316 
317  //
318  // Return either the interrupt status or the raw interrupt status as
319  // requested.
320  //
321  if(bMasked)
322  {
323  ui32Mask = HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK);
324  return(ui32Mask & HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT));
325  }
326  else
327  {
328  return(HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT) & 0x00000003);
329  }
330 }
331 
332 //*****************************************************************************
333 //
361 //
362 //*****************************************************************************
363 __STATIC_INLINE void
364 TRNGIntClear(uint32_t ui32IntFlags)
365 {
366  //
367  // Check the arguments.
368  //
369  ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
370  (ui32IntFlags & TRNG_FRO_SHUTDOWN));
371 
372  //
373  // Clear the requested interrupt sources.
374  //
375  HWREG(TRNG_BASE + TRNG_O_IRQFLAGCLR) = ui32IntFlags;
376 }
377 
378 //*****************************************************************************
379 //
394 //
395 //*****************************************************************************
396 __STATIC_INLINE void
397 TRNGIntRegister(void (*pfnHandler)(void))
398 {
399  //
400  // Register the interrupt handler.
401  //
402  IntRegister(INT_TRNG_IRQ, pfnHandler);
403 
404  //
405  // Enable the TRNG interrupt.
406  //
407  IntEnable(INT_TRNG_IRQ);
408 }
409 
410 //*****************************************************************************
411 //
423 //
424 //*****************************************************************************
425 __STATIC_INLINE void
427 {
428  //
429  // Disable the interrupt.
430  //
431  IntDisable(INT_TRNG_IRQ);
432 
433  //
434  // Unregister the interrupt handler.
435  //
436  IntUnregister(INT_TRNG_IRQ);
437 }
438 
439 //*****************************************************************************
440 //
441 // Support for DriverLib in ROM:
442 // Redirect to implementation in ROM when available.
443 //
444 //*****************************************************************************
445 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
446  #include <driverlib/rom.h>
447  #ifdef ROM_TRNGConfigure
448  #undef TRNGConfigure
449  #define TRNGConfigure ROM_TRNGConfigure
450  #endif
451  #ifdef ROM_TRNGNumberGet
452  #undef TRNGNumberGet
453  #define TRNGNumberGet ROM_TRNGNumberGet
454  #endif
455 #endif
456 
457 //*****************************************************************************
458 //
459 // Mark the end of the C bindings section for C++ compilers.
460 //
461 //*****************************************************************************
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif // __TRNG_H__
467 
468 //*****************************************************************************
469 //
473 //
474 //*****************************************************************************
static void TRNGDisable(void)
Disable the TRNG module.
Definition: trng.h:165
static void TRNGIntEnable(uint32_t ui32IntFlags)
Enables individual TRNG interrupt sources.
Definition: trng.h:250
static uint32_t TRNGIntStatus(bool bMasked)
Gets the current interrupt status of the TRNG module.
Definition: trng.h:313
#define TRNG_FRO_SHUTDOWN
Definition: trng.h:96
static void TRNGIntDisable(uint32_t ui32IntFlags)
Disables individual TRNG interrupt sources.
Definition: trng.h:281
uint32_t TRNGNumberGet(uint32_t ui32Word)
Get a random number from the generator.
Definition: trng.c:95
static void TRNGReset(void)
Reset the TRNG.
Definition: trng.h:225
void TRNGConfigure(uint32_t ui32MinSamplesPerCycle, uint32_t ui32MaxSamplesPerCycle, uint32_t ui32ClocksPerSample)
Configure the true random number generator.
Definition: trng.c:60
#define TRNG_NUMBER_READY
Definition: trng.h:95
static void TRNGIntClear(uint32_t ui32IntFlags)
Clears TRNG interrupt sources.
Definition: trng.h:364
#define ASSERT(expr)
Definition: debug.h:74
static void TRNGIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for a TRNG interrupt.
Definition: trng.h:397
static void TRNGIntUnregister(void)
Unregisters an interrupt handler for a TRNG interrupt.
Definition: trng.h:426
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:200
static uint32_t TRNGStatusGet(void)
Get the status of the TRNG.
Definition: trng.h:206
static void TRNGEnable(void)
Enable the TRNG.
Definition: trng.h:151
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:378
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:152
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:318