Knight KE
/
Game_Master
游戏王对战板,目前code还是空的
mbed-os/targets/TARGET_STM/trng_api.c@0:9b3d4731edbb, 2018-06-21 (annotated)
- Committer:
- WFKnight
- Date:
- Thu Jun 21 13:51:43 2018 +0000
- Revision:
- 0:9b3d4731edbb
UART, RTOS, LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WFKnight | 0:9b3d4731edbb | 1 | /* |
WFKnight | 0:9b3d4731edbb | 2 | * Hardware entropy collector for the STM32 families |
WFKnight | 0:9b3d4731edbb | 3 | * |
WFKnight | 0:9b3d4731edbb | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
WFKnight | 0:9b3d4731edbb | 5 | * SPDX-License-Identifier: Apache-2.0 |
WFKnight | 0:9b3d4731edbb | 6 | * |
WFKnight | 0:9b3d4731edbb | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
WFKnight | 0:9b3d4731edbb | 8 | * not use this file except in compliance with the License. |
WFKnight | 0:9b3d4731edbb | 9 | * You may obtain a copy of the License at |
WFKnight | 0:9b3d4731edbb | 10 | * |
WFKnight | 0:9b3d4731edbb | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
WFKnight | 0:9b3d4731edbb | 12 | * |
WFKnight | 0:9b3d4731edbb | 13 | * Unless required by applicable law or agreed to in writing, software |
WFKnight | 0:9b3d4731edbb | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
WFKnight | 0:9b3d4731edbb | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
WFKnight | 0:9b3d4731edbb | 16 | * See the License for the specific language governing permissions and |
WFKnight | 0:9b3d4731edbb | 17 | * limitations under the License. |
WFKnight | 0:9b3d4731edbb | 18 | * |
WFKnight | 0:9b3d4731edbb | 19 | */ |
WFKnight | 0:9b3d4731edbb | 20 | |
WFKnight | 0:9b3d4731edbb | 21 | #if defined(DEVICE_TRNG) |
WFKnight | 0:9b3d4731edbb | 22 | |
WFKnight | 0:9b3d4731edbb | 23 | #include <stdlib.h> |
WFKnight | 0:9b3d4731edbb | 24 | #include "cmsis.h" |
WFKnight | 0:9b3d4731edbb | 25 | #include "trng_api.h" |
WFKnight | 0:9b3d4731edbb | 26 | #include "mbed_error.h" |
WFKnight | 0:9b3d4731edbb | 27 | #include "mbed_critical.h" |
WFKnight | 0:9b3d4731edbb | 28 | |
WFKnight | 0:9b3d4731edbb | 29 | static uint8_t users = 0; |
WFKnight | 0:9b3d4731edbb | 30 | |
WFKnight | 0:9b3d4731edbb | 31 | void trng_init(trng_t *obj) |
WFKnight | 0:9b3d4731edbb | 32 | { |
WFKnight | 0:9b3d4731edbb | 33 | uint32_t dummy; |
WFKnight | 0:9b3d4731edbb | 34 | |
WFKnight | 0:9b3d4731edbb | 35 | /* We're only supporting a single user of RNG */ |
WFKnight | 0:9b3d4731edbb | 36 | if (core_util_atomic_incr_u8(&users, 1) > 1 ) { |
WFKnight | 0:9b3d4731edbb | 37 | error("Only 1 RNG instance supported\r\n"); |
WFKnight | 0:9b3d4731edbb | 38 | } |
WFKnight | 0:9b3d4731edbb | 39 | |
WFKnight | 0:9b3d4731edbb | 40 | #if defined(TARGET_STM32L4) |
WFKnight | 0:9b3d4731edbb | 41 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
WFKnight | 0:9b3d4731edbb | 42 | |
WFKnight | 0:9b3d4731edbb | 43 | /*Select PLLQ output as RNG clock source */ |
WFKnight | 0:9b3d4731edbb | 44 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG; |
WFKnight | 0:9b3d4731edbb | 45 | PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; |
WFKnight | 0:9b3d4731edbb | 46 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
WFKnight | 0:9b3d4731edbb | 47 | #endif |
WFKnight | 0:9b3d4731edbb | 48 | |
WFKnight | 0:9b3d4731edbb | 49 | /* RNG Peripheral clock enable */ |
WFKnight | 0:9b3d4731edbb | 50 | __HAL_RCC_RNG_CLK_ENABLE(); |
WFKnight | 0:9b3d4731edbb | 51 | |
WFKnight | 0:9b3d4731edbb | 52 | /* Initialize RNG instance */ |
WFKnight | 0:9b3d4731edbb | 53 | obj->handle.Instance = RNG; |
WFKnight | 0:9b3d4731edbb | 54 | obj->handle.State = HAL_RNG_STATE_RESET; |
WFKnight | 0:9b3d4731edbb | 55 | obj->handle.Lock = HAL_UNLOCKED; |
WFKnight | 0:9b3d4731edbb | 56 | |
WFKnight | 0:9b3d4731edbb | 57 | HAL_RNG_Init(&obj->handle); |
WFKnight | 0:9b3d4731edbb | 58 | |
WFKnight | 0:9b3d4731edbb | 59 | /* first random number generated after setting the RNGEN bit should not be used */ |
WFKnight | 0:9b3d4731edbb | 60 | HAL_RNG_GenerateRandomNumber(&obj->handle, &dummy); |
WFKnight | 0:9b3d4731edbb | 61 | } |
WFKnight | 0:9b3d4731edbb | 62 | |
WFKnight | 0:9b3d4731edbb | 63 | void trng_free(trng_t *obj) |
WFKnight | 0:9b3d4731edbb | 64 | { |
WFKnight | 0:9b3d4731edbb | 65 | /*Disable the RNG peripheral */ |
WFKnight | 0:9b3d4731edbb | 66 | HAL_RNG_DeInit(&obj->handle); |
WFKnight | 0:9b3d4731edbb | 67 | /* RNG Peripheral clock disable - assume we're the only users of RNG */ |
WFKnight | 0:9b3d4731edbb | 68 | __HAL_RCC_RNG_CLK_DISABLE(); |
WFKnight | 0:9b3d4731edbb | 69 | |
WFKnight | 0:9b3d4731edbb | 70 | users = 0; |
WFKnight | 0:9b3d4731edbb | 71 | } |
WFKnight | 0:9b3d4731edbb | 72 | |
WFKnight | 0:9b3d4731edbb | 73 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
WFKnight | 0:9b3d4731edbb | 74 | { |
WFKnight | 0:9b3d4731edbb | 75 | int ret = 0; |
WFKnight | 0:9b3d4731edbb | 76 | volatile uint8_t random[4]; |
WFKnight | 0:9b3d4731edbb | 77 | *output_length = 0; |
WFKnight | 0:9b3d4731edbb | 78 | |
WFKnight | 0:9b3d4731edbb | 79 | /* Get Random byte */ |
WFKnight | 0:9b3d4731edbb | 80 | while ((*output_length < length) && (ret ==0)) { |
WFKnight | 0:9b3d4731edbb | 81 | if ( HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random ) != HAL_OK) { |
WFKnight | 0:9b3d4731edbb | 82 | ret = -1; |
WFKnight | 0:9b3d4731edbb | 83 | } else { |
WFKnight | 0:9b3d4731edbb | 84 | for (uint8_t i =0; (i < 4) && (*output_length < length) ; i++) { |
WFKnight | 0:9b3d4731edbb | 85 | *output++ = random[i]; |
WFKnight | 0:9b3d4731edbb | 86 | *output_length += 1; |
WFKnight | 0:9b3d4731edbb | 87 | random[i] = 0; |
WFKnight | 0:9b3d4731edbb | 88 | } |
WFKnight | 0:9b3d4731edbb | 89 | } |
WFKnight | 0:9b3d4731edbb | 90 | } |
WFKnight | 0:9b3d4731edbb | 91 | |
WFKnight | 0:9b3d4731edbb | 92 | /* Just be extra sure that we didn't do it wrong */ |
WFKnight | 0:9b3d4731edbb | 93 | if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { |
WFKnight | 0:9b3d4731edbb | 94 | ret = -1; |
WFKnight | 0:9b3d4731edbb | 95 | } |
WFKnight | 0:9b3d4731edbb | 96 | |
WFKnight | 0:9b3d4731edbb | 97 | return( ret ); |
WFKnight | 0:9b3d4731edbb | 98 | } |
WFKnight | 0:9b3d4731edbb | 99 | |
WFKnight | 0:9b3d4731edbb | 100 | #endif |