1.在sdk_config.h中加入宏 // 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
// RNG_CONFIG_POOL_SIZE - Pool size #ifndef RNG_CONFIG_POOL_SIZE #define RNG_CONFIG_POOL_SIZE 64 #endif
// RNG_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef RNG_CONFIG_IRQ_PRIORITY #define RNG_CONFIG_IRQ_PRIORITY 6 #endif // NRFX_RNG_ENABLED - nrfx_rng - RNG peripheral driver //========================================================== #ifndef NRFX_RNG_ENABLED #define NRFX_RNG_ENABLED 1 #endif // NRFX_RNG_CONFIG_ERROR_CORRECTION - Error correction #ifndef NRFX_RNG_CONFIG_ERROR_CORRECTION #define NRFX_RNG_CONFIG_ERROR_CORRECTION 1 #endif
// NRFX_RNG_CONFIG_IRQ_PRIORITY - Interrupt priority // 0 (highest) // 1 // 2 // 3 // 4 // 5 // 6 // 7
#ifndef NRFX_RNG_CONFIG_IRQ_PRIORITY #define NRFX_RNG_CONFIG_IRQ_PRIORITY 6 #endif
// NRFX_RNG_CONFIG_LOG_ENABLED - Enables logging in the module. //========================================================== #ifndef NRFX_RNG_CONFIG_LOG_ENABLED #define NRFX_RNG_CONFIG_LOG_ENABLED 0 #endif // NRFX_RNG_CONFIG_LOG_LEVEL - Default Severity level // Off // Error // Warning // Info // Debug
#ifndef NRFX_RNG_CONFIG_LOG_LEVEL #define NRFX_RNG_CONFIG_LOG_LEVEL 3 #endif
// NRFX_RNG_CONFIG_INFO_COLOR - ANSI escape code prefix. // Default // Black // Red // Green // Yellow // Blue // Magenta // Cyan // White
#ifndef NRFX_RNG_CONFIG_INFO_COLOR #define NRFX_RNG_CONFIG_INFO_COLOR 0 #endif
// NRFX_RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix. // Default // Black // Red // Green // Yellow // Blue // Magenta // Cyan // White
#ifndef NRFX_RNG_CONFIG_DEBUG_COLOR #define NRFX_RNG_CONFIG_DEBUG_COLOR 0 #endif // NRF_QUEUE_ENABLED - nrf_queue - Queue module //========================================================== #ifndef NRF_QUEUE_ENABLED #define NRF_QUEUE_ENABLED 1 #endif // 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
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?