native: add random peripheral implementation

This commit is contained in:
Ludwig Ortmann 2014-11-19 11:52:57 +01:00
parent d8641ff525
commit adb24b06b8
3 changed files with 108 additions and 0 deletions

View File

@ -1 +1,2 @@
FEATURES_PROVIDED += transceiver periph_cpuid cpp
FEATURES_PROVIDED += periph_random

View File

@ -0,0 +1,35 @@
/**
* Native CPU peripheral configuration
*
* Copyright (C) 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* @ingroup native_cpu
* @{
* @file
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*/
#ifndef PERIPH_CONF_H
#define PERIPH_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Random Number Generator configuration
* @{
*/
#define RANDOM_NUMOF (1U)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* PERIPH_CONF_H */
/** @} */

View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2014 Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup native_cpu
* @{
*
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
*/
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include "cpu-conf.h"
#include "native_internal.h"
#include "periph/random.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
static int powered = 0;
void random_init(void)
{
DEBUGF("random_init: powering on\n");
random_poweron();
}
int random_read(char *buf, unsigned int num)
{
if (!powered) {
warnx("random_read: random device not powered, failing\n");
return 0;
}
DEBUGF("random_read: writing %ui bytes\n", num);
for (unsigned i = 0; i < num; i++) {
_native_syscall_enter();
buf[i] = (char)real_random();
_native_syscall_leave();
}
return num;
}
/**
* seed host random module with @ref _native_id
*/
void random_poweron(void)
{
DEBUGF("random_poweron: power on\n");
powered = 1;
_native_syscall_enter();
real_srandom((unsigned int)_native_id);
_native_syscall_leave();
}
void random_poweroff(void)
{
DEBUGF("random_poweroff: power off\n");
powered = 0;
}
/**
* @}
*/