mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
This updates the lib to the mbed lib v 145

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 167:e84263d55307 40 #include "trng_api.h"
AnnaBridge 167:e84263d55307 41 #include "nrf_drv_rng.h"
AnnaBridge 167:e84263d55307 42
AnnaBridge 167:e84263d55307 43 void trng_init(trng_t *obj)
AnnaBridge 167:e84263d55307 44 {
AnnaBridge 167:e84263d55307 45 (void) obj;
AnnaBridge 167:e84263d55307 46
AnnaBridge 167:e84263d55307 47 (void)nrf_drv_rng_init(NULL);
AnnaBridge 167:e84263d55307 48 }
AnnaBridge 167:e84263d55307 49
AnnaBridge 167:e84263d55307 50 void trng_free(trng_t *obj)
AnnaBridge 167:e84263d55307 51 {
AnnaBridge 167:e84263d55307 52 (void) obj;
AnnaBridge 167:e84263d55307 53
AnnaBridge 167:e84263d55307 54 nrf_drv_rng_uninit();
AnnaBridge 167:e84263d55307 55 }
AnnaBridge 167:e84263d55307 56
AnnaBridge 167:e84263d55307 57 /* Get random data from NRF5x TRNG peripheral.
AnnaBridge 167:e84263d55307 58 *
AnnaBridge 167:e84263d55307 59 * This implementation returns num of random bytes in range <1, length>.
AnnaBridge 167:e84263d55307 60 * For parameters description see trng_api.h file.
AnnaBridge 167:e84263d55307 61 */
AnnaBridge 167:e84263d55307 62 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
AnnaBridge 167:e84263d55307 63 {
AnnaBridge 167:e84263d55307 64 uint8_t bytes_available;
AnnaBridge 167:e84263d55307 65
AnnaBridge 167:e84263d55307 66 (void) obj;
AnnaBridge 167:e84263d55307 67
AnnaBridge 167:e84263d55307 68 nrf_drv_rng_bytes_available(&bytes_available);
AnnaBridge 167:e84263d55307 69
AnnaBridge 167:e84263d55307 70 if (bytes_available == 0) {
AnnaBridge 167:e84263d55307 71 nrf_drv_rng_block_rand(output, 1);
AnnaBridge 167:e84263d55307 72 *output_length = 1;
AnnaBridge 167:e84263d55307 73 } else {
AnnaBridge 167:e84263d55307 74
AnnaBridge 167:e84263d55307 75 if (bytes_available > length) {
AnnaBridge 167:e84263d55307 76 bytes_available = length;
AnnaBridge 167:e84263d55307 77 }
AnnaBridge 167:e84263d55307 78
AnnaBridge 167:e84263d55307 79 if (nrf_drv_rng_rand(output, bytes_available) != NRF_SUCCESS) {
AnnaBridge 167:e84263d55307 80 *output_length = 0;
AnnaBridge 167:e84263d55307 81 return -1;
AnnaBridge 167:e84263d55307 82 } else {
AnnaBridge 167:e84263d55307 83 *output_length = bytes_available;
AnnaBridge 167:e84263d55307 84 }
AnnaBridge 167:e84263d55307 85 }
AnnaBridge 167:e84263d55307 86
AnnaBridge 167:e84263d55307 87 return 0;
AnnaBridge 167:e84263d55307 88 }
AnnaBridge 167:e84263d55307 89
AnnaBridge 167:e84263d55307 90 #endif