Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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