![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/trng_api.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Hardware entropy collector for the K64F, using Freescale's RNGA |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
kadonotakashi | 0:8fdf9a60065b | 5 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 6 | * |
kadonotakashi | 0:8fdf9a60065b | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kadonotakashi | 0:8fdf9a60065b | 8 | * not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 9 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 12 | * |
kadonotakashi | 0:8fdf9a60065b | 13 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kadonotakashi | 0:8fdf9a60065b | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 16 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 17 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 18 | * |
kadonotakashi | 0:8fdf9a60065b | 19 | */ |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | /* |
kadonotakashi | 0:8fdf9a60065b | 22 | * Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34 |
kadonotakashi | 0:8fdf9a60065b | 23 | */ |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | #if defined(DEVICE_TRNG) |
kadonotakashi | 0:8fdf9a60065b | 26 | |
kadonotakashi | 0:8fdf9a60065b | 27 | #include <stdlib.h> |
kadonotakashi | 0:8fdf9a60065b | 28 | #include "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | #include "fsl_common.h" |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "fsl_clock.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | #include "trng_api.h" |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | void trng_init(trng_t *obj) |
kadonotakashi | 0:8fdf9a60065b | 34 | { |
kadonotakashi | 0:8fdf9a60065b | 35 | (void)obj; |
kadonotakashi | 0:8fdf9a60065b | 36 | CLOCK_EnableClock(kCLOCK_Rnga0); |
kadonotakashi | 0:8fdf9a60065b | 37 | CLOCK_DisableClock(kCLOCK_Rnga0); |
kadonotakashi | 0:8fdf9a60065b | 38 | CLOCK_EnableClock(kCLOCK_Rnga0); |
kadonotakashi | 0:8fdf9a60065b | 39 | } |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | void trng_free(trng_t *obj) |
kadonotakashi | 0:8fdf9a60065b | 42 | { |
kadonotakashi | 0:8fdf9a60065b | 43 | (void)obj; |
kadonotakashi | 0:8fdf9a60065b | 44 | CLOCK_DisableClock(kCLOCK_Rnga0); |
kadonotakashi | 0:8fdf9a60065b | 45 | } |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | /* |
kadonotakashi | 0:8fdf9a60065b | 48 | * Get one byte of entropy from the RNG, assuming it is up and running. |
kadonotakashi | 0:8fdf9a60065b | 49 | * As recommended (34.1.1), get only one bit of each output. |
kadonotakashi | 0:8fdf9a60065b | 50 | */ |
kadonotakashi | 0:8fdf9a60065b | 51 | static void trng_get_byte(unsigned char *byte) |
kadonotakashi | 0:8fdf9a60065b | 52 | { |
kadonotakashi | 0:8fdf9a60065b | 53 | *byte = 0; |
kadonotakashi | 0:8fdf9a60065b | 54 | size_t bit; |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | /* 34.5 Steps 3-4-5: poll SR and read from OR when ready */ |
kadonotakashi | 0:8fdf9a60065b | 57 | for( bit = 0; bit < 8; bit++ ) |
kadonotakashi | 0:8fdf9a60065b | 58 | { |
kadonotakashi | 0:8fdf9a60065b | 59 | while((RNG->SR & RNG_SR_OREG_LVL_MASK) == 0 ); |
kadonotakashi | 0:8fdf9a60065b | 60 | *byte |= (RNG->OR & 1) << bit; |
kadonotakashi | 0:8fdf9a60065b | 61 | } |
kadonotakashi | 0:8fdf9a60065b | 62 | } |
kadonotakashi | 0:8fdf9a60065b | 63 | |
kadonotakashi | 0:8fdf9a60065b | 64 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
kadonotakashi | 0:8fdf9a60065b | 65 | { |
kadonotakashi | 0:8fdf9a60065b | 66 | (void)obj; |
kadonotakashi | 0:8fdf9a60065b | 67 | size_t i; |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | /* Set "Interrupt Mask", "High Assurance" and "Go", |
kadonotakashi | 0:8fdf9a60065b | 70 | * unset "Clear interrupt" and "Sleep" */ |
kadonotakashi | 0:8fdf9a60065b | 71 | RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK; |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | for (i = 0; i < length; i++) { |
kadonotakashi | 0:8fdf9a60065b | 74 | trng_get_byte(output + i); |
kadonotakashi | 0:8fdf9a60065b | 75 | } |
kadonotakashi | 0:8fdf9a60065b | 76 | |
kadonotakashi | 0:8fdf9a60065b | 77 | /* Just be extra sure that we didn't do it wrong */ |
kadonotakashi | 0:8fdf9a60065b | 78 | if ((RNG->SR & RNG_SR_SECV_MASK) != 0) { |
kadonotakashi | 0:8fdf9a60065b | 79 | return -1; |
kadonotakashi | 0:8fdf9a60065b | 80 | } |
kadonotakashi | 0:8fdf9a60065b | 81 | |
kadonotakashi | 0:8fdf9a60065b | 82 | *output_length = length; |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 85 | } |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | #endif |