Skip to content

Commit 595b9cf

Browse files
mmahadevan1080xc0170
authored andcommitted
K22F: Enable TRNG (#3599)
Signed-off-by: Mahadevan Mahesh <[email protected]>
1 parent 2ce1a8d commit 595b9cf

File tree

2 files changed

+84
-2
lines changed

2 files changed

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

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@
493493
"macros": ["CPU_MK22FN512VLH12", "FSL_RTOS_MBED"],
494494
"inherits": ["Target"],
495495
"detect_code": ["0231"],
496-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
496+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
497497
"device_name": "MK22DN512xxx5"
498498
},
499499
"K22F": {
@@ -2498,7 +2498,7 @@
24982498
"macros_add": ["BOARD_PCA10040", "NRF52_PAN_12", "NRF52_PAN_15", "NRF52_PAN_58", "NRF52_PAN_55", "NRF52_PAN_54", "NRF52_PAN_31", "NRF52_PAN_30", "NRF52_PAN_51", "NRF52_PAN_36", "NRF52_PAN_53", "S132", "CONFIG_GPIO_AS_PINRESET", "BLE_STACK_SUPPORT_REQD", "SWI_DISABLE0", "NRF52_PAN_20", "NRF52_PAN_64", "NRF52_PAN_62", "NRF52_PAN_63"],
24992499
"device_has": ["ANALOGIN", "ERROR_PATTERN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"],
25002500
"release_versions": ["2", "5"],
2501-
"overrides": {"uart_hwfc": 0},
2501+
"overrides": {"uart_hwfc": 0},
25022502
"device_name": "nRF52832_xxAA"
25032503
},
25042504
"UBLOX_EVK_NINA_B1": {

0 commit comments

Comments
 (0)