From adb24b06b8ffc853721c2942ada879ddd15ef33d Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Wed, 19 Nov 2014 11:52:57 +0100 Subject: [PATCH] native: add random peripheral implementation --- boards/native/Makefile.features | 1 + cpu/native/include/periph_conf.h | 35 ++++++++++++++++ cpu/native/periph/random.c | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 cpu/native/include/periph_conf.h create mode 100644 cpu/native/periph/random.c diff --git a/boards/native/Makefile.features b/boards/native/Makefile.features index c933e2bf3f..2c1e593763 100644 --- a/boards/native/Makefile.features +++ b/boards/native/Makefile.features @@ -1 +1,2 @@ FEATURES_PROVIDED += transceiver periph_cpuid cpp +FEATURES_PROVIDED += periph_random diff --git a/cpu/native/include/periph_conf.h b/cpu/native/include/periph_conf.h new file mode 100644 index 0000000000..0774418052 --- /dev/null +++ b/cpu/native/include/periph_conf.h @@ -0,0 +1,35 @@ +/** + * Native CPU peripheral configuration + * + * Copyright (C) 2014 Ludwig Ortmann + * + * 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 + */ + +#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 */ +/** @} */ diff --git a/cpu/native/periph/random.c b/cpu/native/periph/random.c new file mode 100644 index 0000000000..427a1f2e75 --- /dev/null +++ b/cpu/native/periph/random.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2014 Ludwig Ortmann + * + * 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 + */ + +#include +#include +#include + +#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; +} + +/** + * @} + */