USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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