From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.
Dependents: motor_driver motor_driver_screaming_fix
targets/TARGET_STM/trng_api.c@0:083111ae2a11, 2020-11-26 (annotated)
- Committer:
- saloutos
- Date:
- Thu Nov 26 04:08:56 2020 +0000
- Revision:
- 0:083111ae2a11
first commit of leaned mbed dev lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saloutos | 0:083111ae2a11 | 1 | /* |
saloutos | 0:083111ae2a11 | 2 | * Hardware entropy collector for the STM32 families |
saloutos | 0:083111ae2a11 | 3 | * |
saloutos | 0:083111ae2a11 | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
saloutos | 0:083111ae2a11 | 5 | * SPDX-License-Identifier: Apache-2.0 |
saloutos | 0:083111ae2a11 | 6 | * |
saloutos | 0:083111ae2a11 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
saloutos | 0:083111ae2a11 | 8 | * not use this file except in compliance with the License. |
saloutos | 0:083111ae2a11 | 9 | * You may obtain a copy of the License at |
saloutos | 0:083111ae2a11 | 10 | * |
saloutos | 0:083111ae2a11 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
saloutos | 0:083111ae2a11 | 12 | * |
saloutos | 0:083111ae2a11 | 13 | * Unless required by applicable law or agreed to in writing, software |
saloutos | 0:083111ae2a11 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
saloutos | 0:083111ae2a11 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
saloutos | 0:083111ae2a11 | 16 | * See the License for the specific language governing permissions and |
saloutos | 0:083111ae2a11 | 17 | * limitations under the License. |
saloutos | 0:083111ae2a11 | 18 | * |
saloutos | 0:083111ae2a11 | 19 | */ |
saloutos | 0:083111ae2a11 | 20 | |
saloutos | 0:083111ae2a11 | 21 | #if defined(DEVICE_TRNG) |
saloutos | 0:083111ae2a11 | 22 | |
saloutos | 0:083111ae2a11 | 23 | #include <stdlib.h> |
saloutos | 0:083111ae2a11 | 24 | #include "cmsis.h" |
saloutos | 0:083111ae2a11 | 25 | #include "trng_api.h" |
saloutos | 0:083111ae2a11 | 26 | |
saloutos | 0:083111ae2a11 | 27 | /** trng_get_byte |
saloutos | 0:083111ae2a11 | 28 | * @brief Get one byte of entropy from the RNG, assuming it is up and running. |
saloutos | 0:083111ae2a11 | 29 | * @param obj TRNG obj |
saloutos | 0:083111ae2a11 | 30 | * @param pointer to the hardware generated random byte. |
saloutos | 0:083111ae2a11 | 31 | */ |
saloutos | 0:083111ae2a11 | 32 | static void trng_get_byte(trng_t *obj, unsigned char *byte ) |
saloutos | 0:083111ae2a11 | 33 | { |
saloutos | 0:083111ae2a11 | 34 | *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); |
saloutos | 0:083111ae2a11 | 35 | } |
saloutos | 0:083111ae2a11 | 36 | |
saloutos | 0:083111ae2a11 | 37 | void trng_init(trng_t *obj) |
saloutos | 0:083111ae2a11 | 38 | { |
saloutos | 0:083111ae2a11 | 39 | #if defined(TARGET_STM32L4) |
saloutos | 0:083111ae2a11 | 40 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
saloutos | 0:083111ae2a11 | 41 | |
saloutos | 0:083111ae2a11 | 42 | /*Select PLLQ output as RNG clock source */ |
saloutos | 0:083111ae2a11 | 43 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG; |
saloutos | 0:083111ae2a11 | 44 | PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; |
saloutos | 0:083111ae2a11 | 45 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
saloutos | 0:083111ae2a11 | 46 | #endif |
saloutos | 0:083111ae2a11 | 47 | |
saloutos | 0:083111ae2a11 | 48 | /* RNG Peripheral clock enable */ |
saloutos | 0:083111ae2a11 | 49 | __HAL_RCC_RNG_CLK_ENABLE(); |
saloutos | 0:083111ae2a11 | 50 | |
saloutos | 0:083111ae2a11 | 51 | /* Initialize RNG instance */ |
saloutos | 0:083111ae2a11 | 52 | obj->handle.Instance = RNG; |
saloutos | 0:083111ae2a11 | 53 | HAL_RNG_Init(&obj->handle); |
saloutos | 0:083111ae2a11 | 54 | |
saloutos | 0:083111ae2a11 | 55 | /* first random number generated after setting the RNGEN bit should not be used */ |
saloutos | 0:083111ae2a11 | 56 | HAL_RNG_GetRandomNumber(&obj->handle); |
saloutos | 0:083111ae2a11 | 57 | |
saloutos | 0:083111ae2a11 | 58 | } |
saloutos | 0:083111ae2a11 | 59 | |
saloutos | 0:083111ae2a11 | 60 | void trng_free(trng_t *obj) |
saloutos | 0:083111ae2a11 | 61 | { |
saloutos | 0:083111ae2a11 | 62 | /*Disable the RNG peripheral */ |
saloutos | 0:083111ae2a11 | 63 | HAL_RNG_DeInit(&obj->handle); |
saloutos | 0:083111ae2a11 | 64 | /* RNG Peripheral clock disable - assume we're the only users of RNG */ |
saloutos | 0:083111ae2a11 | 65 | __HAL_RCC_RNG_CLK_DISABLE(); |
saloutos | 0:083111ae2a11 | 66 | } |
saloutos | 0:083111ae2a11 | 67 | |
saloutos | 0:083111ae2a11 | 68 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
saloutos | 0:083111ae2a11 | 69 | { |
saloutos | 0:083111ae2a11 | 70 | int ret; |
saloutos | 0:083111ae2a11 | 71 | |
saloutos | 0:083111ae2a11 | 72 | /* Get Random byte */ |
saloutos | 0:083111ae2a11 | 73 | for( uint32_t i = 0; i < length; i++ ){ |
saloutos | 0:083111ae2a11 | 74 | trng_get_byte(obj, output + i ); |
saloutos | 0:083111ae2a11 | 75 | } |
saloutos | 0:083111ae2a11 | 76 | |
saloutos | 0:083111ae2a11 | 77 | *output_length = length; |
saloutos | 0:083111ae2a11 | 78 | /* Just be extra sure that we didn't do it wrong */ |
saloutos | 0:083111ae2a11 | 79 | if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { |
saloutos | 0:083111ae2a11 | 80 | ret = -1; |
saloutos | 0:083111ae2a11 | 81 | } else { |
saloutos | 0:083111ae2a11 | 82 | ret = 0; |
saloutos | 0:083111ae2a11 | 83 | } |
saloutos | 0:083111ae2a11 | 84 | |
saloutos | 0:083111ae2a11 | 85 | return( ret ); |
saloutos | 0:083111ae2a11 | 86 | } |
saloutos | 0:083111ae2a11 | 87 | |
saloutos | 0:083111ae2a11 | 88 | #endif |