122 lines
4.6 KiB
C
122 lines
4.6 KiB
C
/*
|
|
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
|
* Copyright 2016-2017 NXP
|
|
* All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
/*******************************************************************************
|
|
* Includes
|
|
******************************************************************************/
|
|
#include "pin_mux.h"
|
|
#include "clock_config.h"
|
|
#include "board.h"
|
|
#include "fsl_trng.h"
|
|
#include "fsl_debug_console.h"
|
|
|
|
/*******************************************************************************
|
|
* Definitions
|
|
******************************************************************************/
|
|
#define TRNG0 TRNG
|
|
#define TRNG_EXAMPLE_RANDOM_NUMBER 10
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
/*!
|
|
* @brief TRNG example.
|
|
* @details Generate random numbers and print them to terminal.
|
|
*/
|
|
|
|
int main(void)
|
|
{
|
|
uint32_t i;
|
|
trng_config_t trngConfig;
|
|
status_t status;
|
|
uint32_t data[TRNG_EXAMPLE_RANDOM_NUMBER];
|
|
|
|
/* Init hardware*/
|
|
BOARD_ConfigMPU();
|
|
BOARD_InitBootPins();
|
|
BOARD_InitBootClocks();
|
|
BOARD_InitDebugConsole();
|
|
|
|
PRINTF("TRNG Peripheral Driver Example\r\n");
|
|
|
|
/* Initialize TRNG configuration structure to default.*/
|
|
/*
|
|
* trngConfig.lock = TRNG_USER_CONFIG_DEFAULT_LOCK;
|
|
* trngConfig.clockMode = kTRNG_ClockModeRingOscillator;
|
|
* trngConfig.ringOscDiv = TRNG_USER_CONFIG_DEFAULT_OSC_DIV;
|
|
* trngConfig.sampleMode = kTRNG_SampleModeRaw;
|
|
* trngConfig.entropyDelay = TRNG_USER_CONFIG_DEFAULT_ENTROPY_DELAY;
|
|
* trngConfig.sampleSize = TRNG_USER_CONFIG_DEFAULT_SAMPLE_SIZE;
|
|
* trngConfig.sparseBitLimit = TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT;
|
|
* trngConfig.retryCount = TRNG_USER_CONFIG_DEFAULT_RETRY_COUNT;
|
|
* trngConfig.longRunMaxLimit = TRNG_USER_CONFIG_DEFAULT_RUN_MAX_LIMIT;
|
|
* trngConfig.monobitLimit.maximum = TRNG_USER_CONFIG_DEFAULT_MONOBIT_MAXIMUM;
|
|
* trngConfig.monobitLimit.minimum = TRNG_USER_CONFIG_DEFAULT_MONOBIT_MINIMUM;
|
|
* trngConfig.runBit1Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MAXIMUM;
|
|
* trngConfig.runBit1Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MINIMUM;
|
|
* trngConfig.runBit2Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MAXIMUM;
|
|
* trngConfig.runBit2Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MINIMUM;
|
|
* trngConfig.runBit3Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MAXIMUM;
|
|
* trngConfig.runBit3Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MINIMUM;
|
|
* trngConfig.runBit4Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MAXIMUM;
|
|
* trngConfig.runBit4Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MINIMUM;
|
|
* trngConfig.runBit5Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MAXIMUM;
|
|
* trngConfig.runBit5Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MINIMUM;
|
|
* trngConfig.runBit6PlusLimit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MAXIMUM;
|
|
* trngConfig.runBit6PlusLimit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MINIMUM;
|
|
* trngConfig.pokerLimit.maximum = TRNG_USER_CONFIG_DEFAULT_POKER_MAXIMUM;
|
|
* trngConfig.pokerLimit.minimum = TRNG_USER_CONFIG_DEFAULT_POKER_MINIMUM;
|
|
* trngConfig.frequencyCountLimit.maximum = TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM;
|
|
* trngConfig.frequencyCountLimit.minimum = TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM;
|
|
*/
|
|
TRNG_GetDefaultConfig(&trngConfig);
|
|
|
|
/* Initialize TRNG */
|
|
status = TRNG_Init(TRNG0, &trngConfig);
|
|
if (kStatus_Success == status)
|
|
{
|
|
while (1)
|
|
{
|
|
PRINTF("Generate %d random numbers: \r\n", TRNG_EXAMPLE_RANDOM_NUMBER);
|
|
|
|
/* Get Random data*/
|
|
status = TRNG_GetRandomData(TRNG0, data, sizeof(data));
|
|
if (status == kStatus_Success)
|
|
{
|
|
/* Print data*/
|
|
for (i = 0; i < TRNG_EXAMPLE_RANDOM_NUMBER; i++)
|
|
{
|
|
PRINTF("Random[%d] = 0x%X\r\n", i, data[i]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
PRINTF("TRNG failed!\r\n");
|
|
}
|
|
|
|
/* Print a note.*/
|
|
PRINTF("\r\n Press any key to continue... \r\n");
|
|
GETCHAR();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
PRINTF("TRNG initialize failed!\r\n");
|
|
}
|
|
|
|
return -1;
|
|
}
|