1.在sdk_config.h中加入宏
// <e> RNG_ENABLED - nrf_drv_rng - RNG peripheral driver - legacy layer
//==========================================================
#ifndef RNG_ENABLED
#define RNG_ENABLED 1
#endif
#ifndef RNG_CONFIG_ERROR_CORRECTION
#define RNG_CONFIG_ERROR_CORRECTION 1
#endif
// <o> RNG_CONFIG_POOL_SIZE - Pool size
#ifndef RNG_CONFIG_POOL_SIZE
#define RNG_CONFIG_POOL_SIZE 64
#endif
// <o> RNG_CONFIG_IRQ_PRIORITY - Interrupt priority
// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#ifndef RNG_CONFIG_IRQ_PRIORITY
#define RNG_CONFIG_IRQ_PRIORITY 6
#endif
// <e> NRFX_RNG_ENABLED - nrfx_rng - RNG peripheral driver
//==========================================================
#ifndef NRFX_RNG_ENABLED
#define NRFX_RNG_ENABLED 1
#endif
// <q> NRFX_RNG_CONFIG_ERROR_CORRECTION - Error correction
#ifndef NRFX_RNG_CONFIG_ERROR_CORRECTION
#define NRFX_RNG_CONFIG_ERROR_CORRECTION 1
#endif
// <o> NRFX_RNG_CONFIG_IRQ_PRIORITY - Interrupt priority
// <0=> 0 (highest)
// <1=> 1
// <2=> 2
// <3=> 3
// <4=> 4
// <5=> 5
// <6=> 6
// <7=> 7
#ifndef NRFX_RNG_CONFIG_IRQ_PRIORITY
#define NRFX_RNG_CONFIG_IRQ_PRIORITY 6
#endif
// <e> NRFX_RNG_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef NRFX_RNG_CONFIG_LOG_ENABLED
#define NRFX_RNG_CONFIG_LOG_ENABLED 0
#endif
// <o> NRFX_RNG_CONFIG_LOG_LEVEL - Default Severity level
// <0=> Off
// <1=> Error
// <2=> Warning
// <3=> Info
// <4=> Debug
#ifndef NRFX_RNG_CONFIG_LOG_LEVEL
#define NRFX_RNG_CONFIG_LOG_LEVEL 3
#endif
// <o> NRFX_RNG_CONFIG_INFO_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_RNG_CONFIG_INFO_COLOR
#define NRFX_RNG_CONFIG_INFO_COLOR 0
#endif
// <o> NRFX_RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix.
// <0=> Default
// <1=> Black
// <2=> Red
// <3=> Green
// <4=> Yellow
// <5=> Blue
// <6=> Magenta
// <7=> Cyan
// <8=> White
#ifndef NRFX_RNG_CONFIG_DEBUG_COLOR
#define NRFX_RNG_CONFIG_DEBUG_COLOR 0
#endif
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
//==========================================================
#ifndef NRF_QUEUE_ENABLED
#define NRF_QUEUE_ENABLED 1
#endif
// <q> NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module
#ifndef NRF_QUEUE_CLI_CMDS
#define NRF_QUEUE_CLI_CMDS 0
#endif
2.导入nrfx_rng.c,nrf_drv_rng.c,nrf_ringbuf.c,nrf_queue.c到工程
3.引入头文件
#include "nrf_drv_rng.h"
4.定义一个随机产生函数
#define RANDOM_BUFF_SIZE 16 /**< Random numbers buffer size. */
/** @brief Function for getting vector of random numbers.
*
* @param[out] p_buff Pointer to unit8_t buffer for storing the bytes.
* @param[in] length Number of bytes to take from pool and place in p_buff.
*
* @retval Number of bytes actually placed in p_buff.
*/
static uint8_t random_vector_generate(uint8_t * p_buff, uint8_t size)
{
uint32_t err_code;
uint8_t available;
nrf_drv_rng_bytes_available(&available); // nction for getting the number of currently available random bytes.
// Parameters
//[out] p_bytes_available The number of bytes currently available in the pool.
uint8_t length = MIN(size, available); //得到最小长度
err_code = nrf_drv_rng_rand(p_buff, length); // Function for getting the vector of random numbers.
// Parameters
// [out] p_buff Pointer to uint8_t buffer for storing the bytes.
// [in] length Number of bytes to take from the pool and place in p_buff.
// Return values
// NRF_SUCCESS If the requested bytes were written to p_buff.
// NRF_ERROR_NOT_FOUND If no bytes were written to the buffer because there were not enough bytes available in the pool.
APP_ERROR_CHECK(err_code);
return length;
}
5.在主函数中处理
/**
* @brief Function for main application entry.
*/
int main(void)
{
uint32_t err_code;
bsp_board_init(BSP_INIT_LEDS);
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
UART_HWFC,
false,
#if defined (UART_PRESENT)
NRF_UART_BAUDRATE_115200
#else
NRF_UARTE_BAUDRATE_115200
#endif
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_rng_init(NULL); // Function for initializing the nrf_drv_rng module.
// Parameters
// [in] p_config Initial configuration.
// Return values
// NRF_SUCCESS Driver was successfully initialized.
// NRF_ERROR_MODULE_ALREADY_INITIALIZED Driver was already initialized.
APP_ERROR_CHECK(err_code);
while (true)
{
uint8_t p_buff[RANDOM_BUFF_SIZE];
uint8_t length = random_vector_generate(p_buff,RANDOM_BUFF_SIZE);
printf("Random Vector:");
for(int i=0;i<length;i++)
{
printf("%x,",p_buff[i]);
}
printf("length=%d\n",length);
nrf_delay_ms(1000);
}
}