您当前的位置: 首页 >  仙剑情缘

NRF52832 RNG

仙剑情缘 发布时间:2018-11-01 10:15:45 ,浏览量:6

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

关注
打赏
查看更多评论

仙剑情缘

暂无认证

  • 6浏览

    0关注

    138博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录