码迷,mamicode.com
首页 > 其他好文 > 详细

NRF51822之RNG

时间:2017-10-20 15:53:59      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:one   false   timeout   col   val   extern   byte   random   esc   

在裸机下官方已经提供另一个RNG的例子(RF51_SDK_10.0.0_dc26b5e\examples\peripheral\rng)

好了现在我将给出在蓝牙模式下如何使用例子

 

#include "random.h"

void random_create(uint8_t* p_result, uint8_t length)
{
    uint32_t err_code;

    while (length)
    {
        uint8_t available = 0;
        err_code = sd_rand_application_bytes_available_get(&available);
        APP_ERROR_CHECK(err_code);
        if (available)
        {
            available = available < length ? available : length;
            err_code = sd_rand_application_vector_get(p_result, available);
            APP_ERROR_CHECK(err_code);
            p_result += available;
            length -= available;
        }
    }
}

void randombytes(uint8_t* p_result, uint64_t length)
{
    random_create(p_result, (uint8_t)length);
}

 

/*
 * random.h
 *
 *  Created on:  Oct  20, 2017
 *      Author: libra
 */

#ifndef HOMEKIT_RANDOM_H_
#define HOMEKIT_RANDOM_H_


#include <stdint.h>
#include "nrf_soc.h"
#include "app_error.h"

extern void random_create(uint8_t* p_result, uint8_t length);
extern void randombytes(uint8_t* p_result, uint64_t length);

#endif /* HOMEKIT_RANDOM_H_ */

 

好了现在我们在ble_app_template基础上进行修改(下面给出主要的测试部分带)

APP_TIMER_DEF(m_app_timer_id);                                                 
#define TIMER_INTERVAL           APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER) 

static void timer_timeout_handler(void * p_context)
{
    
     uint8_t rng_result[8];
    
    randombytes(rng_result,sizeof(rng_result));
        
#ifdef RTT_LOG_ENABLED

    loge("rng_result[8] %x  %x  %x  %x  %x  %x  %x  %x",rng_result[0],                                                        rng_result[1],                                                        rng_result[2],                                                        rng_result[3],                                                        rng_result[4],                                                        rng_result[5],                                                        rng_result[6],                                                        rng_result[7] );

#endif //RTT_LOG_ENABLED

}

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
static void timers_init(void)
{

    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);

    // Create timers.

    /* YOUR_JOB: Create any timers to be used by the application.
                 Below is an example of how to create a timer.
                 For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
                 one.*/
    uint32_t err_code;
    err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
    APP_ERROR_CHECK(err_code); 
}

static void application_timers_start(void)
{
    /* YOUR_JOB: Start your timers. below is an example of how to start a timer.*/
    uint32_t err_code;
    err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code); 

}

 

技术分享

 

NRF51822之RNG

标签:one   false   timeout   col   val   extern   byte   random   esc   

原文地址:http://www.cnblogs.com/libra13179/p/7699581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!