helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Tue Mar 20 16:56:18 2018 +0000
Revision:
183:a56a73fd2a6f
Parent:
167:e84263d55307
mbed-dev library. Release version 160

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /*
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2017 Nordic Semiconductor ASA
AnnaBridge 167:e84263d55307 3 * All rights reserved.
AnnaBridge 167:e84263d55307 4 *
AnnaBridge 167:e84263d55307 5 * Redistribution and use in source and binary forms, with or without modification,
AnnaBridge 167:e84263d55307 6 * are permitted provided that the following conditions are met:
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * 1. Redistributions of source code must retain the above copyright notice, this list
AnnaBridge 167:e84263d55307 9 * of conditions and the following disclaimer.
AnnaBridge 167:e84263d55307 10 *
AnnaBridge 167:e84263d55307 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
AnnaBridge 167:e84263d55307 12 * integrated circuit in a product or a software update for such product, must reproduce
AnnaBridge 167:e84263d55307 13 * the above copyright notice, this list of conditions and the following disclaimer in
AnnaBridge 167:e84263d55307 14 * the documentation and/or other materials provided with the distribution.
AnnaBridge 167:e84263d55307 15 *
AnnaBridge 167:e84263d55307 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
AnnaBridge 167:e84263d55307 17 * used to endorse or promote products derived from this software without specific prior
AnnaBridge 167:e84263d55307 18 * written permission.
AnnaBridge 167:e84263d55307 19 *
AnnaBridge 167:e84263d55307 20 * 4. This software, with or without modification, must only be used with a
AnnaBridge 167:e84263d55307 21 * Nordic Semiconductor ASA integrated circuit.
AnnaBridge 167:e84263d55307 22 *
AnnaBridge 167:e84263d55307 23 * 5. Any software provided in binary or object form under this license must not be reverse
AnnaBridge 167:e84263d55307 24 * engineered, decompiled, modified and/or disassembled.
AnnaBridge 167:e84263d55307 25 *
AnnaBridge 167:e84263d55307 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
AnnaBridge 167:e84263d55307 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
AnnaBridge 167:e84263d55307 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AnnaBridge 167:e84263d55307 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
AnnaBridge 167:e84263d55307 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
AnnaBridge 167:e84263d55307 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
AnnaBridge 167:e84263d55307 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
AnnaBridge 167:e84263d55307 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
AnnaBridge 167:e84263d55307 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
AnnaBridge 167:e84263d55307 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AnnaBridge 167:e84263d55307 36 *
AnnaBridge 167:e84263d55307 37 */
AnnaBridge 167:e84263d55307 38
AnnaBridge 167:e84263d55307 39 #if defined(DEVICE_TRNG)
AnnaBridge 183:a56a73fd2a6f 40
AnnaBridge 183:a56a73fd2a6f 41 #include "hal/trng_api.h"
AnnaBridge 183:a56a73fd2a6f 42 #include "hal/lp_ticker_api.h"
AnnaBridge 183:a56a73fd2a6f 43
AnnaBridge 167:e84263d55307 44 #include "nrf_drv_rng.h"
AnnaBridge 167:e84263d55307 45
AnnaBridge 183:a56a73fd2a6f 46 #define DEFAULT_TIMEOUT_US (1000*1000)
AnnaBridge 183:a56a73fd2a6f 47
AnnaBridge 183:a56a73fd2a6f 48 /* Macro for testing if the SoftDevice is active, regardless of whether the
AnnaBridge 183:a56a73fd2a6f 49 * application is build with the SoftDevice or not.
AnnaBridge 183:a56a73fd2a6f 50 */
AnnaBridge 183:a56a73fd2a6f 51 #if defined(SOFTDEVICE_PRESENT)
AnnaBridge 183:a56a73fd2a6f 52 #include "nrf_sdm.h"
AnnaBridge 183:a56a73fd2a6f 53 static uint8_t wrapper(void) {
AnnaBridge 183:a56a73fd2a6f 54 uint8_t softdevice_is_enabled;
AnnaBridge 183:a56a73fd2a6f 55 ret_code_t result = sd_softdevice_is_enabled(&softdevice_is_enabled);
AnnaBridge 183:a56a73fd2a6f 56 return ((result == NRF_SUCCESS) && (softdevice_is_enabled == 1));
AnnaBridge 183:a56a73fd2a6f 57 }
AnnaBridge 183:a56a73fd2a6f 58 #define NRF_HAL_SD_IS_ENABLED() wrapper()
AnnaBridge 183:a56a73fd2a6f 59 #else
AnnaBridge 183:a56a73fd2a6f 60 #define NRF_HAL_SD_IS_ENABLED() 0
AnnaBridge 183:a56a73fd2a6f 61 #endif
AnnaBridge 183:a56a73fd2a6f 62
AnnaBridge 183:a56a73fd2a6f 63 /** Initialize the TRNG peripheral
AnnaBridge 183:a56a73fd2a6f 64 *
AnnaBridge 183:a56a73fd2a6f 65 * @param obj The TRNG object
AnnaBridge 183:a56a73fd2a6f 66 */
AnnaBridge 167:e84263d55307 67 void trng_init(trng_t *obj)
AnnaBridge 167:e84263d55307 68 {
AnnaBridge 167:e84263d55307 69 (void) obj;
AnnaBridge 167:e84263d55307 70
AnnaBridge 183:a56a73fd2a6f 71 /* Initialize low power ticker. Used for timeouts. */
AnnaBridge 183:a56a73fd2a6f 72 static bool first_init = true;
AnnaBridge 183:a56a73fd2a6f 73
AnnaBridge 183:a56a73fd2a6f 74 if (first_init) {
AnnaBridge 183:a56a73fd2a6f 75 first_init = false;
AnnaBridge 183:a56a73fd2a6f 76 lp_ticker_init();
AnnaBridge 183:a56a73fd2a6f 77 }
AnnaBridge 167:e84263d55307 78 }
AnnaBridge 167:e84263d55307 79
AnnaBridge 183:a56a73fd2a6f 80 /** Deinitialize the TRNG peripheral
AnnaBridge 183:a56a73fd2a6f 81 *
AnnaBridge 183:a56a73fd2a6f 82 * @param obj The TRNG object
AnnaBridge 183:a56a73fd2a6f 83 */
AnnaBridge 167:e84263d55307 84 void trng_free(trng_t *obj)
AnnaBridge 167:e84263d55307 85 {
AnnaBridge 167:e84263d55307 86 (void) obj;
AnnaBridge 167:e84263d55307 87 }
AnnaBridge 167:e84263d55307 88
AnnaBridge 183:a56a73fd2a6f 89 /** Get random data from TRNG peripheral
AnnaBridge 167:e84263d55307 90 *
AnnaBridge 183:a56a73fd2a6f 91 * @param obj The TRNG object
AnnaBridge 183:a56a73fd2a6f 92 * @param output The pointer to an output array
AnnaBridge 183:a56a73fd2a6f 93 * @param length The size of output data, to avoid buffer overwrite
AnnaBridge 183:a56a73fd2a6f 94 * @param output_length The length of generated data
AnnaBridge 183:a56a73fd2a6f 95 * @return 0 success, -1 fail
AnnaBridge 167:e84263d55307 96 */
AnnaBridge 167:e84263d55307 97 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
AnnaBridge 167:e84263d55307 98 {
AnnaBridge 167:e84263d55307 99 (void) obj;
AnnaBridge 167:e84263d55307 100
AnnaBridge 183:a56a73fd2a6f 101 /* Use SDK RNG driver if SoftDevice is enabled. */
AnnaBridge 183:a56a73fd2a6f 102 if (NRF_HAL_SD_IS_ENABLED()) {
AnnaBridge 183:a56a73fd2a6f 103
AnnaBridge 183:a56a73fd2a6f 104 /* Initialize driver once. */
AnnaBridge 183:a56a73fd2a6f 105 static bool nordic_driver_init = true;
AnnaBridge 183:a56a73fd2a6f 106
AnnaBridge 183:a56a73fd2a6f 107 if (nordic_driver_init) {
AnnaBridge 183:a56a73fd2a6f 108 nordic_driver_init = false;
AnnaBridge 183:a56a73fd2a6f 109 nrf_drv_rng_init(NULL);
AnnaBridge 183:a56a73fd2a6f 110 }
AnnaBridge 183:a56a73fd2a6f 111
AnnaBridge 183:a56a73fd2a6f 112 /* Query how many bytes are available. */
AnnaBridge 183:a56a73fd2a6f 113 uint8_t bytes_available;
AnnaBridge 183:a56a73fd2a6f 114 nrf_drv_rng_bytes_available(&bytes_available);
AnnaBridge 167:e84263d55307 115
AnnaBridge 183:a56a73fd2a6f 116 /* If no bytes are cached, block until at least 1 byte is available. */
AnnaBridge 183:a56a73fd2a6f 117 if (bytes_available == 0) {
AnnaBridge 183:a56a73fd2a6f 118 nrf_drv_rng_block_rand(output, 1);
AnnaBridge 183:a56a73fd2a6f 119 *output_length = 1;
AnnaBridge 183:a56a73fd2a6f 120 } else {
AnnaBridge 183:a56a73fd2a6f 121
AnnaBridge 183:a56a73fd2a6f 122 /* Get up to the requested number of bytes. */
AnnaBridge 183:a56a73fd2a6f 123 if (bytes_available > length) {
AnnaBridge 183:a56a73fd2a6f 124 bytes_available = length;
AnnaBridge 183:a56a73fd2a6f 125 }
AnnaBridge 183:a56a73fd2a6f 126
AnnaBridge 183:a56a73fd2a6f 127 ret_code_t result = nrf_drv_rng_rand(output, bytes_available);
AnnaBridge 183:a56a73fd2a6f 128
AnnaBridge 183:a56a73fd2a6f 129 /* Set output length with available bytes. */
AnnaBridge 183:a56a73fd2a6f 130 if (result == NRF_SUCCESS) {
AnnaBridge 183:a56a73fd2a6f 131 *output_length = bytes_available;
AnnaBridge 183:a56a73fd2a6f 132 } else {
AnnaBridge 183:a56a73fd2a6f 133 *output_length = 0;
AnnaBridge 183:a56a73fd2a6f 134 }
AnnaBridge 183:a56a73fd2a6f 135 }
AnnaBridge 167:e84263d55307 136 } else {
AnnaBridge 167:e84263d55307 137
AnnaBridge 183:a56a73fd2a6f 138 /* Initialize low-level registers once. */
AnnaBridge 183:a56a73fd2a6f 139 static bool nordic_register_init = true;
AnnaBridge 183:a56a73fd2a6f 140
AnnaBridge 183:a56a73fd2a6f 141 if (nordic_register_init) {
AnnaBridge 183:a56a73fd2a6f 142 nordic_register_init = false;
AnnaBridge 183:a56a73fd2a6f 143
AnnaBridge 183:a56a73fd2a6f 144 /* Enable RNG */
AnnaBridge 183:a56a73fd2a6f 145 nrf_rng_error_correction_enable();
AnnaBridge 183:a56a73fd2a6f 146 nrf_rng_task_trigger(NRF_RNG_TASK_START);
AnnaBridge 167:e84263d55307 147 }
AnnaBridge 167:e84263d55307 148
AnnaBridge 183:a56a73fd2a6f 149 /* Copy out one byte at a time. */
AnnaBridge 183:a56a73fd2a6f 150 size_t index = 0;
AnnaBridge 183:a56a73fd2a6f 151 for ( ; index < length; index++) {
AnnaBridge 183:a56a73fd2a6f 152
AnnaBridge 183:a56a73fd2a6f 153 /* Setup stop watch for timeout. */
AnnaBridge 183:a56a73fd2a6f 154 uint32_t start_us = lp_ticker_read();
AnnaBridge 183:a56a73fd2a6f 155 uint32_t now_us = start_us;
AnnaBridge 183:a56a73fd2a6f 156
AnnaBridge 183:a56a73fd2a6f 157 /* Block until timeout or random numer is ready. */
AnnaBridge 183:a56a73fd2a6f 158 while (((now_us - start_us) < DEFAULT_TIMEOUT_US) &&
AnnaBridge 183:a56a73fd2a6f 159 !nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) {
AnnaBridge 183:a56a73fd2a6f 160 now_us = lp_ticker_read();
AnnaBridge 183:a56a73fd2a6f 161 }
AnnaBridge 183:a56a73fd2a6f 162
AnnaBridge 183:a56a73fd2a6f 163 /* Abort if timeout was reached. */
AnnaBridge 183:a56a73fd2a6f 164 if ((now_us - start_us) >= DEFAULT_TIMEOUT_US) {
AnnaBridge 183:a56a73fd2a6f 165 break;
AnnaBridge 183:a56a73fd2a6f 166 } else {
AnnaBridge 183:a56a73fd2a6f 167
AnnaBridge 183:a56a73fd2a6f 168 /* Read random byte and clear event in preparation for the next byte. */
AnnaBridge 183:a56a73fd2a6f 169 nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
AnnaBridge 183:a56a73fd2a6f 170 output[index] = nrf_rng_random_value_get();
AnnaBridge 183:a56a73fd2a6f 171 }
AnnaBridge 167:e84263d55307 172 }
AnnaBridge 183:a56a73fd2a6f 173
AnnaBridge 183:a56a73fd2a6f 174 /* Set output length with available bytes. */
AnnaBridge 183:a56a73fd2a6f 175 *output_length = index;
AnnaBridge 167:e84263d55307 176 }
AnnaBridge 167:e84263d55307 177
AnnaBridge 183:a56a73fd2a6f 178 /* Set return value based on how many bytes was read. */
AnnaBridge 183:a56a73fd2a6f 179 return (*output_length == 0) ? -1 : 0;
AnnaBridge 167:e84263d55307 180 }
AnnaBridge 167:e84263d55307 181
AnnaBridge 167:e84263d55307 182 #endif