Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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