Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
targets/TARGET_ONSEMI/TARGET_NCS36510/ncs36510_trng_api.c@160:d5399cc887bb, 2017-03-14 (annotated)
- Committer:
- <>
- Date:
- Tue Mar 14 16:40:56 2017 +0000
- Revision:
- 160:d5399cc887bb
- Parent:
- 150:02e0a0aed4ec
This updates the lib to the mbed lib v138
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 150:02e0a0aed4ec | 1 | /** |
<> | 150:02e0a0aed4ec | 2 | ****************************************************************************** |
<> | 150:02e0a0aed4ec | 3 | * @file trng_api.c |
<> | 150:02e0a0aed4ec | 4 | * @brief Implementation of TRNG functionality. |
<> | 150:02e0a0aed4ec | 5 | * @internal |
<> | 150:02e0a0aed4ec | 6 | * @author ON Semiconductor. |
<> | 150:02e0a0aed4ec | 7 | * $Rev: $ |
<> | 150:02e0a0aed4ec | 8 | * $Date: $ |
<> | 150:02e0a0aed4ec | 9 | ****************************************************************************** |
<> | 150:02e0a0aed4ec | 10 | * Copyright 2016 Semiconductor Components Industries LLC (d/b/a ON Semiconductor). |
<> | 150:02e0a0aed4ec | 11 | * All rights reserved. This software and/or documentation is licensed by ON Semiconductor |
<> | 150:02e0a0aed4ec | 12 | * under limited terms and conditions. The terms and conditions pertaining to the software |
<> | 150:02e0a0aed4ec | 13 | * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf |
<> | 150:02e0a0aed4ec | 14 | * (ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software) and |
<> | 150:02e0a0aed4ec | 15 | * if applicable the software license agreement. Do not use this software and/or |
<> | 150:02e0a0aed4ec | 16 | * documentation unless you have carefully read and you agree to the limited terms and |
<> | 150:02e0a0aed4ec | 17 | * conditions. By using this software and/or documentation, you agree to the limited |
<> | 150:02e0a0aed4ec | 18 | * terms and conditions. |
<> | 150:02e0a0aed4ec | 19 | * |
<> | 150:02e0a0aed4ec | 20 | * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
<> | 150:02e0a0aed4ec | 21 | * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
<> | 150:02e0a0aed4ec | 22 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
<> | 150:02e0a0aed4ec | 23 | * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
<> | 150:02e0a0aed4ec | 24 | * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
<> | 150:02e0a0aed4ec | 25 | * @endinternal |
<> | 150:02e0a0aed4ec | 26 | * |
<> | 150:02e0a0aed4ec | 27 | * @ingroup TRNG |
<> | 150:02e0a0aed4ec | 28 | * |
<> | 150:02e0a0aed4ec | 29 | */ |
<> | 150:02e0a0aed4ec | 30 | #if DEVICE_TRNG |
<> | 150:02e0a0aed4ec | 31 | |
<> | 150:02e0a0aed4ec | 32 | /************************************************************************************************* |
<> | 150:02e0a0aed4ec | 33 | * * |
<> | 150:02e0a0aed4ec | 34 | * Header files * |
<> | 150:02e0a0aed4ec | 35 | * * |
<> | 150:02e0a0aed4ec | 36 | *************************************************************************************************/ |
<> | 150:02e0a0aed4ec | 37 | #include "trng_api.h" |
<> | 150:02e0a0aed4ec | 38 | #include "memory_map.h" |
<> | 150:02e0a0aed4ec | 39 | #include "ncs36510_trng.h" |
<> | 150:02e0a0aed4ec | 40 | #include "clock.h" |
<> | 160:d5399cc887bb | 41 | #include "mbed_wait_api.h" |
<> | 150:02e0a0aed4ec | 42 | |
<> | 150:02e0a0aed4ec | 43 | /************************************************************************************************* |
<> | 150:02e0a0aed4ec | 44 | * * |
<> | 150:02e0a0aed4ec | 45 | * Functions * |
<> | 150:02e0a0aed4ec | 46 | * * |
<> | 150:02e0a0aed4ec | 47 | *************************************************************************************************/ |
<> | 150:02e0a0aed4ec | 48 | |
<> | 150:02e0a0aed4ec | 49 | |
<> | 150:02e0a0aed4ec | 50 | void trng_init(trng_t *obj) |
<> | 150:02e0a0aed4ec | 51 | { |
<> | 150:02e0a0aed4ec | 52 | /* Enable TRNG */ |
<> | 150:02e0a0aed4ec | 53 | CLOCK_ENABLE(CLOCK_RAND); |
<> | 150:02e0a0aed4ec | 54 | |
<> | 150:02e0a0aed4ec | 55 | /* Initialize TRNG */ |
<> | 150:02e0a0aed4ec | 56 | RANDREG->CONTROL.WORD = False; |
<> | 150:02e0a0aed4ec | 57 | |
<> | 150:02e0a0aed4ec | 58 | return; |
<> | 150:02e0a0aed4ec | 59 | } |
<> | 150:02e0a0aed4ec | 60 | void trng_free(trng_t *obj) |
<> | 150:02e0a0aed4ec | 61 | { |
<> | 150:02e0a0aed4ec | 62 | /* Stop TRNG */ |
<> | 150:02e0a0aed4ec | 63 | RANDREG->CONTROL.WORD = False; |
<> | 150:02e0a0aed4ec | 64 | |
<> | 150:02e0a0aed4ec | 65 | /* Disable TRNG */ |
<> | 150:02e0a0aed4ec | 66 | CLOCK_DISABLE(CLOCK_RAND); |
<> | 150:02e0a0aed4ec | 67 | |
<> | 150:02e0a0aed4ec | 68 | return; |
<> | 150:02e0a0aed4ec | 69 | } |
<> | 150:02e0a0aed4ec | 70 | |
<> | 150:02e0a0aed4ec | 71 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
<> | 150:02e0a0aed4ec | 72 | { |
<> | 150:02e0a0aed4ec | 73 | uint32_t MSLRandom = 0, Index, TempLen, *TempPtr = (uint32_t*)output; |
<> | 150:02e0a0aed4ec | 74 | |
<> | 150:02e0a0aed4ec | 75 | RANDREG->CONTROL.BITS.METASTABLE_LATCH_EN = TRNG_ENABLE; /* ENable MSL TRNG */ |
<> | 150:02e0a0aed4ec | 76 | RANDREG->CONTROL.BITS.MEATSTABLE_SPEED = TRNG_FAST_MODE; /* Meta-stable Latch TRNG Speed Control */ |
<> | 150:02e0a0aed4ec | 77 | RANDREG->CONTROL.BITS.MODE = TRNG_ON_READ_EVENT; /* TRNG is only updated on a read event of the TRNG register */ |
<> | 150:02e0a0aed4ec | 78 | |
<> | 150:02e0a0aed4ec | 79 | wait_us(1); /* Wait till MSL generates random number after enable for the first time */ |
<> | 150:02e0a0aed4ec | 80 | |
<> | 150:02e0a0aed4ec | 81 | TempLen = length / 4; |
<> | 150:02e0a0aed4ec | 82 | |
<> | 150:02e0a0aed4ec | 83 | for(Index = 0; Index < TempLen; Index++) |
<> | 150:02e0a0aed4ec | 84 | { |
<> | 150:02e0a0aed4ec | 85 | MSLRandom = RANDREG->METASTABLE_LATCH_VAL; |
<> | 150:02e0a0aed4ec | 86 | *TempPtr++ = MSLRandom ; |
<> | 150:02e0a0aed4ec | 87 | } |
<> | 150:02e0a0aed4ec | 88 | |
<> | 150:02e0a0aed4ec | 89 | TempLen = length % 4; |
<> | 150:02e0a0aed4ec | 90 | Index *= 4; |
<> | 150:02e0a0aed4ec | 91 | |
<> | 150:02e0a0aed4ec | 92 | if(TempLen-- > 0) |
<> | 150:02e0a0aed4ec | 93 | { |
<> | 150:02e0a0aed4ec | 94 | MSLRandom = RANDREG->METASTABLE_LATCH_VAL; |
<> | 150:02e0a0aed4ec | 95 | *(output + Index++) = (MSLRandom >> 0) & 0xFF; |
<> | 150:02e0a0aed4ec | 96 | if(TempLen-- > 0) |
<> | 150:02e0a0aed4ec | 97 | { |
<> | 150:02e0a0aed4ec | 98 | *(output + Index++) = (MSLRandom >> 8) & 0xFF; |
<> | 150:02e0a0aed4ec | 99 | if(TempLen-- > 0) |
<> | 150:02e0a0aed4ec | 100 | { |
<> | 150:02e0a0aed4ec | 101 | *(output + Index++) = (MSLRandom >> 16) & 0xFF; |
<> | 150:02e0a0aed4ec | 102 | } |
<> | 150:02e0a0aed4ec | 103 | } |
<> | 150:02e0a0aed4ec | 104 | } |
<> | 150:02e0a0aed4ec | 105 | |
<> | 150:02e0a0aed4ec | 106 | RANDREG->CONTROL.BITS.METASTABLE_LATCH_EN = TRNG_DISABLE; /* Disable MSL */ |
<> | 150:02e0a0aed4ec | 107 | |
<> | 150:02e0a0aed4ec | 108 | *output_length = Index; |
<> | 150:02e0a0aed4ec | 109 | |
<> | 150:02e0a0aed4ec | 110 | return 0; /* Success */ |
<> | 150:02e0a0aed4ec | 111 | } |
<> | 150:02e0a0aed4ec | 112 | |
<> | 150:02e0a0aed4ec | 113 | #endif /* DEVICE_TRNG */ |